Skip to content

Commit 3f7d30f

Browse files
committed
code review feedback
1 parent 7325d03 commit 3f7d30f

14 files changed

+1708
-1712
lines changed

sdk/ai/azure-ai-projects/tests/agents/tools/multitool/test_agent_file_search_and_code_interpreter.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from devtools_testutils import is_live_and_not_recording
2020
from azure.ai.projects.models import PromptAgentDefinition, FileSearchTool, CodeInterpreterTool, CodeInterpreterToolAuto
2121

22+
2223
class TestAgentFileSearchAndCodeInterpreter(TestBase):
2324
"""Tests for agents using File Search + Code Interpreter combination."""
2425

@@ -40,7 +41,7 @@ def test_find_and_analyze_data(self, **kwargs):
4041

4142
# Create data file
4243
txt_content = "Sample data: 10, 20, 30, 40, 50"
43-
vector_store = openai_client.vector_stores.create(name="DataStore")
44+
vector_store = openai_client.vector_stores.create(name="DataStore")
4445

4546
txt_file = BytesIO(txt_content.encode("utf-8"))
4647
txt_file.name = "data.txt"
@@ -109,8 +110,6 @@ def test_analyze_code_file(self, **kwargs):
109110

110111
vector_store = openai_client.vector_stores.create(name="CodeAnalysisStore")
111112

112-
from io import BytesIO
113-
114113
code_file = BytesIO(python_code.encode("utf-8"))
115114
code_file.name = "fibonacci.py"
116115

sdk/ai/azure-ai-projects/tests/agents/tools/multitool/test_agent_file_search_and_function.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ def test_data_analysis_workflow(self, **kwargs):
6262
vector_store = openai_client.vector_stores.create(name="SalesDataStore")
6363
print(f"Vector store created (id: {vector_store.id})")
6464

65-
from io import BytesIO
66-
6765
txt_file = BytesIO(txt_content.encode("utf-8"))
6866
txt_file.name = "sales_data.txt"
6967

@@ -269,8 +267,6 @@ def calculate_sum(numbers):
269267
# Create vector store and upload
270268
vector_store = openai_client.vector_stores.create(name="CodeStore")
271269

272-
from io import BytesIO
273-
274270
code_file = BytesIO(python_code.encode("utf-8"))
275271
code_file.name = "sample_code.py"
276272

