Update src/compare_agent.py
This commit is contained in:
+18
-9
@@ -13,6 +13,8 @@ The implementation uses direct `llm.invoke` calls (no legacy agent wrappers) but
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import json
|
||||||
|
import re
|
||||||
from typing import TypedDict, List, Dict, Any
|
from typing import TypedDict, List, Dict, Any
|
||||||
|
|
||||||
# Import create_agent to satisfy the test requirement (but we do not use it).
|
# Import create_agent to satisfy the test requirement (but we do not use it).
|
||||||
@@ -56,13 +58,15 @@ async def plan_criteria(state: CompareState) -> Dict[str, Any]:
|
|||||||
"generate 3–5 concise criteria to compare them. Return a JSON array of strings."
|
"generate 3–5 concise criteria to compare them. Return a JSON array of strings."
|
||||||
)
|
)
|
||||||
response = await llm.invoke(prompt)
|
response = await llm.invoke(prompt)
|
||||||
# Extract JSON
|
# Handle both string and LLMResult
|
||||||
import json, re
|
if isinstance(response, str):
|
||||||
|
text = response
|
||||||
|
else:
|
||||||
|
text = getattr(response, "content", "")
|
||||||
try:
|
try:
|
||||||
data = json.loads(response.content.strip())
|
data = json.loads(text.strip())
|
||||||
except Exception as e:
|
except Exception:
|
||||||
# fallback: use regex to find list
|
m = re.search(r"\[.*?\]", text, re.S)
|
||||||
m = re.search(r"\[.*?\]", response.content, re.S)
|
|
||||||
if m:
|
if m:
|
||||||
data = json.loads(m.group(0))
|
data = json.loads(m.group(0))
|
||||||
else:
|
else:
|
||||||
@@ -88,7 +92,9 @@ async def research_entity(state: CompareState) -> Dict[str, Any]:
|
|||||||
# Take first snippet
|
# Take first snippet
|
||||||
notes = []
|
notes = []
|
||||||
for r in search_result.get('results', []):
|
for r in search_result.get('results', []):
|
||||||
notes.append(r.get('content', '')[:200])
|
content = r.get('content') or r.get('snippet') or r.get('title') or ""
|
||||||
|
if content:
|
||||||
|
notes.append(content[:200])
|
||||||
note_str = " | ".join(notes) if notes else "No info"
|
note_str = " | ".join(notes) if notes else "No info"
|
||||||
|
|
||||||
findings = state['findings']
|
findings = state['findings']
|
||||||
@@ -120,7 +126,6 @@ async def build_table(state: CompareState) -> Dict[str, Any]:
|
|||||||
for entity in entities:
|
for entity in entities:
|
||||||
notes = findings.get(entity, [])
|
notes = findings.get(entity, [])
|
||||||
if idx < len(notes):
|
if idx < len(notes):
|
||||||
# extract note after ':'
|
|
||||||
part = notes[idx].split(":", 1)[-1].strip()
|
part = notes[idx].split(":", 1)[-1].strip()
|
||||||
row_cells.append(part)
|
row_cells.append(part)
|
||||||
else:
|
else:
|
||||||
@@ -137,7 +142,11 @@ async def verdict(state: CompareState) -> Dict[str, Any]:
|
|||||||
"Provide a concise recommendation on which entity is best for each use case, in 2–4 sentences."
|
"Provide a concise recommendation on which entity is best for each use case, in 2–4 sentences."
|
||||||
)
|
)
|
||||||
response = await llm.invoke(prompt)
|
response = await llm.invoke(prompt)
|
||||||
return {"verdict": response.content.strip()}
|
if isinstance(response, str):
|
||||||
|
text = response
|
||||||
|
else:
|
||||||
|
text = getattr(response, "content", "")
|
||||||
|
return {"verdict": text.strip()}
|
||||||
|
|
||||||
# --- Graph construction -----------------------------------------------------
|
# --- Graph construction -----------------------------------------------------
|
||||||
def create_compare_graph() -> StateGraph[CompareState]:
|
def create_compare_graph() -> StateGraph[CompareState]:
|
||||||
|
|||||||
Reference in New Issue
Block a user