Datasets:
Download main.py from tesraghavan/agent-trace: direct link, hf CLI and curl.
- Browser
- Download file 3.95 kB
-
https://huggingface.co/datasets/tesraghavan/agent-trace/resolve/main/main.py
- Command line
-
hf download hf://datasets/tesraghavan/agent-trace/main.py
-
curl -L -o main.py https://huggingface.co/datasets/tesraghavan/agent-trace/resolve/main/main.py
3.95 kB
| """MVP: Generate a single instrumented trace from an LLM agent using tools.""" | |
| import os | |
| import sys | |
| from pathlib import Path | |
| from smolagents import OpenAIServerModel, PythonInterpreterTool, ToolCallingAgent | |
| from src.instrumentation import instrument_model, instrument_tools | |
| from src.run_metadata import ( | |
| build_run_id, | |
| build_trace_metadata, | |
| normalize_chat_template, | |
| normalize_server_args, | |
| write_run_manifest, | |
| ) | |
| from src.tools import BashTool | |
| from src.tracing import TraceCollector | |
| # Configuration | |
| MODEL_ID = "Qwen/Qwen3-0.6B" | |
| API_BASE = "http://127.0.0.1:8080/v1" | |
| OUTPUT_PATH = "datasets/traces.jsonl" | |
| PROMPT = "Search for lines containing 'ERROR' in testdata/noise.txt using ripgrep (rg) and count how many there are" | |
| TOOL_CHOICE = "auto" | |
| CONTEXT_LENGTH = int(os.getenv("LLAMA_CTX", "16384")) | |
| CHAT_TEMPLATE = os.getenv("LLAMA_CHAT_TEMPLATE_FILE") | |
| MODEL_ARTIFACT = os.getenv("LLAMA_MODEL") | |
| QUANTIZATION = os.getenv("TRACE_MODEL_QUANTIZATION") | |
| SERVING_ENGINE = os.getenv("TRACE_SERVING_ENGINE", "llama.cpp") | |
| SERVING_ENGINE_VERSION = os.getenv("TRACE_SERVING_ENGINE_VERSION") | |
| TESTDATA = Path(__file__).parent / "testdata" | |
| def main(): | |
| repo_root = Path(__file__).resolve().parent | |
| run_id = build_run_id("single-trace") | |
| server_args_normalized = normalize_server_args( | |
| api_base=API_BASE, | |
| tool_choice=TOOL_CHOICE, | |
| context_length=CONTEXT_LENGTH, | |
| chat_template=normalize_chat_template(CHAT_TEMPLATE), | |
| jinja=None, | |
| flash_attn=None, | |
| n_gpu_layers=None, | |
| temperature=None, | |
| top_p=None, | |
| ) | |
| base_metadata = build_trace_metadata( | |
| repo_root=repo_root, | |
| run_id=run_id, | |
| model_id=MODEL_ID, | |
| api_base=API_BASE, | |
| model_artifact=MODEL_ARTIFACT, | |
| quantization=QUANTIZATION, | |
| serving_engine=SERVING_ENGINE, | |
| serving_engine_version=SERVING_ENGINE_VERSION, | |
| tool_choice=TOOL_CHOICE, | |
| context_length=CONTEXT_LENGTH, | |
| max_steps=None, | |
| dataset_name="single_trace_demo", | |
| dataset_split=None, | |
| dataset_offset=None, | |
| chat_template=CHAT_TEMPLATE, | |
| prompt_sanitization_version="none", | |
| fixture_dir=TESTDATA if TESTDATA.exists() else None, | |
| server_args_normalized=server_args_normalized, | |
| ) | |
| manifest_path = write_run_manifest( | |
| repo_root=repo_root, | |
| run_id=run_id, | |
| trace_metadata=base_metadata, | |
| output_path=OUTPUT_PATH, | |
| raw_command=[sys.executable, *sys.argv], | |
| ) | |
| # Initialize model pointing to local llama-server | |
| model = OpenAIServerModel( | |
| model_id=MODEL_ID, | |
| api_base=API_BASE, | |
| api_key="not-needed", | |
| tool_choice=TOOL_CHOICE, # "required" suppresses reasoning_content | |
| ) | |
| # Create tools | |
| tools = [ | |
| PythonInterpreterTool(), | |
| BashTool(), | |
| ] | |
| # Set up trace collection | |
| collector = TraceCollector(prompt=PROMPT, model=MODEL_ID) | |
| collector.extra_metadata = base_metadata | |
| # Instrument tools and model to capture telemetry | |
| instrumented_tools = instrument_tools(tools, collector) | |
| instrument_model(model, collector) | |
| # Create agent with instrumented tools | |
| agent = ToolCallingAgent( | |
| tools=instrumented_tools, | |
| model=model, | |
| ) | |
| # Run the agent | |
| print(f"Running agent with prompt: {PROMPT}") | |
| print(f"Run ID: {run_id}") | |
| print(f"Run manifest: {manifest_path}") | |
| collector.start() | |
| result = None | |
| try: | |
| result = agent.run(PROMPT, return_full_result=True) | |
| print(f"Agent result: {result.output}") | |
| except Exception as e: | |
| print(f"Agent error: {e}") | |
| collector.stop() | |
| # Extract LLM steps from result (includes reasoning if available) | |
| if result: | |
| collector.record_llm_steps_from_result(result) | |
| # Save trace | |
| collector.save(OUTPUT_PATH) | |
| print(f"Trace saved to {OUTPUT_PATH}") | |
| if __name__ == "__main__": | |
| main() | |