Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- app/__pycache__/main.cpython-312.pyc +0 -0
- app/main.py +51 -45
- requirements.txt +1 -0
app/__pycache__/main.cpython-312.pyc
CHANGED
|
Binary files a/app/__pycache__/main.cpython-312.pyc and b/app/__pycache__/main.cpython-312.pyc differ
|
|
|
app/main.py
CHANGED
|
@@ -267,25 +267,28 @@ def llm_chat_with_fallback(
|
|
| 267 |
candidate_models = get_model_candidates(model_id)
|
| 268 |
|
| 269 |
if hf_token:
|
| 270 |
-
hf_client = OpenAI(base_url="https://router.huggingface.co/v1", api_key=hf_token)
|
| 271 |
primary_errors: list[str] = []
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
result["error_primary"] = " | ".join(primary_errors[:4]) or "Primary returned empty content."
|
| 290 |
else:
|
| 291 |
result["error_primary"] = "Missing HUGGINGFACE_API_TOKEN"
|
|
@@ -294,17 +297,20 @@ def llm_chat_with_fallback(
|
|
| 294 |
result["error_fallback"] = "Missing GROQ_API_KEY (fallback not available)"
|
| 295 |
return result
|
| 296 |
|
| 297 |
-
groq_client = OpenAI(base_url="https://api.groq.com/openai/v1", api_key=groq_key)
|
| 298 |
fallback_errors: list[str] = []
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
result["error_fallback"] = " | ".join(fallback_errors[:4]) or "Fallback returned empty content."
|
| 309 |
return result
|
| 310 |
|
|
@@ -916,21 +922,21 @@ def query(payload: QueryRequest) -> QueryResponse:
|
|
| 916 |
citations = build_citations(answer, retrieved)
|
| 917 |
response_chunks = [RetrievedChunk(**chunk) for chunk in retrieved]
|
| 918 |
return QueryResponse(answer=answer, retrievedChunks=response_chunks, citations=citations)
|
| 919 |
-
except Exception as exc:
|
| 920 |
-
trace = traceback.format_exc(limit=4)
|
| 921 |
-
|
| 922 |
-
|
| 923 |
-
|
| 924 |
-
|
| 925 |
-
|
| 926 |
-
|
| 927 |
-
|
| 928 |
-
|
| 929 |
-
|
| 930 |
-
"
|
| 931 |
-
|
| 932 |
-
),
|
| 933 |
-
retrievedChunks=[fallback],
|
| 934 |
-
citations=[Citation(id="S1", page=1, chunkType="main_text", text=fallback.text)],
|
| 935 |
-
)
|
| 936 |
|
|
|
|
| 267 |
candidate_models = get_model_candidates(model_id)
|
| 268 |
|
| 269 |
if hf_token:
|
|
|
|
| 270 |
primary_errors: list[str] = []
|
| 271 |
+
try:
|
| 272 |
+
hf_client = OpenAI(base_url="https://router.huggingface.co/v1", api_key=hf_token)
|
| 273 |
+
for candidate in candidate_models:
|
| 274 |
+
try:
|
| 275 |
+
routed = hf_routed_model(candidate)
|
| 276 |
+
content, raw = openai_chat(hf_client, routed, messages, temperature, max_tokens)
|
| 277 |
+
if normalize_text(content):
|
| 278 |
+
result.update(
|
| 279 |
+
{
|
| 280 |
+
"content": content,
|
| 281 |
+
"primary_used": True,
|
| 282 |
+
"raw": raw,
|
| 283 |
+
"used_model": candidate,
|
| 284 |
+
}
|
| 285 |
+
)
|
| 286 |
+
return result
|
| 287 |
+
primary_errors.append(f"{candidate}: empty content")
|
| 288 |
+
except Exception as exc: # pragma: no cover
|
| 289 |
+
primary_errors.append(f"{candidate}: {type(exc).__name__}: {exc}")
|
| 290 |
+
except Exception as exc: # pragma: no cover
|
| 291 |
+
primary_errors.append(f"hf_client_init: {type(exc).__name__}: {exc}")
|
| 292 |
result["error_primary"] = " | ".join(primary_errors[:4]) or "Primary returned empty content."
|
| 293 |
else:
|
| 294 |
result["error_primary"] = "Missing HUGGINGFACE_API_TOKEN"
|
|
|
|
| 297 |
result["error_fallback"] = "Missing GROQ_API_KEY (fallback not available)"
|
| 298 |
return result
|
| 299 |
|
|
|
|
| 300 |
fallback_errors: list[str] = []
|
| 301 |
+
try:
|
| 302 |
+
groq_client = OpenAI(base_url="https://api.groq.com/openai/v1", api_key=groq_key)
|
| 303 |
+
for candidate in candidate_models:
|
| 304 |
+
try:
|
| 305 |
+
content, raw = openai_chat(groq_client, candidate, messages, temperature, max_tokens)
|
| 306 |
+
if normalize_text(content):
|
| 307 |
+
result.update({"content": content, "raw": raw, "used_model": candidate})
|
| 308 |
+
return result
|
| 309 |
+
fallback_errors.append(f"{candidate}: empty content")
|
| 310 |
+
except Exception as exc: # pragma: no cover
|
| 311 |
+
fallback_errors.append(f"{candidate}: {type(exc).__name__}: {exc}")
|
| 312 |
+
except Exception as exc: # pragma: no cover
|
| 313 |
+
fallback_errors.append(f"groq_client_init: {type(exc).__name__}: {exc}")
|
| 314 |
result["error_fallback"] = " | ".join(fallback_errors[:4]) or "Fallback returned empty content."
|
| 315 |
return result
|
| 316 |
|
|
|
|
| 922 |
citations = build_citations(answer, retrieved)
|
| 923 |
response_chunks = [RetrievedChunk(**chunk) for chunk in retrieved]
|
| 924 |
return QueryResponse(answer=answer, retrievedChunks=response_chunks, citations=citations)
|
| 925 |
+
except Exception as exc:
|
| 926 |
+
trace = traceback.format_exc(limit=4)
|
| 927 |
+
print("query_exception:", trace)
|
| 928 |
+
fallback = RetrievedChunk(
|
| 929 |
+
id="S1",
|
| 930 |
+
page=1,
|
| 931 |
+
chunkType="main_text",
|
| 932 |
+
text=f"rag-service exception: {type(exc).__name__}: {exc}",
|
| 933 |
+
)
|
| 934 |
+
return QueryResponse(
|
| 935 |
+
answer=(
|
| 936 |
+
"RAG query failed and returned a guarded fallback response. "
|
| 937 |
+
"Check rag-service logs for details."
|
| 938 |
+
),
|
| 939 |
+
retrievedChunks=[fallback],
|
| 940 |
+
citations=[Citation(id="S1", page=1, chunkType="main_text", text=fallback.text)],
|
| 941 |
+
)
|
| 942 |
|
requirements.txt
CHANGED
|
@@ -2,6 +2,7 @@ fastapi==0.115.5
|
|
| 2 |
uvicorn[standard]==0.32.0
|
| 3 |
pydantic==2.9.2
|
| 4 |
openai==1.54.4
|
|
|
|
| 5 |
langchain-text-splitters==0.3.2
|
| 6 |
langchain-huggingface==0.1.0
|
| 7 |
langchain-community==0.3.7
|
|
|
|
| 2 |
uvicorn[standard]==0.32.0
|
| 3 |
pydantic==2.9.2
|
| 4 |
openai==1.54.4
|
| 5 |
+
httpx==0.27.2
|
| 6 |
langchain-text-splitters==0.3.2
|
| 7 |
langchain-huggingface==0.1.0
|
| 8 |
langchain-community==0.3.7
|