Spaces:
Sleeping
Sleeping
Upload research_synthesis.py
Browse files
metadata_search/research_synthesis.py
CHANGED
|
@@ -341,12 +341,12 @@ def register_research_synthesis_route(research_bp):
|
|
| 341 |
# Optional research-only provider selection. Existing HF requests
|
| 342 |
# remain the default and keep their original auto-routing behavior.
|
| 343 |
provider = (_clean(body.get("provider")) or "hf").lower()
|
| 344 |
-
if provider not in {"hf", "azure"}:
|
| 345 |
-
return jsonify({"ok": False, "error": "provider must be hf or
|
| 346 |
-
if provider
|
| 347 |
if os.getenv("ENABLE_EXTERNAL_LLM", "false").lower() not in {"1", "true", "yes"}:
|
| 348 |
return jsonify({"ok": False, "error": "External LLM is disabled"}), 403
|
| 349 |
-
# A research-only secret is mandatory for paid
|
| 350 |
research_token = os.getenv("WATERSCOPE_RESEARCH_TOKEN", "").strip()
|
| 351 |
supplied_token = request.headers.get("X-Research-Token", "").strip()
|
| 352 |
if not research_token or not hmac.compare_digest(supplied_token, research_token):
|
|
@@ -357,7 +357,7 @@ def register_research_synthesis_route(research_bp):
|
|
| 357 |
if application not in AGENT_APPLICATIONS:
|
| 358 |
return jsonify({
|
| 359 |
"ok": False,
|
| 360 |
-
"error": "For
|
| 361 |
+ ", ".join(AGENT_APPLICATIONS),
|
| 362 |
}), 400
|
| 363 |
|
|
@@ -468,7 +468,7 @@ def register_research_synthesis_route(research_bp):
|
|
| 468 |
|
| 469 |
model_start = time.perf_counter()
|
| 470 |
external_result = None
|
| 471 |
-
if provider
|
| 472 |
from llm_services.llm_client import run_application
|
| 473 |
# The same complete context, question and application prompt
|
| 474 |
# are passed to both model paths. HF's prefix_context() is
|
|
@@ -477,15 +477,17 @@ def register_research_synthesis_route(research_bp):
|
|
| 477 |
application=application,
|
| 478 |
context=model_context,
|
| 479 |
question=question_to_model,
|
| 480 |
-
provider=
|
| 481 |
-
model=None, #
|
| 482 |
max_completion_tokens=max_new_tokens_requested,
|
| 483 |
)
|
| 484 |
answer = external_result["answer"]
|
| 485 |
strategy = f"waterscope:{application}"
|
| 486 |
diagnostics = {
|
| 487 |
-
"provider":
|
| 488 |
"model": external_result.get("model_returned"),
|
|
|
|
|
|
|
| 489 |
"finish_reason": external_result.get("finish_reason"),
|
| 490 |
"metering": external_result.get("metering"),
|
| 491 |
"prompt_version": external_result.get("prompt_version"),
|
|
@@ -526,6 +528,7 @@ def register_research_synthesis_route(research_bp):
|
|
| 526 |
"provider_used": provider,
|
| 527 |
"external_model": external_result.get("model_returned") if external_result else None,
|
| 528 |
"external_usage": external_result.get("metering") if external_result else None,
|
|
|
|
| 529 |
"application_requested": application,
|
| 530 |
"application_used": application_used,
|
| 531 |
"model_requested": model or "",
|
|
@@ -578,7 +581,7 @@ def register_research_synthesis_route(research_bp):
|
|
| 578 |
"mcp" if chat_api.use_mcp() else "direct"
|
| 579 |
),
|
| 580 |
"diagnostics": diagnostics,
|
| 581 |
-
"llm_backend":
|
| 582 |
"trace_status": {
|
| 583 |
"captured_full_context": bool(
|
| 584 |
full_context
|
|
@@ -607,7 +610,7 @@ def register_research_synthesis_route(research_bp):
|
|
| 607 |
MAX_RESEARCH_PAPERS
|
| 608 |
),
|
| 609 |
"token_override_scope": (
|
| 610 |
-
"external_api" if provider
|
| 611 |
),
|
| 612 |
},
|
| 613 |
}
|
|
|
|
| 341 |
# Optional research-only provider selection. Existing HF requests
|
| 342 |
# remain the default and keep their original auto-routing behavior.
|
| 343 |
provider = (_clean(body.get("provider")) or "hf").lower()
|
| 344 |
+
if provider not in {"hf", "azure", "deepseek"}:
|
| 345 |
+
return jsonify({"ok": False, "error": "provider must be hf, azure or deepseek"}), 400
|
| 346 |
+
if provider in {"azure", "deepseek"}:
|
| 347 |
if os.getenv("ENABLE_EXTERNAL_LLM", "false").lower() not in {"1", "true", "yes"}:
|
| 348 |
return jsonify({"ok": False, "error": "External LLM is disabled"}), 403
|
| 349 |
+
# A research-only secret is mandatory for paid external API calls.
|
| 350 |
research_token = os.getenv("WATERSCOPE_RESEARCH_TOKEN", "").strip()
|
| 351 |
supplied_token = request.headers.get("X-Research-Token", "").strip()
|
| 352 |
if not research_token or not hmac.compare_digest(supplied_token, research_token):
|
|
|
|
| 357 |
if application not in AGENT_APPLICATIONS:
|
| 358 |
return jsonify({
|
| 359 |
"ok": False,
|
| 360 |
+
"error": "For external LLMs, specify an application: "
|
| 361 |
+ ", ".join(AGENT_APPLICATIONS),
|
| 362 |
}), 400
|
| 363 |
|
|
|
|
| 468 |
|
| 469 |
model_start = time.perf_counter()
|
| 470 |
external_result = None
|
| 471 |
+
if provider in {"azure", "deepseek"}:
|
| 472 |
from llm_services.llm_client import run_application
|
| 473 |
# The same complete context, question and application prompt
|
| 474 |
# are passed to both model paths. HF's prefix_context() is
|
|
|
|
| 477 |
application=application,
|
| 478 |
context=model_context,
|
| 479 |
question=question_to_model,
|
| 480 |
+
provider=provider,
|
| 481 |
+
model=(model if provider == "deepseek" else None), # Azure uses AZURE_LLM_DEPLOYMENT
|
| 482 |
max_completion_tokens=max_new_tokens_requested,
|
| 483 |
)
|
| 484 |
answer = external_result["answer"]
|
| 485 |
strategy = f"waterscope:{application}"
|
| 486 |
diagnostics = {
|
| 487 |
+
"provider": provider,
|
| 488 |
"model": external_result.get("model_returned"),
|
| 489 |
+
"model_requested": external_result.get("model_requested"),
|
| 490 |
+
"thinking_mode": external_result.get("thinking_mode"),
|
| 491 |
"finish_reason": external_result.get("finish_reason"),
|
| 492 |
"metering": external_result.get("metering"),
|
| 493 |
"prompt_version": external_result.get("prompt_version"),
|
|
|
|
| 528 |
"provider_used": provider,
|
| 529 |
"external_model": external_result.get("model_returned") if external_result else None,
|
| 530 |
"external_usage": external_result.get("metering") if external_result else None,
|
| 531 |
+
"thinking_mode": external_result.get("thinking_mode") if external_result else None,
|
| 532 |
"application_requested": application,
|
| 533 |
"application_used": application_used,
|
| 534 |
"model_requested": model or "",
|
|
|
|
| 581 |
"mcp" if chat_api.use_mcp() else "direct"
|
| 582 |
),
|
| 583 |
"diagnostics": diagnostics,
|
| 584 |
+
"llm_backend": provider if provider in {"azure", "deepseek"} else chat_api.llm_backend_name(),
|
| 585 |
"trace_status": {
|
| 586 |
"captured_full_context": bool(
|
| 587 |
full_context
|
|
|
|
| 610 |
MAX_RESEARCH_PAPERS
|
| 611 |
),
|
| 612 |
"token_override_scope": (
|
| 613 |
+
"external_api" if provider in {"azure", "deepseek"} else "isolated_function_clone"
|
| 614 |
),
|
| 615 |
},
|
| 616 |
}
|