-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
309 lines (262 loc) · 12 KB
/
app.py
File metadata and controls
309 lines (262 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"""Streamlit web interface for the Multi-Agent Research Workflow."""
from __future__ import annotations
import asyncio
import logging
import uuid
import warnings
warnings.filterwarnings("ignore", category=ResourceWarning)
logging.getLogger("primp").setLevel(logging.ERROR)
logging.basicConfig(level=logging.WARNING)
import streamlit as st
from langgraph.checkpoint.memory import MemorySaver
from config import get_settings
from src.database.engine import init_db
from src.graph import build_graph
# ── Page config ──────────────────────────────────────────────────────────────
st.set_page_config(
page_title="AI Research Assistant",
page_icon="🔍",
layout="wide",
)
# ── Session state init ────────────────────────────────────────────────────────
def _init_state() -> None:
defaults = {
"stage": "idle", # idle | awaiting_human | running | done
"session_id": None,
"query": "",
"plan": [],
"search_queries": [],
"completed_nodes": [],
"report": None,
"errors": [],
"checkpointer": None,
"graph": None,
}
for key, val in defaults.items():
if key not in st.session_state:
st.session_state[key] = val
# Build graph once per browser session
if st.session_state.checkpointer is None:
st.session_state.checkpointer = MemorySaver()
st.session_state.graph = build_graph(
checkpointer=st.session_state.checkpointer
)
_init_state()
# ── Async helpers ─────────────────────────────────────────────────────────────
async def _phase1(query: str, session_id: str, graph) -> dict:
"""Run planner → pause at human_review. Returns state snapshot values."""
await init_db()
config = {"configurable": {"thread_id": session_id}}
initial_state = {
"query": query,
"session_id": session_id,
"plan": [],
"search_queries": [],
"search_results": [],
"extracted_sources": [],
"summaries": [],
"final_report": None,
"human_feedback": None,
"retry_counts": {},
"error_log": [],
"node_costs": [],
"status": "running",
}
nodes: list[str] = []
async for event in graph.astream(initial_state, config, stream_mode="updates"):
for node_name in event:
if node_name != "__interrupt__":
nodes.append(node_name)
snapshot = await graph.aget_state(config)
return {"snapshot": snapshot, "nodes": nodes}
async def _phase2(session_id: str, feedback: str, graph) -> dict:
"""Resume after human review → run until done or next interrupt.
Returns 'next' so the caller can detect if the graph paused at
human_review again (redirect path) instead of completing.
"""
config = {"configurable": {"thread_id": session_id}}
await graph.aupdate_state(config, {"human_feedback": feedback})
nodes: list[str] = []
async for event in graph.astream(None, config, stream_mode="updates"):
for node_name in event:
if node_name != "__interrupt__":
nodes.append(node_name)
final = await graph.aget_state(config)
return {"values": final.values, "nodes": nodes, "next": list(final.next or [])}
# ── Sidebar ───────────────────────────────────────────────────────────────────
with st.sidebar:
st.title("AI Research Assistant")
st.caption("LangGraph · Groq · DuckDuckGo")
st.divider()
st.markdown("**Stack**")
st.markdown("- LLM: `llama-3.3-70b-versatile` (Groq free)")
st.markdown("- Search: DuckDuckGo (no API key)")
st.markdown("- Orchestration: LangGraph")
st.divider()
if st.session_state.session_id:
st.caption(f"Session: `{st.session_state.session_id[:8]}…`")
if st.session_state.stage != "idle":
if st.button("New Research", use_container_width=True):
for key in ["stage", "session_id", "query", "plan", "search_queries",
"completed_nodes", "report", "errors"]:
st.session_state[key] = [] if key in ("plan", "search_queries",
"completed_nodes", "errors") else \
None if key in ("report", "session_id") else \
"idle" if key == "stage" else ""
st.rerun()
# ── Main area ─────────────────────────────────────────────────────────────────
stage = st.session_state.stage
# ── IDLE ──────────────────────────────────────────────────────────────────────
if stage == "idle":
st.header("What do you want to research?")
query = st.text_area(
"Research query",
placeholder="e.g. Latest advances in quantum computing",
height=100,
label_visibility="collapsed",
)
auto_approve = st.checkbox("Auto-approve plan (skip review step)")
if st.button("Start Research", type="primary", disabled=not query.strip()):
st.session_state.query = query.strip()
st.session_state.session_id = str(uuid.uuid4())
st.session_state.completed_nodes = []
with st.spinner("Planning your research…"):
result = asyncio.run(
_phase1(
st.session_state.query,
st.session_state.session_id,
st.session_state.graph,
)
)
snapshot = result["snapshot"]
st.session_state.completed_nodes.extend(result["nodes"])
if snapshot.next and "human_review" in snapshot.next:
st.session_state.plan = snapshot.values.get("plan", [])
st.session_state.search_queries = snapshot.values.get("search_queries", [])
if auto_approve:
st.session_state.stage = "running"
st.session_state._pending_feedback = "approved"
else:
st.session_state.stage = "awaiting_human"
else:
# Graph completed without interrupting (no checkpointer edge case)
st.session_state.report = snapshot.values.get("final_report")
st.session_state.errors = snapshot.values.get("error_log", [])
st.session_state.stage = "done"
st.rerun()
# ── AWAITING HUMAN ────────────────────────────────────────────────────────────
elif stage == "awaiting_human":
st.header("Review the Research Plan")
col_plan, col_queries = st.columns(2)
with col_plan:
st.subheader("Plan")
for item in st.session_state.plan:
st.markdown(f"- {item}")
with col_queries:
st.subheader("Search Queries")
for q in st.session_state.search_queries:
st.markdown(f"- `{q}`")
st.divider()
st.subheader("What would you like to do?")
choice = st.radio(
"Action",
options=["Approve — run with this plan", "Redirect — re-plan with my feedback"],
label_visibility="collapsed",
)
if choice.startswith("Redirect"):
redirect_text = st.text_area(
"Your feedback to the planner",
placeholder="e.g. Focus only on open-source solutions",
height=80,
)
btn_label = "Send Feedback & Re-plan"
pending = redirect_text.strip() or "approved"
else:
redirect_text = ""
btn_label = "Approve & Start Research"
pending = "approved"
if st.button(btn_label, type="primary", disabled=(choice.startswith("Redirect") and not redirect_text.strip())):
st.session_state.stage = "running"
st.session_state._pending_feedback = pending
st.rerun()
# ── RUNNING ───────────────────────────────────────────────────────────────────
elif stage == "running":
st.header("Running Research Pipeline")
feedback = st.session_state.pop("_pending_feedback", "approved")
node_display = {
"human_review": "Human Review",
"searcher": "Searching the web",
"extractor": "Extracting sources",
"summarizer": "Summarizing sources",
"synthesizer": "Synthesizing report",
"storage_agent": "Saving results",
}
with st.status("Running agents…", expanded=True) as status:
for node in st.session_state.completed_nodes:
label = node_display.get(node, node)
st.write(f"✓ {label}")
result = asyncio.run(
_phase2(
st.session_state.session_id,
feedback,
st.session_state.graph,
)
)
for node in result["nodes"]:
label = node_display.get(node, node)
st.write(f"✓ {label}")
status.update(label="Done!", state="complete")
final_values = result["values"]
if "human_review" in result["next"]:
# Redirect path: planner re-ran and is waiting for human review again
st.session_state.plan = final_values.get("plan", [])
st.session_state.search_queries = final_values.get("search_queries", [])
st.session_state.completed_nodes.extend(result["nodes"])
st.session_state.stage = "awaiting_human"
else:
st.session_state.report = final_values.get("final_report")
st.session_state.errors = final_values.get("error_log", [])
st.session_state.stage = "done"
st.rerun()
# ── DONE ──────────────────────────────────────────────────────────────────────
elif stage == "done":
report = st.session_state.report
if not report:
st.error("Research completed but no report was generated.")
if st.session_state.errors:
for e in st.session_state.errors:
st.warning(f"[{e.get('node', '?')}] {e.get('error', '')}")
else:
st.success(f"Research complete: **{report.get('query', '')}**")
st.divider()
# Executive summary
st.subheader("Executive Summary")
st.write(report.get("executive_summary", ""))
# Key findings
findings = report.get("key_findings", [])
if findings:
st.subheader("Key Findings")
for i, f in enumerate(findings, 1):
st.markdown(f"**{i}.** {f}")
# Sources
sources = report.get("sources", [])
if sources:
st.subheader("Sources")
for s in sources:
url = s.get("url", "")
title = s.get("title", url)
contribution = s.get("key_contribution", "")
st.markdown(f"- [{title}]({url}) — {contribution}")
# Metadata footer
meta = report.get("metadata", {})
if meta:
st.divider()
st.caption(
f"Confidence: **{meta.get('confidence', 'N/A')}** · "
f"Sources: **{meta.get('num_sources', 0)}** · "
f"Gaps: {meta.get('gaps', 'None noted')}"
)
if st.session_state.errors:
with st.expander("Errors encountered"):
for e in st.session_state.errors:
st.error(f"[{e.get('node', '?')}] {e.get('error', '')}")