-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli.py
More file actions
105 lines (83 loc) · 3.16 KB
/
Copy pathcli.py
File metadata and controls
105 lines (83 loc) · 3.16 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
import typer
from . import config
from .data import load_cases
app = typer.Typer(add_completion=False, help="Example Opik use-case demo — the full eval loop.")
@app.command()
def run(input: str, context: list[str] = typer.Option(None, "--context", "-c")) -> None:
"""Run the app on a single input (traced in Opik when credentials are set)."""
if config.DRY_RUN:
typer.echo("[DRY RUN] Opik creds not set — would run the app on:")
typer.echo(f" input: {input}")
for line in context or []:
typer.echo(f" context: {line}")
return
from .app import run as run_app
result = run_app({"input": input, "context": context or []})
typer.echo(f"\nOutput:\n{result['output']}")
@app.command("eval")
def eval_cmd() -> None:
"""Create the Opik dataset + test suite and run evaluation (metrics + assertions)."""
cases = load_cases()
if config.DRY_RUN:
typer.echo(
"[DRY RUN] Opik creds not set. Would create dataset "
f"'{config.DATASET_NAME}' and test suite '{config.SUITE_NAME}' with:"
)
for case in cases:
typer.echo(f" input: {case['input']}")
for assertion in case["assertions"]:
typer.echo(f" assert: {assertion}")
return
from . import evaluation
suite_result, _ = evaluation.run_eval(cases)
typer.echo(f"Test-suite pass rate: {suite_result.pass_rate:.0%}")
url = getattr(suite_result, "experiment_url", None)
if url:
typer.echo(f"Experiment: {url}")
@app.command()
def optimize() -> None:
"""Run Optimization Studio against the eval dataset to improve the prompt."""
cases = load_cases()
if config.DRY_RUN:
typer.echo(
"[DRY RUN] Would run MetaPromptOptimizer on prompt "
f"'{config.PROMPT_NAME}' against dataset '{config.DATASET_NAME}'."
)
return
import opik
from . import evaluation, optimization
client = opik.Opik()
dataset = evaluation.build_dataset(client, cases)
result = optimization.run_optimization(dataset)
typer.echo(f"Score: {result.initial_score} -> {result.score}")
link = result.get_run_link() if hasattr(result, "get_run_link") else None
if link:
typer.echo(f"Optimization run: {link}")
@app.command()
def promote() -> None:
"""Optimise the prompt and save the result to the Opik Prompt Library (versioned)."""
cases = load_cases()
if config.DRY_RUN:
typer.echo(
f"[DRY RUN] Would save optimised prompt '{config.PROMPT_NAME}' to the Opik Prompt Library."
)
return
import opik
from . import evaluation, optimization, prompts
client = opik.Opik()
dataset = evaluation.build_dataset(client, cases)
result = optimization.run_optimization(dataset)
prompts.promote(client, result)
typer.echo(
f"Saved prompt '{config.PROMPT_NAME}' to the Prompt Library (optimised score {result.score:.3f})."
)
@app.command("run-all")
def run_all() -> None:
"""Run the full demo loop: eval -> optimize -> promote."""
eval_cmd()
optimize()
promote()
def main() -> None:
app()
if __name__ == "__main__":
main()