-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyandex_gpt.py
59 lines (48 loc) · 1.76 KB
/
yandex_gpt.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
import logging
import requests
from config import *
from creds import get_creds
logging.basicConfig(
filename=LOGS,
level=logging.ERROR,
format="%(asctime)s FILE: %(filename)s IN: %(funcName)s MESSAGE: %(message)s",
filemode="w",
)
IAM_TOKEN, FOLDER_ID = get_creds()
def count_gpt_tokens(messages):
url = "https://llm.api.cloud.yandex.net/foundationModels/v1/tokenizeCompletion"
headers = {
"Authorization": f"Bearer {IAM_TOKEN}",
"Content-Type": "application/json",
}
data = {"modelUri": f"gpt://{FOLDER_ID}/yandexgpt-lite", "messages": messages}
try:
return len(requests.post(url=url, json=data, headers=headers).json()["tokens"])
except Exception as e:
logging.error(e)
return 0
def ask_gpt(messages):
url = "https://llm.api.cloud.yandex.net/foundationModels/v1/completion"
headers = {
"Authorization": f"Bearer {IAM_TOKEN}",
"Content-Type": "application/json",
}
data = {
"modelUri": f"gpt://{FOLDER_ID}/yandexgpt-lite",
"completionOptions": {
"stream": False,
"temperature": 0.7,
"maxTokens": MAX_GPT_TOKENS,
},
"messages": SYSTEM_PROMPT + messages,
}
try:
response = requests.post(url, headers=headers, json=data)
if response.status_code != 200:
return False, f"Ошибка GPT. Статус-код: {response.status_code}", None
answer = response.json()["result"]["alternatives"][0]["message"]["text"]
tokens_in_answer = count_gpt_tokens([{"role": "assistant", "text": answer}])
return True, answer, tokens_in_answer
except Exception as e:
logging.error(e)
return False, "Ошибка при обращении к GPT", None