Varshith dharmaj commited on
Upload core/verification_engine.py with huggingface_hub
Browse files- core/verification_engine.py +74 -0
core/verification_engine.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 3 |
+
from typing import List, Dict, Any, Optional
|
| 4 |
+
import logging
|
| 5 |
+
|
| 6 |
+
from models.llm_agent import LLMAgent
|
| 7 |
+
from consensus.consensus_mechanism import compute_neurosymbolic_consensus
|
| 8 |
+
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
+
|
| 11 |
+
def run_verification_parallel(
|
| 12 |
+
problem: str,
|
| 13 |
+
steps: Optional[List[str]] = None,
|
| 14 |
+
model_name: str = "Ensemble",
|
| 15 |
+
model_list: Optional[List[str]] = None
|
| 16 |
+
):
|
| 17 |
+
"""
|
| 18 |
+
Run verification with Multi-Agent LLMs in parallel.
|
| 19 |
+
Yields intermediate results for UI streaming, and computes empirical neuro-symbolic consensus.
|
| 20 |
+
"""
|
| 21 |
+
start_time = time.time()
|
| 22 |
+
|
| 23 |
+
agent_names = model_list if model_list else ["Gemini 1.5 Pro", "GPT-4", "Claude 3.5 Sonnet", "Llama 3"]
|
| 24 |
+
agents = []
|
| 25 |
+
|
| 26 |
+
for name in agent_names:
|
| 27 |
+
# Route ALL Multi-Agent logic through the genuine LLM backend defined in LLMAgent
|
| 28 |
+
agents.append(LLMAgent(model_name=name, use_real_api=True))
|
| 29 |
+
|
| 30 |
+
logger.info(f"Dispatching problem to {len(agents)} agents in parallel...")
|
| 31 |
+
|
| 32 |
+
agent_results = {}
|
| 33 |
+
with ThreadPoolExecutor(max_workers=max(1, len(agents))) as executor:
|
| 34 |
+
future_to_agent = {executor.submit(agent.generate_solution, problem): agent for agent in agents}
|
| 35 |
+
|
| 36 |
+
for future in as_completed(future_to_agent):
|
| 37 |
+
agent = future_to_agent[future]
|
| 38 |
+
try:
|
| 39 |
+
res = future.result()
|
| 40 |
+
# Ensure the strict triplet format
|
| 41 |
+
agent_results[agent.model_name] = {
|
| 42 |
+
"final_answer": res.get("final_answer", "ERROR"),
|
| 43 |
+
"reasoning_trace": res.get("reasoning_trace", []),
|
| 44 |
+
"confidence_explanation": res.get("confidence_explanation", "")
|
| 45 |
+
}
|
| 46 |
+
except Exception as exc:
|
| 47 |
+
logger.error(f"Agent {agent.model_name} failed: {exc}")
|
| 48 |
+
agent_results[agent.model_name] = {
|
| 49 |
+
"final_answer": "ERROR",
|
| 50 |
+
"reasoning_trace": [],
|
| 51 |
+
"confidence_explanation": str(exc)
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
# Yield partial result to stream to UI
|
| 55 |
+
yield {
|
| 56 |
+
"type": "partial",
|
| 57 |
+
"agent_name": agent.model_name,
|
| 58 |
+
"agent_result": agent_results[agent.model_name]
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
# Compute true Hybrid Consensus (SymPy + Divergence Matrix + Domain Weights)
|
| 62 |
+
consensus_result = compute_neurosymbolic_consensus(agent_results)
|
| 63 |
+
|
| 64 |
+
processing_time = time.time() - start_time
|
| 65 |
+
|
| 66 |
+
# Yield final result
|
| 67 |
+
yield {
|
| 68 |
+
"type": "final",
|
| 69 |
+
"problem": problem,
|
| 70 |
+
"base_steps": steps,
|
| 71 |
+
"model_results": agent_results,
|
| 72 |
+
"consensus": consensus_result,
|
| 73 |
+
"processing_time": processing_time
|
| 74 |
+
}
|