Upload aco/cli.py
Browse files- aco/cli.py +123 -0
aco/cli.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""CLI for Agent Cost Optimizer."""
|
| 2 |
+
|
| 3 |
+
import argparse
|
| 4 |
+
import json
|
| 5 |
+
import sys
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
from aco.optimizer import AgentCostOptimizer
|
| 9 |
+
from aco.config import ACOConfig
|
| 10 |
+
from aco.benchmarks.benchmark_suite import BenchmarkSuite
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def main():
|
| 14 |
+
parser = argparse.ArgumentParser(description="Agent Cost Optimizer")
|
| 15 |
+
subparsers = parser.add_subparsers(dest="command", help="Command to run")
|
| 16 |
+
|
| 17 |
+
# Optimize command
|
| 18 |
+
opt_parser = subparsers.add_parser("optimize", help="Optimize an agent request")
|
| 19 |
+
opt_parser.add_argument("--config", "-c", default="config.yaml", help="Config file path")
|
| 20 |
+
opt_parser.add_argument("--request", "-r", required=True, help="User request text")
|
| 21 |
+
opt_parser.add_argument("--output", "-o", default="-", help="Output file (default: stdout)")
|
| 22 |
+
|
| 23 |
+
# Benchmark command
|
| 24 |
+
bench_parser = subparsers.add_parser("benchmark", help="Run benchmark suite")
|
| 25 |
+
bench_parser.add_argument("--config", "-c", default="config.yaml", help="Config file path")
|
| 26 |
+
bench_parser.add_argument("--tasks", "-n", type=int, default=1000, help="Number of tasks")
|
| 27 |
+
bench_parser.add_argument("--output", "-o", default="benchmark_results.json", help="Output path")
|
| 28 |
+
bench_parser.add_argument("--ablations", action="store_true", help="Run ablation study")
|
| 29 |
+
|
| 30 |
+
# Report command
|
| 31 |
+
report_parser = subparsers.add_parser("report", help="Generate report from benchmark results")
|
| 32 |
+
report_parser.add_argument("--input", "-i", required=True, help="Benchmark results JSON")
|
| 33 |
+
report_parser.add_argument("--output", "-o", default="-", help="Output file")
|
| 34 |
+
|
| 35 |
+
args = parser.parse_args()
|
| 36 |
+
|
| 37 |
+
if args.command == "optimize":
|
| 38 |
+
_cmd_optimize(args)
|
| 39 |
+
elif args.command == "benchmark":
|
| 40 |
+
_cmd_benchmark(args)
|
| 41 |
+
elif args.command == "report":
|
| 42 |
+
_cmd_report(args)
|
| 43 |
+
else:
|
| 44 |
+
parser.print_help()
|
| 45 |
+
sys.exit(1)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def _cmd_optimize(args):
|
| 49 |
+
config = ACOConfig.from_yaml(args.config) if Path(args.config).exists() else ACOConfig()
|
| 50 |
+
optimizer = AgentCostOptimizer(config)
|
| 51 |
+
result = optimizer.optimize(args.request)
|
| 52 |
+
|
| 53 |
+
output = {
|
| 54 |
+
"trace_id": result.trace_id,
|
| 55 |
+
"model": result.routing_decision.model_id,
|
| 56 |
+
"tier": result.routing_decision.tier,
|
| 57 |
+
"estimated_cost": result.estimated_cost,
|
| 58 |
+
"estimated_latency_ms": result.estimated_latency_ms,
|
| 59 |
+
"confidence": result.confidence,
|
| 60 |
+
"reasoning": result.reasoning,
|
| 61 |
+
"tool_decisions": [
|
| 62 |
+
{"tool": d.tool_name, "decision": d.decision.value, "cost": d.estimated_cost}
|
| 63 |
+
for d in result.tool_decisions
|
| 64 |
+
],
|
| 65 |
+
"verifier": result.verifier_decision.decision.value if result.verifier_decision else None,
|
| 66 |
+
"doom_score": result.doom_assessment.confidence if result.doom_assessment else None,
|
| 67 |
+
"meta_tool_match": result.meta_tool_match is not None,
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
json_str = json.dumps(output, indent=2)
|
| 71 |
+
if args.output == "-":
|
| 72 |
+
print(json_str)
|
| 73 |
+
else:
|
| 74 |
+
with open(args.output, "w") as f:
|
| 75 |
+
f.write(json_str)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def _cmd_benchmark(args):
|
| 79 |
+
config = ACOConfig.from_yaml(args.config) if Path(args.config).exists() else ACOConfig()
|
| 80 |
+
suite = BenchmarkSuite(config)
|
| 81 |
+
|
| 82 |
+
print(f"Generating {args.tasks} synthetic traces...")
|
| 83 |
+
traces = suite.generate_benchmark_data(args.tasks)
|
| 84 |
+
|
| 85 |
+
print("Running baselines...")
|
| 86 |
+
results = suite.run_all_baselines(traces)
|
| 87 |
+
|
| 88 |
+
if args.ablations:
|
| 89 |
+
print("Running ablations...")
|
| 90 |
+
ablation_results = suite.run_ablations(traces)
|
| 91 |
+
results.update(ablation_results)
|
| 92 |
+
|
| 93 |
+
suite.export(results, args.output)
|
| 94 |
+
|
| 95 |
+
# Print report
|
| 96 |
+
report = suite.report(results)
|
| 97 |
+
print(report)
|
| 98 |
+
|
| 99 |
+
print(f"\nResults saved to {args.output}")
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def _cmd_report(args):
|
| 103 |
+
with open(args.input, "r") as f:
|
| 104 |
+
data = json.load(f)
|
| 105 |
+
|
| 106 |
+
# Reconstruct BenchmarkResults for reporting
|
| 107 |
+
from aco.benchmarks.benchmark_suite import BenchmarkResult
|
| 108 |
+
results = {}
|
| 109 |
+
for name, d in data.items():
|
| 110 |
+
results[name] = BenchmarkResult(**d)
|
| 111 |
+
|
| 112 |
+
suite = BenchmarkSuite()
|
| 113 |
+
report = suite.report(results)
|
| 114 |
+
|
| 115 |
+
if args.output == "-":
|
| 116 |
+
print(report)
|
| 117 |
+
else:
|
| 118 |
+
with open(args.output, "w") as f:
|
| 119 |
+
f.write(report)
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
if __name__ == "__main__":
|
| 123 |
+
main()
|