A programming framework for agentic AI
feat: update OpenAIAssistantAgent to support AsyncAzureOpenAI client (#5312)
Eric Zhu756e2a4865b8
diff --git a/python/packages/autogen-ext/src/autogen_ext/agents/openai/_openai_assistant_agent.py b/python/packages/autogen-ext/src/autogen_ext/agents/openai/_openai_assistant_agent.pyindex d738a5fae..0419d2954 100644--- a/python/packages/autogen-ext/src/autogen_ext/agents/openai/_openai_assistant_agent.py+++ b/python/packages/autogen-ext/src/autogen_ext/agents/openai/_openai_assistant_agent.py@@ -37,7 +37,7 @@ from autogen_core.models._model_client import ChatCompletionClientfrom autogen_core.models._types import FunctionExecutionResultfrom autogen_core.tools import FunctionTool, Tool-from openai import NOT_GIVEN, AsyncClient, NotGiven+from openai import NOT_GIVEN, AsyncAzureOpenAI, AsyncOpenAI, NotGivenfrom openai.pagination import AsyncCursorPagefrom openai.resources.beta.threads import AsyncMessages, AsyncRuns, AsyncThreadsfrom openai.types import FileObject@@ -78,16 +78,17 @@ def _convert_tool_to_function_param(tool: Tool) -> "FunctionToolParam":class OpenAIAssistantAgent(BaseChatAgent):- """An agent implementation that uses the OpenAI Assistant API to generate responses.+ """An agent implementation that uses the Assistant API to generate responses.Installation:.. code-block:: bashpip install "autogen-ext[openai]"+ # pip install "autogen-ext[openai,azure]" # For Azure OpenAI Assistant- This agent leverages the OpenAI Assistant API to create AI assistants with capabilities like:+ This agent leverages the Assistant API to create AI assistants with capabilities like:* Code interpretation and execution* File handling and search@@ -111,10 +112,13 @@ class OpenAIAssistantAgent(BaseChatAgent):You can use an existing thread or assistant by providing the `thread_id` or `assistant_id` parameters.- Example:+ Examples:++ Use the assistant to analyze data in a CSV file:+.. code-block:: python- from openai import AsyncClient+ from openai import AsyncOpenAIfrom autogen_core import CancellationTokenimport asynciofrom autogen_ext.agents.openai import OpenAIAssistantAgent@@ -125,7 +129,7 @@ class OpenAIAssistantAgent(BaseChatAgent):cancellation_token = CancellationToken()# Create an OpenAI client- client = AsyncClient(api_key="your-api-key", base_url="your-base-url")+ client = AsyncOpenAI(api_key="your-api-key", base_url="your-base-url")# Create an assistant with code interpreterassistant = OpenAIAssistantAgent(@@ -154,10 +158,55 @@ class OpenAIAssistantAgent(BaseChatAgent):asyncio.run(example())+ Use Azure OpenAI Assistant with AAD authentication:++ .. code-block:: python++ from openai import AsyncAzureOpenAI+ import asyncio+ from azure.identity import DefaultAzureCredential, get_bearer_token_provider+ from autogen_core import CancellationToken+ from autogen_ext.agents.openai import OpenAIAssistantAgent+ from autogen_agentchat.messages import TextMessage+++ async def example():+ cancellation_token = CancellationToken()++ # Create an Azure OpenAI client+ token_provider = get_bearer_token_provider(DefaultAzureCredential())+ client = AsyncAzureOpenAI(+ azure_deployment="YOUR_AZURE_DEPLOYMENT",+ api_version="YOUR_API_VERSION",+ azure_endpoint="YOUR_AZURE_ENDPOINT",+ azure_ad_token_provider=token_provider,+ )++ # Create an assistant with code interpreter+ assistant = OpenAIAssistantAgent(+ name="Python Helper",+ description="Helps with Python programming",+ client=client,+ model="gpt-4o",+ instructions="You are a helpful Python programming assistant.",+ tools=["code_interpreter"],+ )++ # Get response from the assistant+ response = await assistant.on_messages([TextMessage(source="user", content="Hello.")], cancellation_token)++ print(response)++ # Clean up resources+ await assistant.delete_assistant(cancellation_token)+++ asyncio.run(example())+Args:name (str): Name of the assistantdescription (str): Description of the assistant's purpose- client (AsyncClient): OpenAI API client instance+ client (AsyncOpenAI | AsyncAzureOpenAI): OpenAI client or Azure OpenAI client instancemodel (str): Model to use (e.g. "gpt-4")instructions (str): System instructions for the assistanttools (Optional[Iterable[Union[Literal["code_interpreter", "file_search"], Tool | Callable[..., Any] | Callable[..., Awaitable[Any]]]]]): Tools the assistant can use@@ -174,7 +223,7 @@ class OpenAIAssistantAgent(BaseChatAgent):self,name: str,description: str,- client: "AsyncClient",+ client: AsyncOpenAI | AsyncAzureOpenAI,model: str,instructions: str,tools: Optional[