-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.py
More file actions
149 lines (135 loc) · 4.41 KB
/
Copy pathcli.py
File metadata and controls
149 lines (135 loc) · 4.41 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
#!/usr/bin/env python3
from __future__ import annotations
import asyncio
from typing import Optional
import typer
from rich.console import Console
from detector import AuditRunner
from detector.config import resolve_target_spec
from detector.report import format_json_report, format_text_report
from detector.store import AuditStore
from detector.config import (
resolve_cors_allow_origin,
resolve_dashboard_api_base_url,
resolve_db_path,
)
from detector.web import serve_dashboard
app = typer.Typer(add_completion=False)
console = Console()
status_console = Console(stderr=True)
@app.callback(invoke_without_command=True)
def main(ctx: typer.Context) -> None:
if ctx.invoked_subcommand is None:
console.print(ctx.get_help())
raise typer.Exit()
@app.command()
def audit(
model: Optional[str] = typer.Option(
None,
"--model",
"-m",
help="Target model name. Defaults by provider when omitted.",
envvar="LLM_AUDIT_MODEL",
),
api_key: Optional[str] = typer.Option(
None,
"--api-key",
"-k",
help="API key. Falls back to env if omitted.",
envvar="LLM_AUDIT_API_KEY",
),
base_url: Optional[str] = typer.Option(
None,
"--base-url",
"-b",
help="API base URL. Optional for native providers.",
envvar="LLM_AUDIT_BASE_URL",
),
provider: Optional[str] = typer.Option(
None,
"--provider",
help="Provider style: openai, anthropic, google, or openai-compatible.",
envvar="LLM_AUDIT_PROVIDER",
),
db_path: Optional[str] = typer.Option(
None,
"--db-path",
help="SQLite file path for saved audits.",
envvar="LLM_AUDIT_DB_PATH",
),
json_output: bool = typer.Option(False, "--json", help="Print JSON only."),
):
try:
target = resolve_target_spec(
provider=provider,
base_url=base_url,
api_key=api_key,
model=model,
)
except ValueError as exc:
console.print(f"[bold red]Config error:[/] {exc}")
raise typer.Exit(code=2)
report = asyncio.run(AuditRunner(target).run())
store = AuditStore(resolve_db_path(db_path))
run_id = store.save_report(report)
output = format_json_report(report) if json_output else format_text_report(report)
console.print(output)
status_console.print(f"[dim]Saved run {run_id} to {store.path}[/dim]")
@app.command()
def history(
limit: int = typer.Option(20, "--limit", "-n", min=1, max=200, help="Number of runs to show."),
db_path: Optional[str] = typer.Option(
None,
"--db-path",
help="SQLite file path for saved audits.",
envvar="LLM_AUDIT_DB_PATH",
),
):
store = AuditStore(resolve_db_path(db_path))
runs = store.list_runs(limit=limit)
if not runs:
console.print("No saved runs yet.")
return
console.print("[bold]Audit History[/bold]")
for run in runs:
console.print(
f"#{run.run_id} | {run.created_at} | {run.provider} | {run.resolved_model} | "
f"{run.quality_score:.0f}/100 ({run.quality_tier}) | "
f"{run.likely_model_range or '-'} | {run.claim_consistency_confidence:.0f}% | "
f"{run.summary}"
)
@app.command()
def dashboard(
host: str = typer.Option("127.0.0.1", "--host", help="Dashboard host."),
port: int = typer.Option(8765, "--port", help="Dashboard port."),
db_path: Optional[str] = typer.Option(
None,
"--db-path",
help="SQLite file path for saved audits.",
envvar="LLM_AUDIT_DB_PATH",
),
api_base_url: Optional[str] = typer.Option(
None,
"--api-base-url",
help="Public backend API base URL for a separated frontend.",
envvar="LLM_AUDIT_DASHBOARD_API_BASE_URL",
),
cors_allow_origin: Optional[str] = typer.Option(
None,
"--cors-allow-origin",
help="Allowed frontend origin for cross-origin requests.",
envvar="LLM_AUDIT_CORS_ALLOW_ORIGIN",
),
):
status_console.print(
f"[dim]Starting dashboard on http://{host}:{port} using {resolve_db_path(db_path)}[/dim]"
)
serve_dashboard(
host=host,
port=port,
db_path=db_path,
api_base_url=resolve_dashboard_api_base_url(api_base_url),
cors_allow_origin=resolve_cors_allow_origin(cors_allow_origin),
)
if __name__ == "__main__":
app()