Spaces:
Running
Running
DJAKOUA KWANKAM BRAYAN STEVE commited on
Commit ·
6aebc02
1
Parent(s): c8a3cfa
fix: implement provider routing and dynamic tools parameter fallback on API
Browse files
app.py
CHANGED
|
@@ -115,18 +115,48 @@ async def chat(request: Request):
|
|
| 115 |
}
|
| 116 |
all_tools.append(search_tool_def)
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
# Boucle d'agent côté serveur pour exécuter search_web de manière transparente
|
|
|
|
| 119 |
while True:
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
choice = response.choices[0]
|
|
|
|
| 130 |
|
| 131 |
# Vérifier si l'IA veut appeler des outils
|
| 132 |
if choice.message.tool_calls:
|
|
|
|
| 115 |
}
|
| 116 |
all_tools.append(search_tool_def)
|
| 117 |
|
| 118 |
+
# Choisir le client et le fournisseur (provider) appropriés
|
| 119 |
+
provider = None
|
| 120 |
+
if model and ("Llama-3.3-70B" in model or "Llama-3.1-70B" in model):
|
| 121 |
+
provider = "together"
|
| 122 |
+
|
| 123 |
+
if provider:
|
| 124 |
+
local_client = InferenceClient(model=model, provider=provider, token=token)
|
| 125 |
+
else:
|
| 126 |
+
local_client = client
|
| 127 |
+
|
| 128 |
# Boucle d'agent côté serveur pour exécuter search_web de manière transparente
|
| 129 |
+
use_tools = True
|
| 130 |
while True:
|
| 131 |
+
try:
|
| 132 |
+
if provider:
|
| 133 |
+
response = local_client.chat_completion(
|
| 134 |
+
messages=messages,
|
| 135 |
+
tools=all_tools if use_tools else None,
|
| 136 |
+
max_tokens=max_tokens,
|
| 137 |
+
temperature=temperature,
|
| 138 |
+
top_p=top_p,
|
| 139 |
+
stream=False
|
| 140 |
+
)
|
| 141 |
+
else:
|
| 142 |
+
response = local_client.chat_completion(
|
| 143 |
+
model=model,
|
| 144 |
+
messages=messages,
|
| 145 |
+
tools=all_tools if use_tools else None,
|
| 146 |
+
max_tokens=max_tokens,
|
| 147 |
+
temperature=temperature,
|
| 148 |
+
top_p=top_p,
|
| 149 |
+
stream=False
|
| 150 |
+
)
|
| 151 |
+
except Exception as e:
|
| 152 |
+
err_msg = str(e)
|
| 153 |
+
if use_tools and ("tools" in err_msg or "UNSUPPORTED_OPENAI_PARAMS" in err_msg or "422" in err_msg):
|
| 154 |
+
use_tools = False
|
| 155 |
+
continue
|
| 156 |
+
else:
|
| 157 |
+
raise e
|
| 158 |
choice = response.choices[0]
|
| 159 |
+
|
| 160 |
|
| 161 |
# Vérifier si l'IA veut appeler des outils
|
| 162 |
if choice.message.tool_calls:
|