-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
552 lines (495 loc) · 22.5 KB
/
Copy pathapp.py
File metadata and controls
552 lines (495 loc) · 22.5 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
from __future__ import annotations
import html
import json
import os
import sys
from typing import Any
import streamlit as st
def _load_streamlit_secrets() -> None:
keys = [
"OPENAI_API_KEY",
"OPENAI_MODEL",
"OPENAI_MODEL_DEEP",
"OPENAI_EMBEDDING_MODEL",
"DIALECTICA_DEMO_LLM",
"DIALECTICA_DEPLOYMENT_MODE",
"DIALECTICA_MCP_URL",
"MCP_AUTH_TOKEN",
"MCP_TIMEOUT_SECONDS",
"MAX_ANALYSES_PER_SESSION",
"MAX_DEEP_ANALYSES_PER_SESSION",
"MAX_QUESTION_CHARS",
"MAX_SOURCES_PUBLIC",
"MAX_RETRIEVAL_ITERATIONS",
]
try:
for key in keys:
if key in st.secrets and not os.getenv(key):
os.environ[key] = str(st.secrets[key])
except Exception:
pass
_load_streamlit_secrets()
from dialectica.config import settings # noqa: E402
from dialectica.demo_data import CORPORA, DEFAULT_CORPUS_ID, get_corpus # noqa: E402
from dialectica.mcp_client import MCPToolClient, build_tool_client # noqa: E402
from dialectica.ui import ( # noqa: E402
apply_theme,
render_evidence_card,
render_evidence_graph,
render_header,
render_json_schema,
render_status_row,
render_tool_log,
render_workflow_steps,
)
from dialectica.workflow import DialecticaWorkflow # noqa: E402
st.set_page_config(
page_title="Dialectica AI",
page_icon="🔎",
layout="wide",
initial_sidebar_state="expanded",
)
apply_theme()
def init_state() -> None:
defaults: dict[str, Any] = {
"analysis": None,
"analyses_used": 0,
"deep_analyses_used": 0,
"active_corpus_id": DEFAULT_CORPUS_ID,
"corpus_selector": DEFAULT_CORPUS_ID,
"question": get_corpus(DEFAULT_CORPUS_ID).questions[0],
"mcp_tools": [],
"mcp_resources": [],
"mcp_prompts": [],
"server_health": {},
}
for key, value in defaults.items():
if key not in st.session_state:
st.session_state[key] = value
init_state()
def new_client() -> MCPToolClient:
return build_tool_client(settings)
@st.cache_data(ttl=45, show_spinner=False)
def read_server_state() -> tuple[
bool,
str,
dict[str, Any],
list[dict[str, Any]],
list[dict[str, Any]],
]:
client = new_client()
ok, detail, health = client.health()
tools: list[dict[str, Any]] = []
corpora: list[dict[str, Any]] = []
if ok:
try:
tools = client.list_tools()
corpora = client.call("list_corpora", {}).get("corpora", [])
except Exception:
tools = []
corpora = []
return ok, detail, health, tools, corpora
mcp_ok, mcp_detail, server_health, detected_tools, server_corpora = read_server_state()
st.session_state.server_health = server_health
if mcp_ok:
st.session_state.mcp_tools = detected_tools
openai_live = bool(settings.openai_api_key) and not settings.demo_llm
index_stats = {
"documents": int(server_health.get("indexed_documents", 0)),
"passages": int(server_health.get("indexed_passages", 0)),
"corpus_count": int(server_health.get("corpus_count", len(server_corpora))),
"embedding_backend": server_health.get("embedding_backend", "Not initialized"),
}
corpus_stats_by_id = {item.get("corpus_id"): item for item in server_corpora}
def clear_current_analysis() -> None:
"""Clear visible results without resetting public usage allowances."""
st.session_state.analysis = None
corpus = get_corpus(st.session_state.active_corpus_id)
st.session_state.question = corpus.questions[0]
def change_corpus() -> None:
corpus_id = st.session_state.corpus_selector
st.session_state.active_corpus_id = corpus_id
st.session_state.analysis = None
st.session_state.question = get_corpus(corpus_id).questions[0]
with st.sidebar:
st.markdown("### Dialectica status")
st.caption("A bounded, inspectable evidence-reasoning workflow")
if openai_live:
st.markdown('<div class="sidebar-ok">OpenAI project key active</div>', unsafe_allow_html=True)
else:
st.markdown('<div class="sidebar-warn">Deterministic demo reasoning active</div>', unsafe_allow_html=True)
if mcp_ok:
st.markdown('<div class="sidebar-ok">Remote MCP server connected</div>', unsafe_allow_html=True)
else:
st.markdown('<div class="sidebar-warn">MCP server unavailable</div>', unsafe_allow_html=True)
st.metric("Prepared papers", index_stats["documents"])
st.metric("Indexed passages", index_stats["passages"])
st.metric("Evidence corpora", index_stats["corpus_count"])
remaining = max(0, settings.max_analyses_per_session - int(st.session_state.analyses_used))
st.metric("Analyses remaining", remaining)
st.markdown(f"**Corpus:** {get_corpus(st.session_state.active_corpus_id).short_label}")
st.caption(f"Embeddings: {index_stats['embedding_backend']}")
st.caption("Sources: peer-reviewed ACL Anthology papers")
st.caption("Evidence map: run-scoped JSON through MCP")
st.caption("The public demo never asks visitors for an API key.")
st.divider()
if st.button("Clear current analysis", use_container_width=True):
clear_current_analysis()
st.rerun()
render_header()
render_status_row(
[
("OpenAI GPT" if openai_live else "Demo reasoner", True),
("Remote MCP", mcp_ok),
("LangGraph ready", True),
("ChromaDB ready", mcp_ok and index_stats["passages"] > 0),
]
)
with st.expander("How Dialectica AI works"):
st.markdown(
"""
Dialectica analyzes a research question through a bounded, stateful workflow:
1. **Planner:** decomposes the question into focused and counterevidence queries.
2. **Researcher:** calls remote MCP retrieval tools against the ChromaDB passage index.
3. **Evidence extractor:** produces typed claims, stances, quotations, and limitations.
4. **Verifier:** checks exact citation matches and whether passages support each claim.
5. **Critic:** evaluates coverage and can route LangGraph through one refined search.
6. **Synthesizer:** writes a conclusion using verified evidence only.
7. **Persistence:** saves and retrieves a run-scoped evidence map through MCP.
**LangGraph controls the workflow. MCP exposes reusable capabilities. ChromaDB stores curated evidence notes with paper metadata. OpenAI models produce schema-validated reasoning outputs.**
"""
)
if not openai_live:
st.info(
"The application is currently using deterministic demonstration reasoning. "
"Add the project OpenAI key to `.env` locally or Streamlit Secrets online to enable live model results."
)
if not mcp_ok:
st.error(
"The remote evidence service is not ready. On free hosting it may need about a minute to wake up. "
"Wait briefly, then retry the connection."
)
if st.button("Retry MCP connection"):
read_server_state.clear()
st.rerun()
with st.expander("Technical connection detail"):
st.code(mcp_detail, language="text")
st.stop()
if settings.is_cloud and not settings.mcp_auth_token:
st.error("Cloud deployment is missing the server-side MCP_AUTH_TOKEN secret.")
st.stop()
st.markdown("## 1. Evidence source")
st.markdown(
'<p class="section-help">Choose one curated corpus of peer-reviewed, open-access AI papers. The corpus selection updates the example questions automatically.</p>',
unsafe_allow_html=True,
)
selected_corpus_id = st.selectbox(
"Prepared open-access corpus",
options=list(CORPORA),
format_func=lambda corpus_id: CORPORA[corpus_id].label,
key="corpus_selector",
on_change=change_corpus,
)
selected_corpus = get_corpus(selected_corpus_id)
selected_stats = corpus_stats_by_id.get(selected_corpus_id, {})
years = sorted({paper.year for paper in selected_corpus.papers})
st.info(
f"**{selected_corpus.label}** — {int(selected_stats.get('documents', len(selected_corpus.papers)))} papers, "
f"{int(selected_stats.get('passages', sum(len(p.passages) for p in selected_corpus.papers)))} indexed evidence notes, "
f"{min(years)}–{max(years)}. {selected_corpus.description}"
)
st.caption(
"The indexed text consists of curator-written, attribution-preserving summaries derived from CC BY 4.0 ACL Anthology papers. "
"Evidence cards link to each original paper for the authors' wording and full context."
)
with st.expander("Papers in this corpus"):
for paper in selected_corpus.papers:
st.markdown(
f"**{paper.title}** \n"
f"{paper.citation_label} · {paper.venue} · pp. {paper.page_range} · {paper.license} \n"
f"[{paper.doi}]({paper.source_url})"
)
st.markdown("## 2. Ask a research question")
st.markdown(
'<p class="section-help">Choose an example or write a focused question that can be answered from the prepared evidence.</p>',
unsafe_allow_html=True,
)
example_col, use_col = st.columns([4, 1])
with example_col:
selected_example = st.selectbox(
"Try an example question",
list(selected_corpus.questions),
key=f"example_question_{selected_corpus_id}",
)
with use_col:
st.write("")
st.write("")
if st.button("Use example", use_container_width=True):
st.session_state.question = selected_example
st.rerun()
question = st.text_area(
"Research question",
key="question",
height=110,
max_chars=settings.max_question_chars,
placeholder="Does chain-of-thought prompting improve reasoning faithfulness?",
)
st.caption(f"Maximum {settings.max_question_chars} characters. No API key is required from the visitor.")
st.markdown("## 3. Analyze evidence")
st.markdown(
'<p class="section-help">The execution view exposes real LangGraph nodes and MCP tool calls while the main workflow remains simple.</p>',
unsafe_allow_html=True,
)
with st.expander("Advanced analysis settings"):
mode_label = st.radio("Analysis mode", ["Standard", "Deep Review"], horizontal=True)
max_sources = st.slider(
"Maximum source passages",
4,
settings.max_sources_public,
min(8, settings.max_sources_public),
)
max_iterations = st.select_slider(
"Maximum retrieval iterations",
options=list(range(1, settings.max_retrieval_iterations + 1)),
value=settings.max_retrieval_iterations,
help="The workflow is deliberately bounded to prevent uncontrolled loops.",
)
st.caption(
f"Standard model: `{settings.standard_model}` · Deep-review model: `{settings.deep_model}`"
)
remaining = settings.max_analyses_per_session - int(st.session_state.analyses_used)
deep_remaining = settings.max_deep_analyses_per_session - int(st.session_state.deep_analyses_used)
mode = "deep" if mode_label == "Deep Review" else "standard"
analyze_disabled = (
not question.strip()
or remaining <= 0
or (mode == "deep" and deep_remaining <= 0)
or selected_corpus_id not in corpus_stats_by_id
)
if mode == "deep" and deep_remaining <= 0:
st.warning("The Deep Review allowance for this browser session has been used.")
if st.button("Analyze Evidence", type="primary", use_container_width=True, disabled=analyze_disabled):
run_client = new_client()
workflow = DialecticaWorkflow(settings, tools=run_client)
state = workflow.initial_state(
question=question,
corpus_id=selected_corpus_id,
mode=mode,
max_sources=max_sources,
max_iterations=max_iterations,
)
merged_state: dict[str, Any] = dict(state)
try:
st.session_state.mcp_tools = run_client.list_tools()
try:
st.session_state.mcp_resources = run_client.list_resources()
st.session_state.mcp_prompts = run_client.list_prompts()
except Exception:
st.session_state.mcp_resources = []
st.session_state.mcp_prompts = []
with st.status("Dialectica is analyzing the evidence...", expanded=True) as status:
for node, update in workflow.stream(state):
merged_state.update(update)
steps = update.get("workflow_steps") or merged_state.get("workflow_steps") or []
latest = steps[-1] if steps else {"summary": "Completed"}
status.write(f"✓ **{node.replace('_', ' ').title()}** — {latest.get('summary', '')}")
status.update(label="Evidence analysis complete", state="complete", expanded=False)
st.session_state.analysis = merged_state
st.session_state.analyses_used += 1
if mode == "deep":
st.session_state.deep_analyses_used += 1
st.rerun()
except Exception as exc:
message = str(exc)
if "401" in message or "Unauthorized" in message:
friendly = "The Streamlit app could not authenticate with the MCP server. Check that both deployments use the same MCP_AUTH_TOKEN."
elif "429" in message or "quota" in message.lower() or "rate" in message.lower():
friendly = "The public OpenAI project has reached a temporary usage or rate limit. Please try again later."
elif "timeout" in message.lower() or "connect" in message.lower():
friendly = "The evidence service is temporarily unavailable or waking up. Please retry shortly."
else:
friendly = "The workflow stopped safely before presenting an unverified result. Please retry with an example question."
st.error(friendly)
if not settings.is_cloud and os.getenv("DIALECTICA_DEBUG", "").lower() in {"1", "true", "yes"}:
st.exception(exc)
if remaining <= 0:
st.warning("The public analysis allowance for this browser session has been used.")
analysis = st.session_state.analysis
if analysis:
st.divider()
st.markdown("## Evidence analysis workspace")
overview_tab, evidence_tab, graph_tab, workflow_tab, quality_tab, mcp_tab = st.tabs(
["Overview", "Evidence", "Evidence Map", "LangGraph Workflow", "Run Quality", "MCP Tools"]
)
report = analysis.get("final_report", {})
evidence_items = analysis.get("evidence_items", [])
verifications = analysis.get("verification_items", [])
verification_map = {item["evidence_id"]: item for item in verifications}
accepted_ids = {item["evidence_id"] for item in verifications if item.get("accepted")}
accepted_evidence = [item for item in evidence_items if item.get("evidence_id") in accepted_ids]
with overview_tab:
verdict = str(report.get("verdict", "insufficient evidence")).title()
confidence = str(report.get("confidence", "low")).title()
st.markdown(
f"""
<section class="verdict-box">
<div class="small-cap">Overall assessment</div>
<h3>{html.escape(verdict)} · {html.escape(confidence)} confidence</h3>
<p>{html.escape(str(report.get('answer', 'No synthesis is available.')))}</p>
</section>
""",
unsafe_allow_html=True,
)
papers_used = len({item.get("document_id") for item in accepted_evidence})
opposed = sum(1 for item in accepted_evidence if item.get("stance") == "opposes")
coverage = float(analysis.get("critic", {}).get("coverage_score", 0.0))
cols = st.columns(4)
cols[0].metric("Verified evidence", len(accepted_evidence))
cols[1].metric("Papers used", papers_used)
cols[2].metric("Opposing findings", opposed)
cols[3].metric("Evidence coverage", f"{coverage:.0%}")
st.markdown("### Evidence balance")
st.markdown(f"**Supporting:** {report.get('supporting_summary', 'Not available')}")
st.markdown(f"**Opposing:** {report.get('opposing_summary', 'Not available')}")
st.markdown(f"**Key qualification:** {report.get('qualification', 'Not available')}")
with st.expander("Limitations"):
for limitation in report.get("limitations", []):
st.markdown(f"- {limitation}")
export_payload = {
"run_id": analysis.get("run_id"),
"question": analysis.get("question"),
"report": report,
"evidence": accepted_evidence,
"verifications": [item for item in verifications if item.get("evidence_id") in accepted_ids],
"workflow": analysis.get("workflow_steps", []),
"mcp_tool_log": analysis.get("tool_log", []),
}
markdown_export = "\n".join(
[
"# Dialectica AI Evidence Report",
"",
f"**Question:** {analysis.get('question', '')}",
"",
f"## {verdict} — {confidence} confidence",
"",
report.get("answer", ""),
"",
"## Key qualification",
"",
report.get("qualification", ""),
"",
"## Limitations",
*[f"- {item}" for item in report.get("limitations", [])],
]
)
download_cols = st.columns(2)
download_cols[0].download_button(
"Download JSON report",
data=json.dumps(export_payload, indent=2, ensure_ascii=False),
file_name=f"{analysis.get('run_id', 'dialectica')}.json",
mime="application/json",
use_container_width=True,
)
download_cols[1].download_button(
"Download Markdown report",
data=markdown_export,
file_name=f"{analysis.get('run_id', 'dialectica')}.md",
mime="text/markdown",
use_container_width=True,
)
with evidence_tab:
support_tab, oppose_tab, uncertain_tab, rejected_tab = st.tabs(
["Supporting", "Opposing", "Uncertain", "Rejected"]
)
groups = {"supports": support_tab, "opposes": oppose_tab, "uncertain": uncertain_tab}
for stance, tab in groups.items():
with tab:
items = [
item for item in evidence_items
if item.get("stance") == stance
and verification_map.get(item.get("evidence_id"), {}).get("accepted")
]
if not items:
st.info(f"No verified {stance} evidence was found.")
for item in items:
render_evidence_card(item, verification_map.get(item["evidence_id"]))
with rejected_tab:
rejected = [
item for item in evidence_items
if not verification_map.get(item.get("evidence_id"), {}).get("accepted")
]
if not rejected:
st.info("The verifier did not reject any extracted evidence in this run.")
for item in rejected:
render_evidence_card(item, verification_map.get(item["evidence_id"]))
with graph_tab:
st.caption("The evidence map is saved and retrieved through MCP as a run-scoped JSON projection. Paper nodes retain source metadata and links.")
render_evidence_graph(analysis.get("question", "Research question"), evidence_items, verifications)
with workflow_tab:
st.markdown("### LangGraph execution")
st.caption("The critic conditionally routes to refinement or synthesis. The workflow is bounded to at most two retrieval passes.")
render_workflow_steps(analysis.get("workflow_steps", []))
path = " → ".join(step.get("node", "") for step in analysis.get("workflow_steps", []))
st.code(f"START → {path} → END", language="text")
cols = st.columns(3)
total_ms = sum(int(item.get("duration_ms", 0)) for item in analysis.get("workflow_steps", []))
cols[0].metric("Workflow duration", f"{total_ms / 1000:.1f} s")
cols[1].metric("Retrieval iterations", analysis.get("iteration", 1))
cols[2].metric("MCP calls", len(analysis.get("tool_log", [])))
with quality_tab:
total = len(verifications)
citation_valid = sum(1 for item in verifications if item.get("citation_found"))
accepted_count = len(accepted_evidence)
tool_log = analysis.get("tool_log", [])
successful_tools = sum(1 for item in tool_log if item.get("success"))
has_counterevidence = any(item.get("stance") == "opposes" for item in accepted_evidence)
cols = st.columns(4)
cols[0].metric("Citation validity", f"{(citation_valid / total if total else 0):.0%}")
cols[1].metric("Evidence acceptance", f"{(accepted_count / total if total else 0):.0%}")
cols[2].metric("Tool success", f"{(successful_tools / len(tool_log) if tool_log else 0):.0%}")
cols[3].metric("Counterevidence", "Present" if has_counterevidence else "Missing")
with mcp_tab:
st.markdown("### MCP Server Explorer")
server_cols = st.columns(4)
server_cols[0].metric("Connection", "Connected")
server_cols[1].metric("Transport", "Streamable HTTP")
server_cols[2].metric("Tools", len(st.session_state.mcp_tools or []))
server_cols[3].metric("Calls this run", len(analysis.get("tool_log", [])))
st.caption("Authentication is server-to-server bearer-token authentication; the token is stored only in deployment secrets.")
for tool in st.session_state.mcp_tools or []:
with st.expander(tool.get("name", "tool")):
st.write(tool.get("description") or "No description supplied.")
schema = tool.get("input_schema") or {}
if schema:
render_json_schema(schema)
if st.session_state.mcp_resources:
st.markdown("### MCP resources")
st.json(st.session_state.mcp_resources)
if st.session_state.mcp_prompts:
st.markdown("### MCP prompts")
st.json(st.session_state.mcp_prompts)
st.markdown("### Current run tool log")
render_tool_log(analysis.get("tool_log", []))
st.markdown("### Safe manual tool test")
manual_query = st.text_input(
"Semantic search query",
value=(
"post-hoc rationale faithfulness"
if selected_corpus_id == DEFAULT_CORPUS_ID
else "retrieval hallucination groundedness"
),
key="manual_mcp_query",
max_chars=180,
)
if st.button("Call semantic_search through MCP"):
try:
result = new_client().call(
"semantic_search",
{"query": manual_query, "corpus_id": selected_corpus_id, "top_k": 3},
)
st.json(result)
except Exception:
st.error("The safe MCP test could not be completed. Please retry shortly.")
st.caption(
f"Python {sys.version_info.major}.{sys.version_info.minor} · Dialectica AI v1.1-open-access · Remote MCP + LangGraph"
)