-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChat_GPT_Function.py
150 lines (133 loc) · 5.41 KB
/
Chat_GPT_Function.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from openai import OpenAI
from dotenv import load_dotenv
import os
import requests
import json
import time
load_dotenv(override=True)
gpt_api_key = os.getenv("GPT_API_KEY")
openrouter_deepseek_api_key = os.getenv("OPENROUTER_DEEPSEEK_API_KEY")
def gpt(model: str, prompt: str, sys_prompt: str, temp: float,):
client = OpenAI(api_key= gpt_api_key)
response = client.chat.completions.create(
model = model,
messages=[
{
"role": "system",
"content": sys_prompt
},
{
"role": "user",
"content": prompt
}
],
temperature = temp,
# max_tokens=64,
top_p=1
)
output = response.choices[0].message.content.strip()
return output
def deepseek(prompt: str, sys_prompt: str, max_retries = 3):
for attempt in range(max_retries):
try:
response = requests.post(
url="https://openrouter.ai/api/v1/chat/completions",
headers={
"Authorization": f"Bearer {openrouter_deepseek_api_key}"
},
json={
"model": "deepseek/deepseek-r1:free",
"messages": [
{
"role": "system",
"content": sys_prompt
},
{
"role": "user",
"content": prompt
}
],
"provider": {
"order": ["Chutes", "Targon", "Azure"],
"allow_fallbacks": False
},
"include_reasoning": True
}
)
response_data = response.json()
print(f"API Response (Attempt {attempt + 1}):", json.dumps(response_data, indent=2))
# Handle 500 errors
if response.status_code == 500:
error_message = response_data.get("error", {}).get("message", "Unknown error")
print(f"Attempt {attempt + 1}/{max_retries}: Got server error: {error_message}")
if attempt < max_retries - 1:
wait_time = (attempt + 1) * 2
print(f"Waiting {wait_time} seconds before retrying...")
time.sleep(wait_time)
continue
raise Exception(f"Failed after {max_retries} attempts. Last error: {error_message}")
# Check for missing or empty choices
if not response_data.get("choices"):
print("No choices in response")
if attempt < max_retries - 1:
wait_time = (attempt + 1) * 2
print(f"Waiting {wait_time} seconds before retrying...")
time.sleep(wait_time)
continue
raise Exception("No choices in API response after all retries")
message = response_data["choices"][0].get("message", {})
content = message.get("content")
# Handle empty or missing content
if not content:
print("Empty or missing content in response")
if attempt < max_retries - 1:
wait_time = (attempt + 1) * 2
print(f"Waiting {wait_time} seconds before retrying...")
time.sleep(wait_time)
continue
raise Exception("Empty or missing content in response after all retries")
# Check for reasoning
reasoning = message.get("reasoning")
if reasoning:
print("Reasoning:", reasoning.strip())
return content.strip()
except requests.exceptions.RequestException as e:
print(f"Request error on attempt {attempt + 1}: {str(e)}")
if attempt < max_retries - 1:
wait_time = (attempt + 1) * 2
print(f"Waiting {wait_time} seconds before retrying...")
time.sleep(wait_time)
continue
raise Exception(f"Request failed after {max_retries} attempts: {str(e)}")
except json.JSONDecodeError as e:
print(f"JSON decode error on attempt {attempt + 1}: {str(e)}")
print(f"Raw response: {response.text}")
if attempt < max_retries - 1:
wait_time = (attempt + 1) * 2
print(f"Waiting {wait_time} seconds before retrying...")
time.sleep(wait_time)
continue
raise Exception(f"Invalid JSON response after {max_retries} attempts")
raise Exception("All retry attempts failed")
def dalle3(prompt: str, quality: str, size: str, style: str):
client = OpenAI(api_key= gpt_api_key)
response = client.images.generate(
model = "dall-e-3",
prompt = prompt,
size = size,
quality = quality,
style = style,
n=1,
)
image_url = response.data[0].url
return image_url
def dalle2(prompt: str, size: str):
client = OpenAI(api_key= gpt_api_key)
response = client.images.generate(
model = "dall-e-2",
prompt = prompt,
size = size,
n=1,
)
image_url = response.data[0].url
return image_url