Skip to content

A programming framework for agentic AI

feat: [draft] update version of azureaiagent (#6581)

Victor Dibia9bbcfa03ac8c
diff --git a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/models.ipynb b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/models.ipynb
index 4700897e7..106e727ba 100644
--- a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/models.ipynb
+++ b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/models.ipynb
@@ -162,10 +162,10 @@
"metadata": {},
"outputs": [],
"source": [
+ "from autogen_core.models import UserMessage\n",
"from autogen_ext.auth.azure import AzureTokenProvider\n",
"from autogen_ext.models.openai import AzureOpenAIChatCompletionClient\n",
"from azure.identity import DefaultAzureCredential\n",
- "from autogen_core.models import UserMessage\n",
"\n",
"# Create the token provider\n",
"token_provider = AzureTokenProvider(\n",
diff --git a/python/packages/autogen-core/docs/src/user-guide/extensions-user-guide/azure-foundry-agent.ipynb b/python/packages/autogen-core/docs/src/user-guide/extensions-user-guide/azure-foundry-agent.ipynb
new file mode 100644
index 000000000..c84360122
--- /dev/null
+++ b/python/packages/autogen-core/docs/src/user-guide/extensions-user-guide/azure-foundry-agent.ipynb
@@ -0,0 +1,131 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Azure AI Foundry Agent\n",
+ "\n",
+ "In AutoGen, you can build and deploy agents that are backed by the [Azure AI Foundry Agent Service](https://learn.microsoft.com/en-us/azure/ai-services/agents/overview) using the {py:class}`~autogen_ext.agents.azure._azure_ai_agent.AzureAIAgent` class. Here, important aspects of the agent including the provisioned model, tools (e.g, code interpreter, bing search grounding, file search etc.), observability, and security are managed by Azure. This allows you to focus on building your agent without worrying about the underlying infrastructure.\n",
+ "\n",
+ "In this guide, we will explore an example of creating an Azure AI Foundry Agent using the `AzureAIAgent` that can address tasks using the Azure Grounding with Bing Search tool."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# pip install \"autogen-ext[azure]\" # For Azure AI Foundry Agent Service"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Bing Search Grounding \n",
+ "\n",
+ "An {py:class}`~autogen_ext.agents.azure._azure_ai_agent.AzureAIAgent` can be assigned a set of tools including [Grounding with Bing Search](https://learn.microsoft.com/en-us/azure/ai-services/agents/how-to/tools/bing-grounding?tabs=python&pivots=overview#setup). \n",
+ "\n",
+ "Grounding with Bing Search allows your Azure AI Agents to incorporate real-time public web data when generating responses. You need to create a Grounding with Bing Search resource, and then connect this resource to your Azure AI Agents. When a user sends a query, Azure AI Agents decide if Grounding with Bing Search should be leveraged or not. If so, it will leverage Bing to search over public web data and return relevant chunks. Lastly, Azure AI Agents will use returned chunks to generate a response.\n",
+ "\n",
+ "## Prerequisites\n",
+ "\n",
+ "- You need to have an Azure subscription.\n",
+ "- You need to have the Azure CLI installed and configured. (also login using the command `az login` to enable default credentials)\n",
+ "- You need to have the `autogen-ext[azure]` package installed.\n",
+ "\n",
+ "You can create a [Grounding with Bing Search resource in the Azure portal](https://portal.azure.com/#create/Microsoft.BingGroundingSearch). Note that you will need to have owner or contributor role in your subscription or resource group to create it. Once you have created your resource, you can then pass it to the Azure Foundry Agent using the resource name.\n",
+ "\n",
+ "In the following example, we will create a new Azure Foundry Agent that uses the Grounding with Bing Search resource.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import os\n",
+ "\n",
+ "import dotenv\n",
+ "from autogen_agentchat.messages import TextMessage\n",
+ "from autogen_core import CancellationToken\n",
+ "from autogen_ext.agents.azure import AzureAIAgent\n",
+ "from azure.ai.agents.models import BingGroundingTool\n",
+ "from azure.ai.projects.aio import AIProjectClient\n",
+ "from azure.identity.aio import DefaultAzureCredential\n",
+ "\n",
+ "dotenv.load_dotenv()\n",
+ "\n",
+ "\n",
+ "async def bing_example() -> None:\n",
+ " async with DefaultAzureCredential() as credential: # type: ignore\n",
+ " async with AIProjectClient( # type: ignore\n",
+ " credential=credential, endpoint=os.getenv(\"AZURE_PROJECT_ENDPOINT\", \"\")\n",
+ " ) as project_client:\n",
+ " conn = await project_client.connections.get(name=os.getenv(\"BING_CONNECTION_NAME\", \"\"))\n",
+ "\n",
+ " bing_tool = BingGroundingTool(conn.id)\n",
+ " agent_with_bing_grounding = AzureAIAgent(\n",
+ " name=\"bing_agent\",\n",
+ " description=\"An AI assistant with Bing grounding\",\n",
+ " project_client=project_client,\n",
+ " deployment_name=\"gpt-4o\",\n",
+ " instructions=\"You are a helpful assistant.\",\n",
+ " tools=bing_tool.definitions,\n",
+ " metadata={\"source\": \"AzureAIAgent\"},\n",
+ " )\n",
+ "\n",
+ " # For the bing grounding tool to return the citations, the message must contain an instruction for the model to do return them.\n",
+ " # For example: \"Please provide citations for the answers\"\n",
+ "\n",
+ " result = await agent_with_bing_grounding.on_messages(\n",
+ " messages=[\n",
+ " TextMessage(\n",
+ " content=\"What is Microsoft's annual leave policy? Provide citations for your answers.\",\n",
+ " source=\"user\",\n",
+ " )\n",
+ " ],\n",
+ " cancellation_token=CancellationToken(),\n",
+ " message_limit=5,\n",
+ " )\n",
+ " print(result)\n",
+ "\n",
+ "\n",
+ "await bing_example()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Note that you can also provide other Azure Backed [tools](https://learn.microsoft.com/en-us/azure/ai-services/agents/how-to/tools/overview) and local client side functions to the agent.\n",
+ "\n",
+ "See the {py:class}`~autogen_ext.agents.azure._azure_ai_agent.AzureAIAgent` class api documentation for more details on how to create an Azure Foundry Agent."
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": ".venv",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.11.12"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}
diff --git a/python/packages/autogen-core/docs/src/user-guide/extensions-user-guide/index.md b/python/packages/autogen-core/docs/src/user-guide/extensions-user-guide/index.md
index c0f918ba2..964acbbc0 100644
--- a/python/packages/autogen-core/docs/src/user-guide/extensions-user-guide/index.md
+++ b/python/packages/autogen-core/docs/src/user-guide/extensions-user-guide/index.md
@@ -22,6 +22,7 @@ create-your-own
:caption: Guides
azure-container-code-executor
+azure-foundry-agent
```
AutoGen is designed to be extensible. The `autogen-ext` package contains the built-in component implementations maintained by the AutoGen project.
diff --git a/python/packages/autogen-ext/pyproject.toml b/python/packages/autogen-ext/pyproject.toml
index a86156591..ba4cd4bff 100644
--- a/python/packages/autogen-ext/pyproject.toml
+++ b/python/packages/autogen-ext/pyproject.toml
@@ -22,8 +22,8 @@ dependencies = [
anthropic = ["anthropic>=0.48"]
langchain = ["langchain_core~= 0.3.3"]
azure = [
- "azure-ai-inference>=1.0.0b7",
- "azure-ai-projects>=1.0.0b8",
+ "azure-ai-inference>=1.0.0b9",
+ "azure-ai-projects>=1.0.0b11",
"azure-core",
"azure-identity",
"azure-search-documents>=11.4.0",
diff --git a/python/packages/autogen-ext/src/autogen_ext/agents/azure/_azure_ai_agent.py b/python/packages/autogen-ext/src/autogen_ext/agents/azure/_azure_ai_agent.py
index a64fcb70e..e258725f1 100644
--- a/python/packages/autogen-ext/src/autogen_ext/agents/azure/_azure_ai_agent.py
+++ b/python/packages/autogen-ext/src/autogen_ext/agents/azure/_azure_ai_agent.py
@@ -33,8 +33,36 @@ from autogen_core import CancellationToken, FunctionCall
from autogen_core.models._types import FunctionExecutionResult
from autogen_core.tools import FunctionTool, Tool
-import azure.ai.projects.models as models
-from azure.ai.projects import _types
+from azure.ai.agents.models import (
+ Agent,
+ AgentsResponseFormat,
+ AgentThread,
+ AzureAISearchToolDefinition,
+ AzureFunctionToolDefinition,
+ BingGroundingToolDefinition,
+ CodeInterpreterToolDefinition,
+ CodeInterpreterToolResource,
+ FileInfo,
+ FilePurpose,
+ FileSearchToolDefinition,
+ FileSearchToolResource,
+ FileState,
+ FunctionDefinition,
+ FunctionToolDefinition,
+ ListSortOrder,
+ MessageRole,
+ MessageTextUrlCitationAnnotation,
+ RunStatus,
+ ThreadRun,
+ ToolDefinition,
+ ToolOutput,
+ ToolResources,
+ VectorStore,
+ VectorStoreChunkingStrategyRequest,
+ VectorStoreDataSource,
+ VectorStoreExpirationPolicy,
+)
+from azure.ai.agents.models._patch import ThreadMessage
from azure.ai.projects.aio import AIProjectClient
from ._types import AzureAIAgentState, ListToolType
@@ -89,43 +117,42 @@ class AzureAIAgent(BaseChatAgent):
from autogen_ext.agents.azure._azure_ai_agent import AzureAIAgent
from azure.ai.projects.aio import AIProjectClient
from azure.identity.aio import DefaultAzureCredential
- import azure.ai.projects.models as models
+ from azure.ai.agents.models import BingGroundingTool
import dotenv
async def bing_example():
- credential = DefaultAzureCredential()
-
- async with AIProjectClient.from_connection_string( # type: ignore
- credential=credential, conn_str=os.getenv("AI_PROJECT_CONNECTION_STRING", "")
- ) as project_client:
- conn = await project_client.connections.get(connection_name=os.getenv("BING_CONNECTION_NAME", ""))
-
- bing_tool = models.BingGroundingTool(conn.id)
- agent_with_bing_grounding = AzureAIAgent(
- name="bing_agent",
- description="An AI assistant with Bing grounding",
- project_client=project_client,
- deployment_name="gpt-4o",
- instructions="You are a helpful assistant.",
- tools=bing_tool.definitions,
- metadata={"source": "AzureAIAgent"},
- )
+ async with DefaultAzureCredential() as credential:
+ async with AIProjectClient( # type: ignore
+ credential=credential, endpoint=os.getenv("AZURE_PROJECT_ENDPOINT", "")
+ ) as project_client:
+ conn = await project_client.connections.get(name=os.getenv("BING_CONNECTION_NAME", ""))
- # For the bing grounding tool to return the citations, the message must contain an instruction for the model to do return them.
- # For example: "Please provide citations for the answers"
+ bing_tool = BingGroundingTool(conn.id)
+ agent_with_bing_grounding = AzureAIAgent(
+ name="bing_agent",
+ description="An AI assistant with Bing grounding",
+ project_client=project_client,
+ deployment_name="gpt-4o",
+ instructions="You are a helpful assistant.",
+ tools=bing_tool.definitions,
+ metadata={"source": "AzureAIAgent"},
+ )
- result = await agent_with_bing_grounding.on_messages(
- messages=[
- TextMessage(
- content="What is Microsoft's annual leave policy? Provide citations for your answers.",
- source="user",
- )
- ],
- cancellation_token=CancellationToken(),
- message_limit=5,
- )
- print(result)
+ # For the bing grounding tool to return the citations, the message must contain an instruction for the model to do return them.
+ # For example: "Please provide citations for the answers"
+
+ result = await agent_with_bing_grounding.on_messages(
+ messages=[
+ TextMessage(
+ content="What is Microsoft\\'s annual leave policy? Provide citations for your answers.",
+ source="user",
+ )
+ ],
+ cancellation_token=CancellationToken(),
+ message_limit=5,
+ )
+ print(result)
if __name__ == "__main__":
@@ -160,36 +187,39 @@ class AzureAIAgent(BaseChatAgent):
urllib.request.urlretrieve(readme_url, temp_file.name)
print(f"Downloaded README.md to {temp_file.name}")
- credential = DefaultAzureCredential()
- async with AIProjectClient.from_connection_string( # type: ignore
- credential=credential, conn_str=os.getenv("AI_PROJECT_CONNECTION_STRING", "")
- ) as project_client:
- agent_with_file_search = AzureAIAgent(
- name="file_search_agent",
- description="An AI assistant with file search capabilities",
- project_client=project_client,
- deployment_name="gpt-4o",
- instructions="You are a helpful assistant.",
- tools=["file_search"],
- metadata={"source": "AzureAIAgent"},
- )
+ async with DefaultAzureCredential() as credential:
+ async with AIProjectClient( # type: ignore
+ credential=credential, endpoint=os.getenv("AZURE_PROJECT_ENDPOINT", "")
+ ) as project_client:
+ agent_with_file_search = AzureAIAgent(
+ name="file_search_agent",
+ description="An AI assistant with file search capabilities",
+ project_client=project_client,
+ deployment_name="gpt-4.1-mini",
+ instructions="You are a helpful assistant.",
+ tools=["file_search"],
+ metadata={"source": "AzureAIAgent"},
+ )
- ct: CancellationToken = CancellationToken()
- # Use the downloaded README file for file search
- await agent_with_file_search.on_upload_for_file_search(
- file_paths=[temp_file.name],
- vector_store_name="file_upload_index",
- vector_store_metadata={"source": "AzureAIAgent"},
- cancellation_token=ct,
- )
- result = await agent_with_file_search.on_messages(
- messages=[
- TextMessage(content="Hello, what is AutoGen and what capabilities does it have?", source="user")
- ],
- cancellation_token=ct,
- message_limit=5,
- )
- print(result)
+ ct: CancellationToken = CancellationToken()
+ # Use the downloaded README file for file search
+ await agent_with_file_search.on_upload_for_file_search(
+ file_paths=[temp_file.name],
+ vector_store_name="file_upload_index",
+ vector_store_metadata={"source": "AzureAIAgent"},
+ cancellation_token=ct,
+ vector_store_polling_interval=60,
+ )
+ result = await agent_with_file_search.on_messages(
+ messages=[
+ TextMessage(
+ content="Hello, what is AutoGen and what capabilities does it have?", source="user"
+ )
+ ],
+ cancellation_token=ct,
+ message_limit=5,
+ )
+ print(result)
finally:
# Clean up the temporary file
if temp_file and os.path.exists(temp_file.name):
@@ -217,36 +247,37 @@ class AzureAIAgent(BaseChatAgent):
async def code_interpreter_example():
- credential = DefaultAzureCredential()
- async with AIProjectClient.from_connection_string( # type: ignore
- credential=credential, conn_str=os.getenv("AI_PROJECT_CONNECTION_STRING", "")
- ) as project_client:
- agent_with_code_interpreter = AzureAIAgent(
- name="code_interpreter_agent",
- description="An AI assistant with code interpreter capabilities",
- project_client=project_client,
- deployment_name="gpt-4o",
- instructions="You are a helpful assistant.",
- tools=["code_interpreter"],
- metadata={"source": "AzureAIAgent"},
- )
+ async with DefaultAzureCredential() as credential:
+ async with AIProjectClient( # type: ignore
+ credential=credential, endpoint=os.getenv("AZURE_PROJECT_ENDPOINT", "")
+ ) as project_client:
+ agent_with_code_interpreter = AzureAIAgent(
+ name="code_interpreter_agent",
+ description="An AI assistant with code interpreter capabilities",
+ project_client=project_client,
+ deployment_name="gpt-4.1-mini",
+ instructions="You are a helpful assistant.",
+ tools=["code_interpreter"],
+ metadata={"source": "AzureAIAgent"},
+ )
- await agent_with_code_interpreter.on_upload_for_code_interpreter(
- file_paths="/workspaces/autogen/python/packages/autogen-core/docs/src/user-guide/core-user-guide/cookbook/data/nifty_500_quarterly_results.csv",
- cancellation_token=CancellationToken(),
- )
+ await agent_with_code_interpreter.on_upload_for_code_interpreter(
+ file_paths="/workspaces/autogen/python/packages/autogen-core/docs/src/user-guide/core-user-guide/cookbook/data/nifty_500_quarterly_results.csv",
+ cancellation_token=CancellationToken(),
+ polling_interval=5,
+ )
- result = await agent_with_code_interpreter.on_messages(
- messages=[
- TextMessage(
- content="Aggregate the number of stocks per industry and give me a markdown table as a result?",
- source="user",
- )
- ],
- cancellation_token=CancellationToken(),
- )
+ result = await agent_with_code_interpreter.on_messages(
+ messages=[
+ TextMessage(
+ content="Aggregate the number of stocks per industry and give me a markdown table as a result?",
+ source="user",
+ )
+ ],
+ cancellation_token=CancellationToken(),
+ )
- print(result)
+ print(result)
if __name__ == "__main__":
@@ -265,9 +296,9 @@ class AzureAIAgent(BaseChatAgent):
agent_id: Optional[str] = None,
thread_id: Optional[str] = None,
metadata: Optional[Dict[str, str]] = None,
- response_format: Optional["_types.AgentsApiResponseFormatOption"] = None,
+ response_format: Optional[AgentsResponseFormat] = None,
temperature: Optional[float] = None,
- tool_resources: Optional["models.ToolResources"] = None,
+ tool_resources: Optional[ToolResources] = None,
top_p: Optional[float] = None,
) -> None:
"""
@@ -300,12 +331,12 @@ class AzureAIAgent(BaseChatAgent):
self._original_tools: list[Tool] = []
- converted_tools: List["models.ToolDefinition"] = []
+ converted_tools: List[ToolDefinition] = []
self._add_tools(tools, converted_tools)
self._project_client = project_client
- self._agent: Optional["models.Agent"] = None
- self._thread: Optional["models.AgentThread"] = None
+ self._agent: Optional[Agent] = None
+ self._thread: Optional[AgentThread] = None
self._init_thread_id = thread_id
self._deployment_name = deployment_name
self._instructions = instructions
@@ -365,16 +396,16 @@ class AzureAIAgent(BaseChatAgent):
return self._instructions
@property
- def tools(self) -> List[models.ToolDefinition]:
+ def tools(self) -> List[ToolDefinition]:
"""
Get the list of tools available to the agent.
Returns:
- List[models.ToolDefinition]: The list of tool definitions.
+ List[ToolDefinition]: The list of tool definitions.
"""
return self._api_tools
- def _add_tools(self, tools: Optional[ListToolType], converted_tools: List["models.ToolDefinition"]) -> None:
+ def _add_tools(self, tools: Optional[ListToolType], converted_tools: List[ToolDefinition]) -> None:
"""
Convert various tool formats to Azure AI Agent tool definitions.
@@ -391,20 +422,20 @@ class AzureAIAgent(BaseChatAgent):
for tool in tools:
if isinstance(tool, str):
if tool == "file_search":
- converted_tools.append(models.FileSearchToolDefinition())
+ converted_tools.append(FileSearchToolDefinition())
elif tool == "code_interpreter":
- converted_tools.append(models.CodeInterpreterToolDefinition())
+ converted_tools.append(CodeInterpreterToolDefinition())
elif tool == "bing_grounding":
- converted_tools.append(models.BingGroundingToolDefinition()) # type: ignore
+ converted_tools.append(BingGroundingToolDefinition()) # type: ignore
elif tool == "azure_ai_search":
- converted_tools.append(models.AzureAISearchToolDefinition())
+ converted_tools.append(AzureAISearchToolDefinition())
elif tool == "azure_function":
- converted_tools.append(models.AzureFunctionToolDefinition()) # type: ignore
- elif tool == "sharepoint_grounding":
- converted_tools.append(models.SharepointToolDefinition()) # type: ignore
+ converted_tools.append(AzureFunctionToolDefinition()) # type: ignore
+ # elif tool == "sharepoint_grounding":
+ # converted_tools.append(SharepointToolDefinition()) # type: ignore
else:
raise ValueError(f"Unsupported tool string: {tool}")
- elif isinstance(tool, models.ToolDefinition):
+ elif isinstance(tool, ToolDefinition):
converted_tools.append(tool)
elif isinstance(tool, Tool):
self._original_tools.append(tool)
@@ -420,7 +451,7 @@ class AzureAIAgent(BaseChatAgent):
else:
raise ValueError(f"Unsupported tool type: {type(tool)}")
- def _convert_tool_to_function_tool_definition(self, tool: Tool) -> models.FunctionToolDefinition:
+ def _convert_tool_to_function_tool_definition(self, tool: Tool) -> FunctionToolDefinition:
"""
Convert an autogen Tool to an Azure AI Agent function tool definition.
@@ -442,9 +473,9 @@ class AzureAIAgent(BaseChatAgent):
if "required" in schema["parameters"]:
parameters["required"] = schema["parameters"]["required"]
- func_definition = models.FunctionDefinition(name=tool.name, description=tool.description, parameters=parameters)
+ func_definition = FunctionDefinition(name=tool.name, description=tool.description, parameters=parameters)
- return models.FunctionToolDefinition(
+ return FunctionToolDefinition(
function=func_definition,
)
@@ -482,13 +513,13 @@ class AzureAIAgent(BaseChatAgent):
if self._thread is None or create_new_thread:
if self._init_thread_id and create_new_thread is False:
- self._thread = await self._project_client.agents.get_thread(thread_id=self._init_thread_id)
+ self._thread = await self._project_client.agents.threads.get(thread_id=self._init_thread_id)
# Retrieve initial state only once
if not self._initial_state_retrieved:
await self._retrieve_initial_state()
self._initial_state_retrieved = True
else:
- self._thread = await self._project_client.agents.create_thread()
+ self._thread = await self._project_client.agents.threads.create()
async def _retrieve_initial_state(self) -> None:
"""
@@ -500,17 +531,12 @@ class AzureAIAgent(BaseChatAgent):
"""
# Retrieve all initial message IDs
initial_message_ids: Set[str] = set()
- after: str | None = None
- while True:
- msgs: models.OpenAIPageableListOfThreadMessage = await self._project_client.agents.list_messages(
- thread_id=self.thread_id, after=after, order=models.ListSortOrder.ASCENDING, limit=100
- )
-
- for msg in msgs.data:
- initial_message_ids.add(msg.id)
- if not msgs.has_more:
- break
- after = msgs.data[-1].id
+ async for msg in self._project_client.agents.messages.list(
+ thread_id=self.thread_id,
+ order=ListSortOrder.ASCENDING,
+ limit=100,
+ ):
+ initial_message_ids.add(msg.id)
self._initial_message_ids = initial_message_ids
async def _execute_tool_call(self, tool_call: FunctionCall, cancellation_token: CancellationToken) -> str:
@@ -540,7 +566,7 @@ class AzureAIAgent(BaseChatAgent):
self,
file_paths: str | Iterable[str],
purpose: str = "assistant",
- sleep_interval: float = 0.5,
+ polling_interval: float = 0.5,
cancellation_token: Optional[CancellationToken] = None,
) -> List[str]:
"""
@@ -552,7 +578,7 @@ class AzureAIAgent(BaseChatAgent):
Args:
file_paths (str | Iterable[str]): Path(s) to file(s) to upload
purpose (str): The purpose of the file, defaults to "assistant"
- sleep_interval (float): Time to sleep between polling for file status
+ polling_interval (float): Time to sleep between polling for file status
cancellation_token (Optional[CancellationToken]): Token for cancellation handling
Returns:
@@ -573,15 +599,15 @@ class AzureAIAgent(BaseChatAgent):
for file_path in file_paths:
file_name = os.path.basename(file_path)
- file: models.OpenAIFile = await cancellation_token.link_future(
+ file: FileInfo = await cancellation_token.link_future(
asyncio.ensure_future(
- self._project_client.agents.upload_file_and_poll(
- file_path=file_path, purpose=purpose, sleep_interval=sleep_interval
+ self._project_client.agents.files.upload_and_poll(
+ file_path=file_path, purpose=purpose, polling_interval=polling_interval
)
)
)
- if file.status != models.FileState.PROCESSED:
+ if file.status != FileState.PROCESSED:
raise ValueError(f"File upload failed with status {file.status}")
trace_logger.debug(f"File uploaded successfully: {file.id}, {file_name}")
@@ -605,7 +631,7 @@ class AzureAIAgent(BaseChatAgent):
It delegates to on_messages_stream and returns the final response.
Args:
- messages (Sequence[ChatMessage]): The messages to process
+ messages (Sequence[BaseChatMessage]): The messages to process
cancellation_token (CancellationToken): Token for cancellation handling
message_limit (int, optional): Maximum number of messages to retrieve from the thread
@@ -627,7 +653,7 @@ class AzureAIAgent(BaseChatAgent):
messages: Sequence[BaseChatMessage],
cancellation_token: Optional[CancellationToken] = None,
message_limit: int = 1,
- sleep_interval: float = 0.5,
+ polling_interval: float = 0.5,
) -> AsyncGenerator[AgentEvent | ChatMessage | Response, None]:
"""
Process incoming messages and yield streaming responses from the Azure AI agent.
@@ -642,10 +668,10 @@ class AzureAIAgent(BaseChatAgent):
the complete Response with the agent's message.
Args:
- messages (Sequence[ChatMessage]): The messages to process
+ messages (Sequence[BaseChatMessage]): The messages to process
cancellation_token (CancellationToken): Token for cancellation handling
message_limit (int, optional): Maximum number of messages to retrieve from the thread
- sleep_interval (float, optional): Time to sleep between polling for run status
+ polling_interval (float, optional): Time to sleep between polling for run status
Yields:
AgentEvent | ChatMessage | Response: Events during processing and the final response
@@ -669,9 +695,9 @@ class AzureAIAgent(BaseChatAgent):
inner_messages: List[AgentEvent | ChatMessage] = []
# Create and start a run
- run: models.ThreadRun = await cancellation_token.link_future(
+ run: ThreadRun = await cancellation_token.link_future(
asyncio.ensure_future(
- self._project_client.agents.create_run(
+ self._project_client.agents.runs.create(
thread_id=self.thread_id,
agent_id=self._get_agent_id,
)
@@ -682,18 +708,18 @@ class AzureAIAgent(BaseChatAgent):
while True:
run = await cancellation_token.link_future(
asyncio.ensure_future(
- self._project_client.agents.get_run(
+ self._project_client.agents.runs.get(
thread_id=self.thread_id,
run_id=run.id,
)
)
)
- if run.status == models.RunStatus.FAILED:
+ if run.status == RunStatus.FAILED:
raise ValueError(f"Run failed: {run.last_error}")
# If the run requires action (function calls), execute tools and continue
- if run.status == models.RunStatus.REQUIRES_ACTION and run.required_action is not None:
+ if run.status == RunStatus.REQUIRES_ACTION and run.required_action is not None:
tool_calls: List[FunctionCall] = []
submit_tool_outputs = getattr(run.required_action, "submit_tool_outputs", None)
if submit_tool_outputs and hasattr(submit_tool_outputs, "tool_calls"):
@@ -740,44 +766,47 @@ class AzureAIAgent(BaseChatAgent):
# Submit tool outputs back to the run
run = await cancellation_token.link_future(
asyncio.ensure_future(
- self._project_client.agents.submit_tool_outputs_to_run(
+ self._project_client.agents.runs.submit_tool_outputs(
thread_id=self.thread_id,
run_id=run.id,
- tool_outputs=[
- models.ToolOutput(tool_call_id=t.call_id, output=t.content) for t in tool_outputs
- ],
+ tool_outputs=[ToolOutput(tool_call_id=t.call_id, output=t.content) for t in tool_outputs],
)
)
)
continue
- if run.status == models.RunStatus.COMPLETED:
+ if run.status == RunStatus.COMPLETED:
break
# TODO support for parameter to control polling interval
- await asyncio.sleep(sleep_interval)
+ await asyncio.sleep(polling_interval)
# After run is completed, get the messages
trace_logger.debug("Retrieving messages from thread")
- agent_messages: models.OpenAIPageableListOfThreadMessage = await cancellation_token.link_future(
- asyncio.ensure_future(
- self._project_client.agents.list_messages(
- thread_id=self.thread_id, order=models.ListSortOrder.DESCENDING, limit=message_limit
- )
- )
- )
-
- if not agent_messages.data:
+ # Collect up to message_limit messages in DESCENDING order, support cancellation
+ agent_messages: List[ThreadMessage] = []
+ async for msg in self._project_client.agents.messages.list(
+ thread_id=self.thread_id,
+ order=ListSortOrder.DESCENDING,
+ limit=message_limit,
+ ):
+ if cancellation_token.is_cancelled():
+ trace_logger.debug("Message retrieval cancelled by token.")
+ break
+ agent_messages.append(msg)
+ if len(agent_messages) >= message_limit:
+ break
+ if not agent_messages:
raise ValueError("No messages received from assistant")
- # Get the last message from the agent
- last_message: Optional[models.ThreadMessage] = agent_messages.get_last_message_by_role(models.MessageRole.AGENT)
-
+ # Get the last message from the agent (role=AGENT)
+ last_message: Optional[ThreadMessage] = next(
+ (m for m in agent_messages if getattr(m, "role", None) == "agent"), None
+ )
if not last_message:
trace_logger.debug("No message with AGENT role found, falling back to first message")
- last_message = agent_messages.data[0] # Fallback to first message
-
- if not last_message.content:
+ last_message = agent_messages[0] # Fallback to first message
+ if not getattr(last_message, "content", None):
raise ValueError("No content in the last message")
# Extract text content
@@ -793,7 +822,7 @@ class AzureAIAgent(BaseChatAgent):
annotations = getattr(last_message, "annotations", [])
if isinstance(annotations, list) and annotations:
- annotations = cast(List[models.MessageTextUrlCitationAnnotation], annotations)
+ annotations = cast(List[MessageTextUrlCitationAnnotation], annotations)
trace_logger.debug(f"Found {len(annotations)} annotations")
for annotation in annotations:
@@ -850,10 +879,10 @@ class AzureAIAgent(BaseChatAgent):
await cancellation_token.link_future(
asyncio.ensure_future(
- self._project_client.agents.create_message(
+ self._project_client.agents.messages.create(
thread_id=self.thread_id,
+ role=MessageRole.USER,
content=content,
- role=models.MessageRole.USER,
)
)
)
@@ -914,7 +943,7 @@ class AzureAIAgent(BaseChatAgent):
self,
file_paths: str | Iterable[str],
cancellation_token: Optional[CancellationToken] = None,
- sleep_interval: float = 0.5,
+ polling_interval: float = 0.5,
) -> None:
"""
Upload files to be used with the code interpreter tool.
@@ -925,7 +954,7 @@ class AzureAIAgent(BaseChatAgent):
Args:
file_paths (str | Iterable[str]): Path(s) to file(s) to upload
cancellation_token (Optional[CancellationToken]): Token for cancellation handling
- sleep_interval (float): Time to sleep between polling for file status
+ polling_interval (float): Time to sleep between polling for file status
Raises:
ValueError: If file upload fails or the agent doesn't have code interpreter capability
@@ -938,26 +967,26 @@ class AzureAIAgent(BaseChatAgent):
file_ids = await self._upload_files(
file_paths=file_paths,
cancellation_token=cancellation_token,
- sleep_interval=sleep_interval,
- purpose=models.FilePurpose.AGENTS,
+ polling_interval=polling_interval,
+ purpose=FilePurpose.AGENTS,
)
# Update thread with the new files
- thread: models.AgentThread = await cancellation_token.link_future(
- asyncio.ensure_future(self._project_client.agents.get_thread(thread_id=self.thread_id))
+ thread: AgentThread = await cancellation_token.link_future(
+ asyncio.ensure_future(self._project_client.agents.threads.get(thread_id=self.thread_id))
)
- tool_resources: models.ToolResources = thread.tool_resources or models.ToolResources()
- code_interpreter_resource = tool_resources.code_interpreter or models.CodeInterpreterToolResource()
+ tool_resources: ToolResources = thread.tool_resources or ToolResources()
+ code_interpreter_resource = tool_resources.code_interpreter or CodeInterpreterToolResource()
existing_file_ids: List[str] = code_interpreter_resource.file_ids or []
existing_file_ids.extend(file_ids)
await cancellation_token.link_future(
asyncio.ensure_future(
- self._project_client.agents.update_thread(
+ self._project_client.agents.threads.update(
thread_id=self.thread_id,
- tool_resources=models.ToolResources(
- code_interpreter=models.CodeInterpreterToolResource(file_ids=existing_file_ids)
+ tool_resources=ToolResources(
+ code_interpreter=CodeInterpreterToolResource(file_ids=existing_file_ids)
),
)
)
@@ -968,11 +997,11 @@ class AzureAIAgent(BaseChatAgent):
file_paths: str | Iterable[str],
cancellation_token: CancellationToken,
vector_store_name: Optional[str] = None,
- data_sources: Optional[List[models.VectorStoreDataSource]] = None,
- expires_after: Optional[models.VectorStoreExpirationPolicy] = None,
- chunking_strategy: Optional[models.VectorStoreChunkingStrategyRequest] = None,
+ data_sources: Optional[List[VectorStoreDataSource]] = None,
+ expires_after: Optional[VectorStoreExpirationPolicy] = None,
+ chunking_strategy: Optional[VectorStoreChunkingStrategyRequest] = None,
vector_store_metadata: Optional[Dict[str, str]] = None,
- vector_store_polling_sleep_interval: float = 1,
+ vector_store_polling_interval: float = 1,
) -> None:
"""
Upload files to be used with the file search tool.
@@ -984,11 +1013,11 @@ class AzureAIAgent(BaseChatAgent):
file_paths (str | Iterable[str]): Path(s) to file(s) to upload
cancellation_token (CancellationToken): Token for cancellation handling
vector_store_name (Optional[str]): Name to assign to the vector store if creating a new one
- data_sources (Optional[List[models.VectorStoreDataSource]]): Additional data sources for the vector store
- expires_after (Optional[models.VectorStoreExpirationPolicy]): Expiration policy for vector store content
- chunking_strategy (Optional[models.VectorStoreChunkingStrategyRequest]): Strategy for chunking file content
+ data_sources (Optional[List[VectorStoreDataSource]]): Additional data sources for the vector store
+ expires_after (Optional[VectorStoreExpirationPolicy]): Expiration policy for vector store content
+ chunking_strategy (Optional[VectorStoreChunkingStrategyRequest]): Strategy for chunking file content
vector_store_metadata (Optional[Dict[str, str]]): Additional metadata for the vector store
- vector_store_polling_sleep_interval (float): Time to sleep between polling for vector store status
+ vector_store_polling_interval (float): Time to sleep between polling for vector store status
Raises:
ValueError: If file search is not enabled for this agent or file upload fails
@@ -996,23 +1025,23 @@ class AzureAIAgent(BaseChatAgent):
await self._ensure_initialized()
# Check if file_search is enabled in tools
- if not any(tool.get("type") == "file_search" for tool in self._api_tools):
+ if not any(isinstance(tool, FileSearchToolDefinition) for tool in self._api_tools):
raise ValueError(
"File search is not enabled for this assistant. Add a file_search tool when creating the assistant."
)
# Create vector store if not already created
if self._vector_store_id is None:
- vector_store: models.VectorStore = await cancellation_token.link_future(
+ vector_store: VectorStore = await cancellation_token.link_future(
asyncio.ensure_future(
- self._project_client.agents.create_vector_store_and_poll(
+ self._project_client.agents.vector_stores.create_and_poll(
file_ids=[],
name=vector_store_name,
data_sources=data_sources,
expires_after=expires_after,
chunking_strategy=chunking_strategy,
metadata=vector_store_metadata,
- sleep_interval=vector_store_polling_sleep_interval,
+ polling_interval=vector_store_polling_interval,
)
)
)
@@ -1024,30 +1053,38 @@ class AzureAIAgent(BaseChatAgent):
self._project_client.agents.update_agent(
agent_id=self._get_agent_id,
tools=self._api_tools,
- tool_resources=models.ToolResources(
- file_search=models.FileSearchToolResource(vector_store_ids=[self._vector_store_id])
+ tool_resources=ToolResources(
+ file_search=FileSearchToolResource(vector_store_ids=[self._vector_store_id])
),
)
)
)
file_ids = await self._upload_files(
- file_paths=file_paths, cancellation_token=cancellation_token, purpose=models.FilePurpose.AGENTS
+ file_paths=file_paths, cancellation_token=cancellation_token, purpose=FilePurpose.AGENTS
)
# Create file batch with the file IDs
await cancellation_token.link_future(
asyncio.ensure_future(
- self._project_client.agents.create_vector_store_file_batch_and_poll(
- vector_store_id=self._vector_store_id, file_ids=file_ids
+ self._project_client.agents.vector_store_file_batches.create_and_poll(
+ vector_store_id=self._vector_store_id,
+ file_ids=file_ids,
+ polling_interval=vector_store_polling_interval,
)
)
)
+ async def close(self) -> None:
+ """
+ Close the Azure AI agent and release any resources.
+ """
+ await self._project_client.close()
+
if __name__ == "__main__":
# Example usage of AzureAIAgent
- # Replace with your actual connection string and credentials
+ # Replace with your actual endpoint and credentials
"""
TODO:
[X] Support for file upload
diff --git a/python/packages/autogen-ext/src/autogen_ext/agents/azure/_types.py b/python/packages/autogen-ext/src/autogen_ext/agents/azure/_types.py
index a58f88df4..e431d4265 100644
--- a/python/packages/autogen-ext/src/autogen_ext/agents/azure/_types.py
+++ b/python/packages/autogen-ext/src/autogen_ext/agents/azure/_types.py
@@ -3,7 +3,14 @@ from typing import Any, Awaitable, Callable, Iterable, List, Literal, Optional,
from autogen_core.tools import Tool
from pydantic import BaseModel, Field
-import azure.ai.projects.models as models
+from azure.ai.agents.models import (
+ AzureAISearchToolDefinition,
+ AzureFunctionToolDefinition,
+ BingGroundingToolDefinition,
+ CodeInterpreterToolDefinition,
+ FileSearchToolDefinition,
+ MessageTextUrlCitationAnnotation,
+)
ListToolType = Iterable[
Union[
@@ -13,14 +20,12 @@ ListToolType = Iterable[
"bing_grounding",
"azure_ai_search",
"azure_function",
- "sharepoint_grounding",
],
- models.BingGroundingToolDefinition,
- models.CodeInterpreterToolDefinition,
- models.SharepointToolDefinition,
- models.AzureAISearchToolDefinition,
- models.FileSearchToolDefinition,
- models.AzureFunctionToolDefinition,
+ BingGroundingToolDefinition,
+ CodeInterpreterToolDefinition,
+ AzureAISearchToolDefinition,
+ FileSearchToolDefinition,
+ AzureFunctionToolDefinition,
Tool,
Callable[..., Any],
Callable[..., Awaitable[Any]],
@@ -52,5 +57,5 @@ class AzureAIAgentState(BaseModel):
uploaded_file_ids: List[str] = Field(default_factory=list)
-def has_annotations(obj: Any) -> TypeGuard[list[models.MessageTextUrlCitationAnnotation]]:
+def has_annotations(obj: Any) -> TypeGuard[list[MessageTextUrlCitationAnnotation]]:
return obj is not None and isinstance(obj, list)
diff --git a/python/packages/autogen-ext/tests/test_azure_ai_agent.py b/python/packages/autogen-ext/tests/test_azure_ai_agent.py
index 41901fd31..a20a49c1d 100644
--- a/python/packages/autogen-ext/tests/test_azure_ai_agent.py
+++ b/python/packages/autogen-ext/tests/test_azure_ai_agent.py
@@ -1,10 +1,9 @@
import json
from asyncio import CancelledError
from types import SimpleNamespace
-from typing import Any, List, Optional, Union
+from typing import Any, AsyncGenerator, List, Optional, Union
from unittest.mock import AsyncMock, MagicMock, call
-import azure.ai.projects.models as models
import pytest
from autogen_agentchat.base._chat_agent import Response
from autogen_agentchat.messages import TextMessage, ToolCallExecutionEvent
@@ -12,8 +11,20 @@ from autogen_core._cancellation_token import CancellationToken
from autogen_core.tools._function_tool import FunctionTool
from autogen_ext.agents.azure._azure_ai_agent import AzureAIAgent
from autogen_ext.agents.azure._types import ListToolType
+from azure.ai.agents.models import (
+ AzureAISearchToolDefinition,
+ AzureFunctionToolDefinition,
+ BingGroundingToolDefinition,
+ CodeInterpreterToolDefinition,
+ FilePurpose,
+ FileSearchToolDefinition,
+ FileState,
+ RequiredAction,
+ RunStatus,
+ SubmitToolOutputsAction,
+ ThreadMessage,
+)
from azure.ai.projects.aio import AIProjectClient
-from azure.ai.projects.models import ThreadMessage
class FakeText:
@@ -152,53 +163,28 @@ FakeMessageType = Union[
]
-class FakeOpenAIPageableListOfThreadMessage:
- def __init__(
- self,
- data: List[FakeMessageType],
- has_more: bool = False,
- ) -> None:
- self.data = data
- self._has_more = has_more
-
- @property
- def has_more(self) -> bool:
- return self._has_more
-
- @property
- def text_messages(self) -> List[ThreadMessage | FakeTextContent]:
- """Returns all text message contents in the messages.
-
- :rtype: List[FakeMessage]
- """
- texts = [content for msg in self.data for content in msg.text_messages] # type: ignore
- return texts # type: ignore
-
- def get_last_message_by_role(
- self, role: models.MessageRole
- ) -> Optional[ThreadMessage | FakeMessage | FakeMessageWithAnnotation | FakeMessageWithUrlCitationAnnotation]:
- """Returns the last message from a sender in the specified role.
-
- :param role: The role of the sender.
- :type role: MessageRole
+async def mock_messages_list(**kwargs: Any) -> AsyncGenerator[FakeMessage, None]:
+ """Mock async generator for messages.list()"""
+ messages = [FakeMessage("msg-mock", "response")]
+ for message in messages:
+ yield message
- :return: The last message from a sender in the specified role.
- :rtype: ~azure.ai.projects.models.ThreadMessage
- """
- for msg in self.data:
- if msg.role == role:
- return msg # type: ignore
- return None
+async def mock_messages_list_empty(**kwargs: Any) -> AsyncGenerator[FakeMessage, None]:
+ """Mock async generator that yields no messages"""
+ # This generator yields nothing, simulating an empty message list
+ return
+ yield # This line is never reached but makes this a generator
-def mock_list(
- data: Optional[List[FakeMessageType]] = None,
- has_more: bool = False,
-) -> FakeOpenAIPageableListOfThreadMessage:
- if data is None or len(data) == 0:
- data = [FakeMessage("msg-mock", "response")]
- return FakeOpenAIPageableListOfThreadMessage(data, has_more=has_more)
+async def mock_messages_list_multiple(**kwargs: Any) -> AsyncGenerator[FakeMessage, None]:
+ """Mock async generator for multiple messages (pagination test)"""
+ messages = [
+ FakeMessage("msg-mock-1", "response-1"),
+ FakeMessage("msg-mock-2", "response-2"),
+ ]
+ for message in messages:
+ yield message
def create_agent(
@@ -226,25 +212,47 @@ def create_agent(
def mock_project_client() -> MagicMock:
client = MagicMock(spec=AIProjectClient)
- agents = MagicMock()
-
- client.agents = agents
-
+ # Create separate operation groups to match the actual SDK structure
+ client.agents = MagicMock()
+ client.runs = MagicMock()
+ client.messages = MagicMock()
+ client.threads = MagicMock()
+ client.files = MagicMock()
+ client.vector_stores = MagicMock()
+ client.vector_store_files = MagicMock()
+ client.vector_store_file_batches = MagicMock()
+
+ # Agent operations
client.agents.create_agent = AsyncMock(return_value=MagicMock(id="assistant-mock"))
client.agents.get_agent = AsyncMock(return_value=MagicMock(id="assistant-mock"))
+ client.agents.update_agent = AsyncMock()
+ client.agents.delete_agent = AsyncMock()
agent_run = MagicMock()
agent_run.id = "run-mock"
- agent_run.status = "completed"
+ agent_run.status = RunStatus.COMPLETED
+
+ client.agents.runs = MagicMock()
+ client.agents.runs.create = AsyncMock(return_value=agent_run)
+ client.agents.runs.get = AsyncMock(return_value=agent_run)
+ client.agents.runs.submit_tool_outputs = AsyncMock(return_value=agent_run)
- client.agents.create_run = AsyncMock(return_value=agent_run)
- client.agents.get_run = AsyncMock(return_value=agent_run)
- client.agents.list_messages = AsyncMock(return_value=mock_list())
+ client.agents.messages = MagicMock()
+ client.agents.messages.list = mock_messages_list
+ client.agents.messages.create = AsyncMock()
- client.agents.create_message = AsyncMock()
+ client.agents.threads = MagicMock()
+ client.agents.threads.get = AsyncMock(return_value=MagicMock(id="thread-mock"))
+ client.agents.threads.create = AsyncMock(return_value=MagicMock(id="thread-mock"))
+ client.agents.threads.update = AsyncMock()
- client.agents.get_thread = AsyncMock(id="thread-mock", return_value=MagicMock(id="thread-mock"))
- client.agents.create_thread = AsyncMock(return_value=MagicMock(id="thread-mock"))
+ client.agents.files = MagicMock()
+ client.agents.files.upload_and_poll = AsyncMock(return_value=MagicMock(id="file-mock", status=FileState.PROCESSED))
+
+ client.agents.vector_stores = MagicMock()
+ client.agents.vector_stores.create_and_poll = AsyncMock(return_value=MagicMock(id="vector_store_id"))
+ client.agents.vector_store_file_batches = MagicMock()
+ client.agents.vector_store_file_batches.create_and_poll = AsyncMock()
return client
@@ -276,7 +284,8 @@ async def test_on_reset(mock_project_client: MagicMock) -> None:
await agent.on_reset(CancellationToken())
- mock_project_client.agents.create_thread.assert_called_once()
+ # The agent might call create_thread multiple times during initialization, so check if it was called at least once
+ assert mock_project_client.agents.threads.create.call_count > 0
@pytest.mark.asyncio
@@ -294,18 +303,18 @@ async def test_save_and_load_state(mock_project_client: MagicMock) -> None:
@pytest.mark.asyncio
async def test_on_upload_for_code_interpreter(mock_project_client: MagicMock) -> None:
- file_mock = AsyncMock()
+ file_mock = MagicMock()
file_mock.id = "file-mock"
- file_mock.status = "processed"
+ file_mock.status = FileState.PROCESSED
- thread_mock = AsyncMock()
- thread_mock.tool_resources = AsyncMock()
- thread_mock.tool_resources.code_interpreter = AsyncMock()
+ thread_mock = MagicMock()
+ thread_mock.tool_resources = MagicMock()
+ thread_mock.tool_resources.code_interpreter = MagicMock()
thread_mock.tool_resources.code_interpreter.file_ids = [] # Set as a valid list
- mock_project_client.agents.upload_file_and_poll = AsyncMock(return_value=file_mock)
- mock_project_client.agents.get_thread = AsyncMock(return_value=thread_mock)
- mock_project_client.agents.update_thread = AsyncMock()
+ mock_project_client.agents.files.upload_and_poll = AsyncMock(return_value=file_mock)
+ mock_project_client.agents.threads.get = AsyncMock(return_value=thread_mock)
+ mock_project_client.agents.threads.update = AsyncMock()
agent = create_agent(
mock_project_client,
@@ -314,59 +323,59 @@ async def test_on_upload_for_code_interpreter(mock_project_client: MagicMock) ->
file_paths = ["test_file_1.txt", "test_file_2.txt"]
await agent.on_upload_for_code_interpreter(file_paths)
- mock_project_client.agents.upload_file_and_poll.assert_called()
- mock_project_client.agents.get_thread.assert_called_once()
- mock_project_client.agents.update_thread.assert_called_once()
+ mock_project_client.agents.files.upload_and_poll.assert_called()
+ mock_project_client.agents.threads.get.assert_called_once()
+ mock_project_client.agents.threads.update.assert_called_once()
@pytest.mark.asyncio
async def test_on_upload_for_file_search(mock_project_client: MagicMock) -> None:
- file_mock = AsyncMock()
+ file_mock = MagicMock()
file_mock.id = "file-mock"
- file_mock.status = "processed" # Set a valid status
+ file_mock.status = FileState.PROCESSED # Set a valid status
- mock_project_client.agents.upload_file_and_poll = AsyncMock(return_value=file_mock)
- mock_project_client.agents.create_vector_store_and_poll = AsyncMock(return_value=AsyncMock(id="vector_store_id"))
+ mock_project_client.agents.files.upload_and_poll = AsyncMock(return_value=file_mock)
+ mock_project_client.agents.vector_stores.create_and_poll = AsyncMock(return_value=MagicMock(id="vector_store_id"))
mock_project_client.agents.update_agent = AsyncMock()
- mock_project_client.agents.create_vector_store_file_batch_and_poll = AsyncMock()
+ mock_project_client.agents.vector_store_file_batches.create_and_poll = AsyncMock()
agent = create_agent(mock_project_client, tools=["file_search"])
file_paths = ["test_file_1.txt", "test_file_2.txt"]
await agent.on_upload_for_file_search(file_paths, cancellation_token=CancellationToken())
- mock_project_client.agents.upload_file_and_poll.assert_called()
- mock_project_client.agents.create_vector_store_and_poll.assert_called_once()
+ mock_project_client.agents.files.upload_and_poll.assert_called()
+ mock_project_client.agents.vector_stores.create_and_poll.assert_called_once()
mock_project_client.agents.update_agent.assert_called_once()
- mock_project_client.agents.create_vector_store_file_batch_and_poll.assert_called_once()
+ mock_project_client.agents.vector_store_file_batches.create_and_poll.assert_called_once()
@pytest.mark.asyncio
async def test_upload_files(mock_project_client: MagicMock) -> None:
- mock_project_client.agents.create_vector_store_file_batch_and_poll = AsyncMock()
+ mock_project_client.agents.vector_store_file_batches.create_and_poll = AsyncMock()
mock_project_client.agents.update_agent = AsyncMock()
- mock_project_client.agents.create_vector_store_and_poll = AsyncMock(return_value=AsyncMock(id="vector_store_id"))
+ mock_project_client.agents.vector_stores.create_and_poll = AsyncMock(return_value=MagicMock(id="vector_store_id"))
- mock_project_client.agents.upload_file_and_poll = AsyncMock(
- return_value=AsyncMock(id="file-id", status=models.FileState.PROCESSED)
+ mock_project_client.agents.files.upload_and_poll = AsyncMock(
+ return_value=MagicMock(id="file-id", status=FileState.PROCESSED)
)
agent = create_agent(mock_project_client, tools=["file_search"])
await agent.on_upload_for_file_search(["test_file.txt"], cancellation_token=CancellationToken())
- mock_project_client.agents.upload_file_and_poll.assert_any_await(
- file_path="test_file.txt", purpose=models.FilePurpose.AGENTS, sleep_interval=0.5
+ mock_project_client.agents.files.upload_and_poll.assert_any_await(
+ file_path="test_file.txt", purpose=FilePurpose.AGENTS, polling_interval=0.5
)
@pytest.mark.asyncio
async def test_on_messages_stream(mock_project_client: MagicMock) -> None:
- mock_project_client.agents.create_run = AsyncMock(
- return_value=MagicMock(id="run-id", status=models.RunStatus.COMPLETED)
+ mock_project_client.agents.runs.create = AsyncMock( # Corrected path
+ return_value=MagicMock(id="run-id", status=RunStatus.COMPLETED)
)
- mock_project_client.agents.list_messages = AsyncMock(return_value=mock_list())
+ mock_project_client.agents.messages.list = mock_messages_list # Corrected path
agent = create_agent(mock_project_client)
@@ -411,7 +420,7 @@ async def test_get_agent_id_validation(mock_project_client: MagicMock) -> None:
("bing_grounding", False),
("azure_function", False),
("azure_ai_search", False),
- ("sharepoint_grounding", False),
+ # ("sharepoint_grounding", False),
("unknown_tool", True),
],
)
@@ -422,7 +431,7 @@ async def test_adding_tools_as_literals(
with pytest.raises(ValueError, match=tool_name):
agent = create_agent(mock_project_client, tools=[tool_name]) # mypy ignore
else:
- agent = agent = create_agent(mock_project_client, tools=[tool_name])
+ agent = create_agent(mock_project_client, tools=[tool_name])
assert agent.tools[0].type == tool_name
@@ -430,16 +439,16 @@ async def test_adding_tools_as_literals(
@pytest.mark.parametrize(
"tool_definition",
[
- models.FileSearchToolDefinition(),
- models.CodeInterpreterToolDefinition(),
- models.BingGroundingToolDefinition(), # type: ignore
- models.AzureFunctionToolDefinition(), # type: ignore
- models.AzureAISearchToolDefinition(),
- models.SharepointToolDefinition(), # type: ignore
+ FileSearchToolDefinition(),
+ CodeInterpreterToolDefinition(),
+ BingGroundingToolDefinition(), # type: ignore
+ AzureFunctionToolDefinition(), # type: ignore
+ AzureAISearchToolDefinition(),
+ # SharepointToolDefinition(), # type: ignore
],
)
async def test_adding_tools_as_typed_definition(mock_project_client: MagicMock, tool_definition: Any) -> None:
- agent = agent = create_agent(mock_project_client, tools=[tool_definition])
+ agent = create_agent(mock_project_client, tools=[tool_definition])
assert len(agent.tools) == 1
assert agent.tools[0].type == tool_definition.type
@@ -519,7 +528,7 @@ async def test_agent_initialization_with_no_thread_id(mock_project_client: Magic
await agent.on_messages([TextMessage(content="Hello", source="user")])
- mock_project_client.agents.create_thread.assert_awaited_once()
+ mock_project_client.agents.threads.create.assert_awaited_once() # Corrected path
@pytest.mark.asyncio
@@ -528,22 +537,14 @@ async def test_agent_initialization_with_thread_id(mock_project_client: MagicMoc
await agent.on_messages([TextMessage(content="Hello", source="user")])
- mock_project_client.agents.get_thread.assert_awaited_once()
+ mock_project_client.agents.threads.get.assert_awaited_once() # Corrected path
@pytest.mark.asyncio
async def test_agent_initialization_fetching_multiple_pages_of_thread_messages(mock_project_client: MagicMock) -> None:
- list_messages = [
- FakeOpenAIPageableListOfThreadMessage([FakeMessage("msg-mock-1", "response-1")], has_more=True),
- FakeOpenAIPageableListOfThreadMessage([FakeMessage("msg-mock-2", "response-2")]),
- FakeOpenAIPageableListOfThreadMessage(
- [FakeMessage("msg-mock-1", "response-1"), FakeMessage("msg-mock-2", "response-2")]
- ),
- ]
-
- mock_project_client.agents.get_thread = AsyncMock(id="thread-id", return_value=MagicMock(id="thread-id"))
- # Mock the list_messages method to return multiple pages of messages
- mock_project_client.agents.list_messages = AsyncMock(side_effect=list_messages)
+ mock_project_client.agents.threads.get = AsyncMock(return_value=MagicMock(id="thread-id")) # Corrected path
+ # Mock the list_messages method to return multiple messages
+ mock_project_client.agents.messages.list = mock_messages_list_multiple # Corrected path
agent = create_agent(mock_project_client, thread_id="thread-id")
@@ -578,7 +579,7 @@ async def test_on_messages_with_cancellation(mock_project_client: MagicMock) ->
await agent.on_messages(messages, token)
-def mock_run(action: str, run_id: str, required_action: Optional[models.RequiredAction] = None) -> MagicMock:
+def mock_run(action: str, run_id: str, required_action: Optional[RequiredAction] = None) -> MagicMock:
run = MagicMock()
run.id = run_id
run.status = action
@@ -609,10 +610,21 @@ async def test_on_messages_return_required_action_with_no_tool_raise_error(
) -> None:
agent = create_agent(mock_project_client, tools=registered_tools)
- complete_run = mock_run("completed", "run-mock")
- mock_project_client.agents.submit_tool_outputs_to_run = AsyncMock(return_value=complete_run)
-
- required_action = models.SubmitToolOutputsAction() # type: ignore
+ complete_run = mock_run(RunStatus.COMPLETED, "run-mock")
+ mock_project_client.agents.runs.submit_tool_outputs = AsyncMock(return_value=complete_run) # Corrected path
+
+ required_action = SubmitToolOutputsAction(
+ submit_tool_outputs=SimpleNamespace( # type: ignore
+ tool_calls=[
+ SimpleNamespace(
+ type="function",
+ id="tool-mock",
+ name=tool_name,
+ function=SimpleNamespace(arguments={}, name="function"),
+ )
+ ]
+ )
+ )
required_action.submit_tool_outputs = SimpleNamespace( # type: ignore
tool_calls=[
@@ -622,8 +634,8 @@ async def test_on_messages_return_required_action_with_no_tool_raise_error(
]
) # mypy ignore
- requires_action_run = mock_run("requires_action", "run-mock", required_action)
- mock_project_client.agents.get_run = AsyncMock(side_effect=[requires_action_run, complete_run])
+ requires_action_run = mock_run(RunStatus.REQUIRES_ACTION, "run-mock", required_action)
+ mock_project_client.agents.runs.get = AsyncMock(side_effect=[requires_action_run, complete_run]) # Corrected path
messages = [TextMessage(content="Hello", source="user")]
@@ -654,34 +666,34 @@ async def test_on_message_raise_error_when_stream_return_nothing(mock_project_cl
@pytest.mark.parametrize(
"file_paths, file_status, should_raise_error",
[
- (["file1.txt", "file2.txt"], models.FileState.PROCESSED, False),
- (["file3.txt"], models.FileState.ERROR, True),
+ (["file1.txt", "file2.txt"], FileState.PROCESSED, False),
+ (["file3.txt"], FileState.ERROR, True),
],
)
async def test_uploading_multiple_files(
- mock_project_client: MagicMock, file_paths: list[str], file_status: models.FileState, should_raise_error: bool
+ mock_project_client: MagicMock, file_paths: list[str], file_status: FileState, should_raise_error: bool
) -> None:
agent = create_agent(mock_project_client)
- file_mock = AsyncMock(id="file-id", status=file_status)
- mock_project_client.agents.update_thread = AsyncMock()
- mock_project_client.agents.upload_file_and_poll = AsyncMock(return_value=file_mock)
+ file_mock = MagicMock(id="file-id", status=file_status)
+ mock_project_client.agents.threads.update = AsyncMock()
+ mock_project_client.agents.files.upload_and_poll = AsyncMock(return_value=file_mock)
async def upload_files() -> None:
await agent.on_upload_for_code_interpreter(
file_paths,
cancellation_token=CancellationToken(),
- sleep_interval=0.1,
+ polling_interval=0.1,
)
if should_raise_error:
- with pytest.raises(Exception, match="upload failed with status"):
+ with pytest.raises(ValueError, match="upload failed with status"): # Changed from Exception to ValueError
await upload_files()
else:
await upload_files()
- mock_project_client.agents.upload_file_and_poll.assert_has_calls(
- [call(file_path=file_path, purpose=models.FilePurpose.AGENTS, sleep_interval=0.1) for file_path in file_paths]
+ mock_project_client.agents.files.upload_and_poll.assert_has_calls(
+ [call(file_path=file_path, purpose=FilePurpose.AGENTS, polling_interval=0.1) for file_path in file_paths]
)
@@ -715,13 +727,17 @@ async def test_on_message_stream_mapping_url_citation(
url: str,
title: str,
) -> None:
- mock_project_client.agents.create_run = AsyncMock(
- return_value=MagicMock(id="run-id", status=models.RunStatus.COMPLETED)
+ mock_project_client.agents.runs.create = AsyncMock( # Corrected path and method name
+ return_value=MagicMock(id="run-id", status=RunStatus.COMPLETED)
)
- list = mock_list([fake_message], has_more=False)
+ async def mock_messages_list_with_citation(
+ **kwargs: Any,
+ ) -> AsyncGenerator[FakeMessageWithAnnotation | FakeMessageWithUrlCitationAnnotation, None]:
+ """Mock async generator for messages with citation"""
+ yield fake_message
- mock_project_client.agents.list_messages = AsyncMock(return_value=list)
+ mock_project_client.agents.messages.list = mock_messages_list_with_citation
agent = create_agent(mock_project_client)
@@ -743,9 +759,7 @@ async def test_on_message_stream_mapping_url_citation(
@pytest.mark.asyncio
async def test_on_message_stream_mapping_file_citation(mock_project_client: MagicMock) -> None:
- mock_project_client.agents.create_run = AsyncMock(
- return_value=MagicMock(id="run-id", status=models.RunStatus.COMPLETED)
- )
+ mock_project_client.agents.create_run = AsyncMock(return_value=MagicMock(id="run-id", status=RunStatus.COMPLETED))
expected_file_id = "file_id_1"
expected_quote = "this part of a file"
@@ -756,9 +770,13 @@ async def test_on_message_stream_mapping_file_citation(mock_project_client: Magi
[FakeTextFileCitationAnnotation(FakeTextFileCitationDetails(expected_file_id, expected_quote))],
)
- list = mock_list([fake_message], has_more=False)
+ async def mock_messages_list_with_file_citation(
+ **kwargs: Any,
+ ) -> AsyncGenerator[FakeMessageWithFileCitationAnnotation, None]:
+ """Mock async generator for messages with file citation"""
+ yield fake_message
- mock_project_client.agents.list_messages = AsyncMock(return_value=list)
+ mock_project_client.agents.messages.list = mock_messages_list_with_file_citation
agent = create_agent(mock_project_client)
diff --git a/python/uv.lock b/python/uv.lock
index 6fc4faa3f..b6368ee6e 100644
--- a/python/uv.lock
+++ b/python/uv.lock
@@ -746,8 +746,8 @@ requires-dist = [
{ name = "autogen-agentchat", marker = "extra == 'video-surfer'", editable = "packages/autogen-agentchat" },
{ name = "autogen-agentchat", marker = "extra == 'web-surfer'", editable = "packages/autogen-agentchat" },
{ name = "autogen-core", editable = "packages/autogen-core" },
- { name = "azure-ai-inference", marker = "extra == 'azure'", specifier = ">=1.0.0b7" },
- { name = "azure-ai-projects", marker = "extra == 'azure'", specifier = ">=1.0.0b8" },
+ { name = "azure-ai-inference", marker = "extra == 'azure'", specifier = ">=1.0.0b9" },
+ { name = "azure-ai-projects", marker = "extra == 'azure'", specifier = ">=1.0.0b11" },
{ name = "azure-core", marker = "extra == 'azure'" },
{ name = "azure-identity", marker = "extra == 'azure'" },
{ name = "azure-search-documents", marker = "extra == 'azure'", specifier = ">=11.4.0" },
@@ -909,6 +909,20 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/9e/43/53afb8ba17218f19b77c7834128566c5bbb100a0ad9ba2e8e89d089d7079/autopep8-2.3.2-py2.py3-none-any.whl", hash = "sha256:ce8ad498672c845a0c3de2629c15b635ec2b05ef8177a6e7c91c74f3e9b51128", size = 45807 },
]
+[[package]]
+name = "azure-ai-agents"
+version = "1.0.0"
+source = { registry = "https://pypi.org/simple" }
+dependencies = [
+ { name = "azure-core" },
+ { name = "isodate" },
+ { name = "typing-extensions" },
+]
+sdist = { url = "https://files.pythonhosted.org/packages/fe/65/3718f646fea42cac9a6120621d5ed067d6c00476d47bf329cf80331e965e/azure_ai_agents-1.0.0.tar.gz", hash = "sha256:ccedf84bfafa59db79bb7adcfb0eb2970edc6dff58490d946bb3c0432b19b75f", size = 298143 }
+wheels = [
+ { url = "https://files.pythonhosted.org/packages/5d/6e/94aed845af9f2e6b1498cd5b43635ba6510c9260229e0700448bb24edfb0/azure_ai_agents-1.0.0-py3-none-any.whl", hash = "sha256:89a522b966380018c0f6a46ada62555a65b70d980ce9a22dee00d84efdf91f32", size = 187045 },
+]
+
[[package]]
name = "azure-ai-documentintelligence"
version = "1.0.0"
@@ -925,30 +939,32 @@ wheels = [
[[package]]
name = "azure-ai-inference"
-version = "1.0.0b7"
+version = "1.0.0b9"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "azure-core" },
{ name = "isodate" },
{ name = "typing-extensions" },
]
-sdist = { url = "https://files.pythonhosted.org/packages/af/37/233eee0bebbf631d2f911a9f1ebbc3784b100d9bfb84efc275e71c1ea636/azure_ai_inference-1.0.0b7.tar.gz", hash = "sha256:bd912f71f7f855036ca46c9a21439f290eed5e61da418fd26bbb32e3c68bcce3", size = 175883 }
+sdist = { url = "https://files.pythonhosted.org/packages/4e/6a/ed85592e5c64e08c291992f58b1a94dab6869f28fb0f40fd753dced73ba6/azure_ai_inference-1.0.0b9.tar.gz", hash = "sha256:1feb496bd84b01ee2691befc04358fa25d7c344d8288e99364438859ad7cd5a4", size = 182408 }
wheels = [
- { url = "https://files.pythonhosted.org/packages/cd/b6/5ba830eddc59f820c654694d476c14a3dd9c1f828ff9b48eb8b21dfd5f01/azure_ai_inference-1.0.0b7-py3-none-any.whl", hash = "sha256:59bb6a9ee62bd7654a69ca2bf12fe9335d7045df95b491cb8b5f9e3791c86175", size = 123030 },
+ { url = "https://files.pythonhosted.org/packages/4f/0f/27520da74769db6e58327d96c98e7b9a07ce686dff582c9a5ec60b03f9dd/azure_ai_inference-1.0.0b9-py3-none-any.whl", hash = "sha256:49823732e674092dad83bb8b0d1b65aa73111fab924d61349eb2a8cdc0493990", size = 124885 },
]
[[package]]
name = "azure-ai-projects"
-version = "1.0.0b8"
+version = "1.0.0b11"
source = { registry = "https://pypi.org/simple" }
dependencies = [
+ { name = "azure-ai-agents" },
{ name = "azure-core" },
+ { name = "azure-storage-blob" },
{ name = "isodate" },
{ name = "typing-extensions" },
]
-sdist = { url = "https://files.pythonhosted.org/packages/83/85/66da3fe4affd182aa7788f212a1936a56a700bcbcafe157147a416f38e11/azure_ai_projects-1.0.0b8.tar.gz", hash = "sha256:9e48bf15710cdf40dd3fa1ac0448266bf599dc10c6477aa69ac5c250daef236e", size = 310017 }
+sdist = { url = "https://files.pythonhosted.org/packages/01/4b/0a879eb66b5d9a08ab09292ff9a36a4e9c855b458d0e843b5e838fc6f6fd/azure_ai_projects-1.0.0b11.tar.gz", hash = "sha256:68a115c48cde7d5f9c29aee61c7fbf0b6de69aecbd1dc749b847a1e1348216b5", size = 133087 }
wheels = [
- { url = "https://files.pythonhosted.org/packages/9c/c3/9071dcf8e383cf193253baccbbcdd5eeab181a388628ce51f98a7fb4c104/azure_ai_projects-1.0.0b8-py3-none-any.whl", hash = "sha256:9636aeaa573449d4cff6a38c44cfc16ad08a4f7a598e953654eece5a9f209ec2", size = 190835 },
+ { url = "https://files.pythonhosted.org/packages/3e/2d/5502377ecc07677365a1e86be64d8cb9959eb6e9b605fcc28f1f68d3777a/azure_ai_projects-1.0.0b11-py3-none-any.whl", hash = "sha256:3572f2989627e896ecfebe2fa7326d5b940f920cc581e98809b244af7a38cbf0", size = 130983 },
]
[[package]]