@@ -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