Update multi_inference.py
Browse files- multi_inference.py +27 -27
multi_inference.py
CHANGED
@@ -3,19 +3,7 @@
|
|
3 |
import requests
|
4 |
import os
|
5 |
|
6 |
-
#
|
7 |
-
# DeepSeek API keys
|
8 |
-
DEEPSEEK_KEYS = [
|
9 |
-
os.getenv("DEEPSEEK_KEY", "").strip(), # Top-level key
|
10 |
-
os.getenv("DEEPSEEK_KEY_1", "").strip(),
|
11 |
-
os.getenv("DEEPSEEK_KEY_2", "").strip(),
|
12 |
-
os.getenv("DEEPSEEK_KEY_3", "").strip(),
|
13 |
-
os.getenv("DEEPSEEK_KEY_4", "").strip(),
|
14 |
-
os.getenv("DEEPSEEK_KEY_5", "").strip()
|
15 |
-
]
|
16 |
-
|
17 |
-
|
18 |
-
# OpenAI keys (named open1, open2, etc.)
|
19 |
OPENAI_KEYS = [
|
20 |
os.getenv("open1", "").strip(),
|
21 |
os.getenv("open2", "").strip(),
|
@@ -24,15 +12,25 @@ OPENAI_KEYS = [
|
|
24 |
os.getenv("open5", "").strip()
|
25 |
]
|
26 |
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
try:
|
29 |
-
url = "https://api.
|
30 |
headers = {
|
31 |
"Authorization": f"Bearer {key}",
|
32 |
"Content-Type": "application/json"
|
33 |
}
|
34 |
data = {
|
35 |
-
"model": "
|
36 |
"messages": [
|
37 |
{"role": "system", "content": "You are a helpful assistant."},
|
38 |
{"role": "user", "content": prompt}
|
@@ -42,19 +40,19 @@ def try_deepseek(prompt, key):
|
|
42 |
result = res.json()
|
43 |
if "choices" in result:
|
44 |
return result["choices"][0]["message"]["content"]
|
45 |
-
return f"[ERROR]
|
46 |
except Exception as e:
|
47 |
-
return f"[ERROR]
|
48 |
|
49 |
-
def
|
50 |
try:
|
51 |
-
url = "https://api.
|
52 |
headers = {
|
53 |
"Authorization": f"Bearer {key}",
|
54 |
"Content-Type": "application/json"
|
55 |
}
|
56 |
data = {
|
57 |
-
"model": "
|
58 |
"messages": [
|
59 |
{"role": "system", "content": "You are a helpful assistant."},
|
60 |
{"role": "user", "content": prompt}
|
@@ -64,20 +62,22 @@ def try_openai(prompt, key):
|
|
64 |
result = res.json()
|
65 |
if "choices" in result:
|
66 |
return result["choices"][0]["message"]["content"]
|
67 |
-
return f"[ERROR]
|
68 |
except Exception as e:
|
69 |
-
return f"[ERROR]
|
70 |
|
71 |
def multi_query(prompt):
|
72 |
-
|
|
|
73 |
if key:
|
74 |
-
response =
|
75 |
if not response.startswith("[ERROR]"):
|
76 |
return response
|
77 |
|
78 |
-
|
|
|
79 |
if key:
|
80 |
-
response =
|
81 |
if not response.startswith("[ERROR]"):
|
82 |
return response
|
83 |
|
|
|
3 |
import requests
|
4 |
import os
|
5 |
|
6 |
+
# OpenAI keys (try first)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
OPENAI_KEYS = [
|
8 |
os.getenv("open1", "").strip(),
|
9 |
os.getenv("open2", "").strip(),
|
|
|
12 |
os.getenv("open5", "").strip()
|
13 |
]
|
14 |
|
15 |
+
# DeepSeek keys (fallback)
|
16 |
+
DEEPSEEK_KEYS = [
|
17 |
+
os.getenv("DEEPSEEK_KEY", "").strip(), # top-level key
|
18 |
+
os.getenv("DEEPSEEK_KEY_1", "").strip(),
|
19 |
+
os.getenv("DEEPSEEK_KEY_2", "").strip(),
|
20 |
+
os.getenv("DEEPSEEK_KEY_3", "").strip(),
|
21 |
+
os.getenv("DEEPSEEK_KEY_4", "").strip(),
|
22 |
+
os.getenv("DEEPSEEK_KEY_5", "").strip()
|
23 |
+
]
|
24 |
+
|
25 |
+
def try_openai(prompt, key):
|
26 |
try:
|
27 |
+
url = "https://api.openai.com/v1/chat/completions"
|
28 |
headers = {
|
29 |
"Authorization": f"Bearer {key}",
|
30 |
"Content-Type": "application/json"
|
31 |
}
|
32 |
data = {
|
33 |
+
"model": "gpt-3.5-turbo",
|
34 |
"messages": [
|
35 |
{"role": "system", "content": "You are a helpful assistant."},
|
36 |
{"role": "user", "content": prompt}
|
|
|
40 |
result = res.json()
|
41 |
if "choices" in result:
|
42 |
return result["choices"][0]["message"]["content"]
|
43 |
+
return f"[ERROR] OpenAI: {result.get('error', {}).get('message', 'Unknown error')}"
|
44 |
except Exception as e:
|
45 |
+
return f"[ERROR] OpenAI Exception: {str(e)}"
|
46 |
|
47 |
+
def try_deepseek(prompt, key):
|
48 |
try:
|
49 |
+
url = "https://api.deepseek.com/v1/chat/completions"
|
50 |
headers = {
|
51 |
"Authorization": f"Bearer {key}",
|
52 |
"Content-Type": "application/json"
|
53 |
}
|
54 |
data = {
|
55 |
+
"model": "deepseek-chat",
|
56 |
"messages": [
|
57 |
{"role": "system", "content": "You are a helpful assistant."},
|
58 |
{"role": "user", "content": prompt}
|
|
|
62 |
result = res.json()
|
63 |
if "choices" in result:
|
64 |
return result["choices"][0]["message"]["content"]
|
65 |
+
return f"[ERROR] DeepSeek: {result.get('error', {}).get('message', 'Unknown error')}"
|
66 |
except Exception as e:
|
67 |
+
return f"[ERROR] DeepSeek Exception: {str(e)}"
|
68 |
|
69 |
def multi_query(prompt):
|
70 |
+
# Try OpenAI first
|
71 |
+
for key in OPENAI_KEYS:
|
72 |
if key:
|
73 |
+
response = try_openai(prompt, key)
|
74 |
if not response.startswith("[ERROR]"):
|
75 |
return response
|
76 |
|
77 |
+
# Fallback to DeepSeek
|
78 |
+
for key in DEEPSEEK_KEYS:
|
79 |
if key:
|
80 |
+
response = try_deepseek(prompt, key)
|
81 |
if not response.startswith("[ERROR]"):
|
82 |
return response
|
83 |
|