-
Notifications
You must be signed in to change notification settings - Fork 0
/
yandex_gpt.py
53 lines (47 loc) · 1.7 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import logging
import requests
from config import MAX_GPT_TOKENS, SYSTEM_PROMPT, GPT_URL, GPT_MODEL
from creds import get_creds
IAM_TOKEN, FOLDER_ID = get_creds() # получаем iam_token и folder_id из файлов
def count_gpt_tokens(messages):
headers = {
'Authorization': f'Bearer {IAM_TOKEN}',
'Content-Type': 'application/json'
}
data = {
'modelUri': f"gpt://{FOLDER_ID}/yandexgpt-lite",
"messages": messages
}
try:
response = requests.post(url=GPT_URL, json=data, headers=headers).json()['tokens']
return len(response)
except Exception as e:
logging.error(e)
return 0
def ask_gpt(messages):
headers = {
'Authorization': f'Bearer {IAM_TOKEN}',
'Content-Type': 'application/json'
}
data = {
'modelUri': f"gpt://{FOLDER_ID}/{GPT_MODEL}",
"completionOptions": {
"stream": False,
"temperature": 0.7,
"maxTokens": MAX_GPT_TOKENS
},
"messages": SYSTEM_PROMPT + messages
}
try:
response = requests.post(GPT_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