diff --git a/src/modules/external/yandexgpt/handlers.py b/src/modules/external/yandexgpt/handlers.py new file mode 100644 index 0000000..b8696c3 --- /dev/null +++ b/src/modules/external/yandexgpt/handlers.py @@ -0,0 +1,13 @@ +from aiogram import Bot +from aiogram.types import Message +from src.modules.external.yandexgpt.yandexgpt import * +from src.modules.standard.config.config import get_yandexgpt_token, get_yandexgpt_catalog_id, get_yandexgpt_prompt +import asyncio + + +async def answer_to_message(message: Message, bot: Bot): + yagpt = YandexGPT(get_yandexgpt_token(), get_yandexgpt_catalog_id()) + text = message.text + prompt = get_yandexgpt_prompt() + response = await yagpt.async_yandexgpt(system_prompt=prompt, input_messages=text) + await message.reply(response, parse_mode="Markdown") diff --git a/src/modules/external/yandexgpt/routers.py b/src/modules/external/yandexgpt/routers.py new file mode 100644 index 0000000..b364b2b --- /dev/null +++ b/src/modules/external/yandexgpt/routers.py @@ -0,0 +1,7 @@ +from aiogram import Router, F + +from src.modules.external.yandexgpt.handlers import answer_to_message + +router = Router() +# Если сообщение содержит в начале текст "Гномик" или "гномик", то вызывается функция answer_to_message +router.message.register(answer_to_message, F.text.startswith("Гномик") | F.text.startswith("гномик")) \ No newline at end of file diff --git a/src/modules/external/yandexgpt/yandexgpt.py b/src/modules/external/yandexgpt/yandexgpt.py index f1b6f78..e334311 100644 --- a/src/modules/external/yandexgpt/yandexgpt.py +++ b/src/modules/external/yandexgpt/yandexgpt.py @@ -40,7 +40,7 @@ class YandexGPT: messages.pop(del_msg_id) return messages - async def async_request(*, url, headers, prompt) -> dict: + async def async_request(self, url, headers, prompt) -> dict: async with aiohttp.ClientSession() as session: async with session.post(url, headers=headers, json=prompt) as response: return await response.json() @@ -80,9 +80,10 @@ class YandexGPT: } messages = [{"role": "system", "text": system_prompt}] - for message in input_messages: - messages.append(message) - messages = await self.async_token_check(messages, gpt, max_tokens) + messages.append({"role": "user", "text": input_messages}) + # for message in input_messages: + # messages.append(message) + # messages = await self.async_token_check(messages, gpt, max_tokens) prompt = { "modelUri": gpt, @@ -93,9 +94,9 @@ class YandexGPT: }, "messages": messages } + response = await self.async_request(url=url, headers=headers, prompt=prompt) + return response["result"]["alternatives"][0]["message"]["text"] - response = requests.post(url, headers=headers, json=prompt).text - return json.loads(response)["result"]["alternatives"][0]["message"]["text"] async def async_yandexgpt_translate(self, input_language, output_language, text): input_language = self.languages[input_language]