Update multi_inference.py
Browse files- multi_inference.py +13 -59
multi_inference.py
CHANGED
|
@@ -1,80 +1,34 @@
|
|
| 1 |
-
# multi_inference.py (
|
| 2 |
|
| 3 |
import requests
|
| 4 |
|
| 5 |
-
# ===
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
"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",
|
| 9 |
-
"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",
|
| 10 |
-
"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",
|
| 11 |
-
"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",
|
| 12 |
-
"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"
|
| 13 |
-
]
|
| 14 |
-
|
| 15 |
-
DEEPSEEK_KEYS = [
|
| 16 |
-
"sk-6de286df19b04cac8bf6295a2114b0aa",
|
| 17 |
-
"sk-5b4ecbbd66864a88925525fab14430d1",
|
| 18 |
-
"sk-9c216dd03c9945218f12475a0fa6c8c4",
|
| 19 |
-
"sk-79f0d8a98b88447880c01d012c572831",
|
| 20 |
-
"sk-2be33a5e561b4788b9446e4e4e99bc2e"
|
| 21 |
-
]
|
| 22 |
-
|
| 23 |
-
def try_openai(prompt, key):
|
| 24 |
try:
|
| 25 |
-
url = "https://api.
|
| 26 |
headers = {
|
| 27 |
-
"Authorization": f"Bearer {
|
| 28 |
"Content-Type": "application/json"
|
| 29 |
}
|
| 30 |
data = {
|
| 31 |
-
"model": "
|
| 32 |
"messages": [
|
| 33 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 34 |
{"role": "user", "content": prompt}
|
| 35 |
-
]
|
|
|
|
| 36 |
}
|
| 37 |
-
res = requests.post(url, headers=headers, json=data)
|
| 38 |
-
result = res.json()
|
| 39 |
-
if "choices" in result:
|
| 40 |
-
return result["choices"][0]["message"]["content"]
|
| 41 |
-
return f"[ERROR] OpenAI: {result.get('error', {}).get('message', 'Unknown error')}"
|
| 42 |
-
except Exception as e:
|
| 43 |
-
return f"[ERROR] OpenAI Exception: {str(e)}"
|
| 44 |
|
| 45 |
-
def try_deepseek(prompt, key):
|
| 46 |
-
try:
|
| 47 |
-
url = "https://api.deepseek.com/v1/chat/completions"
|
| 48 |
-
headers = {
|
| 49 |
-
"Authorization": f"Bearer {key}",
|
| 50 |
-
"Content-Type": "application/json"
|
| 51 |
-
}
|
| 52 |
-
data = {
|
| 53 |
-
"model": "deepseek-chat",
|
| 54 |
-
"messages": [
|
| 55 |
-
{"role": "system", "content": "You are a helpful assistant."},
|
| 56 |
-
{"role": "user", "content": prompt}
|
| 57 |
-
]
|
| 58 |
-
}
|
| 59 |
res = requests.post(url, headers=headers, json=data)
|
| 60 |
result = res.json()
|
|
|
|
| 61 |
if "choices" in result:
|
| 62 |
return result["choices"][0]["message"]["content"]
|
| 63 |
-
return f"[ERROR]
|
| 64 |
except Exception as e:
|
| 65 |
-
return f"[ERROR]
|
| 66 |
|
| 67 |
def multi_query(prompt):
|
| 68 |
-
|
| 69 |
-
if key:
|
| 70 |
-
response = try_openai(prompt, key)
|
| 71 |
-
if not response.startswith("[ERROR]"):
|
| 72 |
-
return response
|
| 73 |
-
|
| 74 |
-
for key in DEEPSEEK_KEYS:
|
| 75 |
-
if key:
|
| 76 |
-
response = try_deepseek(prompt, key)
|
| 77 |
-
if not response.startswith("[ERROR]"):
|
| 78 |
-
return response
|
| 79 |
-
|
| 80 |
-
return "[ALL APIs FAILED: Out of quota or invalid keys]"
|
|
|
|
| 1 |
+
# multi_inference.py (Groq only)
|
| 2 |
|
| 3 |
import requests
|
| 4 |
|
| 5 |
+
# === HARDCODED GROQ KEY ===
|
| 6 |
+
GROQ_API_KEY = "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" # ✅ Active key
|
| 7 |
|
| 8 |
+
def try_groq(prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
+
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 11 |
headers = {
|
| 12 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 13 |
"Content-Type": "application/json"
|
| 14 |
}
|
| 15 |
data = {
|
| 16 |
+
"model": "llama3-70b-8192",
|
| 17 |
"messages": [
|
| 18 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 19 |
{"role": "user", "content": prompt}
|
| 20 |
+
],
|
| 21 |
+
"temperature": 0.7
|
| 22 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
res = requests.post(url, headers=headers, json=data)
|
| 25 |
result = res.json()
|
| 26 |
+
|
| 27 |
if "choices" in result:
|
| 28 |
return result["choices"][0]["message"]["content"]
|
| 29 |
+
return f"[ERROR] Groq: {result.get('error', {}).get('message', 'Unknown error')}"
|
| 30 |
except Exception as e:
|
| 31 |
+
return f"[ERROR] Groq Exception: {str(e)}"
|
| 32 |
|
| 33 |
def multi_query(prompt):
|
| 34 |
+
return try_groq(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|