From 661209055d9c5b19fb50dad08ee8e6dc924e77f3 Mon Sep 17 00:00:00 2001 From: armatik Date: Wed, 1 May 2024 01:49:20 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A2=D0=B5=D1=81=D1=82=D0=B8=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20YandexGPT=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/external/yandexgpt/handlers.py | 13 +++++++++++++ src/modules/external/yandexgpt/routers.py | 7 +++++++ src/modules/external/yandexgpt/yandexgpt.py | 13 +++++++------ 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 src/modules/external/yandexgpt/handlers.py create mode 100644 src/modules/external/yandexgpt/routers.py 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]