init
This commit is contained in:
32
bot/ai_tool.py
Normal file
32
bot/ai_tool.py
Normal file
@@ -0,0 +1,32 @@
|
||||
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
|
||||
Reference in New Issue
Block a user