22import requests
33from pydantic import BaseModel
44
5+ import strands
56from strands import Agent
67from strands .models .ollama import OllamaModel
78
@@ -13,35 +14,80 @@ def is_server_available() -> bool:
1314 return False
1415
1516
16- @pytest .fixture
17+ @pytest .fixture ( scope = "module" )
1718def model ():
1819 return OllamaModel (host = "http://localhost:11434" , model_id = "llama3.3:70b" )
1920
2021
21- @pytest .fixture
22- def agent (model ):
23- return Agent (model = model )
22+ @pytest .fixture (scope = "module" )
23+ def tools ():
24+ @strands .tool
25+ def tool_time () -> str :
26+ return "12:00"
2427
28+ @strands .tool
29+ def tool_weather () -> str :
30+ return "sunny"
2531
26- @pytest .mark .skipif (not is_server_available (), reason = "Local Ollama endpoint not available at localhost:11434" )
27- def test_agent (agent ):
28- result = agent ("Say 'hello world' with no other text" )
29- assert isinstance (result .message ["content" ][0 ]["text" ], str )
32+ return [tool_time , tool_weather ]
3033
3134
32- @pytest .mark .skipif (not is_server_available (), reason = "Local Ollama endpoint not available at localhost:11434" )
33- def test_structured_output (agent ):
34- class Weather (BaseModel ):
35- """Extract the time and weather.
35+ @pytest .fixture (scope = "module" )
36+ def agent (model , tools ):
37+ return Agent (model = model , tools = tools )
3638
37- Time format: HH:MM
38- Weather: sunny, cloudy, rainy, etc.
39- """
39+
40+ @pytest .fixture (scope = "module" )
41+ def weather ():
42+ class Weather (BaseModel ):
43+ """Extracts the time and weather from the user's message with the exact strings."""
4044
4145 time : str
4246 weather : str
4347
44- result = agent .structured_output (Weather , "The time is 12:00 and the weather is sunny" )
45- assert isinstance (result , Weather )
46- assert result .time == "12:00"
47- assert result .weather == "sunny"
48+ return Weather (time = "12:00" , weather = "sunny" )
49+
50+
51+ @pytest .mark .skipif (not is_server_available (), reason = "Local Ollama endpoint not available at localhost:11434" )
52+ def test_agent_invoke (agent ):
53+ result = agent ("What is the time and weather in New York?" )
54+ text = result .message ["content" ][0 ]["text" ].lower ()
55+
56+ assert all (string in text for string in ["12:00" , "sunny" ])
57+
58+
59+ @pytest .mark .skipif (not is_server_available (), reason = "Local Ollama endpoint not available at localhost:11434" )
60+ @pytest .mark .asyncio
61+ async def test_agent_invoke_async (agent ):
62+ result = await agent .invoke_async ("What is the time and weather in New York?" )
63+ text = result .message ["content" ][0 ]["text" ].lower ()
64+
65+ assert all (string in text for string in ["12:00" , "sunny" ])
66+
67+
68+ @pytest .mark .skipif (not is_server_available (), reason = "Local Ollama endpoint not available at localhost:11434" )
69+ @pytest .mark .asyncio
70+ async def test_agent_stream_async (agent ):
71+ stream = agent .stream_async ("What is the time and weather in New York?" )
72+ async for event in stream :
73+ _ = event
74+
75+ result = event ["result" ]
76+ text = result .message ["content" ][0 ]["text" ].lower ()
77+
78+ assert all (string in text for string in ["12:00" , "sunny" ])
79+
80+
81+ @pytest .mark .skipif (not is_server_available (), reason = "Local Ollama endpoint not available at localhost:11434" )
82+ def test_agent_structured_output (agent , weather ):
83+ tru_weather = agent .structured_output (type (weather ), "The time is 12:00 and the weather is sunny" )
84+ exp_weather = weather
85+ assert tru_weather == exp_weather
86+
87+
88+ @pytest .mark .skipif (not is_server_available (), reason = "Local Ollama endpoint not available at localhost:11434" )
89+ @pytest .mark .asyncio
90+ async def test_agent_structured_output_async (agent , weather ):
91+ tru_weather = await agent .structured_output_async (type (weather ), "The time is 12:00 and the weather is sunny" )
92+ exp_weather = weather
93+ assert tru_weather == exp_weather
0 commit comments