-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatgpt_api.py
64 lines (49 loc) · 1.78 KB
/
chatgpt_api.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
54
55
56
57
58
59
60
61
62
63
64
from openai import OpenAI
from consts import OPENAI_API_KEY
import sql_queries
client = OpenAI(
api_key=OPENAI_API_KEY
)
chat_history = []
MAX_CHAT_LEN = 20
DISCORD_MSG_LIMIT = 2000
IMG_COST = 0.04
INPUT_TOKEN_COST = 0.15 / 1_000_000
OUTPUT_TOKEN_COST = 0.60 / 1_000_000
def clear_history():
global chat_history
chat_history = []
async def generate_chatgpt_response(user_id: int, query: str):
global chat_history
try:
user_query = {"role": "user", "content": query}
chat_history.append(user_query)
gpt_response = client.chat.completions.create(
model="gpt-4o-mini",
messages=chat_history,
)
completion_cost = INPUT_TOKEN_COST * gpt_response.usage.completion_tokens
prompt_cost = OUTPUT_TOKEN_COST * gpt_response.usage.prompt_tokens
sql_queries.log_cost(user_id, "text", completion_cost + prompt_cost)
response = gpt_response.choices[0].message.content
gpt_response = {"role": "assistant", "content": response}
chat_history.append(gpt_response)
if len(chat_history) > MAX_CHAT_LEN:
chat_history = chat_history[-MAX_CHAT_LEN:]
return response, None
except Exception as err:
return "There was an issue with your query, please try again later.", err
def generate_image(user_id: int, query: str):
try:
response = client.images.generate(
model="dall-e-3",
prompt=query,
size="1024x1024",
quality="standard",
n=1,
)
sql_queries.log_cost(user_id, "image", IMG_COST)
image_url = response.data[0].url
return image_url, None
except Exception as err:
return "There was an error generating your image, please try again later.", err