Spaces:
Running
Running
Update PolyAgent/gradio_interface.py
Browse files
PolyAgent/gradio_interface.py
CHANGED
|
@@ -1230,24 +1230,44 @@ def llm_only_answer(state: Dict[str, Any], model_name: str, prompt: str) -> str:
|
|
| 1230 |
|
| 1231 |
client = InferenceClient(model=model_id, token=HF_TOKEN)
|
| 1232 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1233 |
try:
|
|
|
|
| 1234 |
resp = client.chat_completion(
|
| 1235 |
messages=[
|
| 1236 |
-
{
|
| 1237 |
-
"role": "system",
|
| 1238 |
-
"content": (
|
| 1239 |
-
"You are a polymer R&D assistant. Answer directly and clearly. "
|
| 1240 |
-
"Do not call tools or run web searches. If you are uncertain, state uncertainty."
|
| 1241 |
-
),
|
| 1242 |
-
},
|
| 1243 |
{"role": "user", "content": p},
|
| 1244 |
],
|
| 1245 |
max_tokens=900,
|
| 1246 |
temperature=0.7,
|
| 1247 |
)
|
| 1248 |
return resp.choices[0].message.content or ""
|
|
|
|
| 1249 |
except Exception as e:
|
| 1250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1251 |
|
| 1252 |
|
| 1253 |
def build_ui() -> gr.Blocks:
|
|
|
|
| 1230 |
|
| 1231 |
client = InferenceClient(model=model_id, token=HF_TOKEN)
|
| 1232 |
|
| 1233 |
+
system = (
|
| 1234 |
+
"You are a polymer R&D assistant. Answer directly and clearly. "
|
| 1235 |
+
"Do not call tools or run web searches. If you are uncertain, state uncertainty."
|
| 1236 |
+
)
|
| 1237 |
+
|
| 1238 |
+
# A simple instruct-style prompt that works for text-generation endpoints
|
| 1239 |
+
flat_prompt = f"{system}\n\nUser:\n{p}\n\nAssistant:\n"
|
| 1240 |
+
|
| 1241 |
try:
|
| 1242 |
+
# Try chat endpoint first (works only if the provider exposes the model as chat)
|
| 1243 |
resp = client.chat_completion(
|
| 1244 |
messages=[
|
| 1245 |
+
{"role": "system", "content": system},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1246 |
{"role": "user", "content": p},
|
| 1247 |
],
|
| 1248 |
max_tokens=900,
|
| 1249 |
temperature=0.7,
|
| 1250 |
)
|
| 1251 |
return resp.choices[0].message.content or ""
|
| 1252 |
+
|
| 1253 |
except Exception as e:
|
| 1254 |
+
msg = str(e)
|
| 1255 |
+
|
| 1256 |
+
# If provider says it's not a chat model, fall back to text generation.
|
| 1257 |
+
if ("not a chat model" in msg.lower()) or ("model_not_supported" in msg.lower()):
|
| 1258 |
+
try:
|
| 1259 |
+
out = client.text_generation(
|
| 1260 |
+
flat_prompt,
|
| 1261 |
+
max_new_tokens=900,
|
| 1262 |
+
temperature=0.7,
|
| 1263 |
+
do_sample=True,
|
| 1264 |
+
return_full_text=False,
|
| 1265 |
+
)
|
| 1266 |
+
return out if isinstance(out, str) else str(out)
|
| 1267 |
+
except Exception as e2:
|
| 1268 |
+
return pretty_json({"ok": False, "error": str(e2), "model_id": model_id, "mode": "text_generation"})
|
| 1269 |
+
|
| 1270 |
+
return pretty_json({"ok": False, "error": msg, "model_id": model_id, "mode": "chat_completion"})
|
| 1271 |
|
| 1272 |
|
| 1273 |
def build_ui() -> gr.Blocks:
|