-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponseservice.py
28 lines (25 loc) · 1.19 KB
/
responseservice.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
import openai
class ResponseService:
def __init__(self):
pass
def generate_response_with_usage(self, facts, user_question):
try:
# Генерация ответа через OpenAI ChatCompletion
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": f"На основе следующих фактов ответьте на вопрос пользователя.\n"
f"ФАКТЫ: {facts}\n"
f"ВОПРОС: {user_question}"
}
]
)
# Извлечение ответа и статистики токенов
summary = response.choices[0].message.content
usage = response.usage # Получаем статистику по токенам
return summary, usage
except openai.error.OpenAIError as e:
logging.error(f"Ошибка OpenAI API: {e}")
return "Ошибка при взаимодействии с OpenAI API.", None