33 lines
837 B
Python
33 lines
837 B
Python
import os
|
|
|
|
from dotenv import load_dotenv
|
|
from gigachat import GigaChatAsyncClient
|
|
from gigachat.models import Chat, Messages, MessagesRole
|
|
|
|
from bot.ai_roles import ROLES
|
|
|
|
load_dotenv()
|
|
|
|
_GIGACHAT_CLIENT = GigaChatAsyncClient(
|
|
credentials=os.getenv("GIGA_API_KEY"),
|
|
scope=os.getenv("GIGA_SCOPE"),
|
|
model=os.getenv("GIGA_MODEL"),
|
|
ca_bundle_file="./certs/cert.pem",
|
|
)
|
|
|
|
|
|
async def get_gigachat_response(user_text: str, role: str | None = None) -> str:
|
|
payload = Chat(
|
|
messages=[
|
|
Messages(
|
|
role=MessagesRole.SYSTEM,
|
|
content=ROLES.get(role, "director")["prompt"],
|
|
),
|
|
Messages(role=MessagesRole.USER, content=user_text),
|
|
]
|
|
)
|
|
|
|
response = await _GIGACHAT_CLIENT.achat(payload=payload)
|
|
|
|
return response.choices[0].message.content
|