-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathask_GPT.py
39 lines (35 loc) · 1.39 KB
/
ask_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
from openai import OpenAI
import time
# LLM_MODEL = "gpt-4o"
LLM_MODEL = "deepseek"
def ask_GPT(system,content):
while True:
try:
if LLM_MODEL == "gpt-4o": # GPT-4o api
with open("/Users/liupeiqi/workshop/Research/api_key.txt","r") as f:
api_key = f.read().strip()
client = OpenAI(api_key=api_key)
completion = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": system},
{"role": "user", "content":content}
]
)
elif LLM_MODEL == "deepseek": # Deepseek api
client = OpenAI(api_key="sk-7eb58550af8a4042aca7d33d495ec2e0", base_url="https://api.deepseek.com")
completion = client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "system", "content": system},
{"role": "user", "content": content},
],
stream=False
)
else:
raise ValueError("Invalid LLM_MODEL")
return completion.choices[0].message.content
except Exception as e:
print(e)
time.sleep(1)
continue