-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_helper.py
25 lines (22 loc) · 934 Bytes
/
chat_helper.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
import openai
class ChatHelper:
class Error(Exception):
def __init__(self, message: str):
super().__init__(message)
@staticmethod
async def send_prompt(prompt: str) -> str:
# Note that the constructor is empty because the key is loaded from env.
client: openai.AsyncOpenAI = openai.AsyncOpenAI()
try:
completion: openai.types.chat.chat_completion.ChatCompletion = await client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{
"role": "user",
"content": prompt,
}]
)
if not completion.choices:
raise ChatHelper.Error("ChatGPT did not return any choice.")
return completion.choices[0].message.content
except openai.OpenAIError:
raise ChatHelper.Error("ChatGPT error.")