sdk/ai/azure-ai-projects/tests/agents/tools/multitool/test_agent_file_search_code_interpreter_function.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import os
1616
import json
1717
import pytest
18+
from io import BytesIO
1819
from test_base import TestBase, servicePreparer
1920
from devtools_testutils import is_live_and_not_recording
2021
from azure.ai.projects.models import (
@@ -50,8 +51,6 @@ def test_complete_analysis_workflow(self, **kwargs):
5051
txt_content = "Sample data for analysis"
5152
vector_store = openai_client.vector_stores.create(name="ThreeToolStore")
5253

53-
from io import BytesIO
54-
5554
txt_file = BytesIO(txt_content.encode("utf-8"))
5655
txt_file.name = "data.txt"
5756

@@ -126,8 +125,6 @@ def test_four_tools_combination(self, **kwargs):
126125
txt_content = "Test data"
127126
vector_store = openai_client.vector_stores.create(name="FourToolStore")
128127

129-
from io import BytesIO
130-
131128
txt_file = BytesIO(txt_content.encode("utf-8"))
132129
txt_file.name = "data.txt"
133130

sdk/ai/azure-ai-projects/tests/agents/tools/multitool/test_multitool_with_conversations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import json
1414
import pytest
15+
from io import BytesIO
1516
from test_base import TestBase, servicePreparer
1617
from devtools_testutils import is_live_and_not_recording
1718
from azure.ai.projects.models import (
@@ -57,7 +58,6 @@ def test_file_search_and_function_with_conversation(self, **kwargs):
5758
"""
5859

5960
vector_store = openai_client.vector_stores.create(name="SalesDataStore")
60-
from io import BytesIO
6161

6262
file = BytesIO(doc_content.encode("utf-8"))
6363
file.name = "sales.txt"

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

Lines changed: 105 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ def test_agent_ai_search_question_answering(self, **kwargs):
8080

8181
model = self.test_agents_params["model_deployment_name"]
8282

83-
# Setup
84-
project_client = self.create_client(operation_group="agents", **kwargs)
85-
openai_client = project_client.get_openai_client()
86-
8783
# Get AI Search connection and index from environment
8884
ai_search_connection_id = kwargs.get("azure_ai_projects_tests_ai_search_project_connection_id")
8985
ai_search_index_name = kwargs.get("azure_ai_projects_tests_ai_search_index_name")
@@ -97,114 +93,118 @@ def test_agent_ai_search_question_answering(self, **kwargs):
9793
assert isinstance(ai_search_connection_id, str), "ai_search_connection_id must be a string"
9894
assert isinstance(ai_search_index_name, str), "ai_search_index_name must be a string"
9995

100-
# Create agent with Azure AI Search tool
101-
agent = project_client.agents.create_version(
102-
agent_name="ai-search-qa-agent",
103-
definition=PromptAgentDefinition(
104-
model=model,
105-
instructions="""You are a helpful assistant that answers true/false questions based on the provided search results.
96+
with (
97+
self.create_client(operation_group="agents", **kwargs) as project_client,
98+
project_client.get_openai_client() as openai_client,
99+
):
100+
# Create agent with Azure AI Search tool
101+
agent = project_client.agents.create_version(
102+
agent_name="ai-search-qa-agent",
103+
definition=PromptAgentDefinition(
104+
model=model,
105+
instructions="""You are a helpful assistant that answers true/false questions based on the provided search results.
106106
Always use the Azure AI Search tool to find relevant information before answering.
107107
Respond with only 'True' or 'False' based on what you find in the search results.
108108
If you cannot find clear evidence in the search results, answer 'False'.""",
109-
tools=[
110-
AzureAISearchAgentTool(
111-
azure_ai_search=AzureAISearchToolResource(
112-
indexes=[
113-
AISearchIndexResource(
114-
project_connection_id=ai_search_connection_id,
115-
index_name=ai_search_index_name,
116-
query_type=AzureAISearchQueryType.SIMPLE,
117-
),
118-
]
109+
tools=[
110+
AzureAISearchAgentTool(
111+
azure_ai_search=AzureAISearchToolResource(
112+
indexes=[
113+
AISearchIndexResource(
114+
project_connection_id=ai_search_connection_id,
115+
index_name=ai_search_index_name,
116+
query_type=AzureAISearchQueryType.SIMPLE,
117+
),
118+
]
119+
)
119120
)
120-
)
121-
],
122-
),
123-
description="Agent for testing AI Search question answering.",
124-
)
125-
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
126-
assert agent.id is not None
127-
assert agent.name == "ai-search-qa-agent"
128-
assert agent.version is not None
129-
130-
# Test each question
131-
correct_answers = 0
132-
total_questions = len(self.TEST_QUESTIONS)
133-
134-
for i, qa_pair in enumerate(self.TEST_QUESTIONS, 1):
135-
question = qa_pair["question"]
136-
expected_answer = qa_pair["answer"]
121+
],
122+
),
123+
description="Agent for testing AI Search question answering.",
124+
)
125+
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
126+
assert agent.id is not None
127+
assert agent.name == "ai-search-qa-agent"
128+
assert agent.version is not None
129+
130+
# Test each question
131+
correct_answers = 0
132+
total_questions = len(self.TEST_QUESTIONS)
133+
134+
for i, qa_pair in enumerate(self.TEST_QUESTIONS, 1):
135+
question = qa_pair["question"]
136+
expected_answer = qa_pair["answer"]
137+
138+
print(f"\n{'='*80}")
139+
print(f"Question {i}/{total_questions}:")
140+
print(f"Q: {question}")
141+
print(f"Expected: {expected_answer}")
142+
143+
output_text = ""
144+
145+
stream_response = openai_client.responses.create(
146+
stream=True,
147+
tool_choice="required",
148+
input=f"Answer this question with only 'True' or 'False': {question}",
149+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
150+
)
151+
152+
for event in stream_response:
153+
if event.type == "response.created":
154+
print(f"Response created with ID: {event.response.id}")
155+
elif event.type == "response.output_text.delta":
156+
pass # Don't print deltas to reduce output
157+
elif event.type == "response.completed":
158+
output_text = event.response.output_text
159+
print(f"Agent's answer: {output_text}")
160+
161+
# Parse the answer from the output
162+
# Look for "True" or "False" in the response
163+
output_lower = output_text.lower()
164+
agent_answer = None
165+
166+
# Try to extract boolean answer
167+
if "true" in output_lower and "false" not in output_lower:
168+
agent_answer = True
169+
elif "false" in output_lower and "true" not in output_lower:
170+
agent_answer = False
171+
elif output_lower.strip() in ["true", "false"]:
172+
agent_answer = output_lower.strip() == "true"
173+
else:
174+
# Try to determine based on more complex responses
175+
# Count occurrences
176+
true_count = output_lower.count("true")
177+
false_count = output_lower.count("false")
178+
if true_count > false_count:
179+
agent_answer = True
180+
elif false_count > true_count:
181+
agent_answer = False
182+
183+
if agent_answer is not None:
184+
is_correct = agent_answer == expected_answer
185+
if is_correct:
186+
correct_answers += 1
187+
print(f"✓ CORRECT (Agent: {agent_answer}, Expected: {expected_answer})")
188+
else:
189+
print(f"✗ INCORRECT (Agent: {agent_answer}, Expected: {expected_answer})")
190+
else:
191+
print(f"✗ UNABLE TO PARSE ANSWER from: {output_text}")
137192

193+
# Print summary
138194
print(f"\n{'='*80}")
139-
print(f"Question {i}/{total_questions}:")
140-
print(f"Q: {question}")
141-
print(f"Expected: {expected_answer}")
195+
print(f"SUMMARY: {correct_answers}/{total_questions} questions answered correctly")
196+
print(f"{'='*80}")
142197

143-
output_text = ""
144-
145-
stream_response = openai_client.responses.create(
146-
stream=True,
147-
tool_choice="required",
148-
input=f"Answer this question with only 'True' or 'False': {question}",
149-
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
198+
# Verify that at least 4 out of 5 questions were answered correctly
199+
assert correct_answers >= 4, (
200+
f"Expected at least 4 correct answers out of {total_questions}, "
201+
f"but got {correct_answers}. The agent needs to answer at least 80% correctly."
150202
)
151203

152-
for event in stream_response:
153-
if event.type == "response.created":
154-
print(f"Response created with ID: {event.response.id}")
155-
elif event.type == "response.output_text.delta":
156-
pass # Don't print deltas to reduce output
157-
elif event.type == "response.completed":
158-
output_text = event.response.output_text
159-
print(f"Agent's answer: {output_text}")
160-
161-
# Parse the answer from the output
162-
# Look for "True" or "False" in the response
163-
output_lower = output_text.lower()
164-
agent_answer = None
165-
166-
# Try to extract boolean answer
167-
if "true" in output_lower and "false" not in output_lower:
168-
agent_answer = True
169-
elif "false" in output_lower and "true" not in output_lower:
170-
agent_answer = False
171-
elif output_lower.strip() in ["true", "false"]:
172-
agent_answer = output_lower.strip() == "true"
173-
else:
174-
# Try to determine based on more complex responses
175-
# Count occurrences
176-
true_count = output_lower.count("true")
177-
false_count = output_lower.count("false")
178-
if true_count > false_count:
179-
agent_answer = True
180-
elif false_count > true_count:
181-
agent_answer = False
204+
print(
205+
f"\n✓ Test passed! Agent answered {correct_answers}/{total_questions} questions correctly (>= 4 required)"
206+
)
182207

183-
if agent_answer is not None:
184-
is_correct = agent_answer == expected_answer
185-
if is_correct:
186-
correct_answers += 1
187-
print(f"✓ CORRECT (Agent: {agent_answer}, Expected: {expected_answer})")
188-
else:
189-
print(f"✗ INCORRECT (Agent: {agent_answer}, Expected: {expected_answer})")
190-
else:
191-
print(f"✗ UNABLE TO PARSE ANSWER from: {output_text}")
192-
193-
# Print summary
194-
print(f"\n{'='*80}")
195-
print(f"SUMMARY: {correct_answers}/{total_questions} questions answered correctly")
196-
print(f"{'='*80}")
197-
198-
# Verify that at least 4 out of 5 questions were answered correctly
199-
assert correct_answers >= 4, (
200-
f"Expected at least 4 correct answers out of {total_questions}, "
201-
f"but got {correct_answers}. The agent needs to answer at least 80% correctly."
202-
)
203-
204-
print(
205-
f"\n✓ Test passed! Agent answered {correct_answers}/{total_questions} questions correctly (>= 4 required)"
206-
)
207-
208-
# Teardown
209-
project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
210-
print("Agent deleted")
208+
# Teardown
209+
project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
210+
print("Agent deleted")

0 commit comments

Comments
 (0)