-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
142 lines (118 loc) · 4.46 KB
/
main.py
File metadata and controls
142 lines (118 loc) · 4.46 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
from __future__ import annotations
import json
import sys
from pathlib import Path
import typer
from rich.console import Console
from rich.table import Table
from support_agent.agent import SupportAgent
from support_agent.core.config import Settings
from support_agent.evaluation.evaluator import evaluate_sample
app = typer.Typer(help="Groundline support triage agent.")
console = Console()
DEFAULT_INPUT = Path("support_tickets/support_tickets.csv")
DEFAULT_OUTPUT = Path("support_tickets/output.csv")
DEFAULT_DEBUG_JSONL = Path("code/.cache/debug_predictions.jsonl")
@app.command()
def index(
recreate: bool = typer.Option(False, "--recreate", help="Drop and rebuild the Qdrant collection."),
) -> None:
"""Build the Qdrant semantic index for hybrid retrieval."""
settings = Settings.load()
agent = SupportAgent(settings)
count = agent.build_index(recreate=recreate)
if count:
console.print(f"[green]Indexed {count} support chunks in Qdrant collection '{settings.qdrant_collection}'.[/green]")
else:
console.print("[yellow]Qdrant semantic indexing is disabled or unavailable; BM25 fallback remains active.[/yellow]")
@app.command()
def run(
input_path: Path = typer.Option(
DEFAULT_INPUT,
"--input",
"-i",
help="Input support ticket CSV.",
),
output_path: Path = typer.Option(
DEFAULT_OUTPUT,
"--output",
"-o",
help="Output prediction CSV.",
),
debug_jsonl: Path | None = typer.Option(
DEFAULT_DEBUG_JSONL,
"--debug-jsonl",
help="Developer-only citation/debug JSONL path. Use --no-debug-jsonl to disable.",
),
) -> None:
"""Run the agent over a ticket CSV."""
run_agent(input_path, output_path, debug_jsonl)
def run_agent(
input_path: Path = DEFAULT_INPUT,
output_path: Path = DEFAULT_OUTPUT,
debug_jsonl: Path | None = DEFAULT_DEBUG_JSONL,
) -> None:
settings = Settings.load()
agent = SupportAgent(settings)
predictions = agent.run_csv(input_path, output_path, debug_jsonl)
console.print(f"[green]Wrote {len(predictions)} predictions to {output_path}[/green]")
if debug_jsonl:
console.print(f"[dim]Developer citations written to {debug_jsonl}[/dim]")
@app.command("eval")
def eval_command(
input_path: Path = typer.Option(
Path("support_tickets/sample_support_tickets.csv"),
"--input",
"-i",
help="Sample CSV with expected labels.",
),
) -> None:
"""Evaluate the current agent against the labeled sample CSV."""
result = evaluate_sample(input_path)
summary = result["summary"]
console.print(json.dumps(summary, indent=2))
table = Table(title="Sample Label Evaluation")
table.add_column("Row")
table.add_column("Status")
table.add_column("Request Type")
table.add_column("Product Area")
table.add_column("Expected")
table.add_column("Predicted")
for row in result["rows"]:
table.add_row(
str(row["row"]),
ok(row["status"]),
ok(row["request_type"]),
ok(row["product_area"]),
json.dumps(row["expected"]),
json.dumps(row["predicted"]),
)
console.print(table)
@app.command()
def debug(
issue: str = typer.Option(..., "--issue", "-q", help="Issue text to inspect."),
company: str = typer.Option("None", "--company", "-c", help="Company/domain hint."),
subject: str = typer.Option("", "--subject", "-s", help="Optional subject."),
) -> None:
"""Inspect one ad-hoc ticket and show developer citations."""
from support_agent.core.schemas import Ticket
agent = SupportAgent()
ticket = Ticket(row_id=0, issue=issue, subject=subject, company=company)
prediction = agent.answer(ticket)
console.print(safe_console_text(json.dumps(prediction.to_csv_row(), ensure_ascii=False, indent=2)))
citations = agent.citations.get(0)
for index, citation in enumerate(citations, start=1):
console.print(
f"\n[bold]Citation {index}[/bold] score={citation.score:.2f} "
f"area={citation.product_area} source={citation.source_path}"
)
console.print(safe_console_text(citation.text[:700]))
def ok(value: object) -> str:
return "[green]ok[/green]" if value else "[red]miss[/red]"
def safe_console_text(text: str) -> str:
return text.encode("cp1252", errors="replace").decode("cp1252")
if __name__ == "__main__":
if len(sys.argv) == 1:
run_agent()
else:
app()