mirror of
https://gitflic.ru/project/maks1ms/ocab.git
synced 2024-12-24 00:33:05 +03:00
Доработка реквестов и исправление ошибок
This commit is contained in:
parent
28e157a9b6
commit
b7fdd75458
36
src/modules/external/yandexgpt/yandexgpt.py
vendored
36
src/modules/external/yandexgpt/yandexgpt.py
vendored
@ -8,9 +8,9 @@ from ...standard.config.config import *
|
|||||||
|
|
||||||
|
|
||||||
class YandexGPT:
|
class YandexGPT:
|
||||||
self.token = None
|
token = None
|
||||||
self.catalog_id = None
|
catalog_id = None
|
||||||
self.language = {
|
languages = {
|
||||||
"ru": "русский язык",
|
"ru": "русский язык",
|
||||||
"en": "английский язык",
|
"en": "английский язык",
|
||||||
"de": "немецкий язык",
|
"de": "немецкий язык",
|
||||||
@ -117,7 +117,7 @@ class YandexGPT:
|
|||||||
stream=False, temperature=0.6, max_tokens=8000
|
stream=False, temperature=0.6, max_tokens=8000
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_yandexgpt_text_history(self, messages, stream=False, temperature=0.6, max_tokens=8000):
|
async def async_yandexgpt_text_history(self, input_messages, stream=False, temperature=0.6, max_tokens=8000):
|
||||||
url = "https://llm.api.cloud.yandex.net/foundationModels/v1/completion"
|
url = "https://llm.api.cloud.yandex.net/foundationModels/v1/completion"
|
||||||
gpt = f"gpt://{self.catalog_id}/summarization/latest"
|
gpt = f"gpt://{self.catalog_id}/summarization/latest"
|
||||||
headers = {
|
headers = {
|
||||||
@ -152,18 +152,18 @@ class YandexGPT:
|
|||||||
# TODO: Сделать функцию Vision
|
# TODO: Сделать функцию Vision
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
async def collect_messages(self, message_id, chat_id):
|
async def collect_messages(self, message_id, chat_id, start_message_id):
|
||||||
messages = []
|
messages = []
|
||||||
# Собираем цепочку сообщений в формате: [{"role": "user", "text": "<Имя_пользователя>: Привет!"},
|
# Собираем цепочку сообщений в формате: [{"role": "user", "text": "<Имя_пользователя>: Привет!"},
|
||||||
# {"role": "assistant", "text": "Привет!"}]
|
# {"role": "assistant", "text": "Привет!"}]
|
||||||
while True:
|
while True:
|
||||||
message = get_message_text(chat_id, message_id)
|
message = db_api.get_message_text(chat_id, message_id)
|
||||||
if get_message_ai_model(chat_id, start_message_id) != None:
|
if db_api.get_message_ai_model(chat_id, start_message_id) != None:
|
||||||
messages.append({"role": "assistant", "text": message})
|
messages.append({"role": "assistant", "text": message})
|
||||||
else:
|
else:
|
||||||
sender_name = get_user_name(get_message_sender_id(chat_id, start_message_id))
|
sender_name = db_api.get_user_name(db_api.get_message_sender_id(chat_id, start_message_id))
|
||||||
messages.append({"role": "user", "text": sender_name + ": " + message})
|
messages.append({"role": "user", "text": sender_name + ": " + message})
|
||||||
message_id = get_message_answer_to_message_id(chat_id, message_id)
|
message_id = db_api.get_message_answer_to_message_id(chat_id, message_id)
|
||||||
if message_id is None:
|
if message_id is None:
|
||||||
break
|
break
|
||||||
return messages.reverse()
|
return messages.reverse()
|
||||||
@ -173,11 +173,11 @@ class YandexGPT:
|
|||||||
# Собираем цепочку сообщений в формате: [{"role": "user", "text": "<Имя_пользователя>: Привет!"},
|
# Собираем цепочку сообщений в формате: [{"role": "user", "text": "<Имя_пользователя>: Привет!"},
|
||||||
# {"role": "assistant", "text": "Привет!"}]
|
# {"role": "assistant", "text": "Привет!"}]
|
||||||
while True:
|
while True:
|
||||||
message = get_message_text(chat_id, start_message_id)
|
message = db_api.get_message_text(chat_id, start_message_id)
|
||||||
if get_message_ai_model(chat_id, start_message_id) != None:
|
if db_api.get_message_ai_model(chat_id, start_message_id) != None:
|
||||||
messages.append({"role": "assistant", "text": message})
|
messages.append({"role": "assistant", "text": message})
|
||||||
else:
|
else:
|
||||||
sender_name = get_user_name(get_message_sender_id(chat_id, start_message_id))
|
sender_name = db_api.get_user_name(db_api.get_message_sender_id(chat_id, start_message_id))
|
||||||
messages.append({"role": "user", "text": sender_name + ": " + message})
|
messages.append({"role": "user", "text": sender_name + ": " + message})
|
||||||
start_message_id -= 1
|
start_message_id -= 1
|
||||||
if start_message_id <= end_message_id:
|
if start_message_id <= end_message_id:
|
||||||
@ -185,7 +185,7 @@ class YandexGPT:
|
|||||||
return messages.reverse()
|
return messages.reverse()
|
||||||
|
|
||||||
async def yandexgpt_request(self, message_id = None, type = "yandexgpt-lite", chat_id = None,
|
async def yandexgpt_request(self, message_id = None, type = "yandexgpt-lite", chat_id = None,
|
||||||
message_id_end = None):
|
message_id_end = None, input_language = None, output_language = None, text = None):
|
||||||
if type == "yandexgpt-lite":
|
if type == "yandexgpt-lite":
|
||||||
messages = await self.collect_messages(message_id, chat_id)
|
messages = await self.collect_messages(message_id, chat_id)
|
||||||
return await self.async_yandexgpt_lite(
|
return await self.async_yandexgpt_lite(
|
||||||
@ -202,14 +202,14 @@ class YandexGPT:
|
|||||||
)
|
)
|
||||||
elif type == "yandexgpt-translate":
|
elif type == "yandexgpt-translate":
|
||||||
return await self.async_yandexgpt_translate(
|
return await self.async_yandexgpt_translate(
|
||||||
input_language=get_message_language(chat_id, message_id),
|
input_language,
|
||||||
output_language=get_chat_language(chat_id),
|
output_language,
|
||||||
text=get_message_text(chat_id, message_id)
|
text=db_api.get_message_text(chat_id, message_id)
|
||||||
)
|
)
|
||||||
elif type == "yandexgpt-spelling-check":
|
elif type == "yandexgpt-spelling-check":
|
||||||
return await self.async_yandexgpt_spelling_check(
|
return await self.async_yandexgpt_spelling_check(
|
||||||
input_language=get_message_language(chat_id, message_id),
|
input_language,
|
||||||
text=get_message_text(chat_id, message_id)
|
text=db_api.get_message_text(chat_id, message_id)
|
||||||
)
|
)
|
||||||
elif type == "yandexgpt-text-history":
|
elif type == "yandexgpt-text-history":
|
||||||
messages = await self.collect_messages_for_history(message_id, message_id_end, chat_id)
|
messages = await self.collect_messages_for_history(message_id, message_id_end, chat_id)
|
||||||
|
Loading…
Reference in New Issue
Block a user