| """CLI Commands - run, train, eval""" |
|
|
| import json |
| import time |
| from pathlib import Path |
| from typing import Optional |
|
|
| import click |
| from rich.console import Console |
| from rich.progress import Progress, SpinnerColumn, TextColumn |
|
|
| from src.core import CoderAgent, ResponseValidator |
| from src.animations import Spinner, ProgressBar |
| from src.knowledge import LocalKB |
|
|
|
|
| console = Console() |
|
|
|
|
| @click.command() |
| @click.argument("instruction") |
| @click.option("--model", default="gpt-4", help="AI model") |
| @click.option("--output", "-o", help="Output file for response") |
| @click.option("--validate/--no-validate", default=True, help="Validate response") |
| def run_command(instruction: str, model: str, output: Optional[str], validate: bool): |
| """Run a single instruction and display response""" |
| agent = CoderAgent(model=model) |
| validator = ResponseValidator() |
|
|
| console.print(f"[bold cyan]Question:[/bold cyan] {instruction}\n") |
|
|
| with Spinner("Generating response..."): |
| response = agent.generate_response(instruction) |
|
|
| console.print("[bold green]Response:[/bold green]") |
| console.print(response["response"]) |
|
|
| if validate: |
| console.print("\n[bold yellow]Validating...[/bold yellow]") |
| result = validator.validate(response["response"], instruction) |
| console.print(f"Quality: [bold]{result.quality.value}[/bold]") |
| console.print(f"Score: [bold]{result.score:.2f}[/bold]") |
|
|
| if result.issues: |
| console.print("[bold red]Issues:[/bold red]") |
| for issue in result.issues: |
| console.print(f" - {issue}") |
|
|
| if output: |
| Path(output).write_text(response["response"], encoding="utf-8") |
| console.print(f"\n[green]Response saved to {output}[/green]") |
|
|
|
|
| @click.command() |
| @click.argument("data_dir") |
| @click.option("--epochs", default=10, help="Training epochs") |
| @click.option("--batch-size", default=4, help="Batch size") |
| def train_command(data_dir: str, epochs: int, batch_size: int): |
| """Train agent on trajectory data""" |
| console.print("[bold cyan]Training Agent...[/bold cyan]\n") |
|
|
| data_path = Path(data_dir) |
| if not data_path.exists(): |
| console.print(f"[bold red]Error:[/bold red] {data_dir} not found") |
| return |
|
|
| trajectory_files = list(data_path.glob("*.jsonl")) |
| if not trajectory_files: |
| console.print(f"[bold red]Error:[/bold red] No trajectory files in {data_dir}") |
| return |
|
|
| console.print(f"Found {len(trajectory_files)} trajectory files\n") |
|
|
| for epoch in range(epochs): |
| console.print(f"[bold cyan]Epoch {epoch + 1}/{epochs}[/bold cyan]") |
|
|
| with Progress( |
| SpinnerColumn(), |
| TextColumn("[progress.description]{task.description}"), |
| console=console, |
| ) as progress: |
| task = progress.add_task("Processing...", total=len(trajectory_files)) |
|
|
| for traj_file in trajectory_files: |
| progress.update(task, advance=1) |
|
|
| with open(traj_file, "r", encoding="utf-8") as f: |
| for line in f: |
| data = json.loads(line) |
| |
| pass |
|
|
| time.sleep(0.5) |
|
|
| console.print("\n[bold green]Training completed![/bold green]") |
|
|
|
|
| @click.command() |
| @click.argument("data_dir") |
| @click.option("--verbose", is_flag=True, help="Show detailed results") |
| def eval_command(data_dir: str, verbose: bool): |
| """Evaluate agent on test data""" |
| console.print("[bold cyan]Evaluating Agent...[/bold cyan]\n") |
|
|
| data_path = Path(data_dir) |
| if not data_path.exists(): |
| console.print(f"[bold red]Error:[/bold red] {data_dir} not found") |
| return |
|
|
| kb = LocalKB() |
|
|
| test_queries = [ |
| "Python decorator hta ya py", |
| "JavaScript async/await hta ya", |
| "SQL JOIN operations hta ya", |
| "React useState lo useEffect hta ya", |
| ] |
|
|
| agent = CoderAgent() |
| validator = ResponseValidator() |
|
|
| results = [] |
| with Progress( |
| SpinnerColumn(), TextColumn("[progress.description]{task.description}"), console=console |
| ) as progress: |
| task = progress.add_task("Testing...", total=len(test_queries)) |
|
|
| for query in test_queries: |
| progress.update(task, description=f"Testing: {query[:30]}...") |
|
|
| response = agent.generate_response(query) |
| validation = validator.validate(response["response"], query) |
|
|
| results.append( |
| { |
| "query": query, |
| "quality": validation.quality.value, |
| "score": validation.score, |
| "issues": len(validation.issues), |
| } |
| ) |
|
|
| progress.update(task, advance=1) |
|
|
| console.print("\n[bold]Evaluation Results:[/bold]\n") |
|
|
| total_score = sum(r["score"] for r in results) |
| avg_score = total_score / len(results) if results else 0 |
|
|
| console.print(f"Average Score: [bold]{avg_score:.2f}[/bold]\n") |
|
|
| for r in results: |
| color = "green" if r["score"] >= 0.6 else "yellow" if r["score"] >= 0.4 else "red" |
| console.print(f"[{color}]{r['query']}:[/{color}] {r['score']:.2f} ({r['quality']})") |
|
|
| if verbose and r["issues"] > 0: |
| console.print(f" Issues: {r['issues']}") |
|
|
|
|
| @click.command() |
| @click.argument("query") |
| def search_command(query: str): |
| """Search knowledge base""" |
| kb = LocalKB() |
| results = kb.search(query) |
|
|
| if not results: |
| console.print("[yellow]No results found[/yellow]") |
| return |
|
|
| for result in results: |
| console.print(f"[bold cyan]{result['source']}[/bold cyan]") |
| console.print(result["content"][:200]) |
| console.print() |
|
|
|
|
| @click.command() |
| @click.argument("question") |
| def ask_command(question: str): |
| """Ask a question (alias for run)""" |
| run_command.callback(question, model="gpt-4", output=None, validate=True) |
|
|