Update request format: OpenAI-like message handling
Browse files
app.py
CHANGED
@@ -5,22 +5,12 @@ import os
|
|
5 |
import re
|
6 |
import requests
|
7 |
from fastapi.middleware.cors import CORSMiddleware
|
8 |
-
from pydantic import BaseModel
|
9 |
-
from typing import List,
|
10 |
|
11 |
load_dotenv()
|
12 |
|
13 |
app = FastAPI()
|
14 |
-
ranked_models = [
|
15 |
-
"llama-3.3-70b-versatile",
|
16 |
-
"llama3-70b-8192",
|
17 |
-
"meta-llama/llama-4-maverick-17b-128e-instruct",
|
18 |
-
"meta-llama/llama-4-scout-17b-16e-instruct",
|
19 |
-
"mistral-saba-24b",
|
20 |
-
"gemma2-9b-it",
|
21 |
-
"llama-3.1-8b-instant",
|
22 |
-
"llama3-8b-8192"
|
23 |
-
]
|
24 |
|
25 |
api_keys = []
|
26 |
|
@@ -36,12 +26,32 @@ app.add_middleware(
|
|
36 |
allow_origins=["*"]
|
37 |
)
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
class ChatRequest(BaseModel):
|
40 |
-
models: Optional[List[
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
|
46 |
@app.get("/")
|
47 |
def main_page():
|
@@ -50,41 +60,29 @@ def main_page():
|
|
50 |
@app.post("/chat")
|
51 |
def ask_groq_llm(req: ChatRequest):
|
52 |
models = req.models
|
53 |
-
|
|
|
|
|
54 |
looping = True
|
55 |
-
if models ==
|
56 |
-
while looping:
|
57 |
-
for model in ranked_models:
|
58 |
-
for key in api_keys:
|
59 |
-
resp = requests.post("https://api.groq.com/openai/v1/chat/completions", verify=False, headers={"Content-Type": "application/json", "Authorization": f"Bearer {key}"}, data=json.dumps({"model": model, "messages": [{"role": "user", "content": query}]}))
|
60 |
-
if resp.status_code == 200:
|
61 |
-
respJson = resp.json()
|
62 |
-
print("Asked to", model, "with the key ID", str(api_keys.index(key)+1), ":", query)
|
63 |
-
return {"error": False, "content": respJson["choices"]}
|
64 |
-
print(resp.status_code, resp.text)
|
65 |
-
looping = False
|
66 |
-
return {"error": True, "content": "Aucun des modèles, ni des clés ne fonctionne, patientez ...."}
|
67 |
-
elif len(models) == 1:
|
68 |
while looping:
|
69 |
for key in api_keys:
|
70 |
-
resp = requests.post("https://api.groq.com/openai/v1/chat/completions", verify=False, headers={"Content-Type": "application/json", "Authorization": f"Bearer {key}"}, data=json.dumps({"model": models[0], "messages":
|
71 |
if resp.status_code == 200:
|
72 |
respJson = resp.json()
|
73 |
-
print("Asked to", model, "with the key ID", str(api_keys.index(key)+1), ":",
|
74 |
return {"error": False, "content": respJson["choices"]}
|
75 |
print(resp.status_code, resp.text)
|
76 |
looping = False
|
77 |
return {"error": True, "content": "Aucun des modèles, ni des clés ne fonctionne, patientez ...."}
|
78 |
else:
|
79 |
while looping:
|
80 |
-
|
81 |
-
sorted_models = sorted(models, key=lambda x: order.get(x, float('inf')))
|
82 |
-
for model in sorted_models:
|
83 |
for key in api_keys:
|
84 |
-
resp = requests.post("https://api.groq.com/openai/v1/chat/completions", verify=False, headers={"Content-Type": "application/json", "Authorization": f"Bearer {key}"}, data=json.dumps({"model": model, "messages":
|
85 |
if resp.status_code == 200:
|
86 |
respJson = resp.json()
|
87 |
-
print("Asked to", model, "with the key ID", str(api_keys.index(key)+1), ":",
|
88 |
return {"error": False, "content": respJson["choices"]}
|
89 |
print(resp.status_code, resp.text)
|
90 |
looping = False
|
|
|
5 |
import re
|
6 |
import requests
|
7 |
from fastapi.middleware.cors import CORSMiddleware
|
8 |
+
from pydantic import BaseModel, Field
|
9 |
+
from typing import List, Optional, Literal, Union
|
10 |
|
11 |
load_dotenv()
|
12 |
|
13 |
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
api_keys = []
|
16 |
|
|
|
26 |
allow_origins=["*"]
|
27 |
)
|
28 |
|
29 |
+
class ChatMessage(BaseModel):
|
30 |
+
role: Literal["system", "user", "assistant", "tool"]
|
31 |
+
content: Optional[str] # Null pour certains messages (ex: tool calls)
|
32 |
+
name: Optional[str] = None
|
33 |
+
function_call: Optional[dict] = None # Déprécié
|
34 |
+
tool_call_id: Optional[str] = None
|
35 |
+
tool_calls: Optional[List[dict]] = None
|
36 |
+
|
37 |
class ChatRequest(BaseModel):
|
38 |
+
models: Optional[List[str]] = []
|
39 |
+
messages: List[ChatMessage]
|
40 |
+
temperature: Optional[float] = Field(default=1.0, ge=0.0, le=2.0)
|
41 |
+
top_p: Optional[float] = Field(default=1.0, ge=0.0, le=1.0)
|
42 |
+
n: Optional[int] = Field(default=1, ge=1)
|
43 |
+
stream: Optional[bool] = False
|
44 |
+
stop: Optional[Union[str, List[str]]] = None
|
45 |
+
max_tokens: Optional[int] = None
|
46 |
+
presence_penalty: Optional[float] = Field(default=0.0, ge=-2.0, le=2.0)
|
47 |
+
frequency_penalty: Optional[float] = Field(default=0.0, ge=-2.0, le=2.0)
|
48 |
+
logit_bias: Optional[dict] = None
|
49 |
+
user: Optional[str] = None
|
50 |
+
tools: Optional[List[dict]] = None
|
51 |
+
tool_choice: Optional[Union[str, dict]] = None
|
52 |
|
53 |
+
def clean_message(msg: ChatMessage) -> dict:
|
54 |
+
return {k: v for k, v in msg.model_dump().items() if v is not None}
|
55 |
|
56 |
@app.get("/")
|
57 |
def main_page():
|
|
|
60 |
@app.post("/chat")
|
61 |
def ask_groq_llm(req: ChatRequest):
|
62 |
models = req.models
|
63 |
+
if len(models) == 1 and models[0] == "":
|
64 |
+
raise HTTPException(400, detail="Empty model field")
|
65 |
+
messages = [clean_message(m) for m in req.messages]
|
66 |
looping = True
|
67 |
+
if len(models) == 1:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
while looping:
|
69 |
for key in api_keys:
|
70 |
+
resp = requests.post("https://api.groq.com/openai/v1/chat/completions", verify=False, headers={"Content-Type": "application/json", "Authorization": f"Bearer {key}"}, data=json.dumps({"model": models[0], "messages": messages}))
|
71 |
if resp.status_code == 200:
|
72 |
respJson = resp.json()
|
73 |
+
print("Asked to", model, "with the key ID", str(api_keys.index(key)+1), ":", messages)
|
74 |
return {"error": False, "content": respJson["choices"]}
|
75 |
print(resp.status_code, resp.text)
|
76 |
looping = False
|
77 |
return {"error": True, "content": "Aucun des modèles, ni des clés ne fonctionne, patientez ...."}
|
78 |
else:
|
79 |
while looping:
|
80 |
+
for model in models:
|
|
|
|
|
81 |
for key in api_keys:
|
82 |
+
resp = requests.post("https://api.groq.com/openai/v1/chat/completions", verify=False, headers={"Content-Type": "application/json", "Authorization": f"Bearer {key}"}, data=json.dumps({"model": model, "messages": messages}))
|
83 |
if resp.status_code == 200:
|
84 |
respJson = resp.json()
|
85 |
+
print("Asked to", model, "with the key ID", str(api_keys.index(key)+1), ":", messages)
|
86 |
return {"error": False, "content": respJson["choices"]}
|
87 |
print(resp.status_code, resp.text)
|
88 |
looping = False
|