0
0
mirror of https://gitflic.ru/project/maks1ms/ocab.git synced 2024-12-23 16:23:01 +03:00

Тестирование YandexGPT API

This commit is contained in:
armatik 2024-05-01 01:49:20 +03:00
parent 53ed19d25c
commit 661209055d
3 changed files with 27 additions and 6 deletions

View File

@ -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")

View File

@ -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("гномик"))

View File

@ -40,7 +40,7 @@ class YandexGPT:
messages.pop(del_msg_id) messages.pop(del_msg_id)
return messages 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 aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=prompt) as response: async with session.post(url, headers=headers, json=prompt) as response:
return await response.json() return await response.json()
@ -80,9 +80,10 @@ class YandexGPT:
} }
messages = [{"role": "system", "text": system_prompt}] messages = [{"role": "system", "text": system_prompt}]
for message in input_messages: messages.append({"role": "user", "text": input_messages})
messages.append(message) # for message in input_messages:
messages = await self.async_token_check(messages, gpt, max_tokens) # messages.append(message)
# messages = await self.async_token_check(messages, gpt, max_tokens)
prompt = { prompt = {
"modelUri": gpt, "modelUri": gpt,
@ -93,9 +94,9 @@ class YandexGPT:
}, },
"messages": messages "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): async def async_yandexgpt_translate(self, input_language, output_language, text):
input_language = self.languages[input_language] input_language = self.languages[input_language]