-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtui.py
More file actions
287 lines (242 loc) · 10.8 KB
/
Copy pathtui.py
File metadata and controls
287 lines (242 loc) · 10.8 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
import os
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from rich.prompt import Prompt, Confirm
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.rule import Rule
from rich.text import Text
from rich import box
console = Console()
def parse_threshold(raw_threshold):
try:
threshold = float(raw_threshold)
if not (0.0 <= threshold <= 1.0):
raise ValueError
except (TypeError, ValueError):
return 0.30
return threshold
def is_valid_threshold(raw_threshold):
try:
threshold = float(raw_threshold)
except (TypeError, ValueError):
return False
return 0.0 <= threshold <= 1.0
def parse_positive_int(raw_value, default, minimum=1):
try:
return max(minimum, int(raw_value))
except (TypeError, ValueError):
return default
def parse_csv(raw_value):
return [item.strip() for item in (raw_value or "").split(",") if item.strip()]
def collect_inputs():
console.clear()
console.print(Panel.fit(
"[bold cyan]Halgorithem[/bold cyan] [dim]©2026 Tangible Research[/dim]\n"
"[dim]AI Output Verification Engine[/dim]",
border_style="cyan",
padding=(1, 4),
))
console.print()
existing_key = os.environ.get("OPENAI_API_KEY", "")
if existing_key:
console.print(f"[dim]Using existing OPENAI_API_KEY ({existing_key[:4]}...)[/dim]")
else:
api_key = Prompt.ask("[bold green]OpenAI API key[/bold green]")
if not api_key.strip():
console.print("[red]No API key provided. Exiting.[/red]")
raise SystemExit(1)
os.environ["OPENAI_API_KEY"] = api_key
console.print(f"[dim green]Key set ({api_key[:4]}...)[/dim green]")
console.print()
console.print(Rule("[bold]Source Documents[/bold]", style="cyan"))
console.print()
source_mode = Prompt.ask(
"[bold blue]Source type[/bold blue]",
choices=["urls", "files", "both"],
default="urls",
)
urls, truth_file_paths = [], []
if source_mode in ("urls", "both"):
urls = parse_csv(Prompt.ask("[bold blue]URLs[/bold blue] [dim](comma-separated)[/dim]", default=""))
if urls:
console.print(f" [dim]→ {len(urls)} URL(s) queued[/dim]")
if source_mode in ("files", "both"):
truth_file_paths = parse_csv(Prompt.ask("[bold blue]File paths[/bold blue] [dim](comma-separated)[/dim]", default=""))
if truth_file_paths:
console.print(f" [dim]→ {len(truth_file_paths)} file(s) queued[/dim]")
if not urls and not truth_file_paths:
console.print("[red]No sources provided. Exiting.[/red]")
raise SystemExit(1)
console.print()
console.print(Rule("[bold]Settings[/bold]", style="cyan"))
console.print()
raw_threshold = Prompt.ask(
"[bold magenta]Verification threshold[/bold magenta] [dim](0.0–1.0)[/dim]",
default="0.30",
)
threshold = parse_threshold(raw_threshold)
if not is_valid_threshold(raw_threshold):
console.print("[yellow]Invalid threshold — defaulting to 0.30[/yellow]")
raw_chunks = Prompt.ask(
"[bold magenta]Sentences per chunk[/bold magenta]",
default="2",
)
sentences_per_chunk = parse_positive_int(raw_chunks, default=2, minimum=1)
raw_overlap = Prompt.ask(
"[bold magenta]Sentence overlap[/bold magenta]",
default="1",
)
sentence_overlap = parse_positive_int(raw_overlap, default=1, minimum=0)
console.print()
console.print(Rule("[bold]Query[/bold]", style="cyan"))
console.print()
prompt = Prompt.ask("[bold yellow]Your prompt[/bold yellow]")
console.print()
return {
"prompt": prompt,
"urls": urls,
"truth_file_paths": truth_file_paths,
"threshold": threshold,
"sentences_per_chunk": sentences_per_chunk,
"sentence_overlap": sentence_overlap,
}
def run_engine(config, engine_class=None):
if engine_class is None:
from engine import Engine
engine_class = Engine
eng = engine_class(
sentences_per_chunk=config["sentences_per_chunk"],
sentence_overlap=config["sentence_overlap"],
)
with Progress(
SpinnerColumn(style="cyan"),
TextColumn("[progress.description]{task.description}"),
console=console,
transient=True,
) as progress:
task = progress.add_task("Scraping sources...", total=None)
source_docs = eng._load_sources(config["urls"], config["truth_file_paths"])
progress.update(task, description="Generating AI response...")
ai_output = eng.generate(config["prompt"], source_docs)
progress.update(task, description="Verifying claims...")
verification = eng.verify(ai_output, source_docs, threshold=config["threshold"])
return source_docs, ai_output, verification
def render_results(source_docs, ai_output, verification, config):
console.clear()
console.print(Panel.fit(
"[bold cyan]Results[/bold cyan]",
border_style="cyan",
padding=(0, 4),
))
console.print()
# AI Output
console.print(Rule("[bold]AI Output[/bold]", style="dim"))
console.print()
console.print(ai_output)
console.print()
# Sources
console.print(Rule("[bold]Sources[/bold]", style="dim"))
console.print()
for s in source_docs:
console.print(f" [dim cyan]•[/dim cyan] {s['file_path']}")
console.print()
# Settings used
console.print(Rule("[bold]Run Config[/bold]", style="dim"))
console.print()
config_table = Table(box=box.SIMPLE, show_header=False, padding=(0, 2))
config_table.add_column(style="dim")
config_table.add_column(style="bold white")
config_table.add_row("Threshold", str(config["threshold"]))
config_table.add_row("Sentences/chunk", str(config["sentences_per_chunk"]))
config_table.add_row("Overlap", str(config["sentence_overlap"]))
config_table.add_row("Sources", str(len(source_docs)))
diagnostics = verification.get("diagnostics") or {}
if diagnostics:
config_table.add_row("Embedder", str(diagnostics.get("embedder", "unknown")))
if diagnostics.get("embedding_model"):
config_table.add_row("Embedding model", str(diagnostics["embedding_model"]))
if diagnostics.get("embedding_fallback_reason"):
config_table.add_row("Fallback", str(diagnostics["embedding_fallback_reason"])[:80])
console.print(config_table)
console.print()
# Summary
claims = verification["claims"]
total = len(claims)
supported = sum(1 for c in claims if c["status"] == "SUPPORTED")
weak = sum(1 for c in claims if c["status"] == "WEAK_SUPPORT")
contradicts = sum(1 for c in claims if c["status"] == "CONTRADICTION")
hallucinated = sum(1 for c in claims if c["status"] == "HALLUCINATION")
denials = sum(1 for c in claims if c["status"] == "UNVERIFIABLE_DENIAL")
console.print(Rule("[bold]Verification Summary[/bold]", style="dim"))
console.print()
summary_table = Table(box=box.SIMPLE, show_header=False, padding=(0, 2))
summary_table.add_column(style="dim")
summary_table.add_column(justify="right", style="bold")
summary_table.add_row("Supported", f"[green]{supported}/{total}[/green]")
summary_table.add_row("Weak support", f"[yellow]{weak}/{total}[/yellow]")
summary_table.add_row("Contradictions", f"[red]{contradicts}/{total}[/red]")
summary_table.add_row("Hallucinations", f"[bold red]{hallucinated}/{total}[/bold red]")
summary_table.add_row("Unverifiable denials", f"[magenta]{denials}/{total}[/magenta]")
console.print(summary_table)
console.print()
# Claim detail
bad_claims = [c for c in claims if c["status"] in ("CONTRADICTION", "HALLUCINATION")]
uncertain_claims = [c for c in claims if c["status"] == "UNVERIFIABLE_DENIAL"]
ok_claims = [c for c in claims if c["status"] in ("SUPPORTED", "WEAK_SUPPORT")]
if ok_claims and Confirm.ask("[dim]Show supported claims too?[/dim]", default=False):
console.print(Rule("[bold]Supported Claims[/bold]", style="dim green"))
console.print()
for c in ok_claims:
colour = "green" if c["status"] == "SUPPORTED" else "yellow"
label = "SUPPORTED" if c["status"] == "SUPPORTED" else "WEAK"
console.print(f" [{colour}]{label}[/{colour}] {c.get('claim','')}")
if c.get("unsupported_terms"):
console.print(f" [dim]unsupported terms: {', '.join(c['unsupported_terms'])}[/dim]")
console.print()
if uncertain_claims:
console.print(Rule("[bold]Needs Review[/bold]", style="magenta"))
console.print()
for c in uncertain_claims:
console.print(Panel(
f"[magenta]{c['status']}[/magenta] [dim]score {round(c.get('score', 0), 3)}[/dim]\n\n"
f"{c.get('claim', '')}\n\n"
+ (f"[dim]Unsupported terms:[/dim] {', '.join(c['unsupported_terms'])}\n"
if c.get("unsupported_terms") else "")
+ (f"\n[dim]Closest chunk ({c.get('matched_source','')}, "
f"chunk {c.get('matched_chunk_id','')}):[/dim]\n{c.get('chunk_text','')}"
if c.get("chunk_text") else ""),
border_style="magenta",
padding=(1, 2),
))
console.print()
if bad_claims:
console.print(Rule("[bold]Issues[/bold]", style="red"))
console.print()
for c in bad_claims:
colour = "red" if c["status"] == "HALLUCINATION" else "bold red"
console.print(Panel(
f"[{colour}]{c['status']}[/{colour}] [dim]score {round(c.get('score', 0), 3)}[/dim]\n\n"
f"{c.get('claim', '')}\n\n"
+ (f"[dim]Reason:[/dim] {c.get('reason', '')}\n" if c.get('reason') else "")
+ (f"[dim]AI numbers:[/dim] {c.get('ai_numbers','')} "
f"[dim]Truth numbers:[/dim] {c.get('truth_numbers','')}\n"
if c.get("ai_numbers") else "")
+ (f"[dim]Unsupported terms:[/dim] {', '.join(c['unsupported_terms'])}\n"
if c.get("unsupported_terms") else "")
+ (f"\n[dim]Closest chunk ({c.get('matched_source','')}, "
f"chunk {c.get('matched_chunk_id','')}):[/dim]\n{c.get('chunk_text','')}"
if c.get("chunk_text") else ""),
border_style="red",
padding=(1, 2),
))
console.print()
else:
console.print("[bold green]✓ No hallucinations or contradictions found.[/bold green]")
console.print()
def main():
config = collect_inputs()
source_docs, ai_output, verification = run_engine(config)
render_results(source_docs, ai_output, verification, config)
if __name__ == "__main__":
main()