Skip to content

Commit 84bf300

Browse files
authored
Add async web search tool test. Add recordings to web search sync & async tests (#44374)
1 parent 5eed65c commit 84bf300

File tree

3 files changed

+79
-13
lines changed

3 files changed

+79
-13
lines changed

sdk/ai/azure-ai-projects/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "python",
44
"TagPrefix": "python/ai/azure-ai-projects",
5-
"Tag": "python/ai/azure-ai-projects_1fcc2917aa"
5+
"Tag": "python/ai/azure-ai-projects_febb246e47"
66
}

sdk/ai/azure-ai-projects/tests/agents/tools/test_agent_web_search.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,15 @@
55
# ------------------------------------
66
# cSpell:disable
77

8-
import pytest
98
from test_base import TestBase, servicePreparer
10-
from devtools_testutils import is_live_and_not_recording
9+
from devtools_testutils import recorded_by_proxy, RecordedTransport
1110
from azure.ai.projects.models import PromptAgentDefinition, WebSearchPreviewTool, ApproximateLocation
1211

1312

1413
class TestAgentWebSearch(TestBase):
1514

1615
@servicePreparer()
17-
@pytest.mark.skipif(
18-
condition=(not is_live_and_not_recording()),
19-
reason="Skipped because we cannot record network calls with OpenAI client",
20-
)
16+
@recorded_by_proxy(RecordedTransport.AZURE_CORE, RecordedTransport.HTTPX)
2117
def test_agent_web_search(self, **kwargs):
2218
"""
2319
Test agent with Web Search tool for real-time information.
@@ -47,9 +43,11 @@ def test_agent_web_search(self, **kwargs):
4743
self.create_client(operation_group="agents", **kwargs) as project_client,
4844
project_client.get_openai_client() as openai_client,
4945
):
46+
agent_name = "web-search-agent"
47+
5048
# Create agent with web search tool
5149
agent = project_client.agents.create_version(
52-
agent_name="web-search-agent",
50+
agent_name=agent_name,
5351
definition=PromptAgentDefinition(
5452
model=model,
5553
instructions="You are a helpful assistant that can search the web for current information.",
@@ -61,10 +59,7 @@ def test_agent_web_search(self, **kwargs):
6159
),
6260
description="Agent for testing web search capabilities.",
6361
)
64-
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
65-
assert agent.id is not None
66-
assert agent.name == "web-search-agent"
67-
assert agent.version is not None
62+
self._validate_agent_version(agent, expected_name=agent_name)
6863

6964
# Ask a question that requires web search for current information
7065
print("\nAsking agent about current weather...")
@@ -81,7 +76,7 @@ def test_agent_web_search(self, **kwargs):
8176
print(f"\nAgent's response: {response_text[:300]}...")
8277

8378
# Verify we got a meaningful response
84-
assert len(response_text) > 30, "Expected a substantial response from the agent"
79+
assert len(response_text) > 30, f"Expected a substantial response from the agent. Got '{response_text}'"
8580

8681
# The response should mention weather-related terms or Seattle
8782
response_lower = response_text.lower()
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)