Varshith dharmaj commited on
Upload services/core_engine/pipeline_streamer.py with huggingface_hub
Browse files
services/core_engine/pipeline_streamer.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
import logging
|
| 3 |
+
from typing import List, Dict, Any, Optional
|
| 4 |
+
from .agent_orchestrator import AGENT_PROFILES, simulate_agent_execution
|
| 5 |
+
from .consensus_module import evaluate_consensus
|
| 6 |
+
|
| 7 |
+
logger = logging.getLogger(__name__)
|
| 8 |
+
|
| 9 |
+
def run_neurosymbolic_pipeline_stream(
|
| 10 |
+
problem: str,
|
| 11 |
+
steps: Optional[List[str]] = None,
|
| 12 |
+
model_name: str = "Ensemble",
|
| 13 |
+
model_list: Optional[List[str]] = None
|
| 14 |
+
):
|
| 15 |
+
"""
|
| 16 |
+
Adapter that executes the Phase 9 Core Neuro-Symbolic 4-Agent pipeline
|
| 17 |
+
but yields asynchronous dictionary chunks formatted exactly as the
|
| 18 |
+
Streamlit UI expects from the legacy system.
|
| 19 |
+
"""
|
| 20 |
+
start_time = time.time()
|
| 21 |
+
logger.info(f"Dispatching problem to 4 Phase 9 Agents in parallel...")
|
| 22 |
+
|
| 23 |
+
agent_results = {}
|
| 24 |
+
|
| 25 |
+
# We execute sequentially to avoid the WinError 6 thread crashing bug on Windows
|
| 26 |
+
for agent_profile in AGENT_PROFILES:
|
| 27 |
+
agent_name = agent_profile["name"]
|
| 28 |
+
try:
|
| 29 |
+
res = simulate_agent_execution(agent_profile, problem, steps=steps)
|
| 30 |
+
raw_response = res["response"]
|
| 31 |
+
agent_results[agent_name] = {
|
| 32 |
+
"final_answer": raw_response.get("Answer", "ERROR"),
|
| 33 |
+
"reasoning_trace": raw_response.get("Reasoning Trace", []),
|
| 34 |
+
"confidence_explanation": raw_response.get("Confidence Explanation", "")
|
| 35 |
+
}
|
| 36 |
+
except Exception as exc:
|
| 37 |
+
logger.error(f"Agent {agent_name} failed: {exc}")
|
| 38 |
+
agent_results[agent_name] = {
|
| 39 |
+
"final_answer": "ERROR",
|
| 40 |
+
"reasoning_trace": [],
|
| 41 |
+
"confidence_explanation": str(exc)
|
| 42 |
+
}
|
| 43 |
+
res = {
|
| 44 |
+
"agent": agent_name,
|
| 45 |
+
"response": {
|
| 46 |
+
"Answer": "ERROR",
|
| 47 |
+
"Reasoning Trace": [],
|
| 48 |
+
"Confidence Explanation": str(exc)
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
# Yield partial result to stream to UI exactly as it expects
|
| 53 |
+
yield {
|
| 54 |
+
"type": "partial",
|
| 55 |
+
"agent_name": agent_name,
|
| 56 |
+
"agent_result": agent_results[agent_name]
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
# After all agents execute, compute true Phase 9 consensus (Math-Verify + QWED)
|
| 60 |
+
# Reconstruct the raw responses format for evaluate_consensus
|
| 61 |
+
raw_responses_for_consensus = []
|
| 62 |
+
for a_name, a_result in agent_results.items():
|
| 63 |
+
raw_responses_for_consensus.append({
|
| 64 |
+
"agent": a_name,
|
| 65 |
+
"response": {
|
| 66 |
+
"Answer": a_result["final_answer"],
|
| 67 |
+
"Reasoning Trace": a_result["reasoning_trace"],
|
| 68 |
+
"Confidence Explanation": a_result["confidence_explanation"]
|
| 69 |
+
}
|
| 70 |
+
})
|
| 71 |
+
|
| 72 |
+
phase9_consensus = evaluate_consensus(raw_responses_for_consensus)
|
| 73 |
+
|
| 74 |
+
# Map the new phase 9 consensus output to the legacy UI schema
|
| 75 |
+
# The UI expects decision.get("final_verdict") and decision.get("overall_confidence")
|
| 76 |
+
decision = {
|
| 77 |
+
"final_verdict": "VALID" if phase9_consensus["winning_score"] > 0.6 else "ERRONEOUS",
|
| 78 |
+
"overall_confidence": phase9_consensus["winning_score"], # 0.0 to 1.0 mapped to UI
|
| 79 |
+
"verified_answer": phase9_consensus["final_verified_answer"],
|
| 80 |
+
"divergence_groups": phase9_consensus["divergence_groups"],
|
| 81 |
+
"detail_scores": phase9_consensus["detail_scores"]
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
# Map any flagged errors based on low symbolic validation
|
| 85 |
+
classified_errors = []
|
| 86 |
+
for ds in phase9_consensus["detail_scores"]:
|
| 87 |
+
if ds["V_sym"] < 0.5:
|
| 88 |
+
classified_errors.append({
|
| 89 |
+
"step_number": 0,
|
| 90 |
+
"category": f"Symbolic Validation Failure ({ds['agent']})",
|
| 91 |
+
"found": ds['raw_answer'],
|
| 92 |
+
"correct": phase9_consensus["final_verified_answer"]
|
| 93 |
+
})
|
| 94 |
+
|
| 95 |
+
processing_time = time.time() - start_time
|
| 96 |
+
|
| 97 |
+
# Yield final result
|
| 98 |
+
yield {
|
| 99 |
+
"type": "final",
|
| 100 |
+
"problem": problem,
|
| 101 |
+
"base_steps": steps,
|
| 102 |
+
"model_results": agent_results,
|
| 103 |
+
"consensus": decision,
|
| 104 |
+
"classified_errors": classified_errors,
|
| 105 |
+
"processing_time": processing_time
|
| 106 |
+
}
|