-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathtest_compatibility.py
More file actions
100 lines (77 loc) · 2.71 KB
/
test_compatibility.py
File metadata and controls
100 lines (77 loc) · 2.71 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
100
"""
Test that code_along implementations have compatible APIs with picoagents.
Run: python test_compatibility.py
"""
import asyncio
import sys
from typing import TYPE_CHECKING
# Toggle which implementation to test
USE_FULL_LIBRARY = "--full" in sys.argv
if TYPE_CHECKING:
# For static analysis, always use code_along types
from ch04_v4_streaming import Agent, ListMemory
elif USE_FULL_LIBRARY:
print("Testing: picoagents (full library)")
from picoagents import Agent # type: ignore[assignment]
from picoagents.memory import ListMemory # type: ignore[assignment]
else:
print("Testing: code_along (v4)")
from ch04_v4_streaming import Agent, ListMemory
def get_weather(location: str) -> str:
"""Get weather for a location."""
return f"Sunny in {location}"
async def test_basic_agent():
"""Test basic agent creation and run."""
agent = Agent(
name="test_agent",
instructions="You are helpful.",
model="gpt-4.1-mini"
)
response = await agent.run("Say hello")
assert response.final_content, "Should have response content"
print(f" ✓ Basic agent: {response.final_content[:50]}...")
async def test_agent_with_tools():
"""Test agent with tools."""
agent = Agent(
name="tool_agent",
instructions="Use tools when asked about weather.",
model="gpt-4.1-mini",
tools=[get_weather]
)
response = await agent.run("What's the weather in Paris?")
assert "Paris" in response.final_content or "Sunny" in response.final_content
print(f" ✓ Agent with tools: {response.final_content[:50]}...")
async def test_agent_with_memory():
"""Test agent with memory."""
memory = ListMemory()
agent = Agent(
name="memory_agent",
instructions="Remember what the user tells you.",
model="gpt-4.1-mini",
memory=memory
)
await agent.run("My name is Bob")
response = await agent.run("What's my name?")
assert "Bob" in response.final_content
print(f" ✓ Agent with memory: {response.final_content[:50]}...")
async def test_streaming():
"""Test streaming interface."""
agent = Agent(
name="stream_agent",
instructions="Be brief.",
model="gpt-4.1-mini"
)
events = []
async for item in agent.run_stream("Say hi"):
events.append(item)
assert len(events) > 0, "Should yield events"
print(f" ✓ Streaming: {len(events)} events yielded")
async def main():
print("\n--- Running compatibility tests ---\n")
await test_basic_agent()
await test_agent_with_tools()
await test_agent_with_memory()
await test_streaming()
print("\n--- All tests passed ---\n")
if __name__ == "__main__":
asyncio.run(main())