2
2
import requests
3
3
from pydantic import BaseModel
4
4
5
+ import strands
5
6
from strands import Agent
6
7
from strands .models .ollama import OllamaModel
7
8
@@ -13,35 +14,80 @@ def is_server_available() -> bool:
13
14
return False
14
15
15
16
16
- @pytest .fixture
17
+ @pytest .fixture ( scope = "module" )
17
18
def model ():
18
19
return OllamaModel (host = "http://localhost:11434" , model_id = "llama3.3:70b" )
19
20
20
21
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"
24
27
28
+ @strands .tool
29
+ def tool_weather () -> str :
30
+ return "sunny"
25
31
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 ]
30
33
31
34
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 )
36
38
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."""
40
44
41
45
time : str
42
46
weather : str
43
47
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