-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai.py
More file actions
27 lines (24 loc) · 806 Bytes
/
ai.py
File metadata and controls
27 lines (24 loc) · 806 Bytes
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
from openai import OpenAI
from dotenv import load_dotenv
import os
import time
load_dotenv()
def query_ai(messages,model="gpt-3.5-turbo"):
client = OpenAI(
api_key = os.getenv('OPENAI_API_KEY'),
base_url = os.getenv('OPENAI_BASE_URL') or "https://api.openai.com/v1"
)
for i in range(8): # retry 3 times
#print(messages)
print("waiting AI response....")
try:
chat_completion = client.chat.completions.create(
messages=messages,
model=model,
)
except Exception as e:
print("An unexpected error occurred: ", e)
time.sleep(1)
continue
#print(chat_completion.choices[0].message.content)
return chat_completion.choices[0].message.content