Spaces:
Sleeping
Sleeping
Upload research_synthesis.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164178 unmodified lines4264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984999 unmodified lines509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607"""Research-only multi-publication synthesis endpoint for WaterScope-AI Search.NEW FILE--------metadata_search/research_synthesis.pyPurpose-------Expose POST /api/research/synthesis under the existing research Blueprint.The endpoint reuses the CURRENT production /api/chat view instead ofreimplementing the platform pipeline. It temporarily raises the server-sideselected-paper limit for the research call, instruments the productioncontext-building functions, and returns an evidence trace for reproducibility.The public /api/chat route and its normal paper limits are not changed.Expected registration---------------------In metadata_search/research_routes.py, after research_bp is created:from metadata_search.research_synthesis import register_research_synthesis_routeregister_research_synthesis_route(research_bp)The existing research_host_app.py can remain unchanged.Optional protection-------------------If environment variable WATERSCOPE_RESEARCH_TOKEN is set, requests must send:X-Research-Token: <same value>This is recommended for a research endpoint that can invoke the GPU model."""from __future__ import annotationsimport osimport threadingimport timefrom typing import Any, Callablefrom flask import current_app, jsonify, requestMAX_RESEARCH_PAPERS = 20_RESEARCH_LOCK = threading.RLock()def _clean_text(value: Any) -> str:if value is None:return ""return str(value).strip()def _word_count(text: str) -> int:return len(_clean_text(text).split())def _extract_text(value: Any) -> str:"""Best-effort extraction of the main text from a function result."""if isinstance(value, str):return valueif isinstance(value, dict):preferred = ("context","text","content","prompt","message","response","answer",)for key in preferred:item = value.get(key)if isinstance(item, str) and item.strip():return itemfor item in value.values():found = _extract_text(item)if found:return foundif isinstance(value, (list, tuple)):# Prefer the longest string-like element; context builders often return# (context, metadata) or similar.candidates = []for item in value:found = _extract_text(item)if found:candidates.append(found)if candidates:return max(candidates, key=len)return ""def _extract_answer_from_payload(payload: Any) -> str:"""Best-effort answer extraction from the existing /api/chat response."""if isinstance(payload, str):return payload.strip()if not isinstance(payload, dict):return _clean_text(payload)for key in ("response","answer","text","output","content","assistant_response","final_answer","message",):value = payload.get(key)if isinstance(value, str) and value.strip():return value.strip()if isinstance(value, dict):for subkey in ("content", "text", "response", "answer"):subvalue = value.get(subkey)if isinstance(subvalue, str) and subvalue.strip():return subvalue.strip()# One-level recursive fallback.for value in payload.values():if isinstance(value, dict):found = _extract_answer_from_payload(value)if found:return foundreturn ""def _normalize_summaries(raw: Any,document_ids: list[str],) -> list[dict[str, Any]]:"""Normalize common summaries_for_documents() return shapes.Supported examples include:{document_id: "summary"}{document_id: {"summary": "...", "title": "..."}}[{"document_id": "...", "summary": "..."}]["summary 1", "summary 2", ...]"""rows: list[dict[str, Any]] = []if isinstance(raw, dict):# Sometimes the actual mapping is nested under "summaries".if "summaries" in raw and isinstance(raw["summaries"], (dict, list)):return _normalize_summaries(raw["summaries"], document_ids)for doc_id in document_ids:value = raw.get(doc_id)if value is None:# tolerate non-string keysvalue = raw.get(str(doc_id))if isinstance(value, dict):summary = _clean_text(value.get("summary")or value.get("text")or value.get("content")or value.get("paper_summary"))title = _clean_text(value.get("title"))else:summary = _clean_text(value)title = ""rows.append({"document_id": str(doc_id),"title": title,"full_summary": summary,})return rowsif isinstance(raw, (list, tuple)):# List of dictionaries with IDs.if raw and all(isinstance(x, dict) for x in raw):by_id = {}no_id = []for item in raw:doc_id = _clean_text(item.get("document_id")or item.get("doc_id")or item.get("id"))if doc_id:by_id[doc_id] = itemelse:no_id.append(item)if by_id:for doc_id in document_ids:item = by_id.get(str(doc_id), {})summary = _clean_text(item.get("summary")or item.get("text")or item.get("content")or item.get("paper_summary"))rows.append({"document_id": str(doc_id),"title": _clean_text(item.get("title")),"full_summary": summary,})return rows# No IDs: preserve positional order.for i, doc_id in enumerate(document_ids):item = raw[i] if i < len(raw) else {}rows.append({"document_id": str(doc_id),"title": _clean_text(item.get("title")),"full_summary": _clean_text(item.get("summary")or item.get("text")or item.get("content")or item.get("paper_summary")),})return rows# Positional list of strings.for i, doc_id in enumerate(document_ids):summary = _clean_text(raw[i]) if i < len(raw) else ""rows.append({"document_id": str(doc_id),"title": "","full_summary": summary,})return rows# Unknown shape.for doc_id in document_ids:rows.append({"document_id": str(doc_id),"title": "","full_summary": "",})return rowsdef _find_chat_view():"""Find the existing production POST /api/chat Flask view by URL rule.This avoids depending on the Python function name."""for rule in current_app.url_map.iter_rules():if rule.rule.rstrip("/") == "/api/chat" and "POST" in rule.methods:view = current_app.view_functions.get(rule.endpoint)if callable(view):return view, rule.endpointraise RuntimeError("Could not find the production POST /api/chat view.")class _PatchSet:"""Small helper to patch and restore module/function globals safely."""def __init__(self):self._saved: list[tuple[dict[str, Any], str, Any, bool]] = []self._seen: set[tuple[int, str]] = set()def set(self, namespace: dict[str, Any], name: str, value: Any):key = (id(namespace), name)if key in self._seen:namespace[name] = valuereturnself._seen.add(key)existed = name in namespaceold = namespace.get(name)self._saved.append((namespace, name, old, existed))namespace[name] = valuedef restore(self):for namespace, name, old, existed in reversed(self._saved):if existed:namespace[name] = oldelse:namespace.pop(name, None)self._saved.clear()self._seen.clear()def _instrument_pipeline(chat_view: Callable,trace: dict[str, Any],patches: _PatchSet,):"""Instrument the same functions used by the production chat view.No context-building logic is duplicated here. Wrappers call the originalfunctions unchanged and only record their inputs/outputs."""chat_globals = getattr(chat_view, "__globals__", None)if not isinstance(chat_globals, dict):return# Research-only override of server-side limits during this single call.for limit_name in ("MAX_CHAT_PAPERS", "MAX_AUTO_PAPERS"):if limit_name in chat_globals:trace.setdefault("patched_limits", {})[limit_name] = {"original": chat_globals[limit_name],"research": MAX_RESEARCH_PAPERS,}patches.set(chat_globals, limit_name, MAX_RESEARCH_PAPERS)build_fn = chat_globals.get("build_context_for_documents")if callable(build_fn):build_globals = getattr(build_fn, "__globals__", {})# Capture source summaries at the exact point used by the context builder.summaries_fn = build_globals.get("summaries_for_documents")if callable(summaries_fn):def summaries_wrapper(*args, **kwargs):result = summaries_fn(*args, **kwargs)trace["summaries_raw"] = resultreturn resultpatches.set(build_globals,"summaries_for_documents",summaries_wrapper,)# Capture every question-aware summary excerpt actually produced.truncate_fn = build_globals.get("truncate_summary")if callable(truncate_fn):def truncate_wrapper(*args, **kwargs):result = truncate_fn(*args, **kwargs)trace.setdefault("truncate_calls", []).append({"input_text": _extract_text(args[0]) if args else "","output_text": _extract_text(result),})return resultpatches.set(build_globals,"truncate_summary",truncate_wrapper,)def build_wrapper(*args, **kwargs):result = build_fn(*args, **kwargs)trace["context_builder_result"] = resulttrace["context_used"] = _extract_text(result)return resultpatches.set(chat_globals,"build_context_for_documents",build_wrapper,)# Capture the final prefixed context/prompt if the production route exposes# prefix_context in its own globals.prefix_fn = chat_globals.get("prefix_context")if callable(prefix_fn):def prefix_wrapper(*args, **kwargs):result = prefix_fn(*args, **kwargs)trace["prefixed_result"] = resulttrace["prefixed_text"] = _extract_text(result)return resultpatches.set(chat_globals, "prefix_context", prefix_wrapper)def _build_evidence_trace(document_ids: list[str],trace: dict[str, Any],) -> list[dict[str, Any]]:summaries = _normalize_summaries(trace.get("summaries_raw"),document_ids,)truncate_calls = trace.get("truncate_calls") or []evidence = []for i, row in enumerate(summaries):excerpt = ""if i < len(truncate_calls):excerpt = _clean_text(truncate_calls[i].get("output_text"))full_summary = _clean_text(row.get("full_summary"))evidence.append({"paper_position": i + 1,"document_id": row["document_id"],"title": row.get("title", ""),"full_summary": full_summary,"full_summary_chars": len(full_summary),"full_summary_words": _word_count(full_summary),8 unmodified lines})# If summaries were not captured but truncation calls were, still return a# positional trace rather than losing the evidence information.if all(not x["full_summary"] for x in evidence) and truncate_calls:evidence = []for i, doc_id in enumerate(document_ids):call = truncate_calls[i] if i < len(truncate_calls) else {}inp = _clean_text(call.get("input_text"))out = _clean_text(call.get("output_text"))evidence.append({"paper_position": i + 1,"document_id": str(doc_id),"title": "","full_summary": inp,"full_summary_chars": len(inp),"full_summary_words": _word_count(inp),"excerpt_used": out,"excerpt_chars": len(out),"excerpt_words": _word_count(out),"excerpt_truncated_vs_full": (bool(inp) and bool(out) and len(out) < len(inp)),})return evidencedef register_research_synthesis_route(research_bp):"""Register POST /synthesis on the existing research Blueprint.With the current research_host_app prefix, the public path is:POST /api/research/synthesis"""@research_bp.route("/synthesis", methods=["POST"])def research_synthesis():expected_token = os.getenv("WATERSCOPE_RESEARCH_TOKEN", "").strip()if expected_token:supplied_token = request.headers.get("X-Research-Token", "").strip()if supplied_token != expected_token:return jsonify({"ok": False, "error": "Unauthorized"}), 401payload = request.get_json(silent=True) or {}if not isinstance(payload, dict):return jsonify({"ok": False, "error": "JSON object required."}), 400question = _clean_text(payload.get("question") or payload.get("message"))document_ids_raw = payload.get("document_ids") or []if not question:return jsonify({"ok": False, "error": "question is required."}), 400if not isinstance(document_ids_raw, list):return jsonify({"ok": False, "error": "document_ids must be a list."}), 400document_ids = [_clean_text(x) for x in document_ids_raw if _clean_text(x)]if not document_ids:return jsonify({"ok": False, "error": "At least one document_id is required."}), 400if len(document_ids) > MAX_RESEARCH_PAPERS:9 unmodified linesif len(set(document_ids)) != len(document_ids):return jsonify({"ok": False,"error": "Duplicate document_ids are not allowed.",}), 400# Pass through the same fields that the production chat route expects.chat_payload = dict(payload)chat_payload["message"] = questionchat_payload["document_ids"] = document_idschat_payload.pop("question", None)trace: dict[str, Any] = {}patches = _PatchSet()t0 = time.perf_counter()# One research request at a time. This keeps trace capture deterministic# and minimizes the window in which production globals are patched.with _RESEARCH_LOCK:try:chat_view, chat_endpoint = _find_chat_view()trace["chat_endpoint"] = chat_endpoint_instrument_pipeline(chat_view, trace, patches)# Create a nested /api/chat request so the production view sees# exactly the payload it normally receives.with current_app.test_request_context("/api/chat",method="POST",json=chat_payload,headers={"X-Research-Internal": "1",},):raw_result = chat_view()flask_response = current_app.make_response(raw_result)status_code = int(flask_response.status_code)production_payload = flask_response.get_json(silent=True)if production_payload is None:production_payload = {"raw_text": flask_response.get_data(as_text=True)}except Exception as exc:return jsonify({"ok": False,"error": f"Research synthesis failed: {exc}",}), 500finally:patches.restore()total_elapsed_s = time.perf_counter() - t0answer = _extract_answer_from_payload(production_payload)evidence = _build_evidence_trace(document_ids, trace)context_used = _clean_text(trace.get("context_used"))prefixed_text = _clean_text(trace.get("prefixed_text"))response = {"ok": 200 <= status_code < 300,"production_status_code": status_code,"question": question,"document_ids_requested": document_ids,"n_papers_requested": len(document_ids),"answer": answer,"evidence": evidence,"context_used": context_used,"context_used_chars": len(context_used),"context_used_words": _word_count(context_used),"prefixed_text": prefixed_text,"prefixed_text_chars": len(prefixed_text),"research_total_elapsed_s": total_elapsed_s,"trace_status": {"captured_source_summaries": any(x.get("full_summary") for x in evidence),"captured_per_paper_excerpts": any(x.get("excerpt_used") for x in evidence),"captured_context": bool(context_used),"captured_prefixed_text": bool(prefixed_text),"truncate_call_count": len(trace.get("truncate_calls") or []),"patched_limits": trace.get("patched_limits", {}),},"production_response": production_payload,}# Preserve the production HTTP status when it is an error.return jsonify(response), (200 if response["ok"] else status_code)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061078 unmodified lines1161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771789 unmodified lines188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320"""Research-only multi-publication synthesis route for WaterScope-AI Search.REPLACE:metadata_search/research_synthesis.pyThis module is registered by the existing metadata_search/research_routes.py:from metadata_search.research_synthesis import register_research_synthesis_routeregister_research_synthesis_route(research_bp)Design------The research route reuses the CURRENT production functions inmetadata_search.chat_api:focus_document_ids_for_questionbuild_context_for_documentsenhance_ordinal_question_generate_answerprefix_contextsanitize/model/agent path through _generate_answerIt bypasses ONLY the normal selected-paper count check so that a researchrequest can supply up to 20 ordered document IDs. The production /api/chatroute and MAX_CHAT_PAPERS=4 remain unchanged.No monkey-patching of production globals is used."""from __future__ import annotationsimport osimport timefrom typing import Anyfrom flask import jsonify, requestimport metadata_search.chat_api as chat_apiMAX_RESEARCH_PAPERS = 20def _clean(value: Any) -> str:if value is None:return ""return str(value).strip()def _word_count(text: str) -> int:return len(_clean(text).split())def _split_context_excerpts(context: str) -> list[str]:"""Extract the exact per-paper excerpt from the assembled production context.metadata_search.mcp_retrieval builds each context block as:Paper N: <title>File: <file_name><excerpt>with blocks separated by "\n\n---\n\n".Reading excerpts back from the final assembled context guarantees that thetrace records what was actually supplied to the model, rather than aseparately reconstructed approximation."""if not context:return []blocks = context.split("\n\n---\n\n")excerpts: list[str] = []for block in blocks:if "\n\n" in block:_, excerpt = block.split("\n\n", 1)excerpts.append(excerpt.strip())else:excerpts.append("")return excerptsdef _evidence_trace(sources: list[dict[str, Any]],context: str,) -> list[dict[str, Any]]:excerpts = _split_context_excerpts(context)rows: list[dict[str, Any]] = []for index, source in enumerate(sources, start=1):full_summary = _clean(source.get("text"))excerpt = excerpts[index - 1] if index - 1 < len(excerpts) else ""rows.append({"paper_position": index,"document_id": _clean(source.get("document_id")),"title": _clean(source.get("title")),"file_name": _clean(source.get("file_name")),"full_summary": full_summary,"full_summary_chars": len(full_summary),"full_summary_words": _word_count(full_summary),8 unmodified lines})return rowsdef _application_from_strategy(strategy: str) -> str:strategy = _clean(strategy)prefix = "waterscope:"if strategy.startswith(prefix):return strategy[len(prefix):]return ""def register_research_synthesis_route(research_bp):"""Register:POST /api/research/synthesisExpected JSON:{"document_ids": ["...", "..."],"question": "...","model": "base","application": "auto"}Optional Space secret:WATERSCOPE_RESEARCH_TOKENIf set, the caller must send:X-Research-Token: <same value>"""@research_bp.post("/synthesis")def research_synthesis():expected_token = os.getenv("WATERSCOPE_RESEARCH_TOKEN", "").strip()if expected_token:supplied = request.headers.get("X-Research-Token", "").strip()if supplied != expected_token:return jsonify({"ok": False, "error": "Unauthorized"}), 401body = request.get_json(force=True, silent=True) or {}question = _clean(body.get("question") or body.get("message"))raw_ids = body.get("document_ids") or []if not question:return jsonify({"ok": False, "error": "question is required"}), 400if not isinstance(raw_ids, list):return jsonify({"ok": False, "error": "document_ids must be a list"}), 400document_ids = [_clean(x) for x in raw_ids if _clean(x)]if not document_ids:return jsonify({"ok": False, "error": "At least one document_id is required"}), 400if len(document_ids) > MAX_RESEARCH_PAPERS:9 unmodified linesif len(set(document_ids)) != len(document_ids):return jsonify({"ok": False, "error": "Duplicate document_ids are not allowed"}), 400application = _clean(body.get("application")) or "auto"model = _clean(body.get("model")) or Nonetotal_start = time.perf_counter()try:# Same production behavior for ordinal-paper questions.# Part 3 prompts do not use "first paper", "second paper", etc.,# so formal runs should retain all supplied IDs.document_ids_used = chat_api.focus_document_ids_for_question(question,document_ids,)context_start = time.perf_counter()context, sources = chat_api.build_context_for_documents(document_ids_used,question=question,)context_elapsed_s = time.perf_counter() - context_startif not sources:raise ValueError("No summaries found for the selected papers.")# Same production prompt enhancement.question_to_model = chat_api.enhance_ordinal_question(question,sources,)# This is the exact paper context after the same production# prefix_context() transformation used inside _generate_answer().model_context = chat_api.prefix_context(context)model_start = time.perf_counter()answer, strategy, diagnostics = chat_api._generate_answer(question_to_model,context,application=application,model=model,)model_elapsed_s = time.perf_counter() - model_starttotal_elapsed_s = time.perf_counter() - total_startevidence = _evidence_trace(sources, context)application_used = _application_from_strategy(strategy)# _generate_answer() does not accept a request-level token argument.# Its agent path uses WATERSCOPE_MAX_TOKENS, defaulting to 1024.effective_max_new_tokens = int(os.getenv("WATERSCOPE_MAX_TOKENS", "1024"))response = {"ok": True,"answer": answer,"strategy": strategy,"application_requested": application,"application_used": application_used,"model_requested": model or "","max_new_tokens_effective": effective_max_new_tokens,"document_ids_requested": document_ids,"document_ids_used": document_ids_used,"n_papers_requested": len(document_ids),"n_papers_used": len(sources),"sources": [{"document_id": _clean(s.get("document_id")),"title": _clean(s.get("title")),"file_name": _clean(s.get("file_name")),}for s in sources],"evidence": evidence,"context_used": context,"context_used_chars": len(context),"context_used_words": _word_count(context),"model_context": model_context,"model_context_chars": len(model_context),"model_context_words": _word_count(model_context),"question_to_model": question_to_model,"context_build_elapsed_s": context_elapsed_s,"model_call_elapsed_s": model_elapsed_s,"research_total_elapsed_s": total_elapsed_s,"retrieval": "mcp" if chat_api.use_mcp() else "direct","diagnostics": diagnostics,"llm_backend": chat_api.llm_backend_name(),"trace_status": {"captured_context": bool(context),"captured_model_context": bool(model_context),"captured_source_summaries": (len(evidence) == len(sources)and all(bool(row["full_summary"]) for row in evidence)),"captured_per_paper_excerpts": (len(evidence) == len(sources)and all(bool(row["excerpt_used"]) for row in evidence)),"source_count": len(sources),"excerpt_count": sum(bool(row["excerpt_used"]) for row in evidence),"production_chat_limit_unchanged": True,"research_paper_limit": MAX_RESEARCH_PAPERS,},}return jsonify(response)except (TypeError, ValueError) as exc:return jsonify({"ok": False,"error": str(exc),"document_ids_requested": document_ids,}), 400except Exception as exc:return jsonify({"ok": False,"error": f"{type(exc).__name__}: {exc}","document_ids_requested": document_ids,}), 500
metadata_search/research_synthesis.py+216-503