forked from openai/openai-agents-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_forensics_example.py
More file actions
99 lines (77 loc) · 3 KB
/
agent_forensics_example.py
File metadata and controls
99 lines (77 loc) · 3 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
"""
OpenAI Agents SDK — Forensic Reporting Example
================================================
Demonstrates how to add forensic reporting to an OpenAI Agents SDK agent
using Agent Forensics. Records every decision, tool call, and LLM interaction,
then generates a forensic report and auto-classifies failure patterns.
Setup:
pip install openai-agents agent-forensics
Usage:
python openai_agents_forensics_example.py
Target repo: openai/openai-agents-python → examples/ or cookbook/
PR title: "Add forensic reporting example with agent-forensics"
"""
import asyncio
from agents import Agent, Runner, function_tool
from agent_forensics import Forensics
# --- Tools ---
@function_tool
def search_products(query: str) -> str:
"""Search the product catalog for items matching the query."""
products = [
{"name": "Logitech M750", "price": 45.00, "in_stock": True},
{"name": "Apple Magic Mouse", "price": 99.00, "in_stock": True},
{"name": "Razer DeathAdder", "price": 69.00, "in_stock": False},
]
results = [p for p in products if query.lower() in p["name"].lower()]
if not results:
results = products # Return all if no match
return str(results)
@function_tool
def purchase(product_name: str, quantity: int = 1) -> str:
"""Purchase a product by name."""
if "razer" in product_name.lower():
return '{"error": "Out of stock"}'
return f'{{"status": "confirmed", "product": "{product_name}", "qty": {quantity}, "order_id": "ORD-001"}}'
# --- Main ---
async def main():
# 1. Initialize forensics
f = Forensics(session="openai-agents-demo", agent="shopping-agent")
# 2. Create agent with forensics hooks (one line!)
agent = Agent(
name="shopping-agent",
instructions=(
"You help users buy products. Search for products first, "
"then purchase the one that best matches the user's request."
),
tools=[search_products, purchase],
hooks=f.openai_agents(), # ← This is all you need
)
# 3. Run the agent
print("Running agent...")
result = await Runner.run(agent, "Buy me a wireless mouse under $50")
print(f"Agent output: {result.final_output}\n")
# 4. Generate forensic report
print("=" * 60)
print("FORENSIC REPORT")
print("=" * 60)
print(f"Events recorded: {len(f.events())}")
print()
# 5. Auto-classify failures
failures = f.classify()
if failures:
print(f"Failures detected: {len(failures)}")
for fail in failures:
print(f" [{fail['severity']}] {fail['type']}: {fail['description']}")
else:
print("No failures detected.")
print()
# 6. Save report
f.save_markdown(".")
print(f"Report saved: forensics-report-openai-agents-demo.md")
# 7. Extract replay config (for deterministic reproduction)
config = f.get_replay_config()
if config["model_config"]:
print(f"Model config: {config['model_config']}")
if __name__ == "__main__":
asyncio.run(main())