-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
85 lines (69 loc) · 2.44 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import re
import aiohttp
from aiogram.types import ReplyKeyboardMarkup, KeyboardButton
from config import FOLDER_ID, API_KEY, STREAM, TEMPERATURE, MAX_TOKEN, GPT_URL
from core import creator
from prompts import WORD_PROMT, SYSTEM_PROMT
def escape_markdown_v2(text):
# Экранирование всех специальных символов для MarkdownV2
escape_chars = r'\_[]()~`>#+-={}.!'
return re.sub(r'([%s])' % re.escape(escape_chars), r'\\\1', text)
def get_word():
return creator.word()
def make_row_keyboard(items):
row = [KeyboardButton(text=item) for item in items]
return ReplyKeyboardMarkup(keyboard=[row], resize_keyboard=True)
async def word_request():
headers = {
"Content-Type": "application/json",
'Authorization': f'Api-Key {API_KEY}',
}
body = {
"modelUri": f"gpt://{FOLDER_ID}/yandexgpt-lite",
"completionOptions": {
"stream": STREAM,
"temperature": TEMPERATURE,
"maxTokens": MAX_TOKEN
},
"messages": [
{
"role": "user",
"text": WORD_PROMT + get_word()
}
]
}
async with aiohttp.ClientSession() as session:
async with session.post(GPT_URL, headers=headers, json=body) as response:
response = await response.json()
result = response["result"]["alternatives"][0]["message"]["text"]
result = list(filter(lambda x: x, result.split("\n")))
answer = '\n'.join(result[:4]) + "\n||" + '\n'.join(result[4:]) + "||"
return answer
async def sentence_checker(text):
headers = {
"Content-Type": "application/json",
'Authorization': f'Api-Key {API_KEY}',
}
body = {
"modelUri": f"gpt://{FOLDER_ID}/yandexgpt-lite",
"completionOptions": {
"stream": STREAM,
"temperature": TEMPERATURE + 0.3,
"maxTokens": MAX_TOKEN
},
"messages": [
{
"role": "system",
"text": SYSTEM_PROMT
},
{
"role": "user",
"text": text
}
]
}
async with aiohttp.ClientSession() as session:
async with session.post(GPT_URL, headers=headers, json=body) as response:
response = await response.json()
result = response["result"]["alternatives"][0]["message"]["text"]
return result