Skip to content

Commit a9a1b1e

Browse files
authored
Python: Azure AI top_p and temperature parameters fix (#1839)
* azure ai parameters fix * added unit tests * unit test fix * fix
1 parent a5db641 commit a9a1b1e

File tree

2 files changed

+85
-5
lines changed

2 files changed

+85
-5
lines changed

python/packages/azure-ai/agent_framework_azure_ai/_chat_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,10 @@ async def _get_agent_id_or_create(self, run_options: dict[str, Any] | None = Non
374374
args["instructions"] = run_options["instructions"]
375375
if "response_format" in run_options:
376376
args["response_format"] = run_options["response_format"]
377+
if "temperature" in run_options:
378+
args["temperature"] = run_options["temperature"]
379+
if "top_p" in run_options:
380+
args["top_p"] = run_options["top_p"]
377381
created_agent = await self.project_client.agents.create_agent(**args)
378382
self.agent_id = str(created_agent.id)
379383
self._agent_definition = created_agent

python/packages/azure-ai/tests/test_azure_ai_agent_client.py

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@
3030
HostedWebSearchTool,
3131
Role,
3232
TextContent,
33+
ToolMode,
3334
UriContent,
3435
)
3536
from agent_framework._serialization import SerializationMixin
3637
from agent_framework.exceptions import ServiceInitializationError
3738
from azure.ai.agents.models import (
39+
AgentsNamedToolChoice,
40+
AgentsNamedToolChoiceType,
3841
CodeInterpreterToolDefinition,
3942
FileInfo,
4043
MessageDeltaChunk,
@@ -268,6 +271,29 @@ def test_azure_ai_chat_client_from_settings() -> None:
268271
assert client.agent_name == "TestAgent"
269272

270273

274+
async def test_azure_ai_chat_client_get_agent_id_or_create_with_temperature_and_top_p(
275+
mock_ai_project_client: MagicMock, azure_ai_unit_test_env: dict[str, str]
276+
) -> None:
277+
"""Test _get_agent_id_or_create with temperature and top_p in run_options."""
278+
azure_ai_settings = AzureAISettings(model_deployment_name=azure_ai_unit_test_env["AZURE_AI_MODEL_DEPLOYMENT_NAME"])
279+
chat_client = create_test_azure_ai_chat_client(mock_ai_project_client, azure_ai_settings=azure_ai_settings)
280+
281+
run_options = {
282+
"model": azure_ai_settings.model_deployment_name,
283+
"temperature": 0.7,
284+
"top_p": 0.9,
285+
}
286+
287+
agent_id = await chat_client._get_agent_id_or_create(run_options) # type: ignore
288+
289+
assert agent_id == "test-agent-id"
290+
# Verify create_agent was called with temperature and top_p parameters
291+
mock_ai_project_client.agents.create_agent.assert_called_once()
292+
call_kwargs = mock_ai_project_client.agents.create_agent.call_args[1]
293+
assert call_kwargs["temperature"] == 0.7
294+
assert call_kwargs["top_p"] == 0.9
295+
296+
271297
async def test_azure_ai_chat_client_get_agent_id_or_create_existing_agent(
272298
mock_ai_project_client: MagicMock,
273299
) -> None:
@@ -695,6 +721,47 @@ async def test_azure_ai_chat_client_create_run_options_with_auto_tool_choice(
695721
assert run_options["tool_choice"] == AgentsToolChoiceOptionMode.AUTO
696722

697723

724+
async def test_azure_ai_chat_client_prepare_tool_choice_none_string(
725+
mock_ai_project_client: MagicMock,
726+
) -> None:
727+
"""Test _prepare_tool_choice when tool_choice is string 'none'."""
728+
chat_client = create_test_azure_ai_chat_client(mock_ai_project_client)
729+
730+
# Create a mock tool for testing
731+
mock_tool = MagicMock()
732+
chat_options = ChatOptions(tools=[mock_tool], tool_choice="none")
733+
734+
# Call the method
735+
chat_client._prepare_tool_choice(chat_options) # type: ignore
736+
737+
# Verify tools are cleared and tool_choice is set to NONE mode
738+
assert chat_options.tools is None
739+
assert chat_options.tool_choice == ToolMode.NONE.mode
740+
741+
742+
async def test_azure_ai_chat_client_create_run_options_tool_choice_required_specific_function(
743+
mock_ai_project_client: MagicMock,
744+
) -> None:
745+
"""Test _create_run_options with ToolMode.REQUIRED specifying a specific function name."""
746+
chat_client = create_test_azure_ai_chat_client(mock_ai_project_client)
747+
748+
required_tool_mode = ToolMode.REQUIRED("specific_function_name")
749+
750+
dict_tool = {"type": "function", "function": {"name": "test_function"}}
751+
752+
chat_options = ChatOptions(tools=[dict_tool], tool_choice=required_tool_mode)
753+
messages = [ChatMessage(role=Role.USER, text="Hello")]
754+
755+
run_options, _ = await chat_client._create_run_options(messages, chat_options) # type: ignore
756+
757+
# Verify tool_choice is set to the specific named function
758+
assert "tool_choice" in run_options
759+
tool_choice = run_options["tool_choice"]
760+
assert isinstance(tool_choice, AgentsNamedToolChoice)
761+
assert tool_choice.type == AgentsNamedToolChoiceType.FUNCTION
762+
assert tool_choice.function.name == "specific_function_name" # type: ignore
763+
764+
698765
async def test_azure_ai_chat_client_create_run_options_with_response_format(
699766
mock_ai_project_client: MagicMock,
700767
) -> None:
@@ -868,9 +935,12 @@ async def test_azure_ai_chat_client_prep_tools_web_search_bing_grounding(mock_ai
868935

869936
assert len(result) == 1
870937
assert result[0] == {"type": "bing_grounding"}
871-
mock_bing_grounding.assert_called_once_with(
872-
connection_id="test-connection-id", count=5, freshness="Day", market="en-US", set_lang="en"
873-
)
938+
call_args = mock_bing_grounding.call_args[1]
939+
assert call_args["count"] == 5
940+
assert call_args["freshness"] == "Day"
941+
assert call_args["market"] == "en-US"
942+
assert call_args["set_lang"] == "en"
943+
assert "connection_id" in call_args
874944

875945

876946
async def test_azure_ai_chat_client_prep_tools_web_search_bing_grounding_with_connection_id(
@@ -953,7 +1023,10 @@ async def test_azure_ai_chat_client_prep_tools_web_search_custom_bing_connection
9531023
# Mock connection get to raise HttpResponseError
9541024
mock_ai_project_client.connections.get = AsyncMock(side_effect=HttpResponseError("Connection not found"))
9551025

956-
with pytest.raises(ServiceInitializationError, match="Bing custom connection 'nonexistent-connection' not found"):
1026+
with pytest.raises(
1027+
ServiceInitializationError,
1028+
match="Bing custom connection 'nonexistent-connection' not found in the Azure AI Project",
1029+
):
9571030
await chat_client._prep_tools([web_search_tool]) # type: ignore
9581031

9591032

@@ -973,7 +1046,10 @@ async def test_azure_ai_chat_client_prep_tools_web_search_bing_grounding_connect
9731046
# Mock connection get to raise HttpResponseError
9741047
mock_ai_project_client.connections.get = AsyncMock(side_effect=HttpResponseError("Connection not found"))
9751048

976-
with pytest.raises(ServiceInitializationError, match="Bing connection 'nonexistent-bing-connection' not found"):
1049+
with pytest.raises(
1050+
ServiceInitializationError,
1051+
match="Bing connection 'nonexistent-bing-connection' not found in the Azure AI Project",
1052+
):
9771053
await chat_client._prep_tools([web_search_tool]) # type: ignore
9781054

9791055

0 commit comments

Comments
 (0)