项目文件夹

文件
Devika efbbbb91ab Add agent observability and tracing
Add an observability layer for agent runs on top of the Agent API. The
runner now emits structured trace events for each step (model calls,
tool calls, tool-policy denials) and routes them to pluggable sinks,
with a trace store and viewer model for inspecting past runs.

- aisuite/tracing: trace sinks (in-memory, local JSONL), a JSONL trace
  store, and a viewer data model for reconstructing runs
- aisuite/agents/runner: emit trace events and propagate run context
- aisuite/agents/context: ActiveRunContext to correlate nested runs
- Runner accepts trace_sinks; RunResult/RunStep carry trace data
- tests/agents: trace sink, viewer, trace-output, and tool-policy
  denial integration coverage

Co-authored-by: Rohit <rohit.prasad15@gmail.com>
2026-05-31 22:47:00 -07:00

56 行
1.4 KiB
Python

"""
Minimal Agent API example.
Set the provider API key in your environment before running, for example:
export OPENAI_API_KEY="..."
python examples/agents/simple_agent.py
"""
import aisuite as ai
def get_weather(city: str) -> str:
"""Get the current weather for a city."""
return f"The weather in {city} is sunny."
def allow_safe_tools(context: ai.ToolPolicyContext) -> bool:
"""Allow only the tools this example expects."""
return context.tool_name in {"get_weather"}
agent = ai.Agent(
name="weather_assistant",
model="openai:gpt-4o",
instructions="Answer briefly. Use tools when they help.",
tools=[get_weather],
model_settings={"temperature": 0.2},
tags=["example", "weather"],
metadata={"app": "simple_agent_example"},
)
result = ai.Runner.run_sync(
agent,
"What is the weather in San Francisco?",
max_turns=3,
run_name="weather_lookup",
group_id="example_conversation_1",
metadata={"request_id": "example_request_1", "user_id": "example_user"},
tool_policy=allow_safe_tools,
)
print(result.final_output)
result.print_trace()
result.write_trace_jsonl(".aisuite/runs.jsonl")
next_result = ai.Runner.continue_sync(
result,
"What about Oakland?",
tool_policy=allow_safe_tools,
)
print(next_result.final_output)
next_result.print_trace()
next_result.write_trace_jsonl(".aisuite/runs.jsonl")