Spaces:
Sleeping
Sleeping
Update core/pipeline_2/logic.py
Browse files- core/pipeline_2/logic.py +17 -3
core/pipeline_2/logic.py
CHANGED
|
@@ -404,9 +404,15 @@ class PipelineRAG:
|
|
| 404 |
def run_stream(self, query: str, ground_truth: str = None):
|
| 405 |
"""Streaming version of Pipeline 2"""
|
| 406 |
start = time.time()
|
|
|
|
|
|
|
| 407 |
decomposition = self._decompose_query(query)
|
|
|
|
| 408 |
|
| 409 |
-
|
|
|
|
|
|
|
|
|
|
| 410 |
def _search_sub_query(sub_q: str) -> list[dict]:
|
| 411 |
return self.retriever.search(sub_q, top_k=20)
|
| 412 |
all_candidates = []
|
|
@@ -414,18 +420,24 @@ class PipelineRAG:
|
|
| 414 |
futures = [pool.submit(_search_sub_query, sq) for sq in decomposition["sub_queries"]]
|
| 415 |
for fut in as_completed(futures): all_candidates.extend(fut.result())
|
| 416 |
unique_candidates = self._deduplicate_papers(all_candidates, top_n=self.retrieval_top_k)
|
|
|
|
|
|
|
| 417 |
top_papers = self.retriever.rerank(query, unique_candidates, top_n=self.rerank_top_n)
|
| 418 |
top_papers = self._ensure_sub_query_coverage(top_papers, decomposition["sub_queries"], top_n=self.rerank_top_n)
|
| 419 |
else:
|
| 420 |
candidates = self.retriever.search(query, top_k=self.retrieval_top_k)
|
|
|
|
| 421 |
top_papers = self.retriever.rerank(query, candidates, top_n=self.rerank_top_n)
|
| 422 |
|
| 423 |
sources = [p.get("title", "") for p in top_papers]
|
| 424 |
yield {"type": "sources", "data": sources}
|
| 425 |
|
| 426 |
-
|
|
|
|
| 427 |
full_context, abstracts, _ = self._build_context(top_papers, query, max_full_text=max_ft)
|
| 428 |
|
|
|
|
|
|
|
| 429 |
system_msg = (
|
| 430 |
"You are an expert AI research assistant specializing in scientific literature synthesis. "
|
| 431 |
"Answer the query using ONLY the provided paper context. Speak with absolute certainty.\n\n"
|
|
@@ -450,7 +462,7 @@ class PipelineRAG:
|
|
| 450 |
|
| 451 |
usage = None
|
| 452 |
for chunk in stream:
|
| 453 |
-
if chunk.choices[0].delta.content:
|
| 454 |
token = chunk.choices[0].delta.content
|
| 455 |
full_answer += token
|
| 456 |
yield {"type": "token", "data": token}
|
|
@@ -460,12 +472,14 @@ class PipelineRAG:
|
|
| 460 |
candidates_token_count=chunk.x_groq.usage.completion_tokens
|
| 461 |
)
|
| 462 |
|
|
|
|
| 463 |
stats = self.metrics.process_metrics(
|
| 464 |
client=None, query=query, answer=full_answer, context=full_context,
|
| 465 |
usage_metadata=usage, start_time=start, abstracts_list=abstracts, ground_truth=ground_truth,
|
| 466 |
model_name=self.model_name
|
| 467 |
)
|
| 468 |
yield {"type": "metrics", "data": stats}
|
|
|
|
| 469 |
|
| 470 |
except Exception as e:
|
| 471 |
logger.error(f"Pipeline 2 stream error: {e}")
|
|
|
|
| 404 |
def run_stream(self, query: str, ground_truth: str = None):
|
| 405 |
"""Streaming version of Pipeline 2"""
|
| 406 |
start = time.time()
|
| 407 |
+
|
| 408 |
+
yield {"type": "status", "data": "ANALYZING QUERY STRUCTURE..."}
|
| 409 |
decomposition = self._decompose_query(query)
|
| 410 |
+
is_multi_hop = decomposition["is_multi_hop"]
|
| 411 |
|
| 412 |
+
yield {"type": "status", "data": f"PLAN: multi_hop={is_multi_hop}"}
|
| 413 |
+
|
| 414 |
+
yield {"type": "status", "data": "EXECUTING HYBRID VECTOR RETRIEVAL..."}
|
| 415 |
+
if is_multi_hop:
|
| 416 |
def _search_sub_query(sub_q: str) -> list[dict]:
|
| 417 |
return self.retriever.search(sub_q, top_k=20)
|
| 418 |
all_candidates = []
|
|
|
|
| 420 |
futures = [pool.submit(_search_sub_query, sq) for sq in decomposition["sub_queries"]]
|
| 421 |
for fut in as_completed(futures): all_candidates.extend(fut.result())
|
| 422 |
unique_candidates = self._deduplicate_papers(all_candidates, top_n=self.retrieval_top_k)
|
| 423 |
+
|
| 424 |
+
yield {"type": "status", "data": f"RETRIEVED {len(unique_candidates)} CANDIDATES. RERANKING..."}
|
| 425 |
top_papers = self.retriever.rerank(query, unique_candidates, top_n=self.rerank_top_n)
|
| 426 |
top_papers = self._ensure_sub_query_coverage(top_papers, decomposition["sub_queries"], top_n=self.rerank_top_n)
|
| 427 |
else:
|
| 428 |
candidates = self.retriever.search(query, top_k=self.retrieval_top_k)
|
| 429 |
+
yield {"type": "status", "data": f"RETRIEVED {len(candidates)} CANDIDATES. RERANKING..."}
|
| 430 |
top_papers = self.retriever.rerank(query, candidates, top_n=self.rerank_top_n)
|
| 431 |
|
| 432 |
sources = [p.get("title", "") for p in top_papers]
|
| 433 |
yield {"type": "sources", "data": sources}
|
| 434 |
|
| 435 |
+
yield {"type": "status", "data": "FETCHING FULL-TEXT & BUILDING CONTEXT..."}
|
| 436 |
+
max_ft = self.max_full_text if is_multi_hop else min(2, self.max_full_text)
|
| 437 |
full_context, abstracts, _ = self._build_context(top_papers, query, max_full_text=max_ft)
|
| 438 |
|
| 439 |
+
yield {"type": "status", "data": "SYNTHESIZING RESPONSE VIA GROQ..."}
|
| 440 |
+
|
| 441 |
system_msg = (
|
| 442 |
"You are an expert AI research assistant specializing in scientific literature synthesis. "
|
| 443 |
"Answer the query using ONLY the provided paper context. Speak with absolute certainty.\n\n"
|
|
|
|
| 462 |
|
| 463 |
usage = None
|
| 464 |
for chunk in stream:
|
| 465 |
+
if chunk.choices and chunk.choices[0].delta.content:
|
| 466 |
token = chunk.choices[0].delta.content
|
| 467 |
full_answer += token
|
| 468 |
yield {"type": "token", "data": token}
|
|
|
|
| 472 |
candidates_token_count=chunk.x_groq.usage.completion_tokens
|
| 473 |
)
|
| 474 |
|
| 475 |
+
yield {"type": "status", "data": "GENERATION COMPLETE. CALCULATING BENCHMARK METRICS..."}
|
| 476 |
stats = self.metrics.process_metrics(
|
| 477 |
client=None, query=query, answer=full_answer, context=full_context,
|
| 478 |
usage_metadata=usage, start_time=start, abstracts_list=abstracts, ground_truth=ground_truth,
|
| 479 |
model_name=self.model_name
|
| 480 |
)
|
| 481 |
yield {"type": "metrics", "data": stats}
|
| 482 |
+
yield {"type": "status", "data": "PIPELINE EXECUTION SUCCESSFUL."}
|
| 483 |
|
| 484 |
except Exception as e:
|
| 485 |
logger.error(f"Pipeline 2 stream error: {e}")
|