|
| 1 | +# pylint: disable=too-many-lines,line-too-long,useless-suppression |
| 2 | +# ------------------------------------ |
| 3 | +# Copyright (c) Microsoft Corporation. |
| 4 | +# Licensed under the MIT License. |
| 5 | +# ------------------------------------ |
| 6 | +# cSpell:disable |
| 7 | + |
| 8 | +from test_base import TestBase, servicePreparer |
| 9 | +from devtools_testutils.aio import recorded_by_proxy_async |
| 10 | +from devtools_testutils import RecordedTransport |
| 11 | +from azure.ai.projects.models import PromptAgentDefinition, WebSearchPreviewTool, ApproximateLocation |
| 12 | + |
| 13 | + |
| 14 | +class TestAgentWebSearchAsync(TestBase): |
| 15 | + |
| 16 | + @servicePreparer() |
| 17 | + @recorded_by_proxy_async(RecordedTransport.AZURE_CORE, RecordedTransport.HTTPX) |
| 18 | + async def test_agent_web_search_async(self, **kwargs): |
| 19 | + |
| 20 | + model = self.test_agents_params["model_deployment_name"] |
| 21 | + |
| 22 | + async with ( |
| 23 | + self.create_async_client(operation_group="agents", **kwargs) as project_client, |
| 24 | + project_client.get_openai_client() as openai_client, |
| 25 | + ): |
| 26 | + agent_name = "web-search-agent-async" |
| 27 | + |
| 28 | + # Create agent with web search tool |
| 29 | + agent = await project_client.agents.create_version( |
| 30 | + agent_name=agent_name, |
| 31 | + definition=PromptAgentDefinition( |
| 32 | + model=model, |
| 33 | + instructions="You are a helpful assistant that can search the web for current information.", |
| 34 | + tools=[ |
| 35 | + WebSearchPreviewTool( |
| 36 | + user_location=ApproximateLocation(country="US", city="Seattle", region="Washington") |
| 37 | + ) |
| 38 | + ], |
| 39 | + ), |
| 40 | + description="Agent for testing web search capabilities.", |
| 41 | + ) |
| 42 | + self._validate_agent_version(agent, expected_name=agent_name) |
| 43 | + |
| 44 | + # Ask a question that requires web search for current information |
| 45 | + print("\nAsking agent about current weather...") |
| 46 | + |
| 47 | + response = await openai_client.responses.create( |
| 48 | + input="What is the current weather in Seattle?", |
| 49 | + extra_body={"agent": {"name": agent.name, "type": "agent_reference"}}, |
| 50 | + ) |
| 51 | + |
| 52 | + self.validate_response(response) |
| 53 | + |
| 54 | + # Get the response text |
| 55 | + response_text = response.output_text |
| 56 | + print(f"\nAgent's response: {response_text[:300]}...") |
| 57 | + |
| 58 | + # Verify we got a meaningful response |
| 59 | + assert len(response_text) > 30, f"Expected a substantial response from the agent. Got '{response_text}'" |
| 60 | + |
| 61 | + # The response should mention weather-related terms or Seattle |
| 62 | + response_lower = response_text.lower() |
| 63 | + assert any( |
| 64 | + keyword in response_lower for keyword in ["weather", "temperature", "seattle", "forecast"] |
| 65 | + ), f"Expected response to contain weather information, but got: {response_text[:200]}" |
| 66 | + |
| 67 | + print("\n✓ Agent successfully used web search tool to get current information") |
| 68 | + |
| 69 | + # Teardown |
| 70 | + await project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version) |
| 71 | + print("Agent deleted") |
0 commit comments