kikocis/llama-stack public
Changesets · Branches · Tags · Compare
| Changeset | a5ddb0e440cc60d377617480fdb1e0faf8c40b8d2bce7de20531d56c23a7a870 |
|---|---|
| Author | Ashwin Bharambe <ashwin.bharambe@gmail.com> · 2024-09-19 13:02 -07:00 |
| Committer | Ashwin Bharambe <ashwin.bharambe@gmail.com> · 2024-09-19 13:02 -07:00 |
| Parents | 705e0654af7d |
1 file changed
modified llama_stack/providers/impls/meta_reference/agents/tests/test_chat_agent.py +102 −13
| @@ -14,7 +14,11 @@ | ||
| 14 | 14 | from llama_stack.apis.safety import * # noqa: F403 |
| 15 | 15 | from llama_stack.apis.agents import * # noqa: F403 |
| 16 | 16 | |
| 17 | -from ..agent_instance import ChatAgent | |
| 17 | +from ..agents import ( | |
| 18 | + AGENT_INSTANCES_BY_ID, | |
| 19 | + MetaReferenceAgentsImpl, | |
| 20 | + MetaReferenceImplConfig, | |
| 21 | +) | |
| 18 | 22 | |
| 19 | 23 | |
| 20 | 24 | class MockInferenceAPI: |
| @@ -42,10 +46,22 @@ | ||
| 42 | 46 | yield ChatCompletionResponseStreamChunk( |
| 43 | 47 | event=ChatCompletionResponseEvent( |
| 44 | 48 | event_type="progress", |
| 45 | - delta="Mock response", | |
| 49 | + delta="AI is a fascinating field...", | |
| 50 | + ) | |
| 51 | + ) | |
| 52 | + yield ChatCompletionResponseStreamChunk( | |
| 53 | + event=ChatCompletionResponseEvent( | |
| 54 | + event_type="progress", | |
| 55 | + delta=ToolCallDelta( | |
| 56 | + content=ToolCall( | |
| 57 | + call_id="123", | |
| 58 | + tool_name=BuiltinTool.brave_search.value, | |
| 59 | + arguments={"query": "AI history"}, | |
| 60 | + ), | |
| 61 | + parse_status="success", | |
| 62 | + ), | |
| 46 | 63 | ) |
| 47 | 64 | ) |
| 48 | - | |
| 49 | 65 | yield ChatCompletionResponseStreamChunk( |
| 50 | 66 | event=ChatCompletionResponseEvent( |
| 51 | 67 | event_type="complete", |
| @@ -149,24 +165,32 @@ | ||
| 149 | 165 | |
| 150 | 166 | |
| 151 | 167 | @pytest.fixture |
| 152 | -def chat_agent(mock_inference_api, mock_safety_api, mock_memory_api): | |
| 153 | - # You'll need to adjust this based on the actual ChatAgent constructor | |
| 168 | +async def chat_agent(mock_inference_api, mock_safety_api, mock_memory_api): | |
| 169 | + impl = MetaReferenceAgentsImpl( | |
| 170 | + config=MetaReferenceImplConfig(), | |
| 171 | + inference_api=mock_inference_api, | |
| 172 | + safety_api=mock_safety_api, | |
| 173 | + memory_api=mock_memory_api, | |
| 174 | + ) | |
| 175 | + await impl.initialize() | |
| 176 | + | |
| 154 | 177 | agent_config = AgentConfig( |
| 155 | 178 | model="test_model", |
| 156 | 179 | instructions="You are a helpful assistant.", |
| 157 | 180 | sampling_params=SamplingParams(), |
| 158 | - tools=[], | |
| 181 | + tools=[ | |
| 182 | + SearchToolDefinition( | |
| 183 | + name="brave_search", | |
| 184 | + api_key="test_key", | |
| 185 | + ), | |
| 186 | + ], | |
| 159 | 187 | tool_choice=ToolChoice.auto, |
| 160 | 188 | input_shields=[], |
| 161 | 189 | output_shields=[], |
| 162 | 190 | ) |
| 163 | - return ChatAgent( | |
| 164 | - agent_config=agent_config, | |
| 165 | - inference_api=mock_inference_api, | |
| 166 | - memory_api=mock_memory_api, | |
| 167 | - safety_api=mock_safety_api, | |
| 168 | - builtin_tools=[], | |
| 169 | - ) | |
| 191 | + response = await impl.create_agent(agent_config) | |
| 192 | + agent = AGENT_INSTANCES_BY_ID[response.agent_id] | |
| 193 | + return agent | |
| 170 | 194 | |
| 171 | 195 | |
| 172 | 196 | @pytest.mark.asyncio |
| @@ -214,3 +238,68 @@ | ||
| 214 | 238 | assert len(responses) == 2 # StepStart, StepComplete |
| 215 | 239 | assert responses[0].event.payload.step_type.value == "shield_call" |
| 216 | 240 | assert not responses[1].event.payload.step_details.response.is_violation |
| 241 | + | |
| 242 | + | |
| 243 | +@pytest.mark.asyncio | |
| 244 | +@pytest.mark.skip(reason="Not yet implemented; need to mock out tool execution easily") | |
| 245 | +async def test_chat_agent_complex_turn(chat_agent): | |
| 246 | + # Setup | |
| 247 | + session = chat_agent.create_session("Test Session") | |
| 248 | + request = AgentTurnCreateRequest( | |
| 249 | + agent_id="random", | |
| 250 | + session_id=session.session_id, | |
| 251 | + messages=[UserMessage(content="Tell me about AI and then use a tool.")], | |
| 252 | + stream=True, | |
| 253 | + ) | |
| 254 | + | |
| 255 | + # Execute the turn | |
| 256 | + responses = [] | |
| 257 | + async for response in chat_agent.create_and_execute_turn(request): | |
| 258 | + responses.append(response) | |
| 259 | + | |
| 260 | + # Assertions | |
| 261 | + assert len(responses) > 0 | |
| 262 | + | |
| 263 | + # Check for the presence of different step types | |
| 264 | + step_types = [ | |
| 265 | + response.event.payload.step_type | |
| 266 | + for response in responses | |
| 267 | + if hasattr(response.event.payload, "step_type") | |
| 268 | + ] | |
| 269 | + | |
| 270 | + assert "shield_call" in step_types, "Shield call step is missing" | |
| 271 | + assert "inference" in step_types, "Inference step is missing" | |
| 272 | + assert "tool_execution" in step_types, "Tool execution step is missing" | |
| 273 | + | |
| 274 | + # Check for the presence of start and complete events | |
| 275 | + event_types = [ | |
| 276 | + response.event.payload.event_type | |
| 277 | + for response in responses | |
| 278 | + if hasattr(response.event.payload, "event_type") | |
| 279 | + ] | |
| 280 | + assert "start" in event_types, "Start event is missing" | |
| 281 | + assert "complete" in event_types, "Complete event is missing" | |
| 282 | + | |
| 283 | + # Check for the presence of tool call | |
| 284 | + tool_calls = [ | |
| 285 | + response.event.payload.tool_call | |
| 286 | + for response in responses | |
| 287 | + if hasattr(response.event.payload, "tool_call") | |
| 288 | + ] | |
| 289 | + assert any( | |
| 290 | + tool_call | |
| 291 | + for tool_call in tool_calls | |
| 292 | + if tool_call and tool_call.content.get("name") == "memory" | |
| 293 | + ), "Memory tool call is missing" | |
| 294 | + | |
| 295 | + # Check for the final turn complete event | |
| 296 | + assert any( | |
| 297 | + isinstance(response.event.payload, AgentTurnResponseTurnCompletePayload) | |
| 298 | + for response in responses | |
| 299 | + ), "Turn complete event is missing" | |
| 300 | + | |
| 301 | + # Verify the turn was added to the session | |
| 302 | + assert len(session.turns) == 1, "Turn was not added to the session" | |
| 303 | + assert ( | |
| 304 | + session.turns[0].input_messages == request.messages | |
| 305 | + ), "Input messages do not match" |