mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: [BREAKING] Remove deprecated Python OpenAI/Azure AI surfaces (#4990)
* [BREAKING] Remove deprecated Python OpenAI/Azure AI surfaces Also clean up follow-on docs, environment guidance, package metadata, and lab test stability. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix deleted semantic-kernel sample links Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Address PR review feedback Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * improve foundry language * Fix A2A Foundry sample regression Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
co-authored by
Copilot
parent
a5eacbbe65
commit
3a49b1d6dd
@@ -1,32 +1,30 @@
|
||||
# Azure AI Package (agent-framework-azure-ai)
|
||||
|
||||
Integration with Azure AI Foundry for persistent agents and project-based agent management.
|
||||
Integration with Azure AI inference embeddings plus shared Azure authentication helpers.
|
||||
|
||||
## Main Classes
|
||||
|
||||
- **`AzureAIAgentClient`** - Chat client for Azure AI Agents (persistent agents with threads)
|
||||
- **`AzureAIClient`** - Client for Azure AI Foundry project-based agents
|
||||
- **`AzureAIAgentsProvider`** - Provider for listing/managing Azure AI agents
|
||||
- **`AzureAIProjectAgentProvider`** - Provider for project-scoped agent management
|
||||
- **`AzureAISettings`** - Pydantic settings for Azure AI configuration
|
||||
- **`AzureAIAgentOptions`** / **`AzureAIProjectAgentOptions`** - Options TypedDicts
|
||||
- **`AzureAIInferenceEmbeddingClient`** - Full-featured Azure AI inference embeddings client
|
||||
- **`RawAzureAIInferenceEmbeddingClient`** - Raw embeddings client without middleware layers
|
||||
- **`AzureAIInferenceEmbeddingOptions`** / **`AzureAIInferenceEmbeddingSettings`** - Embedding options and settings
|
||||
- **`AzureAISettings`** - Shared Azure AI project settings TypedDict
|
||||
- **`AzureCredentialTypes`** / **`AzureTokenProvider`** - Shared Azure authentication helpers
|
||||
|
||||
## Usage
|
||||
|
||||
```python
|
||||
from agent_framework.azure import AzureAIAgentClient
|
||||
from agent_framework_azure_ai import AzureAIInferenceEmbeddingClient
|
||||
|
||||
client = AzureAIAgentClient(
|
||||
endpoint="https://your-project.services.ai.azure.com",
|
||||
agent_id="your-agent-id",
|
||||
client = AzureAIInferenceEmbeddingClient(
|
||||
endpoint="https://<resource>.inference.ai.azure.com",
|
||||
api_key="...",
|
||||
model_id="text-embedding-3-large",
|
||||
)
|
||||
response = await client.get_response("Hello")
|
||||
result = await client.get_embeddings(["Hello"])
|
||||
```
|
||||
|
||||
## Import Path
|
||||
|
||||
```python
|
||||
from agent_framework.azure import AzureAIAgentClient, AzureAIClient
|
||||
# or directly:
|
||||
from agent_framework_azure_ai import AzureAIAgentClient
|
||||
from agent_framework_azure_ai import AzureAIInferenceEmbeddingClient
|
||||
```
|
||||
|
||||
@@ -2,21 +2,6 @@
|
||||
|
||||
import importlib.metadata
|
||||
|
||||
from ._agent_provider import AzureAIAgentsProvider # pyright: ignore[reportDeprecated]
|
||||
from ._chat_client import AzureAIAgentClient, AzureAIAgentOptions # pyright: ignore[reportDeprecated]
|
||||
from ._client import AzureAIClient, AzureAIProjectAgentOptions, RawAzureAIClient # pyright: ignore[reportDeprecated]
|
||||
from ._deprecated_azure_openai import (
|
||||
AzureOpenAIAssistantsClient, # pyright: ignore[reportDeprecated]
|
||||
AzureOpenAIAssistantsOptions,
|
||||
AzureOpenAIChatClient, # pyright: ignore[reportDeprecated]
|
||||
AzureOpenAIChatOptions,
|
||||
AzureOpenAIConfigMixin,
|
||||
AzureOpenAIEmbeddingClient, # pyright: ignore[reportDeprecated]
|
||||
AzureOpenAIResponsesClient, # pyright: ignore[reportDeprecated]
|
||||
AzureOpenAIResponsesOptions,
|
||||
AzureOpenAISettings,
|
||||
AzureUserSecurityContext,
|
||||
)
|
||||
from ._embedding_client import (
|
||||
AzureAIInferenceEmbeddingClient,
|
||||
AzureAIInferenceEmbeddingOptions,
|
||||
@@ -24,7 +9,6 @@ from ._embedding_client import (
|
||||
RawAzureAIInferenceEmbeddingClient,
|
||||
)
|
||||
from ._entra_id_authentication import AzureCredentialTypes, AzureTokenProvider
|
||||
from ._project_provider import AzureAIProjectAgentProvider # pyright: ignore[reportDeprecated]
|
||||
from ._shared import AzureAISettings
|
||||
|
||||
try:
|
||||
@@ -33,29 +17,12 @@ except importlib.metadata.PackageNotFoundError:
|
||||
__version__ = "0.0.0"
|
||||
|
||||
__all__ = [
|
||||
"AzureAIAgentClient",
|
||||
"AzureAIAgentOptions",
|
||||
"AzureAIAgentsProvider",
|
||||
"AzureAIClient",
|
||||
"AzureAIInferenceEmbeddingClient",
|
||||
"AzureAIInferenceEmbeddingOptions",
|
||||
"AzureAIInferenceEmbeddingSettings",
|
||||
"AzureAIProjectAgentOptions",
|
||||
"AzureAIProjectAgentProvider",
|
||||
"AzureAISettings",
|
||||
"AzureCredentialTypes",
|
||||
"AzureOpenAIAssistantsClient",
|
||||
"AzureOpenAIAssistantsOptions",
|
||||
"AzureOpenAIChatClient",
|
||||
"AzureOpenAIChatOptions",
|
||||
"AzureOpenAIConfigMixin",
|
||||
"AzureOpenAIEmbeddingClient",
|
||||
"AzureOpenAIResponsesClient",
|
||||
"AzureOpenAIResponsesOptions",
|
||||
"AzureOpenAISettings",
|
||||
"AzureTokenProvider",
|
||||
"AzureUserSecurityContext",
|
||||
"RawAzureAIClient",
|
||||
"RawAzureAIInferenceEmbeddingClient",
|
||||
"__version__",
|
||||
]
|
||||
|
||||
@@ -1,558 +0,0 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import warnings
|
||||
from collections.abc import Callable, Sequence
|
||||
from typing import Any, Generic, cast
|
||||
|
||||
from agent_framework import (
|
||||
AGENT_FRAMEWORK_USER_AGENT,
|
||||
Agent,
|
||||
BaseContextProvider,
|
||||
FunctionTool,
|
||||
MiddlewareTypes,
|
||||
normalize_tools,
|
||||
)
|
||||
from agent_framework._mcp import MCPTool
|
||||
from agent_framework._settings import load_settings
|
||||
from agent_framework._tools import ToolTypes
|
||||
from azure.ai.agents.aio import AgentsClient
|
||||
from azure.ai.agents.models import Agent as AzureAgent
|
||||
from azure.ai.agents.models import ResponseFormatJsonSchema, ResponseFormatJsonSchemaType
|
||||
from pydantic import BaseModel
|
||||
|
||||
from ._chat_client import AzureAIAgentClient, AzureAIAgentOptions # pyright: ignore[reportDeprecated]
|
||||
from ._entra_id_authentication import AzureCredentialTypes
|
||||
from ._shared import AzureAISettings, to_azure_ai_agent_tools
|
||||
|
||||
if sys.version_info >= (3, 13):
|
||||
from typing import Self, TypeVar # type: ignore # pragma: no cover
|
||||
else:
|
||||
from typing_extensions import Self, TypeVar # type: ignore # pragma: no cover
|
||||
if sys.version_info >= (3, 13):
|
||||
from warnings import deprecated # type: ignore # pragma: no cover
|
||||
else:
|
||||
from typing_extensions import deprecated # type: ignore # pragma: no cover
|
||||
if sys.version_info >= (3, 11):
|
||||
from typing import TypedDict # type: ignore # pragma: no cover
|
||||
else:
|
||||
from typing_extensions import TypedDict # type: ignore # pragma: no cover
|
||||
|
||||
|
||||
# Type variable for options - allows typed Agent[TOptions] returns
|
||||
# Default matches AzureAIAgentClient's default options type
|
||||
OptionsCoT = TypeVar(
|
||||
"OptionsCoT",
|
||||
bound=TypedDict, # type: ignore[valid-type]
|
||||
default="AzureAIAgentOptions",
|
||||
covariant=True,
|
||||
)
|
||||
|
||||
|
||||
@deprecated(
|
||||
"AzureAIAgentClient and the AzureAIAgentsProvider are deprecated. "
|
||||
"They target the V1 Agents Service API and have no direct replacement; "
|
||||
"for new Foundry projects, use FoundryAgent."
|
||||
)
|
||||
class AzureAIAgentsProvider(Generic[OptionsCoT]):
|
||||
"""Provider for Azure AI Agent Service V1 (Persistent Agents API).
|
||||
|
||||
.. deprecated::
|
||||
AzureAIAgentsProvider is deprecated and will be removed in a future release.
|
||||
Use :class:`AzureAIProjectAgentProvider` instead for the V2 (Projects/Responses) API.
|
||||
|
||||
This provider enables creating, retrieving, and wrapping Azure AI agents as Agent
|
||||
instances. It manages the underlying AgentsClient lifecycle and provides a high-level
|
||||
interface for agent operations.
|
||||
|
||||
The provider can be initialized with either:
|
||||
- An existing AgentsClient instance
|
||||
- Azure credentials and endpoint for automatic client creation
|
||||
|
||||
Examples:
|
||||
Using credentials (auto-creates client):
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from agent_framework.azure import AzureAIAgentsProvider
|
||||
from azure.identity.aio import AzureCliCredential
|
||||
|
||||
async with (
|
||||
AzureCliCredential() as credential,
|
||||
AzureAIAgentsProvider(credential=credential) as provider,
|
||||
):
|
||||
agent = await provider.create_agent(
|
||||
name="MyAgent",
|
||||
instructions="You are a helpful assistant.",
|
||||
)
|
||||
result = await agent.run("Hello!")
|
||||
|
||||
Using existing AgentsClient:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from agent_framework.azure import AzureAIAgentsProvider
|
||||
from azure.ai.agents.aio import AgentsClient
|
||||
|
||||
async with AgentsClient(endpoint=endpoint, credential=credential) as client:
|
||||
provider = AzureAIAgentsProvider(agents_client=client)
|
||||
agent = await provider.create_agent(name="MyAgent", instructions="...")
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
agents_client: AgentsClient | None = None,
|
||||
*,
|
||||
project_endpoint: str | None = None,
|
||||
credential: AzureCredentialTypes | None = None,
|
||||
env_file_path: str | None = None,
|
||||
env_file_encoding: str | None = None,
|
||||
) -> None:
|
||||
"""Initialize the Azure AI Agents Provider.
|
||||
|
||||
Args:
|
||||
agents_client: An existing AgentsClient to use. If provided, the provider
|
||||
will not manage its lifecycle.
|
||||
|
||||
Keyword Args:
|
||||
project_endpoint: The Azure AI Project endpoint URL.
|
||||
Can also be set via AZURE_AI_PROJECT_ENDPOINT environment variable.
|
||||
credential: Azure credential for authentication. Accepts a TokenCredential,
|
||||
AsyncTokenCredential, or a callable token provider.
|
||||
Required if agents_client is not provided.
|
||||
env_file_path: Path to .env file for loading settings.
|
||||
env_file_encoding: Encoding of the .env file.
|
||||
|
||||
Raises:
|
||||
ValueError: If required parameters are missing or invalid.
|
||||
"""
|
||||
warnings.warn(
|
||||
"AzureAIAgentsProvider is deprecated and will be removed in a future release; "
|
||||
"use AzureAIProjectAgentProvider instead for the V2 (Projects/Responses) API.",
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
self._settings = load_settings(
|
||||
AzureAISettings,
|
||||
env_prefix="AZURE_AI_",
|
||||
project_endpoint=project_endpoint,
|
||||
env_file_path=env_file_path,
|
||||
env_file_encoding=env_file_encoding,
|
||||
)
|
||||
|
||||
self._should_close_client = False
|
||||
|
||||
if agents_client is not None:
|
||||
self._agents_client = agents_client
|
||||
else:
|
||||
resolved_endpoint = self._settings.get("project_endpoint")
|
||||
if not resolved_endpoint:
|
||||
raise ValueError(
|
||||
"Azure AI project endpoint is required. Provide 'project_endpoint' parameter "
|
||||
"or set 'AZURE_AI_PROJECT_ENDPOINT' environment variable."
|
||||
)
|
||||
if not credential:
|
||||
raise ValueError("Azure credential is required when agents_client is not provided.")
|
||||
self._agents_client = AgentsClient(
|
||||
endpoint=resolved_endpoint,
|
||||
credential=credential, # type: ignore[arg-type]
|
||||
user_agent=AGENT_FRAMEWORK_USER_AGENT,
|
||||
)
|
||||
self._should_close_client = True
|
||||
|
||||
async def __aenter__(self) -> Self:
|
||||
"""Async context manager entry."""
|
||||
return self
|
||||
|
||||
async def __aexit__(
|
||||
self,
|
||||
exc_type: type[BaseException] | None,
|
||||
exc_val: BaseException | None,
|
||||
exc_tb: Any,
|
||||
) -> None:
|
||||
"""Async context manager exit."""
|
||||
await self.close()
|
||||
|
||||
async def close(self) -> None:
|
||||
"""Close the provider and release resources.
|
||||
|
||||
Only closes the AgentsClient if it was created by this provider.
|
||||
"""
|
||||
if self._should_close_client:
|
||||
await self._agents_client.close()
|
||||
|
||||
async def create_agent(
|
||||
self,
|
||||
name: str,
|
||||
*,
|
||||
model: str | None = None,
|
||||
instructions: str | None = None,
|
||||
description: str | None = None,
|
||||
tools: ToolTypes | Callable[..., Any] | Sequence[ToolTypes | Callable[..., Any]] | None = None,
|
||||
default_options: OptionsCoT | None = None,
|
||||
middleware: Sequence[MiddlewareTypes] | None = None,
|
||||
context_providers: Sequence[BaseContextProvider] | None = None,
|
||||
) -> Agent[OptionsCoT]:
|
||||
"""Create a new agent on the Azure AI service and return a Agent.
|
||||
|
||||
.. deprecated::
|
||||
This method is deprecated and will be removed in a future release.
|
||||
Use :meth:`AzureAIProjectAgentProvider.create_agent` instead.
|
||||
|
||||
This method creates a persistent agent on the Azure AI service with the specified
|
||||
configuration and returns a local Agent instance for interaction.
|
||||
|
||||
Args:
|
||||
name: The name for the agent.
|
||||
|
||||
Keyword Args:
|
||||
model: The model deployment name to use. Falls back to
|
||||
AZURE_AI_MODEL_DEPLOYMENT_NAME environment variable if not provided.
|
||||
instructions: Instructions for the agent's behavior.
|
||||
description: A description of the agent's purpose.
|
||||
tools: Tools to make available to the agent.
|
||||
default_options: A TypedDict containing default chat options for the agent.
|
||||
These options are applied to every run unless overridden.
|
||||
middleware: List of middleware to intercept agent and function invocations.
|
||||
context_providers: Context providers to include during agent invocation.
|
||||
|
||||
Returns:
|
||||
Agent: A Agent instance configured with the created agent.
|
||||
|
||||
Raises:
|
||||
ValueError: If model deployment name is not available.
|
||||
|
||||
Examples:
|
||||
.. code-block:: python
|
||||
|
||||
agent = await provider.create_agent(
|
||||
name="WeatherAgent",
|
||||
instructions="You are a helpful weather assistant.",
|
||||
tools=get_weather,
|
||||
)
|
||||
"""
|
||||
warnings.warn(
|
||||
"AzureAIAgentsProvider.create_agent() is deprecated and will be removed in a future release; "
|
||||
"use AzureAIProjectAgentProvider.create_agent() instead.",
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
resolved_model = model or self._settings.get("model_deployment_name")
|
||||
if not resolved_model:
|
||||
raise ValueError(
|
||||
"Model deployment name is required. Provide 'model' parameter "
|
||||
"or set 'AZURE_AI_MODEL_DEPLOYMENT_NAME' environment variable."
|
||||
)
|
||||
|
||||
# Extract response_format from default_options if present
|
||||
opts = dict(default_options) if default_options else {}
|
||||
response_format = opts.get("response_format")
|
||||
|
||||
args: dict[str, Any] = {
|
||||
"model": resolved_model,
|
||||
"name": name,
|
||||
}
|
||||
|
||||
if description:
|
||||
args["description"] = description
|
||||
if instructions:
|
||||
args["instructions"] = instructions
|
||||
|
||||
# Handle response format
|
||||
if response_format and isinstance(response_format, type) and issubclass(response_format, BaseModel):
|
||||
args["response_format"] = self._create_response_format_config(response_format)
|
||||
|
||||
# Normalize and convert tools
|
||||
# Local MCP tools (MCPTool) are handled by Agent at runtime, not stored on the Azure agent
|
||||
normalized_tools = normalize_tools(tools)
|
||||
if normalized_tools:
|
||||
# Collect all non-MCP tools for Azure AI agent creation.
|
||||
# to_azure_ai_agent_tools handles FunctionTool, SDK Tool types (FileSearchTool, etc.), and dicts.
|
||||
non_mcp_tools: list[Any] = [t for t in normalized_tools if not isinstance(t, MCPTool)]
|
||||
if non_mcp_tools:
|
||||
# Pass run_options to capture tool_resources (e.g., for file search vector stores)
|
||||
run_options: dict[str, Any] = {}
|
||||
args["tools"] = to_azure_ai_agent_tools(non_mcp_tools, run_options)
|
||||
if "tool_resources" in run_options:
|
||||
args["tool_resources"] = run_options["tool_resources"]
|
||||
|
||||
# Create the agent on the service
|
||||
created_agent = await self._agents_client.create_agent(**args)
|
||||
|
||||
# Create Agent wrapper
|
||||
return self._to_chat_agent_from_agent(
|
||||
created_agent,
|
||||
normalized_tools,
|
||||
default_options=default_options,
|
||||
middleware=middleware,
|
||||
context_providers=context_providers,
|
||||
)
|
||||
|
||||
async def get_agent(
|
||||
self,
|
||||
id: str,
|
||||
*,
|
||||
tools: ToolTypes | Callable[..., Any] | Sequence[ToolTypes | Callable[..., Any]] | None = None,
|
||||
default_options: OptionsCoT | None = None,
|
||||
middleware: Sequence[MiddlewareTypes] | None = None,
|
||||
context_providers: Sequence[BaseContextProvider] | None = None,
|
||||
) -> Agent[OptionsCoT]:
|
||||
"""Retrieve an existing agent from the service and return a Agent.
|
||||
|
||||
.. deprecated::
|
||||
This method is deprecated and will be removed in a future release.
|
||||
Use :meth:`AzureAIProjectAgentProvider.get_agent` instead.
|
||||
|
||||
This method fetches an agent by ID from the Azure AI service
|
||||
and returns a local Agent instance for interaction.
|
||||
|
||||
Args:
|
||||
id: The ID of the agent to retrieve from the service.
|
||||
|
||||
Keyword Args:
|
||||
tools: Tools to make available to the agent. Required if the agent
|
||||
has function tools that need implementations.
|
||||
default_options: A TypedDict containing default chat options for the agent.
|
||||
These options are applied to every run unless overridden.
|
||||
middleware: List of middleware to intercept agent and function invocations.
|
||||
context_providers: Context providers to include during agent invocation.
|
||||
|
||||
Returns:
|
||||
Agent: A Agent instance configured with the retrieved agent.
|
||||
|
||||
Raises:
|
||||
ValueError: If required function tools are not provided.
|
||||
|
||||
Examples:
|
||||
.. code-block:: python
|
||||
|
||||
agent = await provider.get_agent("agent-123")
|
||||
|
||||
# With function tools
|
||||
agent = await provider.get_agent("agent-123", tools=my_function)
|
||||
"""
|
||||
warnings.warn(
|
||||
"AzureAIAgentsProvider.get_agent() is deprecated and will be removed in a future release; "
|
||||
"use AzureAIProjectAgentProvider.get_agent() instead.",
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
agent = await self._agents_client.get_agent(id)
|
||||
|
||||
# Validate function tools
|
||||
normalized_tools = normalize_tools(tools)
|
||||
self._validate_function_tools(agent.tools, normalized_tools)
|
||||
|
||||
return self._to_chat_agent_from_agent(
|
||||
agent,
|
||||
normalized_tools,
|
||||
default_options=default_options,
|
||||
middleware=middleware,
|
||||
context_providers=context_providers,
|
||||
)
|
||||
|
||||
def as_agent(
|
||||
self,
|
||||
agent: AzureAgent,
|
||||
tools: ToolTypes | Callable[..., Any] | Sequence[ToolTypes | Callable[..., Any]] | None = None,
|
||||
default_options: OptionsCoT | None = None,
|
||||
middleware: Sequence[MiddlewareTypes] | None = None,
|
||||
context_providers: Sequence[BaseContextProvider] | None = None,
|
||||
) -> Agent[OptionsCoT]:
|
||||
"""Wrap an existing Agent SDK object as a Agent without making HTTP calls.
|
||||
|
||||
.. deprecated::
|
||||
This method is deprecated and will be removed in a future release.
|
||||
Use :meth:`AzureAIProjectAgentProvider.as_agent` instead.
|
||||
|
||||
Use this method when you already have an Agent object from a previous
|
||||
SDK operation and want to use it with the Agent Framework.
|
||||
|
||||
Args:
|
||||
agent: The Agent object to wrap.
|
||||
tools: Tools to make available to the agent. Required if the agent
|
||||
has function tools that need implementations.
|
||||
default_options: A TypedDict containing default chat options for the agent.
|
||||
These options are applied to every run unless overridden.
|
||||
middleware: List of middleware to intercept agent and function invocations.
|
||||
context_providers: Context providers to include during agent invocation.
|
||||
|
||||
Returns:
|
||||
Agent: A Agent instance configured with the agent.
|
||||
|
||||
Raises:
|
||||
ValueError: If required function tools are not provided.
|
||||
|
||||
Examples:
|
||||
.. code-block:: python
|
||||
|
||||
# Create agent directly with SDK
|
||||
sdk_agent = await agents_client.create_agent(
|
||||
model="gpt-4",
|
||||
name="MyAgent",
|
||||
instructions="...",
|
||||
)
|
||||
|
||||
# Wrap as Agent
|
||||
chat_agent = provider.as_agent(sdk_agent)
|
||||
"""
|
||||
warnings.warn(
|
||||
"AzureAIAgentsProvider.as_agent() is deprecated and will be removed in a future release; "
|
||||
"use AzureAIProjectAgentProvider.as_agent() instead.",
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
# Validate function tools
|
||||
normalized_tools = normalize_tools(tools)
|
||||
self._validate_function_tools(agent.tools, normalized_tools)
|
||||
|
||||
return self._to_chat_agent_from_agent(
|
||||
agent,
|
||||
normalized_tools,
|
||||
default_options=default_options,
|
||||
middleware=middleware,
|
||||
context_providers=context_providers,
|
||||
)
|
||||
|
||||
def _to_chat_agent_from_agent(
|
||||
self,
|
||||
agent: AzureAgent,
|
||||
provided_tools: Sequence[ToolTypes] | None = None,
|
||||
default_options: OptionsCoT | None = None,
|
||||
middleware: Sequence[MiddlewareTypes] | None = None,
|
||||
context_providers: Sequence[BaseContextProvider] | None = None,
|
||||
) -> Agent[OptionsCoT]:
|
||||
"""Create a Agent from an Agent SDK object.
|
||||
|
||||
Args:
|
||||
agent: The Agent SDK object.
|
||||
provided_tools: User-provided tools (including function implementations).
|
||||
default_options: A TypedDict containing default chat options for the agent.
|
||||
These options are applied to every run unless overridden.
|
||||
middleware: List of middleware to intercept agent and function invocations.
|
||||
context_providers: Context providers to include during agent invocation.
|
||||
"""
|
||||
# Create the underlying client
|
||||
client = AzureAIAgentClient( # pyright: ignore[reportDeprecated]
|
||||
agents_client=self._agents_client,
|
||||
agent_id=agent.id,
|
||||
agent_name=agent.name,
|
||||
agent_description=agent.description,
|
||||
should_cleanup_agent=False, # Provider manages agent lifecycle
|
||||
)
|
||||
|
||||
# Merge tools: convert agent's hosted tools + user-provided function tools
|
||||
merged_tools = self._merge_tools(agent.tools, provided_tools)
|
||||
merged_default_options: dict[str, Any] = dict(default_options) if default_options is not None else {}
|
||||
merged_default_options.setdefault("model_id", agent.model)
|
||||
|
||||
return Agent( # type: ignore[return-value]
|
||||
client=client,
|
||||
id=agent.id,
|
||||
name=agent.name,
|
||||
description=agent.description,
|
||||
instructions=agent.instructions,
|
||||
tools=merged_tools,
|
||||
default_options=cast(Any, merged_default_options),
|
||||
middleware=middleware,
|
||||
context_providers=context_providers,
|
||||
)
|
||||
|
||||
def _merge_tools(
|
||||
self,
|
||||
agent_tools: Sequence[Any] | None,
|
||||
provided_tools: Sequence[ToolTypes] | None,
|
||||
) -> list[ToolTypes]:
|
||||
"""Merge hosted tools from agent with user-provided function tools.
|
||||
|
||||
Args:
|
||||
agent_tools: Tools from the agent definition (Azure AI format).
|
||||
provided_tools: User-provided tools (Agent Framework format).
|
||||
|
||||
Returns:
|
||||
Combined list of tools for the Agent.
|
||||
"""
|
||||
merged: list[ToolTypes] = []
|
||||
|
||||
# Hosted tools (file_search, code_interpreter, bing_grounding, openapi, etc.)
|
||||
# are already defined on the server agent and will be read back by the client
|
||||
# at run time via agent_definition.tools. We skip them here to avoid sending
|
||||
# them again at request time (which causes API errors like unknown vector_store_ids).
|
||||
|
||||
# Add user-provided function tools and MCP tools
|
||||
if provided_tools:
|
||||
for provided_tool in provided_tools:
|
||||
# FunctionTool - has implementation for function calling
|
||||
# MCPTool - Agent handles MCP connection and tool discovery at runtime
|
||||
if isinstance(provided_tool, (FunctionTool, MCPTool)):
|
||||
merged.append(provided_tool) # type: ignore[reportUnknownArgumentType]
|
||||
|
||||
return merged
|
||||
|
||||
def _validate_function_tools(
|
||||
self,
|
||||
agent_tools: Sequence[Any] | None,
|
||||
provided_tools: Sequence[ToolTypes] | None,
|
||||
) -> None:
|
||||
"""Validate that required function tools are provided.
|
||||
|
||||
Raises:
|
||||
ValueError: If agent has function tools but user
|
||||
didn't provide implementations.
|
||||
"""
|
||||
if not agent_tools:
|
||||
return
|
||||
|
||||
# Get function tool names from agent definition
|
||||
function_tool_names: set[str] = set()
|
||||
for tool in agent_tools:
|
||||
if isinstance(tool, dict):
|
||||
tool_dict = cast(dict[str, Any], tool)
|
||||
if tool_dict.get("type") == "function":
|
||||
func_def = cast(dict[str, Any], tool_dict.get("function", {}))
|
||||
name = func_def.get("name")
|
||||
if isinstance(name, str):
|
||||
function_tool_names.add(name)
|
||||
elif hasattr(tool, "type") and tool.type == "function":
|
||||
func_attr = getattr(tool, "function", None)
|
||||
if func_attr and hasattr(func_attr, "name"):
|
||||
function_tool_names.add(str(func_attr.name))
|
||||
|
||||
if not function_tool_names:
|
||||
return
|
||||
|
||||
# Get provided function names
|
||||
provided_names: set[str] = set()
|
||||
if provided_tools:
|
||||
for tool in provided_tools:
|
||||
if isinstance(tool, FunctionTool):
|
||||
provided_names.add(tool.name)
|
||||
|
||||
# Check for missing implementations
|
||||
missing = function_tool_names - provided_names
|
||||
if missing:
|
||||
raise ValueError(
|
||||
f"Agent has function tools that require implementations: {missing}. "
|
||||
"Provide these functions via the 'tools' parameter."
|
||||
)
|
||||
|
||||
def _create_response_format_config(
|
||||
self,
|
||||
response_format: type[BaseModel],
|
||||
) -> ResponseFormatJsonSchemaType:
|
||||
"""Create response format configuration for Azure AI.
|
||||
|
||||
Args:
|
||||
response_format: Pydantic model for structured output.
|
||||
|
||||
Returns:
|
||||
Azure AI response format configuration.
|
||||
"""
|
||||
return ResponseFormatJsonSchemaType(
|
||||
json_schema=ResponseFormatJsonSchema(
|
||||
name=response_format.__name__,
|
||||
schema=response_format.model_json_schema(),
|
||||
)
|
||||
)
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,918 +0,0 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
"""Deprecated Azure OpenAI client classes.
|
||||
|
||||
All classes in this module are deprecated and will be removed in a future release.
|
||||
Migrate to the ``agent_framework_openai`` package equivalents with an ``AsyncAzureOpenAI`` client,
|
||||
or use ``FoundryChatClient`` for Azure AI Foundry projects.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import sys
|
||||
from collections.abc import Mapping, Sequence
|
||||
from contextlib import contextmanager
|
||||
from copy import copy
|
||||
from typing import TYPE_CHECKING, Any, ClassVar, Final, Generic, cast
|
||||
from urllib.parse import urljoin, urlparse
|
||||
|
||||
from agent_framework._middleware import ChatMiddlewareLayer
|
||||
from agent_framework._settings import SecretString, load_settings
|
||||
from agent_framework._telemetry import AGENT_FRAMEWORK_USER_AGENT, APP_INFO, prepend_agent_framework_to_user_agent
|
||||
from agent_framework._tools import FunctionInvocationConfiguration, FunctionInvocationLayer
|
||||
from agent_framework._types import Annotation, Content
|
||||
from agent_framework.observability import ChatTelemetryLayer, EmbeddingTelemetryLayer
|
||||
from agent_framework_openai._assistants_client import (
|
||||
OpenAIAssistantsClient, # type: ignore[reportDeprecated]
|
||||
OpenAIAssistantsOptions,
|
||||
)
|
||||
from agent_framework_openai._chat_client import OpenAIChatOptions, RawOpenAIChatClient
|
||||
from agent_framework_openai._chat_completion_client import OpenAIChatCompletionOptions, RawOpenAIChatCompletionClient
|
||||
from agent_framework_openai._embedding_client import OpenAIEmbeddingOptions, RawOpenAIEmbeddingClient
|
||||
from agent_framework_openai._shared import OpenAIBase
|
||||
from azure.ai.projects.aio import AIProjectClient
|
||||
from openai import AsyncOpenAI
|
||||
from openai.lib.azure import AsyncAzureOpenAI
|
||||
from pydantic import BaseModel
|
||||
|
||||
from ._entra_id_authentication import AzureCredentialTypes, AzureTokenProvider, resolve_credential_to_token_provider
|
||||
|
||||
if sys.version_info >= (3, 13):
|
||||
from typing import TypeVar # type: ignore # pragma: no cover
|
||||
from warnings import deprecated # type: ignore # pragma: no cover
|
||||
else:
|
||||
from typing_extensions import TypeVar, deprecated # type: ignore # pragma: no cover
|
||||
if sys.version_info >= (3, 12):
|
||||
from typing import override # type: ignore # pragma: no cover
|
||||
else:
|
||||
from typing_extensions import override # type: ignore # pragma: no cover
|
||||
if sys.version_info >= (3, 11):
|
||||
from typing import TypedDict # type: ignore # pragma: no cover
|
||||
else:
|
||||
from typing_extensions import TypedDict # type: ignore # pragma: no cover
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from agent_framework._middleware import MiddlewareTypes
|
||||
from openai.types.chat.chat_completion import Choice
|
||||
from openai.types.chat.chat_completion_chunk import Choice as ChunkChoice
|
||||
|
||||
logger: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# region Constants and Settings
|
||||
|
||||
DEFAULT_AZURE_API_VERSION: Final[str] = "2024-10-21"
|
||||
DEFAULT_AZURE_TOKEN_ENDPOINT: Final[str] = "https://cognitiveservices.azure.com/.default" # noqa: S105
|
||||
|
||||
|
||||
class AzureOpenAISettings(TypedDict, total=False):
|
||||
"""AzureOpenAI model settings.
|
||||
|
||||
Settings are resolved in this order: explicit keyword arguments, values from an
|
||||
explicitly provided .env file, then environment variables with the prefix
|
||||
'AZURE_OPENAI_'. If settings are missing after resolution, validation will fail.
|
||||
|
||||
Keyword Args:
|
||||
endpoint: The endpoint of the Azure deployment.
|
||||
chat_deployment_name: The name of the Azure Chat deployment.
|
||||
responses_deployment_name: The name of the Azure Responses deployment.
|
||||
embedding_deployment_name: The name of the Azure Embedding deployment.
|
||||
api_key: The API key for the Azure deployment.
|
||||
api_version: The API version to use.
|
||||
base_url: The url of the Azure deployment.
|
||||
token_endpoint: The token endpoint to use to retrieve the authentication token.
|
||||
"""
|
||||
|
||||
chat_deployment_name: str | None
|
||||
responses_deployment_name: str | None
|
||||
embedding_deployment_name: str | None
|
||||
endpoint: str | None
|
||||
base_url: str | None
|
||||
api_key: SecretString | None
|
||||
api_version: str | None
|
||||
token_endpoint: str | None
|
||||
|
||||
|
||||
def _apply_azure_defaults(
|
||||
settings: AzureOpenAISettings,
|
||||
default_api_version: str = DEFAULT_AZURE_API_VERSION,
|
||||
default_token_endpoint: str = DEFAULT_AZURE_TOKEN_ENDPOINT,
|
||||
) -> None:
|
||||
"""Apply default values for api_version and token_endpoint after loading settings.
|
||||
|
||||
Args:
|
||||
settings: The loaded Azure OpenAI settings dict.
|
||||
default_api_version: The default API version to use if not set.
|
||||
default_token_endpoint: The default token endpoint to use if not set.
|
||||
"""
|
||||
if not settings.get("api_version"):
|
||||
settings["api_version"] = default_api_version
|
||||
if not settings.get("token_endpoint"):
|
||||
settings["token_endpoint"] = default_token_endpoint
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _prefer_single_azure_endpoint_env(*, endpoint: str | None, base_url: str | None) -> Any:
|
||||
"""Preserve the legacy call shape without mutating process-wide environment state."""
|
||||
yield
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region AzureOpenAIConfigMixin
|
||||
|
||||
|
||||
class AzureOpenAIConfigMixin(OpenAIBase):
|
||||
"""Internal class for configuring a connection to an Azure OpenAI service."""
|
||||
|
||||
OTEL_PROVIDER_NAME: ClassVar[str] = "azure.ai.openai"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
deployment_name: str,
|
||||
endpoint: str | None = None,
|
||||
base_url: str | None = None,
|
||||
api_version: str = DEFAULT_AZURE_API_VERSION,
|
||||
api_key: str | None = None,
|
||||
token_endpoint: str | None = None,
|
||||
credential: AzureCredentialTypes | AzureTokenProvider | None = None,
|
||||
default_headers: Mapping[str, str] | None = None,
|
||||
client: AsyncOpenAI | None = None,
|
||||
instruction_role: str | None = None,
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
"""Configure a connection to an Azure OpenAI service.
|
||||
|
||||
Args:
|
||||
deployment_name: Name of the deployment.
|
||||
endpoint: The specific endpoint URL for the deployment.
|
||||
base_url: The base URL for Azure services.
|
||||
api_version: Azure API version.
|
||||
api_key: API key for Azure services.
|
||||
token_endpoint: Azure AD token scope.
|
||||
credential: Azure credential or token provider for authentication.
|
||||
default_headers: Default headers for HTTP requests.
|
||||
client: An existing client to use.
|
||||
instruction_role: The role to use for 'instruction' messages.
|
||||
kwargs: Additional keyword arguments.
|
||||
"""
|
||||
merged_headers = dict(copy(default_headers)) if default_headers else {}
|
||||
if APP_INFO:
|
||||
merged_headers.update(APP_INFO)
|
||||
merged_headers = prepend_agent_framework_to_user_agent(merged_headers)
|
||||
if not client:
|
||||
ad_token_provider = None
|
||||
if not api_key and credential:
|
||||
ad_token_provider = resolve_credential_to_token_provider(credential, token_endpoint)
|
||||
|
||||
if not api_key and not ad_token_provider:
|
||||
raise ValueError("Please provide either api_key, credential, or a client.")
|
||||
|
||||
if not endpoint and not base_url:
|
||||
raise ValueError("Please provide an endpoint or a base_url")
|
||||
|
||||
args: dict[str, Any] = {
|
||||
"default_headers": merged_headers,
|
||||
}
|
||||
if api_version:
|
||||
args["api_version"] = api_version
|
||||
if ad_token_provider:
|
||||
args["azure_ad_token_provider"] = ad_token_provider
|
||||
if api_key:
|
||||
args["api_key"] = api_key
|
||||
if base_url:
|
||||
args["base_url"] = str(base_url)
|
||||
if endpoint and not base_url:
|
||||
args["azure_endpoint"] = str(endpoint)
|
||||
if deployment_name:
|
||||
args["azure_deployment"] = deployment_name
|
||||
if "websocket_base_url" in kwargs:
|
||||
args["websocket_base_url"] = kwargs.pop("websocket_base_url")
|
||||
|
||||
client = AsyncAzureOpenAI(**args)
|
||||
|
||||
self.endpoint = str(endpoint)
|
||||
self.base_url = str(base_url)
|
||||
self.api_version = api_version
|
||||
self.deployment_name = deployment_name
|
||||
self.instruction_role = instruction_role
|
||||
if default_headers:
|
||||
from agent_framework._telemetry import USER_AGENT_KEY
|
||||
|
||||
def_headers = {k: v for k, v in default_headers.items() if k != USER_AGENT_KEY}
|
||||
else:
|
||||
def_headers = None
|
||||
self.default_headers = def_headers
|
||||
|
||||
super().__init__(model_id=deployment_name, client=client, **kwargs)
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region AzureOpenAIResponsesClient
|
||||
|
||||
|
||||
AzureOpenAIResponsesOptionsT = TypeVar(
|
||||
"AzureOpenAIResponsesOptionsT",
|
||||
bound=TypedDict, # type: ignore[valid-type]
|
||||
default="OpenAIChatOptions",
|
||||
covariant=True,
|
||||
)
|
||||
|
||||
AzureOpenAIResponsesOptions = OpenAIChatOptions
|
||||
|
||||
|
||||
@deprecated(
|
||||
"AzureOpenAIResponsesClient is deprecated. "
|
||||
"Use OpenAIChatClient with an AsyncAzureOpenAI client, or FoundryChatClient for Foundry projects."
|
||||
)
|
||||
class AzureOpenAIResponsesClient( # type: ignore[misc]
|
||||
FunctionInvocationLayer[AzureOpenAIResponsesOptionsT],
|
||||
ChatMiddlewareLayer[AzureOpenAIResponsesOptionsT],
|
||||
ChatTelemetryLayer[AzureOpenAIResponsesOptionsT],
|
||||
RawOpenAIChatClient[AzureOpenAIResponsesOptionsT],
|
||||
Generic[AzureOpenAIResponsesOptionsT],
|
||||
):
|
||||
"""Deprecated Azure Responses client. Use OpenAIChatClient with an AsyncAzureOpenAI client instead."""
|
||||
|
||||
OTEL_PROVIDER_NAME: ClassVar[str] = "azure.ai.openai"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
api_key: str | None = None,
|
||||
deployment_name: str | None = None,
|
||||
endpoint: str | None = None,
|
||||
base_url: str | None = None,
|
||||
api_version: str | None = None,
|
||||
token_endpoint: str | None = None,
|
||||
credential: AzureCredentialTypes | AzureTokenProvider | None = None,
|
||||
default_headers: Mapping[str, str] | None = None,
|
||||
async_client: AsyncOpenAI | None = None,
|
||||
project_client: Any | None = None,
|
||||
project_endpoint: str | None = None,
|
||||
allow_preview: bool | None = None,
|
||||
env_file_path: str | None = None,
|
||||
env_file_encoding: str | None = None,
|
||||
instruction_role: str | None = None,
|
||||
middleware: Sequence[MiddlewareTypes] | None = None,
|
||||
function_invocation_configuration: FunctionInvocationConfiguration | None = None,
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
"""Initialize an Azure OpenAI Responses client.
|
||||
|
||||
Keyword Args:
|
||||
api_key: The API key.
|
||||
deployment_name: The deployment name.
|
||||
endpoint: The deployment endpoint.
|
||||
base_url: The deployment base URL.
|
||||
api_version: The deployment API version.
|
||||
token_endpoint: The token endpoint to request an Azure token.
|
||||
credential: Azure credential or token provider for authentication.
|
||||
default_headers: Default headers for HTTP requests.
|
||||
async_client: An existing client to use.
|
||||
project_client: An existing AIProjectClient to use.
|
||||
project_endpoint: The Azure AI Foundry project endpoint URL.
|
||||
allow_preview: Enables preview opt-in on internally-created AIProjectClient.
|
||||
env_file_path: Path to .env file for settings.
|
||||
env_file_encoding: Encoding for .env file.
|
||||
instruction_role: The role to use for 'instruction' messages.
|
||||
middleware: Optional sequence of middleware.
|
||||
function_invocation_configuration: Optional function invocation configuration.
|
||||
kwargs: Additional keyword arguments.
|
||||
"""
|
||||
if (model_id := kwargs.pop("model_id", None)) and not deployment_name:
|
||||
deployment_name = str(model_id)
|
||||
|
||||
if async_client is None and (project_client is not None or project_endpoint is not None):
|
||||
async_client = self._create_client_from_project(
|
||||
project_client=project_client,
|
||||
project_endpoint=project_endpoint,
|
||||
credential=credential,
|
||||
allow_preview=allow_preview,
|
||||
)
|
||||
|
||||
azure_openai_settings = load_settings(
|
||||
AzureOpenAISettings,
|
||||
env_prefix="AZURE_OPENAI_",
|
||||
api_key=api_key,
|
||||
base_url=base_url,
|
||||
endpoint=endpoint,
|
||||
responses_deployment_name=deployment_name,
|
||||
api_version=api_version,
|
||||
env_file_path=env_file_path,
|
||||
env_file_encoding=env_file_encoding,
|
||||
token_endpoint=token_endpoint,
|
||||
)
|
||||
_apply_azure_defaults(azure_openai_settings, default_api_version="preview")
|
||||
endpoint_value = azure_openai_settings.get("endpoint")
|
||||
if (
|
||||
not azure_openai_settings.get("base_url")
|
||||
and endpoint_value
|
||||
and (hostname := urlparse(str(endpoint_value)).hostname)
|
||||
and hostname.endswith(".openai.azure.com")
|
||||
):
|
||||
azure_openai_settings["base_url"] = urljoin(str(endpoint_value), "/openai/v1/")
|
||||
|
||||
responses_deployment_name = azure_openai_settings.get("responses_deployment_name")
|
||||
if not responses_deployment_name:
|
||||
raise ValueError(
|
||||
"Azure OpenAI deployment name is required. Set via 'deployment_name' parameter "
|
||||
"or 'AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME' environment variable."
|
||||
)
|
||||
|
||||
endpoint_value = azure_openai_settings.get("endpoint")
|
||||
client_base_url = azure_openai_settings.get("base_url")
|
||||
if not async_client:
|
||||
# Create the Azure OpenAI client directly
|
||||
merged_headers = dict(copy(default_headers)) if default_headers else {}
|
||||
if APP_INFO:
|
||||
merged_headers.update(APP_INFO)
|
||||
merged_headers = prepend_agent_framework_to_user_agent(merged_headers)
|
||||
|
||||
api_key_secret = azure_openai_settings.get("api_key")
|
||||
ad_token_provider = None
|
||||
if not api_key_secret and credential:
|
||||
ad_token_provider = resolve_credential_to_token_provider(
|
||||
credential, azure_openai_settings.get("token_endpoint")
|
||||
)
|
||||
|
||||
if not api_key_secret and not ad_token_provider:
|
||||
raise ValueError("Please provide either api_key, credential, or a client.")
|
||||
|
||||
if not endpoint_value and not client_base_url:
|
||||
raise ValueError("Please provide an endpoint or a base_url")
|
||||
|
||||
client_args: dict[str, Any] = {"default_headers": merged_headers}
|
||||
if resolved_api_version := azure_openai_settings.get("api_version"):
|
||||
client_args["api_version"] = resolved_api_version
|
||||
if ad_token_provider:
|
||||
client_args["azure_ad_token_provider"] = ad_token_provider
|
||||
if api_key_secret:
|
||||
client_args["api_key"] = api_key_secret.get_secret_value()
|
||||
if client_base_url:
|
||||
client_args["base_url"] = str(client_base_url)
|
||||
if endpoint_value and not client_base_url:
|
||||
client_args["azure_endpoint"] = str(endpoint_value)
|
||||
if responses_deployment_name:
|
||||
client_args["azure_deployment"] = responses_deployment_name
|
||||
if "websocket_base_url" in kwargs:
|
||||
client_args["websocket_base_url"] = kwargs.pop("websocket_base_url")
|
||||
|
||||
async_client = AsyncAzureOpenAI(**client_args)
|
||||
|
||||
# Store Azure-specific attributes for serialization
|
||||
self.endpoint = str(endpoint_value) if endpoint_value else None
|
||||
self.api_version = azure_openai_settings.get("api_version") or ""
|
||||
self.deployment_name = responses_deployment_name
|
||||
|
||||
with _prefer_single_azure_endpoint_env(endpoint=endpoint_value, base_url=client_base_url):
|
||||
super().__init__(
|
||||
async_client=async_client,
|
||||
model=responses_deployment_name,
|
||||
azure_endpoint=str(endpoint_value) if endpoint_value else None,
|
||||
base_url=str(client_base_url) if client_base_url else None,
|
||||
api_version=azure_openai_settings.get("api_version"),
|
||||
instruction_role=instruction_role,
|
||||
default_headers=default_headers,
|
||||
middleware=middleware, # type: ignore[arg-type]
|
||||
function_invocation_configuration=function_invocation_configuration,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _create_client_from_project(
|
||||
*,
|
||||
project_client: AIProjectClient | None,
|
||||
project_endpoint: str | None,
|
||||
credential: AzureCredentialTypes | AzureTokenProvider | None,
|
||||
allow_preview: bool | None = None,
|
||||
) -> AsyncOpenAI:
|
||||
"""Create an AsyncOpenAI client from an Azure AI Foundry project."""
|
||||
if project_client is not None:
|
||||
return project_client.get_openai_client()
|
||||
|
||||
if not project_endpoint:
|
||||
raise ValueError("Azure AI project endpoint is required when project_client is not provided.")
|
||||
if not credential:
|
||||
raise ValueError("Azure credential is required when using project_endpoint without a project_client.")
|
||||
project_client_kwargs: dict[str, Any] = {
|
||||
"endpoint": project_endpoint,
|
||||
"credential": credential, # type: ignore[arg-type]
|
||||
"user_agent": AGENT_FRAMEWORK_USER_AGENT,
|
||||
}
|
||||
if allow_preview is not None:
|
||||
project_client_kwargs["allow_preview"] = allow_preview
|
||||
project_client = AIProjectClient(**project_client_kwargs)
|
||||
return project_client.get_openai_client()
|
||||
|
||||
@override
|
||||
def _check_model_presence(self, options: dict[str, Any]) -> None:
|
||||
if not options.get("model"):
|
||||
if not self.model:
|
||||
raise ValueError("deployment_name must be a non-empty string")
|
||||
options["model"] = self.model
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region AzureOpenAIChatClient
|
||||
|
||||
|
||||
ResponseModelT = TypeVar("ResponseModelT", bound=BaseModel | None, default=None)
|
||||
|
||||
|
||||
class AzureUserSecurityContext(TypedDict, total=False):
|
||||
"""User security context for Azure AI applications.
|
||||
|
||||
These fields help security operations teams investigate and mitigate security
|
||||
incidents by providing context about the application and end user.
|
||||
"""
|
||||
|
||||
application_name: str
|
||||
"""Name of the application making the request."""
|
||||
|
||||
end_user_id: str
|
||||
"""Unique identifier for the end user (recommend hashing username/email)."""
|
||||
|
||||
end_user_tenant_id: str
|
||||
"""Microsoft 365 tenant ID the end user belongs to. Required for multi-tenant apps."""
|
||||
|
||||
source_ip: str
|
||||
"""The original client's IP address."""
|
||||
|
||||
|
||||
class AzureOpenAIChatOptions(OpenAIChatCompletionOptions[ResponseModelT], Generic[ResponseModelT], total=False):
|
||||
"""Azure OpenAI-specific chat options dict.
|
||||
|
||||
Extends OpenAIChatCompletionOptions with Azure-specific options including
|
||||
the "On Your Data" feature and enhanced security context.
|
||||
"""
|
||||
|
||||
data_sources: list[dict[str, Any]]
|
||||
"""Azure "On Your Data" data sources for retrieval-augmented generation."""
|
||||
|
||||
user_security_context: AzureUserSecurityContext
|
||||
"""Enhanced security context for Azure Defender integration."""
|
||||
|
||||
n: int
|
||||
"""Number of chat completion choices to generate for each input message."""
|
||||
|
||||
|
||||
AzureOpenAIChatOptionsT = TypeVar(
|
||||
"AzureOpenAIChatOptionsT",
|
||||
bound=TypedDict, # type: ignore[valid-type]
|
||||
default="AzureOpenAIChatOptions",
|
||||
covariant=True,
|
||||
)
|
||||
|
||||
|
||||
@deprecated("AzureOpenAIChatClient is deprecated. Use OpenAIChatCompletionClient with an AsyncAzureOpenAI client.")
|
||||
class AzureOpenAIChatClient( # type: ignore[misc]
|
||||
FunctionInvocationLayer[AzureOpenAIChatOptionsT],
|
||||
ChatMiddlewareLayer[AzureOpenAIChatOptionsT],
|
||||
ChatTelemetryLayer[AzureOpenAIChatOptionsT],
|
||||
RawOpenAIChatCompletionClient[AzureOpenAIChatOptionsT],
|
||||
Generic[AzureOpenAIChatOptionsT],
|
||||
):
|
||||
"""Deprecated Azure OpenAI Chat client. Use OpenAIChatCompletionClient with AsyncAzureOpenAI instead."""
|
||||
|
||||
OTEL_PROVIDER_NAME: ClassVar[str] = "azure.ai.openai"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
api_key: str | None = None,
|
||||
deployment_name: str | None = None,
|
||||
endpoint: str | None = None,
|
||||
base_url: str | None = None,
|
||||
api_version: str | None = None,
|
||||
token_endpoint: str | None = None,
|
||||
credential: AzureCredentialTypes | AzureTokenProvider | None = None,
|
||||
default_headers: Mapping[str, str] | None = None,
|
||||
async_client: AsyncAzureOpenAI | None = None,
|
||||
additional_properties: dict[str, Any] | None = None,
|
||||
env_file_path: str | None = None,
|
||||
env_file_encoding: str | None = None,
|
||||
instruction_role: str | None = None,
|
||||
middleware: Sequence[MiddlewareTypes] | None = None,
|
||||
function_invocation_configuration: FunctionInvocationConfiguration | None = None,
|
||||
) -> None:
|
||||
"""Initialize an Azure OpenAI Chat completion client.
|
||||
|
||||
Keyword Args:
|
||||
api_key: The API key.
|
||||
deployment_name: The deployment name.
|
||||
endpoint: The deployment endpoint.
|
||||
base_url: The deployment base URL.
|
||||
api_version: The deployment API version.
|
||||
token_endpoint: The token endpoint to request an Azure token.
|
||||
credential: Azure credential or token provider for authentication.
|
||||
default_headers: Default headers for HTTP requests.
|
||||
async_client: An existing client to use.
|
||||
additional_properties: Additional properties stored on the client instance.
|
||||
env_file_path: Path to .env file for settings.
|
||||
env_file_encoding: Encoding for .env file.
|
||||
instruction_role: The role to use for 'instruction' messages.
|
||||
middleware: Optional sequence of middleware.
|
||||
function_invocation_configuration: Optional function invocation configuration.
|
||||
"""
|
||||
azure_openai_settings = load_settings(
|
||||
AzureOpenAISettings,
|
||||
env_prefix="AZURE_OPENAI_",
|
||||
api_key=api_key,
|
||||
base_url=base_url,
|
||||
endpoint=endpoint,
|
||||
chat_deployment_name=deployment_name,
|
||||
api_version=api_version,
|
||||
env_file_path=env_file_path,
|
||||
env_file_encoding=env_file_encoding,
|
||||
token_endpoint=token_endpoint,
|
||||
)
|
||||
_apply_azure_defaults(azure_openai_settings)
|
||||
|
||||
chat_deployment_name = azure_openai_settings.get("chat_deployment_name")
|
||||
if not chat_deployment_name:
|
||||
raise ValueError(
|
||||
"Azure OpenAI deployment name is required. Set via 'deployment_name' parameter "
|
||||
"or 'AZURE_OPENAI_CHAT_DEPLOYMENT_NAME' environment variable."
|
||||
)
|
||||
|
||||
endpoint_value = azure_openai_settings.get("endpoint")
|
||||
base_url_value = azure_openai_settings.get("base_url")
|
||||
if not async_client:
|
||||
# Create the Azure OpenAI client directly
|
||||
merged_headers = dict(copy(default_headers)) if default_headers else {}
|
||||
if APP_INFO:
|
||||
merged_headers.update(APP_INFO)
|
||||
merged_headers = prepend_agent_framework_to_user_agent(merged_headers)
|
||||
|
||||
api_key_secret = azure_openai_settings.get("api_key")
|
||||
ad_token_provider = None
|
||||
if not api_key_secret and credential:
|
||||
ad_token_provider = resolve_credential_to_token_provider(
|
||||
credential, azure_openai_settings.get("token_endpoint")
|
||||
)
|
||||
|
||||
if not api_key_secret and not ad_token_provider:
|
||||
raise ValueError("Please provide either api_key, credential, or a client.")
|
||||
|
||||
if not endpoint_value and not base_url_value:
|
||||
raise ValueError("Please provide an endpoint or a base_url")
|
||||
|
||||
client_args: dict[str, Any] = {"default_headers": merged_headers}
|
||||
if resolved_api_version := azure_openai_settings.get("api_version"):
|
||||
client_args["api_version"] = resolved_api_version
|
||||
if ad_token_provider:
|
||||
client_args["azure_ad_token_provider"] = ad_token_provider
|
||||
if api_key_secret:
|
||||
client_args["api_key"] = api_key_secret.get_secret_value()
|
||||
if base_url_value:
|
||||
client_args["base_url"] = str(base_url_value)
|
||||
if endpoint_value and not base_url_value:
|
||||
client_args["azure_endpoint"] = str(endpoint_value)
|
||||
if chat_deployment_name:
|
||||
client_args["azure_deployment"] = chat_deployment_name
|
||||
|
||||
async_client = AsyncAzureOpenAI(**client_args)
|
||||
|
||||
# Store Azure-specific attributes for serialization
|
||||
self.endpoint = str(azure_openai_settings.get("endpoint") or "")
|
||||
self.api_version = azure_openai_settings.get("api_version") or ""
|
||||
self.deployment_name = chat_deployment_name
|
||||
|
||||
with _prefer_single_azure_endpoint_env(endpoint=endpoint_value, base_url=base_url_value):
|
||||
super().__init__(
|
||||
async_client=async_client,
|
||||
model=chat_deployment_name,
|
||||
azure_endpoint=str(endpoint_value) if endpoint_value else None,
|
||||
base_url=str(base_url_value) if base_url_value else None,
|
||||
api_version=azure_openai_settings.get("api_version"),
|
||||
instruction_role=instruction_role,
|
||||
default_headers=default_headers,
|
||||
additional_properties=additional_properties,
|
||||
middleware=middleware, # type: ignore[arg-type]
|
||||
function_invocation_configuration=function_invocation_configuration,
|
||||
)
|
||||
|
||||
@override
|
||||
def _parse_text_from_openai(self, choice: Choice | ChunkChoice) -> Content | None:
|
||||
"""Parse the choice into a Content object with type='text'.
|
||||
|
||||
Overwritten from RawOpenAIChatCompletionClient to deal with Azure On Your Data function.
|
||||
"""
|
||||
message = getattr(choice, "message", None)
|
||||
if message is None:
|
||||
message = getattr(choice, "delta", None)
|
||||
if message is None: # type: ignore
|
||||
return None
|
||||
if hasattr(message, "refusal") and message.refusal:
|
||||
return Content.from_text(text=message.refusal, raw_representation=choice)
|
||||
if not message.content:
|
||||
return None
|
||||
text_content = Content.from_text(text=message.content, raw_representation=choice)
|
||||
if not message.model_extra or "context" not in message.model_extra:
|
||||
return text_content
|
||||
|
||||
context_raw: object = cast(object, message.context) # type: ignore[union-attr]
|
||||
if isinstance(context_raw, str):
|
||||
try:
|
||||
context_raw = json.loads(context_raw)
|
||||
except json.JSONDecodeError:
|
||||
logger.warning("Context is not a valid JSON string, ignoring context.")
|
||||
return text_content
|
||||
if not isinstance(context_raw, dict):
|
||||
logger.warning("Context is not a valid dictionary, ignoring context.")
|
||||
return text_content
|
||||
context = cast(dict[str, Any], context_raw)
|
||||
if intent := context.get("intent"):
|
||||
text_content.additional_properties = {"intent": intent}
|
||||
citations = context.get("citations")
|
||||
if isinstance(citations, list) and citations:
|
||||
annotations: list[Annotation] = []
|
||||
for citation_raw in cast(list[object], citations):
|
||||
if not isinstance(citation_raw, dict):
|
||||
continue
|
||||
citation = cast(dict[str, Any], citation_raw)
|
||||
annotations.append(
|
||||
Annotation(
|
||||
type="citation",
|
||||
title=citation.get("title", ""),
|
||||
url=citation.get("url", ""),
|
||||
snippet=citation.get("content", ""),
|
||||
file_id=citation.get("filepath", ""),
|
||||
tool_name="Azure-on-your-Data",
|
||||
additional_properties={"chunk_id": citation.get("chunk_id", "")},
|
||||
raw_representation=citation,
|
||||
)
|
||||
)
|
||||
text_content.annotations = annotations
|
||||
return text_content
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region AzureOpenAIAssistantsClient
|
||||
|
||||
|
||||
AzureOpenAIAssistantsOptionsT = TypeVar(
|
||||
"AzureOpenAIAssistantsOptionsT",
|
||||
bound=TypedDict, # type: ignore[valid-type]
|
||||
default="OpenAIAssistantsOptions",
|
||||
covariant=True,
|
||||
)
|
||||
|
||||
AzureOpenAIAssistantsOptions = OpenAIAssistantsOptions
|
||||
|
||||
|
||||
@deprecated(
|
||||
"AzureOpenAIAssistantsClient is deprecated. "
|
||||
"Use OpenAIAssistantsClient (also deprecated) or migrate to OpenAIChatClient."
|
||||
)
|
||||
class AzureOpenAIAssistantsClient(
|
||||
OpenAIAssistantsClient[AzureOpenAIAssistantsOptionsT], # type: ignore[reportDeprecated]
|
||||
Generic[AzureOpenAIAssistantsOptionsT],
|
||||
):
|
||||
"""Deprecated Azure OpenAI Assistants client. Use OpenAIAssistantsClient or migrate to OpenAIChatClient."""
|
||||
|
||||
DEFAULT_AZURE_API_VERSION: ClassVar[str] = "2024-05-01-preview"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
deployment_name: str | None = None,
|
||||
assistant_id: str | None = None,
|
||||
assistant_name: str | None = None,
|
||||
assistant_description: str | None = None,
|
||||
thread_id: str | None = None,
|
||||
api_key: str | None = None,
|
||||
endpoint: str | None = None,
|
||||
base_url: str | None = None,
|
||||
api_version: str | None = None,
|
||||
token_endpoint: str | None = None,
|
||||
credential: AzureCredentialTypes | AzureTokenProvider | None = None,
|
||||
default_headers: Mapping[str, str] | None = None,
|
||||
async_client: AsyncAzureOpenAI | None = None,
|
||||
env_file_path: str | None = None,
|
||||
env_file_encoding: str | None = None,
|
||||
) -> None:
|
||||
"""Initialize an Azure OpenAI Assistants client.
|
||||
|
||||
Keyword Args:
|
||||
deployment_name: The Azure OpenAI deployment name.
|
||||
assistant_id: The ID of an Azure OpenAI assistant to use.
|
||||
assistant_name: The name to use when creating new assistants.
|
||||
assistant_description: The description to use when creating new assistants.
|
||||
thread_id: Default thread ID to use for conversations.
|
||||
api_key: The API key to use.
|
||||
endpoint: The deployment endpoint.
|
||||
base_url: The deployment base URL.
|
||||
api_version: The deployment API version.
|
||||
token_endpoint: The token endpoint to request an Azure token.
|
||||
credential: Azure credential or token provider for authentication.
|
||||
default_headers: Default headers for HTTP requests.
|
||||
async_client: An existing client to use.
|
||||
env_file_path: Path to .env file for settings.
|
||||
env_file_encoding: Encoding for .env file.
|
||||
"""
|
||||
azure_openai_settings = load_settings(
|
||||
AzureOpenAISettings,
|
||||
env_prefix="AZURE_OPENAI_",
|
||||
api_key=api_key,
|
||||
base_url=base_url,
|
||||
endpoint=endpoint,
|
||||
chat_deployment_name=deployment_name,
|
||||
api_version=api_version,
|
||||
env_file_path=env_file_path,
|
||||
env_file_encoding=env_file_encoding,
|
||||
token_endpoint=token_endpoint,
|
||||
)
|
||||
_apply_azure_defaults(azure_openai_settings, default_api_version=self.DEFAULT_AZURE_API_VERSION)
|
||||
|
||||
chat_deployment_name = azure_openai_settings.get("chat_deployment_name")
|
||||
if not chat_deployment_name:
|
||||
raise ValueError(
|
||||
"Azure OpenAI deployment name is required. Set via 'deployment_name' parameter "
|
||||
"or 'AZURE_OPENAI_CHAT_DEPLOYMENT_NAME' environment variable."
|
||||
)
|
||||
|
||||
api_key_secret = azure_openai_settings.get("api_key")
|
||||
token_scope = azure_openai_settings.get("token_endpoint")
|
||||
|
||||
ad_token_provider = None
|
||||
if not async_client and not api_key_secret and credential:
|
||||
ad_token_provider = resolve_credential_to_token_provider(credential, token_scope)
|
||||
|
||||
if not async_client and not api_key_secret and not ad_token_provider:
|
||||
raise ValueError("Please provide either api_key, credential, or a client.")
|
||||
|
||||
if not async_client:
|
||||
client_params: dict[str, Any] = {
|
||||
"default_headers": default_headers,
|
||||
}
|
||||
if resolved_api_version := azure_openai_settings.get("api_version"):
|
||||
client_params["api_version"] = resolved_api_version
|
||||
|
||||
if api_key_secret:
|
||||
client_params["api_key"] = api_key_secret.get_secret_value()
|
||||
elif ad_token_provider:
|
||||
client_params["azure_ad_token_provider"] = ad_token_provider
|
||||
|
||||
if resolved_base_url := azure_openai_settings.get("base_url"):
|
||||
client_params["base_url"] = str(resolved_base_url)
|
||||
elif resolved_endpoint := azure_openai_settings.get("endpoint"):
|
||||
client_params["azure_endpoint"] = str(resolved_endpoint)
|
||||
|
||||
async_client = AsyncAzureOpenAI(**client_params)
|
||||
|
||||
super().__init__(
|
||||
model_id=chat_deployment_name,
|
||||
assistant_id=assistant_id,
|
||||
assistant_name=assistant_name,
|
||||
assistant_description=assistant_description,
|
||||
thread_id=thread_id,
|
||||
async_client=async_client, # type: ignore[reportArgumentType]
|
||||
default_headers=default_headers,
|
||||
)
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region AzureOpenAIEmbeddingClient
|
||||
|
||||
|
||||
AzureOpenAIEmbeddingOptionsT = TypeVar(
|
||||
"AzureOpenAIEmbeddingOptionsT",
|
||||
bound=TypedDict, # type: ignore[valid-type]
|
||||
default="OpenAIEmbeddingOptions",
|
||||
covariant=True,
|
||||
)
|
||||
|
||||
|
||||
@deprecated("AzureOpenAIEmbeddingClient is deprecated. Use OpenAIEmbeddingClient with an AsyncAzureOpenAI client.")
|
||||
class AzureOpenAIEmbeddingClient(
|
||||
EmbeddingTelemetryLayer[str, list[float], AzureOpenAIEmbeddingOptionsT],
|
||||
RawOpenAIEmbeddingClient[AzureOpenAIEmbeddingOptionsT],
|
||||
Generic[AzureOpenAIEmbeddingOptionsT],
|
||||
):
|
||||
"""Deprecated Azure OpenAI embedding client. Use OpenAIEmbeddingClient with AsyncAzureOpenAI instead."""
|
||||
|
||||
OTEL_PROVIDER_NAME: ClassVar[str] = "azure.ai.openai"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
api_key: str | None = None,
|
||||
deployment_name: str | None = None,
|
||||
endpoint: str | None = None,
|
||||
base_url: str | None = None,
|
||||
api_version: str | None = None,
|
||||
token_endpoint: str | None = None,
|
||||
credential: AzureCredentialTypes | AzureTokenProvider | None = None,
|
||||
default_headers: Mapping[str, str] | None = None,
|
||||
async_client: AsyncAzureOpenAI | None = None,
|
||||
otel_provider_name: str | None = None,
|
||||
env_file_path: str | None = None,
|
||||
env_file_encoding: str | None = None,
|
||||
) -> None:
|
||||
"""Initialize an Azure OpenAI embedding client.
|
||||
|
||||
Keyword Args:
|
||||
api_key: The API key.
|
||||
deployment_name: The deployment name.
|
||||
endpoint: The deployment endpoint.
|
||||
base_url: The deployment base URL.
|
||||
api_version: The deployment API version.
|
||||
token_endpoint: The token endpoint to request an Azure token.
|
||||
credential: Azure credential or token provider for authentication.
|
||||
default_headers: Default headers for HTTP requests.
|
||||
async_client: An existing client to use.
|
||||
otel_provider_name: Override the OpenTelemetry provider name.
|
||||
env_file_path: Path to .env file for settings.
|
||||
env_file_encoding: Encoding for .env file.
|
||||
"""
|
||||
azure_openai_settings = load_settings(
|
||||
AzureOpenAISettings,
|
||||
env_prefix="AZURE_OPENAI_",
|
||||
api_key=api_key,
|
||||
base_url=base_url,
|
||||
endpoint=endpoint,
|
||||
embedding_deployment_name=deployment_name,
|
||||
api_version=api_version,
|
||||
env_file_path=env_file_path,
|
||||
env_file_encoding=env_file_encoding,
|
||||
token_endpoint=token_endpoint,
|
||||
)
|
||||
_apply_azure_defaults(azure_openai_settings)
|
||||
|
||||
embedding_deployment_name = azure_openai_settings.get("embedding_deployment_name")
|
||||
if not embedding_deployment_name:
|
||||
raise ValueError(
|
||||
"Azure OpenAI embedding deployment name is required. Set via 'deployment_name' parameter "
|
||||
"or 'AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME' environment variable."
|
||||
)
|
||||
|
||||
endpoint_value = azure_openai_settings.get("endpoint")
|
||||
base_url_value = azure_openai_settings.get("base_url")
|
||||
if not async_client:
|
||||
# Create the Azure OpenAI client directly
|
||||
merged_headers = dict(copy(default_headers)) if default_headers else {}
|
||||
if APP_INFO:
|
||||
merged_headers.update(APP_INFO)
|
||||
merged_headers = prepend_agent_framework_to_user_agent(merged_headers)
|
||||
|
||||
api_key_secret = azure_openai_settings.get("api_key")
|
||||
ad_token_provider = None
|
||||
if not api_key_secret and credential:
|
||||
ad_token_provider = resolve_credential_to_token_provider(
|
||||
credential, azure_openai_settings.get("token_endpoint")
|
||||
)
|
||||
|
||||
if not api_key_secret and not ad_token_provider:
|
||||
raise ValueError("Please provide either api_key, credential, or a client.")
|
||||
|
||||
if not endpoint_value and not base_url_value:
|
||||
raise ValueError("Please provide an endpoint or a base_url")
|
||||
|
||||
client_args: dict[str, Any] = {"default_headers": merged_headers}
|
||||
if resolved_api_version := azure_openai_settings.get("api_version"):
|
||||
client_args["api_version"] = resolved_api_version
|
||||
if ad_token_provider:
|
||||
client_args["azure_ad_token_provider"] = ad_token_provider
|
||||
if api_key_secret:
|
||||
client_args["api_key"] = api_key_secret.get_secret_value()
|
||||
if base_url_value:
|
||||
client_args["base_url"] = str(base_url_value)
|
||||
if endpoint_value and not base_url_value:
|
||||
client_args["azure_endpoint"] = str(endpoint_value)
|
||||
if embedding_deployment_name:
|
||||
client_args["azure_deployment"] = embedding_deployment_name
|
||||
|
||||
async_client = AsyncAzureOpenAI(**client_args)
|
||||
|
||||
# Store Azure-specific attributes for serialization
|
||||
self.endpoint = str(azure_openai_settings.get("endpoint") or "")
|
||||
self.api_version = azure_openai_settings.get("api_version") or ""
|
||||
self.deployment_name = embedding_deployment_name
|
||||
|
||||
with _prefer_single_azure_endpoint_env(endpoint=endpoint_value, base_url=base_url_value):
|
||||
super().__init__(
|
||||
async_client=async_client,
|
||||
model=embedding_deployment_name,
|
||||
azure_endpoint=str(endpoint_value) if endpoint_value else None,
|
||||
base_url=str(base_url_value) if base_url_value else None,
|
||||
api_version=azure_openai_settings.get("api_version"),
|
||||
default_headers=default_headers,
|
||||
)
|
||||
if otel_provider_name is not None:
|
||||
self.OTEL_PROVIDER_NAME = otel_provider_name # type: ignore[misc]
|
||||
|
||||
|
||||
# endregion
|
||||
@@ -1,488 +0,0 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import sys
|
||||
from collections.abc import Callable, Mapping, MutableMapping, Sequence
|
||||
from typing import Any, Generic, cast
|
||||
|
||||
from agent_framework import (
|
||||
AGENT_FRAMEWORK_USER_AGENT,
|
||||
Agent,
|
||||
BaseContextProvider,
|
||||
FunctionTool,
|
||||
MiddlewareTypes,
|
||||
normalize_tools,
|
||||
)
|
||||
from agent_framework._mcp import MCPTool
|
||||
from agent_framework._settings import load_settings
|
||||
from agent_framework._tools import ToolTypes
|
||||
from azure.ai.projects.aio import AIProjectClient
|
||||
from azure.ai.projects.models import (
|
||||
AgentVersionDetails,
|
||||
PromptAgentDefinition,
|
||||
PromptAgentDefinitionTextOptions,
|
||||
)
|
||||
from azure.ai.projects.models import (
|
||||
FunctionTool as AzureFunctionTool,
|
||||
)
|
||||
|
||||
from ._client import AzureAIClient, AzureAIProjectAgentOptions # pyright: ignore[reportDeprecated]
|
||||
from ._entra_id_authentication import AzureCredentialTypes
|
||||
from ._shared import AzureAISettings, create_text_format_config, from_azure_ai_tools, to_azure_ai_tools
|
||||
|
||||
if sys.version_info >= (3, 13):
|
||||
from typing import TypeVar # type: ignore # pragma: no cover
|
||||
from warnings import deprecated # type: ignore # pragma: no cover
|
||||
else:
|
||||
from typing_extensions import TypeVar, deprecated # type: ignore # pragma: no cover
|
||||
if sys.version_info >= (3, 11):
|
||||
from typing import Self, TypedDict # type: ignore # pragma: no cover
|
||||
else:
|
||||
from typing_extensions import Self, TypedDict # type: ignore # pragma: no cover
|
||||
|
||||
|
||||
logger = logging.getLogger("agent_framework.azure")
|
||||
|
||||
|
||||
# Type variable for options - allows typed Agent[OptionsT] returns
|
||||
# Default matches AzureAIClient's default options type
|
||||
OptionsCoT = TypeVar(
|
||||
"OptionsCoT",
|
||||
bound=TypedDict, # type: ignore[valid-type]
|
||||
default="AzureAIProjectAgentOptions",
|
||||
covariant=True,
|
||||
)
|
||||
|
||||
|
||||
@deprecated("AzureAIProjectAgentProvider is deprecated. Use FoundryAgent instead.")
|
||||
class AzureAIProjectAgentProvider(Generic[OptionsCoT]):
|
||||
"""Deprecated provider for Azure AI Agent Service (Responses API).
|
||||
|
||||
This provider is deprecated. Use ``FoundryAgent`` instead to connect to
|
||||
pre-configured agents in Foundry.
|
||||
|
||||
Examples:
|
||||
Using with explicit AIProjectClient:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from agent_framework.azure import AzureAIProjectAgentProvider
|
||||
from azure.ai.projects.aio import AIProjectClient
|
||||
from azure.identity.aio import DefaultAzureCredential
|
||||
|
||||
async with AIProjectClient(endpoint, credential) as client:
|
||||
provider = AzureAIProjectAgentProvider(client)
|
||||
agent = await provider.create_agent(
|
||||
name="MyAgent",
|
||||
model="gpt-4",
|
||||
instructions="You are a helpful assistant.",
|
||||
)
|
||||
response = await agent.run("Hello!")
|
||||
|
||||
Using with credential and endpoint (auto-creates client):
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from agent_framework.azure import AzureAIProjectAgentProvider
|
||||
from azure.identity.aio import DefaultAzureCredential
|
||||
|
||||
async with AzureAIProjectAgentProvider(credential=credential) as provider:
|
||||
agent = await provider.create_agent(
|
||||
name="MyAgent",
|
||||
model="gpt-4",
|
||||
instructions="You are a helpful assistant.",
|
||||
)
|
||||
response = await agent.run("Hello!")
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
project_client: AIProjectClient | None = None,
|
||||
*,
|
||||
project_endpoint: str | None = None,
|
||||
model: str | None = None,
|
||||
credential: AzureCredentialTypes | None = None,
|
||||
allow_preview: bool | None = None,
|
||||
env_file_path: str | None = None,
|
||||
env_file_encoding: str | None = None,
|
||||
) -> None:
|
||||
"""Initialize an Azure AI Project Agent Provider.
|
||||
|
||||
Args:
|
||||
project_client: An existing AIProjectClient to use. If not provided, one will be created.
|
||||
project_endpoint: The Azure AI Project endpoint URL.
|
||||
Can also be set via environment variable AZURE_AI_PROJECT_ENDPOINT.
|
||||
Ignored when a project_client is passed.
|
||||
model: The default model deployment name to use for agent creation.
|
||||
Can also be set via environment variable AZURE_AI_MODEL_DEPLOYMENT_NAME.
|
||||
credential: Azure credential for authentication. Accepts a TokenCredential,
|
||||
AsyncTokenCredential, or a callable token provider.
|
||||
Required when project_client is not provided.
|
||||
allow_preview: Enables preview opt-in on internally-created ``AIProjectClient``.
|
||||
env_file_path: Path to environment file for loading settings.
|
||||
env_file_encoding: Encoding of the environment file.
|
||||
|
||||
Raises:
|
||||
ValueError: If required parameters are missing or invalid.
|
||||
"""
|
||||
self._settings = load_settings(
|
||||
AzureAISettings,
|
||||
env_prefix="AZURE_AI_",
|
||||
project_endpoint=project_endpoint,
|
||||
model_deployment_name=model,
|
||||
env_file_path=env_file_path,
|
||||
env_file_encoding=env_file_encoding,
|
||||
)
|
||||
|
||||
# Track whether we should close client connection
|
||||
self._should_close_client = False
|
||||
|
||||
if project_client is None:
|
||||
resolved_endpoint = self._settings.get("project_endpoint")
|
||||
if not resolved_endpoint:
|
||||
raise ValueError(
|
||||
"Azure AI project endpoint is required. Set via 'project_endpoint' parameter "
|
||||
"or 'AZURE_AI_PROJECT_ENDPOINT' environment variable."
|
||||
)
|
||||
|
||||
if not credential:
|
||||
raise ValueError("Azure credential is required when project_client is not provided.")
|
||||
|
||||
project_client_kwargs: dict[str, Any] = {
|
||||
"endpoint": resolved_endpoint,
|
||||
"credential": credential, # type: ignore[arg-type]
|
||||
"user_agent": AGENT_FRAMEWORK_USER_AGENT,
|
||||
}
|
||||
if allow_preview is not None:
|
||||
project_client_kwargs["allow_preview"] = allow_preview
|
||||
project_client = AIProjectClient(**project_client_kwargs)
|
||||
self._should_close_client = True
|
||||
|
||||
self._project_client = project_client
|
||||
|
||||
async def create_agent(
|
||||
self,
|
||||
name: str,
|
||||
model: str | None = None,
|
||||
instructions: str | None = None,
|
||||
description: str | None = None,
|
||||
tools: ToolTypes | Callable[..., Any] | Sequence[ToolTypes | Callable[..., Any]] | None = None,
|
||||
default_options: OptionsCoT | None = None,
|
||||
middleware: Sequence[MiddlewareTypes] | None = None,
|
||||
context_providers: Sequence[BaseContextProvider] | None = None,
|
||||
) -> Agent[OptionsCoT]:
|
||||
"""Create a new agent on the Azure AI service and return a local Agent wrapper.
|
||||
|
||||
Args:
|
||||
name: The name of the agent to create.
|
||||
model: The model deployment name to use. Falls back to AZURE_AI_MODEL_DEPLOYMENT_NAME
|
||||
environment variable if not provided.
|
||||
instructions: Instructions for the agent.
|
||||
description: A description of the agent.
|
||||
tools: Tools to make available to the agent.
|
||||
default_options: A TypedDict containing default chat options for the agent.
|
||||
These options are applied to every run unless overridden.
|
||||
middleware: List of middleware to intercept agent and function invocations.
|
||||
context_providers: Context providers to include during agent invocation.
|
||||
|
||||
Returns:
|
||||
Agent: A Agent instance configured with the created agent.
|
||||
|
||||
Raises:
|
||||
ValueError: If required parameters are missing.
|
||||
"""
|
||||
# Resolve model from parameter or environment variable
|
||||
resolved_model = model or self._settings.get("model_deployment_name")
|
||||
if not resolved_model:
|
||||
raise ValueError(
|
||||
"Model deployment name is required. Provide 'model' parameter "
|
||||
"or set 'AZURE_AI_MODEL_DEPLOYMENT_NAME' environment variable."
|
||||
)
|
||||
|
||||
# Extract options from default_options if present
|
||||
opts: dict[str, Any] = dict(default_options) if default_options else {}
|
||||
response_format = opts.get("response_format")
|
||||
rai_config = opts.get("rai_config")
|
||||
reasoning = opts.get("reasoning")
|
||||
|
||||
args: dict[str, Any] = {"model": resolved_model}
|
||||
|
||||
if instructions:
|
||||
args["instructions"] = instructions
|
||||
if response_format and isinstance(response_format, (type, dict)):
|
||||
args["text"] = PromptAgentDefinitionTextOptions(
|
||||
format=create_text_format_config(response_format) # type: ignore[arg-type]
|
||||
)
|
||||
if rai_config:
|
||||
args["rai_config"] = rai_config
|
||||
if reasoning:
|
||||
args["reasoning"] = reasoning
|
||||
|
||||
# Normalize tools and separate MCP tools from other tools
|
||||
normalized_tools = normalize_tools(tools)
|
||||
mcp_tools: list[MCPTool] = []
|
||||
non_mcp_tools: list[FunctionTool | MutableMapping[str, Any]] = []
|
||||
|
||||
if normalized_tools:
|
||||
for tool in normalized_tools:
|
||||
if isinstance(tool, MCPTool):
|
||||
mcp_tools.append(tool)
|
||||
elif isinstance(tool, (FunctionTool, MutableMapping)):
|
||||
non_mcp_tools.append(tool) # type: ignore[reportUnknownArgumentType]
|
||||
|
||||
# Connect MCP tools and discover their functions BEFORE creating the agent
|
||||
# This is required because Azure AI Responses API doesn't accept tools at request time
|
||||
mcp_discovered_functions: list[FunctionTool] = []
|
||||
for mcp_tool in mcp_tools:
|
||||
if not mcp_tool.is_connected:
|
||||
await mcp_tool.connect()
|
||||
mcp_discovered_functions.extend(mcp_tool.functions)
|
||||
|
||||
# Combine non-MCP tools with discovered MCP functions for Azure AI
|
||||
all_tools_for_azure: list[FunctionTool | MutableMapping[str, Any]] = list(non_mcp_tools)
|
||||
all_tools_for_azure.extend(mcp_discovered_functions)
|
||||
|
||||
if all_tools_for_azure:
|
||||
args["tools"] = to_azure_ai_tools(all_tools_for_azure)
|
||||
|
||||
create_version_kwargs: dict[str, Any] = {
|
||||
"agent_name": name,
|
||||
"definition": PromptAgentDefinition(**args),
|
||||
"description": description,
|
||||
}
|
||||
|
||||
created_agent = await self._project_client.agents.create_version(**create_version_kwargs)
|
||||
|
||||
return self._to_chat_agent_from_details(
|
||||
created_agent,
|
||||
normalized_tools,
|
||||
default_options=default_options,
|
||||
middleware=middleware,
|
||||
context_providers=context_providers,
|
||||
)
|
||||
|
||||
async def get_agent(
|
||||
self,
|
||||
*,
|
||||
name: str | None = None,
|
||||
reference: Mapping[str, str | None] | None = None,
|
||||
tools: ToolTypes | Callable[..., Any] | Sequence[ToolTypes | Callable[..., Any]] | None = None,
|
||||
default_options: OptionsCoT | None = None,
|
||||
middleware: Sequence[MiddlewareTypes] | None = None,
|
||||
context_providers: Sequence[BaseContextProvider] | None = None,
|
||||
) -> Agent[OptionsCoT]:
|
||||
"""Retrieve an existing agent from the Azure AI service and return a local Agent wrapper.
|
||||
|
||||
You must provide either name or reference. Use `as_agent()` if you already have
|
||||
AgentVersionDetails and want to avoid an async call.
|
||||
|
||||
Args:
|
||||
name: The name of the agent to retrieve (fetches latest version).
|
||||
reference: Mapping containing the agent's ``name`` and optionally a specific ``version``.
|
||||
tools: Tools to make available to the agent. Required if the agent has function tools.
|
||||
default_options: A TypedDict containing default chat options for the agent.
|
||||
These options are applied to every run unless overridden.
|
||||
middleware: List of middleware to intercept agent and function invocations.
|
||||
context_providers: Context providers to include during agent invocation.
|
||||
|
||||
Returns:
|
||||
Agent: A Agent instance configured with the retrieved agent.
|
||||
|
||||
Raises:
|
||||
ValueError: If no identifier is provided or required tools are missing.
|
||||
"""
|
||||
existing_agent: AgentVersionDetails
|
||||
|
||||
reference_name = str(reference.get("name")) if reference and reference.get("name") else None
|
||||
reference_version = str(reference.get("version")) if reference and reference.get("version") else None
|
||||
|
||||
if reference_name and reference_version:
|
||||
# Fetch specific version
|
||||
existing_agent = await self._project_client.agents.get_version(
|
||||
agent_name=reference_name, agent_version=reference_version
|
||||
)
|
||||
elif agent_name := (reference_name if reference_name else name):
|
||||
# Fetch latest version
|
||||
details = await self._project_client.agents.get(agent_name=agent_name)
|
||||
existing_agent = details.versions.latest
|
||||
else:
|
||||
raise ValueError("Either name or reference must be provided to get an agent.")
|
||||
|
||||
if not isinstance(existing_agent.definition, PromptAgentDefinition):
|
||||
raise ValueError("Agent definition must be PromptAgentDefinition to get a Agent.")
|
||||
|
||||
# Validate that required function tools are provided
|
||||
self._validate_function_tools(existing_agent.definition.tools, tools)
|
||||
|
||||
return self._to_chat_agent_from_details(
|
||||
existing_agent,
|
||||
normalize_tools(tools),
|
||||
default_options=default_options,
|
||||
middleware=middleware,
|
||||
context_providers=context_providers,
|
||||
)
|
||||
|
||||
def as_agent(
|
||||
self,
|
||||
details: AgentVersionDetails,
|
||||
tools: ToolTypes | Callable[..., Any] | Sequence[ToolTypes | Callable[..., Any]] | None = None,
|
||||
default_options: OptionsCoT | None = None,
|
||||
middleware: Sequence[MiddlewareTypes] | None = None,
|
||||
context_providers: Sequence[BaseContextProvider] | None = None,
|
||||
) -> Agent[OptionsCoT]:
|
||||
"""Wrap an SDK agent version object into a Agent without making HTTP calls.
|
||||
|
||||
Use this when you already have an AgentVersionDetails from a previous API call.
|
||||
|
||||
Args:
|
||||
details: The AgentVersionDetails to wrap.
|
||||
tools: Tools to make available to the agent. Required if the agent has function tools.
|
||||
default_options: A TypedDict containing default chat options for the agent.
|
||||
These options are applied to every run unless overridden.
|
||||
middleware: List of middleware to intercept agent and function invocations.
|
||||
context_providers: Context providers to include during agent invocation.
|
||||
|
||||
Returns:
|
||||
Agent: A Agent instance configured with the agent version.
|
||||
|
||||
Raises:
|
||||
ValueError: If the agent definition is not a PromptAgentDefinition or required tools are missing.
|
||||
"""
|
||||
if not isinstance(details.definition, PromptAgentDefinition):
|
||||
raise ValueError("Agent definition must be PromptAgentDefinition to create a Agent.")
|
||||
|
||||
# Validate that required function tools are provided
|
||||
self._validate_function_tools(details.definition.tools, tools)
|
||||
|
||||
return self._to_chat_agent_from_details(
|
||||
details,
|
||||
normalize_tools(tools),
|
||||
default_options=default_options,
|
||||
middleware=middleware,
|
||||
context_providers=context_providers,
|
||||
)
|
||||
|
||||
def _to_chat_agent_from_details(
|
||||
self,
|
||||
details: AgentVersionDetails,
|
||||
provided_tools: Sequence[ToolTypes] | None = None,
|
||||
default_options: OptionsCoT | None = None,
|
||||
middleware: Sequence[MiddlewareTypes] | None = None,
|
||||
context_providers: Sequence[BaseContextProvider] | None = None,
|
||||
) -> Agent[OptionsCoT]:
|
||||
"""Create a Agent from an AgentVersionDetails.
|
||||
|
||||
Args:
|
||||
details: The AgentVersionDetails containing the agent definition.
|
||||
provided_tools: User-provided tools (including function implementations).
|
||||
These are merged with hosted tools from the definition.
|
||||
default_options: A TypedDict containing default chat options for the agent.
|
||||
These options are applied to every run unless overridden.
|
||||
middleware: List of middleware to intercept agent and function invocations.
|
||||
context_providers: Context providers to include during agent invocation.
|
||||
"""
|
||||
if not isinstance(details.definition, PromptAgentDefinition):
|
||||
raise ValueError("Agent definition must be PromptAgentDefinition to get a Agent.")
|
||||
|
||||
client = AzureAIClient( # pyright: ignore[reportDeprecated]
|
||||
project_client=self._project_client,
|
||||
agent_name=details.name,
|
||||
agent_version=details.version,
|
||||
agent_description=details.description,
|
||||
model_deployment_name=details.definition.model,
|
||||
)
|
||||
|
||||
# Merge tools: hosted tools from definition + user-provided function tools
|
||||
# from_azure_ai_tools converts hosted tools (MCP, code interpreter, file search, web search)
|
||||
# but function tools need the actual implementations from provided_tools
|
||||
merged_tools = self._merge_tools(details.definition.tools, provided_tools)
|
||||
merged_default_options: dict[str, Any] = dict(default_options) if default_options is not None else {}
|
||||
merged_default_options.setdefault("model_id", details.definition.model)
|
||||
|
||||
return Agent( # type: ignore[return-value]
|
||||
client=client,
|
||||
id=details.id,
|
||||
name=details.name,
|
||||
description=details.description,
|
||||
instructions=details.definition.instructions,
|
||||
tools=merged_tools,
|
||||
default_options=cast(Any, merged_default_options),
|
||||
middleware=middleware,
|
||||
context_providers=context_providers,
|
||||
)
|
||||
|
||||
def _merge_tools(
|
||||
self,
|
||||
definition_tools: Sequence[Any] | None,
|
||||
provided_tools: Sequence[ToolTypes] | None,
|
||||
) -> list[ToolTypes]:
|
||||
"""Merge hosted tools from definition with user-provided function tools.
|
||||
|
||||
Args:
|
||||
definition_tools: Tools from the agent definition (Azure AI format).
|
||||
provided_tools: User-provided tools (Agent Framework format), including function implementations.
|
||||
|
||||
Returns:
|
||||
Combined list of tools for the Agent.
|
||||
"""
|
||||
merged: list[ToolTypes] = []
|
||||
|
||||
# Convert hosted tools from definition (MCP, code interpreter, file search, web search)
|
||||
# Function tools from the definition are skipped - we use user-provided implementations instead
|
||||
hosted_tools = from_azure_ai_tools(definition_tools)
|
||||
for hosted_tool in hosted_tools:
|
||||
# Skip function tool dicts - they don't have implementations
|
||||
if isinstance(hosted_tool, dict) and hosted_tool.get("type") == "function":
|
||||
continue
|
||||
merged.append(hosted_tool)
|
||||
|
||||
# Add user-provided function tools and MCP tools
|
||||
if provided_tools:
|
||||
for provided_tool in provided_tools:
|
||||
# FunctionTool - has implementation for function calling
|
||||
# MCPTool - Agent handles MCP connection and tool discovery at runtime
|
||||
if isinstance(provided_tool, (FunctionTool, MCPTool)):
|
||||
merged.append(provided_tool) # type: ignore[reportUnknownArgumentType]
|
||||
|
||||
return merged
|
||||
|
||||
def _validate_function_tools(
|
||||
self,
|
||||
agent_tools: Sequence[Any] | None,
|
||||
provided_tools: ToolTypes | Callable[..., Any] | Sequence[ToolTypes | Callable[..., Any]] | None,
|
||||
) -> None:
|
||||
"""Validate that required function tools are provided."""
|
||||
# Normalize and validate function tools
|
||||
normalized_tools = normalize_tools(provided_tools)
|
||||
tool_names = {tool.name for tool in normalized_tools if isinstance(tool, FunctionTool)}
|
||||
|
||||
# If function tools exist in agent definition but were not provided,
|
||||
# we need to raise an error, as it won't be possible to invoke the function.
|
||||
missing_tools = [
|
||||
tool.name
|
||||
for tool in (agent_tools or [])
|
||||
if isinstance(tool, AzureFunctionTool) and tool.name not in tool_names
|
||||
]
|
||||
|
||||
if missing_tools:
|
||||
raise ValueError(
|
||||
f"The following prompt agent definition required tools were not provided: {', '.join(missing_tools)}"
|
||||
)
|
||||
|
||||
async def __aenter__(self) -> Self:
|
||||
"""Async context manager entry."""
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: Any) -> None:
|
||||
"""Async context manager exit."""
|
||||
await self.close()
|
||||
|
||||
async def close(self) -> None:
|
||||
"""Close the provider and release resources.
|
||||
|
||||
Only closes the underlying AIProjectClient if it was created by this provider.
|
||||
"""
|
||||
if self._should_close_client:
|
||||
await self._project_client.close()
|
||||
@@ -2,45 +2,13 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import sys
|
||||
import warnings
|
||||
from collections.abc import Mapping, MutableMapping, Sequence
|
||||
from typing import Any, cast
|
||||
|
||||
from agent_framework import (
|
||||
Content,
|
||||
FunctionTool,
|
||||
)
|
||||
from agent_framework.exceptions import IntegrationInvalidRequestException
|
||||
from azure.ai.agents.models import (
|
||||
CodeInterpreterToolDefinition,
|
||||
ToolDefinition,
|
||||
)
|
||||
from azure.ai.projects.models import (
|
||||
CodeInterpreterTool,
|
||||
MCPTool,
|
||||
TextResponseFormatJsonObject,
|
||||
TextResponseFormatJsonSchema,
|
||||
TextResponseFormatText,
|
||||
Tool,
|
||||
WebSearchPreviewTool,
|
||||
)
|
||||
from azure.ai.projects.models import (
|
||||
FileSearchTool as ProjectsFileSearchTool,
|
||||
)
|
||||
from azure.ai.projects.models import (
|
||||
FunctionTool as AzureFunctionTool,
|
||||
)
|
||||
from pydantic import BaseModel
|
||||
|
||||
if sys.version_info >= (3, 11):
|
||||
from typing import TypedDict # pragma: no cover
|
||||
else:
|
||||
from typing_extensions import TypedDict # type: ignore # pragma: no cover
|
||||
|
||||
logger = logging.getLogger("agent_framework.azure")
|
||||
|
||||
|
||||
class AzureAISettings(TypedDict, total=False):
|
||||
"""Azure AI Project settings.
|
||||
@@ -78,518 +46,3 @@ class AzureAISettings(TypedDict, total=False):
|
||||
|
||||
project_endpoint: str | None
|
||||
model_deployment_name: str | None
|
||||
|
||||
|
||||
def _extract_project_connection_id(additional_properties: Mapping[str, Any] | None) -> str | None:
|
||||
"""Extract project_connection_id from tool additional_properties.
|
||||
|
||||
Checks for both direct 'project_connection_id' key (programmatic usage)
|
||||
and 'connection.name' structure (declarative/YAML usage).
|
||||
|
||||
Args:
|
||||
additional_properties: The additional_properties dict from a tool.
|
||||
|
||||
Returns:
|
||||
The project_connection_id if found, None otherwise.
|
||||
"""
|
||||
if not additional_properties:
|
||||
return None
|
||||
|
||||
# Check for direct project_connection_id (programmatic usage)
|
||||
|
||||
if (proj_conn_id := additional_properties.get("project_connection_id")) and isinstance(proj_conn_id, str):
|
||||
return proj_conn_id # type: ignore[no-any-return]
|
||||
|
||||
# Check for connection.name structure (declarative/YAML usage)
|
||||
if (
|
||||
(connection := additional_properties.get("connection"))
|
||||
and isinstance(connection, Mapping)
|
||||
and (name := connection.get("name")) # type: ignore
|
||||
and isinstance(name, str)
|
||||
):
|
||||
return name # type: ignore[no-any-return]
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def resolve_file_ids(file_ids: Sequence[str | Content] | None) -> list[str] | None:
|
||||
"""Resolve a list of file ID values that may include Content objects.
|
||||
|
||||
Accepts plain strings and Content objects with type "hosted_file", extracting
|
||||
the file_id from each. This enables users to pass Content.from_hosted_file()
|
||||
alongside plain file ID strings.
|
||||
|
||||
Args:
|
||||
file_ids: Sequence of file ID strings or Content objects, or None.
|
||||
|
||||
Returns:
|
||||
A list of resolved file ID strings, or None if input is None or empty.
|
||||
|
||||
Raises:
|
||||
ValueError: If a Content object has an unsupported type (not "hosted_file").
|
||||
"""
|
||||
if not file_ids:
|
||||
return None
|
||||
|
||||
resolved: list[str] = []
|
||||
for item in file_ids:
|
||||
if isinstance(item, str):
|
||||
if not item:
|
||||
raise ValueError("file_ids must not contain empty strings.")
|
||||
resolved.append(item)
|
||||
elif isinstance(item, Content):
|
||||
if item.type != "hosted_file":
|
||||
raise ValueError(
|
||||
f"Unsupported Content type '{item.type}' for code interpreter file_ids. "
|
||||
"Only Content.from_hosted_file() is supported."
|
||||
)
|
||||
if item.file_id is None:
|
||||
raise ValueError(
|
||||
"Content.from_hosted_file() item is missing a file_id. "
|
||||
"Ensure the Content object has a valid file_id before using it in file_ids."
|
||||
)
|
||||
resolved.append(item.file_id)
|
||||
|
||||
return resolved if resolved else None
|
||||
|
||||
|
||||
def to_azure_ai_agent_tools(
|
||||
tools: Sequence[FunctionTool | MutableMapping[str, Any]] | None,
|
||||
run_options: dict[str, Any] | None = None,
|
||||
) -> list[ToolDefinition | dict[str, Any]]:
|
||||
"""Convert Agent Framework tools to Azure AI V1 SDK tool definitions.
|
||||
|
||||
.. deprecated::
|
||||
This function is deprecated and will be removed in a future release.
|
||||
Use :func:`to_azure_ai_tools` instead for the V2 (Projects/Responses) API.
|
||||
|
||||
Handles FunctionTool instances and dict-based tools from static factory methods.
|
||||
|
||||
Args:
|
||||
tools: Sequence of Agent Framework tools to convert.
|
||||
run_options: Optional dict with run options.
|
||||
|
||||
Returns:
|
||||
List of Azure AI V1 SDK tool definitions.
|
||||
|
||||
Raises:
|
||||
ValueError: If tool configuration is invalid.
|
||||
"""
|
||||
warnings.warn(
|
||||
"to_azure_ai_agent_tools() is deprecated and will be removed in a future release; "
|
||||
"use to_azure_ai_tools() instead for the V2 (Projects/Responses) API.",
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
if not tools:
|
||||
return []
|
||||
|
||||
tool_definitions: list[ToolDefinition | dict[str, Any]] = []
|
||||
for tool in tools:
|
||||
if isinstance(tool, FunctionTool):
|
||||
tool_definitions.append(tool.to_json_schema_spec()) # type: ignore[reportUnknownArgumentType]
|
||||
elif isinstance(tool, ToolDefinition):
|
||||
# Pass through ToolDefinition subclasses unchanged (includes CodeInterpreterToolDefinition, etc.)
|
||||
tool_definitions.append(tool)
|
||||
elif hasattr(tool, "definitions") and not isinstance(tool, (dict, MutableMapping)):
|
||||
# SDK Tool wrappers (McpTool, FileSearchTool, BingGroundingTool, etc.)
|
||||
tool_definitions.extend(tool.definitions)
|
||||
# Handle tool resources (MCP resources handled separately)
|
||||
if (
|
||||
run_options is not None
|
||||
and hasattr(tool, "resources")
|
||||
and tool.resources
|
||||
and "mcp" not in tool.resources
|
||||
):
|
||||
run_options.setdefault("tool_resources", {})
|
||||
if isinstance(tool.resources, Mapping):
|
||||
run_options["tool_resources"].update(tool.resources)
|
||||
elif isinstance(tool, (dict, MutableMapping)):
|
||||
# Handle dict-based tools - pass through directly
|
||||
tool_dict = tool if isinstance(tool, dict) else dict(tool)
|
||||
tool_definitions.append(tool_dict)
|
||||
else:
|
||||
# Pass through other types unchanged
|
||||
tool_definitions.append(tool)
|
||||
return tool_definitions
|
||||
|
||||
|
||||
def from_azure_ai_agent_tools(
|
||||
tools: Sequence[ToolDefinition | dict[str, Any]] | None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Convert Azure AI V1 SDK tool definitions to dict-based tools.
|
||||
|
||||
.. deprecated::
|
||||
This function is deprecated and will be removed in a future release.
|
||||
Use :func:`from_azure_ai_tools` instead for the V2 (Projects/Responses) API.
|
||||
|
||||
Args:
|
||||
tools: Sequence of Azure AI V1 SDK tool definitions.
|
||||
|
||||
Returns:
|
||||
List of dict-based tool definitions.
|
||||
"""
|
||||
warnings.warn(
|
||||
"from_azure_ai_agent_tools() is deprecated and will be removed in a future release; "
|
||||
"use from_azure_ai_tools() instead for the V2 (Projects/Responses) API.",
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
if not tools:
|
||||
return []
|
||||
|
||||
result: list[dict[str, Any]] = []
|
||||
for tool in tools:
|
||||
# Handle SDK objects
|
||||
if isinstance(tool, CodeInterpreterToolDefinition):
|
||||
result.append({"type": "code_interpreter"})
|
||||
elif isinstance(tool, dict):
|
||||
# Handle dict format
|
||||
converted = _convert_dict_tool(tool)
|
||||
if converted is not None:
|
||||
result.append(converted)
|
||||
elif hasattr(tool, "type"):
|
||||
# Handle other SDK objects by type
|
||||
converted = _convert_sdk_tool(tool)
|
||||
if converted is not None:
|
||||
result.append(converted)
|
||||
return result
|
||||
|
||||
|
||||
def _convert_dict_tool(tool: dict[str, Any]) -> dict[str, Any] | None:
|
||||
"""Convert a dict-format Azure AI tool to dict-based tool format."""
|
||||
tool_type = tool.get("type")
|
||||
|
||||
if tool_type == "code_interpreter":
|
||||
return {"type": "code_interpreter"}
|
||||
|
||||
if tool_type == "file_search":
|
||||
file_search_config = tool.get("file_search", {})
|
||||
vector_store_ids = file_search_config.get("vector_store_ids", [])
|
||||
return {"type": "file_search", "vector_store_ids": vector_store_ids}
|
||||
|
||||
if tool_type == "bing_grounding":
|
||||
bing_config = tool.get("bing_grounding", {})
|
||||
connection_id = bing_config.get("connection_id")
|
||||
return {"type": "bing_grounding", "connection_id": connection_id} if connection_id else None
|
||||
|
||||
if tool_type == "bing_custom_search":
|
||||
bing_config = tool.get("bing_custom_search", {})
|
||||
connection_id = bing_config.get("connection_id")
|
||||
instance_name = bing_config.get("instance_name")
|
||||
# Only return if both required fields are present
|
||||
if connection_id and instance_name:
|
||||
return {
|
||||
"type": "bing_custom_search",
|
||||
"connection_id": connection_id,
|
||||
"instance_name": instance_name,
|
||||
}
|
||||
return None
|
||||
|
||||
if tool_type == "mcp":
|
||||
# MCP tools are defined on the Azure agent, no local handling needed
|
||||
# Azure may not return full server_url, so skip conversion
|
||||
return None
|
||||
|
||||
if tool_type == "function":
|
||||
# Function tools are returned as dicts - users must provide implementations
|
||||
return tool
|
||||
|
||||
# Unknown tool type - pass through
|
||||
return tool
|
||||
|
||||
|
||||
def _convert_sdk_tool(tool: ToolDefinition) -> dict[str, Any] | None:
|
||||
"""Convert an SDK-object Azure AI tool to dict-based tool format."""
|
||||
tool_type = getattr(tool, "type", None)
|
||||
|
||||
if tool_type == "code_interpreter":
|
||||
return {"type": "code_interpreter"}
|
||||
|
||||
if tool_type == "file_search":
|
||||
file_search_config = getattr(tool, "file_search", None)
|
||||
vector_store_ids = getattr(file_search_config, "vector_store_ids", []) if file_search_config else []
|
||||
return {"type": "file_search", "vector_store_ids": vector_store_ids}
|
||||
|
||||
if tool_type == "bing_grounding":
|
||||
bing_config = getattr(tool, "bing_grounding", None)
|
||||
connection_id = getattr(bing_config, "connection_id", None) if bing_config else None
|
||||
return {"type": "bing_grounding", "connection_id": connection_id} if connection_id else None
|
||||
|
||||
if tool_type == "bing_custom_search":
|
||||
bing_config = getattr(tool, "bing_custom_search", None)
|
||||
connection_id = getattr(bing_config, "connection_id", None) if bing_config else None
|
||||
instance_name = getattr(bing_config, "instance_name", None) if bing_config else None
|
||||
# Only return if both required fields are present
|
||||
if connection_id and instance_name:
|
||||
return {
|
||||
"type": "bing_custom_search",
|
||||
"connection_id": connection_id,
|
||||
"instance_name": instance_name,
|
||||
}
|
||||
return None
|
||||
|
||||
if tool_type == "mcp":
|
||||
# MCP tools are defined on the Azure agent, no local handling needed
|
||||
# Azure may not return full server_url, so skip conversion
|
||||
return None
|
||||
|
||||
if tool_type == "function":
|
||||
# Function tools from SDK don't have implementations - skip
|
||||
return None
|
||||
|
||||
# Unknown tool type - convert to dict if possible
|
||||
if hasattr(tool, "as_dict"):
|
||||
return tool.as_dict() # type: ignore[union-attr]
|
||||
return {"type": tool_type} if tool_type else {}
|
||||
|
||||
|
||||
def from_azure_ai_tools(tools: Sequence[Tool | dict[str, Any]] | None) -> list[dict[str, Any]]:
|
||||
"""Parses and converts a sequence of Azure AI tools into dict-based tools.
|
||||
|
||||
Args:
|
||||
tools: A sequence of tool objects or dictionaries
|
||||
defining the tools to be parsed. Can be None.
|
||||
|
||||
Returns:
|
||||
list[dict[str, Any]]: A list of dict-based tool definitions.
|
||||
"""
|
||||
agent_tools: list[dict[str, Any]] = []
|
||||
if not tools:
|
||||
return agent_tools
|
||||
for tool in tools:
|
||||
# Handle raw dictionary tools
|
||||
tool_dict = tool if isinstance(tool, dict) else dict(tool)
|
||||
tool_type = tool_dict.get("type")
|
||||
|
||||
if tool_type == "mcp":
|
||||
mcp_tool = cast(MCPTool, tool_dict)
|
||||
result: dict[str, Any] = {
|
||||
"type": "mcp",
|
||||
"server_label": mcp_tool.get("server_label", ""),
|
||||
"server_url": mcp_tool.get("server_url", ""),
|
||||
}
|
||||
if description := mcp_tool.get("server_description"):
|
||||
result["server_description"] = description
|
||||
if headers := mcp_tool.get("headers"):
|
||||
result["headers"] = headers
|
||||
if allowed_tools := mcp_tool.get("allowed_tools"):
|
||||
result["allowed_tools"] = allowed_tools
|
||||
if require_approval := mcp_tool.get("require_approval"):
|
||||
result["require_approval"] = require_approval
|
||||
if project_connection_id := mcp_tool.get("project_connection_id"):
|
||||
result["project_connection_id"] = project_connection_id
|
||||
agent_tools.append(result)
|
||||
elif tool_type == "code_interpreter":
|
||||
ci_tool = cast(CodeInterpreterTool, tool_dict)
|
||||
container = ci_tool.get("container", {})
|
||||
result = {"type": "code_interpreter"}
|
||||
if "file_ids" in container:
|
||||
result["file_ids"] = container["file_ids"]
|
||||
agent_tools.append(result)
|
||||
elif tool_type == "file_search":
|
||||
fs_tool = cast(ProjectsFileSearchTool, tool_dict)
|
||||
result = {"type": "file_search"}
|
||||
if "vector_store_ids" in fs_tool:
|
||||
result["vector_store_ids"] = fs_tool["vector_store_ids"]
|
||||
if max_results := fs_tool.get("max_num_results"):
|
||||
result["max_num_results"] = max_results
|
||||
agent_tools.append(result)
|
||||
elif tool_type == "web_search_preview":
|
||||
ws_tool = cast(WebSearchPreviewTool, tool_dict)
|
||||
result = {"type": "web_search_preview"}
|
||||
if user_location := ws_tool.get("user_location"):
|
||||
result["user_location"] = {
|
||||
"city": user_location.get("city"),
|
||||
"country": user_location.get("country"),
|
||||
"region": user_location.get("region"),
|
||||
"timezone": user_location.get("timezone"),
|
||||
}
|
||||
agent_tools.append(result)
|
||||
else:
|
||||
agent_tools.append(tool_dict)
|
||||
return agent_tools
|
||||
|
||||
|
||||
def to_azure_ai_tools(
|
||||
tools: Sequence[FunctionTool | MutableMapping[str, Any] | Tool] | None,
|
||||
) -> list[Tool | dict[str, Any]]:
|
||||
"""Converts Agent Framework tools into Azure AI compatible tools.
|
||||
|
||||
Handles FunctionTool instances and passes through SDK Tool types directly.
|
||||
|
||||
Args:
|
||||
tools: A sequence of Agent Framework tool objects, SDK Tool types, or dictionaries
|
||||
defining the tools to be converted. Can be None.
|
||||
|
||||
Returns:
|
||||
list[Tool | dict[str, Any]]: A list of converted tools compatible with Azure AI.
|
||||
"""
|
||||
azure_tools: list[Tool | dict[str, Any]] = []
|
||||
if not tools:
|
||||
return azure_tools
|
||||
|
||||
for tool in tools:
|
||||
if isinstance(tool, FunctionTool):
|
||||
params = tool.parameters()
|
||||
params["additionalProperties"] = False
|
||||
azure_tools.append(
|
||||
AzureFunctionTool(
|
||||
name=tool.name,
|
||||
parameters=params,
|
||||
strict=False,
|
||||
description=tool.description,
|
||||
)
|
||||
)
|
||||
elif isinstance(tool, Tool):
|
||||
# Pass through SDK Tool types directly (CodeInterpreterTool, FileSearchTool, etc.)
|
||||
azure_tools.append(tool)
|
||||
elif isinstance(tool, MutableMapping):
|
||||
# Convert mutable mappings into plain dicts for stable typing.
|
||||
tool_dict: dict[str, Any] = dict(tool)
|
||||
if tool_dict.get("type") == "mcp":
|
||||
azure_tools.append(_prepare_mcp_tool_dict_for_azure_ai(tool_dict))
|
||||
else:
|
||||
azure_tools.append(tool_dict)
|
||||
else:
|
||||
# Pass through any other supported tool objects unchanged.
|
||||
azure_tools.append(tool)
|
||||
|
||||
return azure_tools
|
||||
|
||||
|
||||
def _prepare_mcp_tool_dict_for_azure_ai(tool_dict: dict[str, Any]) -> MCPTool:
|
||||
"""Convert dict-based MCP tool to Azure AI MCPTool format.
|
||||
|
||||
Args:
|
||||
tool_dict: The dict-based MCP tool configuration.
|
||||
|
||||
Returns:
|
||||
MCPTool: The converted Azure AI MCPTool.
|
||||
"""
|
||||
server_label = tool_dict.get("server_label", "")
|
||||
server_url = tool_dict.get("server_url", "")
|
||||
mcp: MCPTool = MCPTool(server_label=server_label, server_url=server_url)
|
||||
|
||||
if description := tool_dict.get("server_description"):
|
||||
mcp["server_description"] = description
|
||||
|
||||
# Check for project_connection_id
|
||||
project_connection_id = tool_dict.get("project_connection_id")
|
||||
if not isinstance(project_connection_id, str):
|
||||
additional_properties = tool_dict.get("additional_properties")
|
||||
project_connection_id = (
|
||||
_extract_project_connection_id(additional_properties) # pyright: ignore[reportUnknownArgumentType]
|
||||
if isinstance(additional_properties, Mapping)
|
||||
else None
|
||||
)
|
||||
|
||||
if project_connection_id:
|
||||
mcp["project_connection_id"] = project_connection_id
|
||||
elif headers := tool_dict.get("headers"):
|
||||
mcp["headers"] = headers
|
||||
|
||||
if allowed_tools := tool_dict.get("allowed_tools"):
|
||||
mcp["allowed_tools"] = list(allowed_tools)
|
||||
|
||||
if require_approval := tool_dict.get("require_approval"):
|
||||
mcp["require_approval"] = require_approval
|
||||
|
||||
return mcp
|
||||
|
||||
|
||||
def create_text_format_config(
|
||||
response_format: type[BaseModel] | Mapping[str, Any],
|
||||
) -> TextResponseFormatJsonSchema | TextResponseFormatJsonObject | TextResponseFormatText:
|
||||
"""Convert response_format into Azure text format configuration."""
|
||||
if isinstance(response_format, type) and issubclass(response_format, BaseModel):
|
||||
schema = response_format.model_json_schema()
|
||||
# Ensure additionalProperties is explicitly false to satisfy Azure validation
|
||||
if isinstance(schema, dict):
|
||||
schema.setdefault("additionalProperties", False)
|
||||
return TextResponseFormatJsonSchema(
|
||||
name=response_format.__name__,
|
||||
schema=schema,
|
||||
strict=True,
|
||||
)
|
||||
|
||||
if isinstance(response_format, Mapping):
|
||||
format_config = _convert_response_format(response_format)
|
||||
format_type = format_config.get("type")
|
||||
if format_type == "json_schema":
|
||||
# Ensure schema includes additionalProperties=False to satisfy Azure validation
|
||||
schema = dict(format_config.get("schema", {})) # type: ignore[assignment]
|
||||
schema.setdefault("additionalProperties", False)
|
||||
config_kwargs: dict[str, Any] = {
|
||||
"name": format_config.get("name") or "response",
|
||||
"schema": schema,
|
||||
}
|
||||
if "strict" in format_config:
|
||||
config_kwargs["strict"] = format_config["strict"]
|
||||
if "description" in format_config:
|
||||
config_kwargs["description"] = format_config["description"]
|
||||
return TextResponseFormatJsonSchema(**config_kwargs)
|
||||
if format_type == "json_object":
|
||||
return TextResponseFormatJsonObject()
|
||||
if format_type == "text":
|
||||
return TextResponseFormatText()
|
||||
|
||||
raise IntegrationInvalidRequestException("response_format must be a Pydantic model or mapping.")
|
||||
|
||||
|
||||
def _convert_response_format(response_format: Mapping[str, Any]) -> dict[str, Any]:
|
||||
"""Convert Chat style response_format into Responses text format config."""
|
||||
if "format" in response_format and isinstance(response_format["format"], Mapping):
|
||||
return dict(cast("Mapping[str, Any]", response_format["format"]))
|
||||
|
||||
format_type = response_format.get("type")
|
||||
if format_type == "json_schema":
|
||||
schema_section = response_format.get("json_schema", response_format)
|
||||
if not isinstance(schema_section, Mapping):
|
||||
raise IntegrationInvalidRequestException("json_schema response_format must be a mapping.")
|
||||
schema_section_typed = cast("Mapping[str, Any]", schema_section)
|
||||
schema: Any = schema_section_typed.get("schema")
|
||||
if schema is None:
|
||||
raise IntegrationInvalidRequestException("json_schema response_format requires a schema.")
|
||||
name: str = str(
|
||||
schema_section_typed.get("name")
|
||||
or schema_section_typed.get("title")
|
||||
or (cast("Mapping[str, Any]", schema).get("title") if isinstance(schema, Mapping) else None)
|
||||
or "response"
|
||||
)
|
||||
format_config: dict[str, Any] = {
|
||||
"type": "json_schema",
|
||||
"name": name,
|
||||
"schema": schema,
|
||||
}
|
||||
if "strict" in schema_section:
|
||||
format_config["strict"] = schema_section["strict"]
|
||||
if "description" in schema_section and schema_section["description"] is not None:
|
||||
format_config["description"] = schema_section["description"]
|
||||
return format_config
|
||||
|
||||
if format_type in {"json_object", "text"}:
|
||||
return {"type": format_type}
|
||||
|
||||
# Handle raw JSON schemas (e.g. {"type": "object", "properties": {...}})
|
||||
# by wrapping them in the expected json_schema envelope.
|
||||
# Detect by checking for JSON Schema primitive types or known schema keywords.
|
||||
json_schema_keywords = {"properties", "anyOf", "oneOf", "allOf", "$ref", "$defs"}
|
||||
json_schema_primitive_types = {"object", "array", "string", "number", "integer", "boolean", "null"}
|
||||
if format_type in json_schema_primitive_types or (
|
||||
format_type is None and any(k in response_format for k in json_schema_keywords)
|
||||
):
|
||||
schema = dict(response_format)
|
||||
if schema.get("type") == "object" and "additionalProperties" not in schema:
|
||||
schema["additionalProperties"] = False
|
||||
# Pop title from schema since OpenAI strict mode rejects unknown keys;
|
||||
# use it as the schema name in the envelope instead.
|
||||
name = str(schema.pop("title", None) or "response")
|
||||
return {
|
||||
"type": "json_schema",
|
||||
"name": name,
|
||||
"schema": schema,
|
||||
"strict": True,
|
||||
}
|
||||
|
||||
raise IntegrationInvalidRequestException("Unsupported response_format provided for Azure AI client.")
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
from typing import Any
|
||||
|
||||
from agent_framework import Message
|
||||
from pytest import fixture
|
||||
|
||||
|
||||
# region: Connector Settings fixtures
|
||||
@fixture
|
||||
def exclude_list(request: Any) -> list[str]:
|
||||
"""Fixture that returns a list of environment variables to exclude."""
|
||||
return request.param if hasattr(request, "param") else []
|
||||
|
||||
|
||||
@fixture
|
||||
def override_env_param_dict(request: Any) -> dict[str, str]:
|
||||
"""Fixture that returns a dict of environment variables to override."""
|
||||
return request.param if hasattr(request, "param") else {}
|
||||
|
||||
|
||||
# These two fixtures are used for multiple things, also non-connector tests
|
||||
@fixture()
|
||||
def azure_openai_unit_test_env(monkeypatch, exclude_list, override_env_param_dict): # type: ignore
|
||||
"""Fixture to set environment variables for AzureOpenAISettings."""
|
||||
|
||||
if exclude_list is None:
|
||||
exclude_list = []
|
||||
|
||||
if override_env_param_dict is None:
|
||||
override_env_param_dict = {}
|
||||
|
||||
env_vars = {
|
||||
"AZURE_OPENAI_ENDPOINT": "https://test-endpoint.com",
|
||||
"AZURE_OPENAI_CHAT_DEPLOYMENT_NAME": "test_chat_deployment",
|
||||
"AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME": "test_chat_deployment",
|
||||
"AZURE_OPENAI_TEXT_DEPLOYMENT_NAME": "test_text_deployment",
|
||||
"AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME": "test_embedding_deployment",
|
||||
"AZURE_OPENAI_TEXT_TO_IMAGE_DEPLOYMENT_NAME": "test_text_to_image_deployment",
|
||||
"AZURE_OPENAI_AUDIO_TO_TEXT_DEPLOYMENT_NAME": "test_audio_to_text_deployment",
|
||||
"AZURE_OPENAI_TEXT_TO_AUDIO_DEPLOYMENT_NAME": "test_text_to_audio_deployment",
|
||||
"AZURE_OPENAI_REALTIME_DEPLOYMENT_NAME": "test_realtime_deployment",
|
||||
"AZURE_OPENAI_API_KEY": "test_api_key",
|
||||
"AZURE_OPENAI_API_VERSION": "2023-03-15-preview",
|
||||
"AZURE_OPENAI_BASE_URL": "https://test_text_deployment.test-base-url.com",
|
||||
"AZURE_OPENAI_TOKEN_ENDPOINT": "https://test-token-endpoint.com",
|
||||
}
|
||||
|
||||
env_vars.update(override_env_param_dict) # type: ignore
|
||||
|
||||
for key, value in env_vars.items():
|
||||
if key in exclude_list:
|
||||
monkeypatch.delenv(key, raising=False) # type: ignore
|
||||
continue
|
||||
monkeypatch.setenv(key, value) # type: ignore
|
||||
|
||||
return env_vars
|
||||
|
||||
|
||||
@fixture(scope="function")
|
||||
def chat_history() -> list[Message]:
|
||||
return []
|
||||
@@ -1,409 +0,0 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
from typing import Annotated
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from agent_framework import (
|
||||
SupportsChatGetResponse,
|
||||
tool,
|
||||
)
|
||||
from agent_framework._settings import SecretString
|
||||
from agent_framework.azure import AzureOpenAIAssistantsClient
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
def create_test_azure_assistants_client(
|
||||
mock_async_azure_openai: MagicMock,
|
||||
deployment_name: str | None = None,
|
||||
assistant_id: str | None = None,
|
||||
assistant_name: str | None = None,
|
||||
thread_id: str | None = None,
|
||||
should_delete_assistant: bool = False,
|
||||
) -> AzureOpenAIAssistantsClient:
|
||||
"""Helper function to create AzureOpenAIAssistantsClient instances for testing."""
|
||||
client = AzureOpenAIAssistantsClient(
|
||||
deployment_name=deployment_name or "test_chat_deployment",
|
||||
assistant_id=assistant_id,
|
||||
assistant_name=assistant_name,
|
||||
thread_id=thread_id,
|
||||
api_key="test-api-key",
|
||||
endpoint="https://test-endpoint.com",
|
||||
async_client=mock_async_azure_openai,
|
||||
)
|
||||
# Set the _should_delete_assistant flag directly if needed
|
||||
if should_delete_assistant:
|
||||
object.__setattr__(client, "_should_delete_assistant", True)
|
||||
return client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_async_azure_openai() -> MagicMock:
|
||||
"""Mock AsyncAzureOpenAI client."""
|
||||
mock_client = MagicMock()
|
||||
|
||||
# Mock beta.assistants
|
||||
mock_client.beta.assistants.create = AsyncMock(return_value=MagicMock(id="test-assistant-id"))
|
||||
mock_client.beta.assistants.delete = AsyncMock()
|
||||
|
||||
# Mock beta.threads
|
||||
mock_client.beta.threads.create = AsyncMock(return_value=MagicMock(id="test-thread-id"))
|
||||
mock_client.beta.threads.delete = AsyncMock()
|
||||
|
||||
# Mock beta.threads.runs
|
||||
mock_client.beta.threads.runs.create = AsyncMock(return_value=MagicMock(id="test-run-id"))
|
||||
mock_client.beta.threads.runs.retrieve = AsyncMock()
|
||||
mock_client.beta.threads.runs.submit_tool_outputs = AsyncMock()
|
||||
|
||||
# Mock beta.threads.messages
|
||||
mock_client.beta.threads.messages.create = AsyncMock()
|
||||
mock_client.beta.threads.messages.list = AsyncMock(return_value=MagicMock(data=[]))
|
||||
|
||||
return mock_client
|
||||
|
||||
|
||||
def test_azure_assistants_client_init_with_client(mock_async_azure_openai: MagicMock) -> None:
|
||||
"""Test AzureOpenAIAssistantsClient initialization with existing client."""
|
||||
client = create_test_azure_assistants_client(
|
||||
mock_async_azure_openai,
|
||||
deployment_name="test_chat_deployment",
|
||||
assistant_id="existing-assistant-id",
|
||||
thread_id="test-thread-id",
|
||||
)
|
||||
|
||||
assert client.client is mock_async_azure_openai
|
||||
assert client.model == "test_chat_deployment"
|
||||
assert client.assistant_id == "existing-assistant-id"
|
||||
assert client.thread_id == "test-thread-id"
|
||||
assert not client._should_delete_assistant # type: ignore
|
||||
assert isinstance(client, SupportsChatGetResponse)
|
||||
|
||||
|
||||
def test_azure_assistants_client_init_auto_create_client(
|
||||
azure_openai_unit_test_env: dict[str, str],
|
||||
mock_async_azure_openai: MagicMock,
|
||||
) -> None:
|
||||
"""Test AzureOpenAIAssistantsClient initialization with auto-created client."""
|
||||
client = AzureOpenAIAssistantsClient(
|
||||
deployment_name=azure_openai_unit_test_env["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"],
|
||||
assistant_name="TestAssistant",
|
||||
api_key=azure_openai_unit_test_env["AZURE_OPENAI_API_KEY"],
|
||||
endpoint=azure_openai_unit_test_env["AZURE_OPENAI_ENDPOINT"],
|
||||
async_client=mock_async_azure_openai,
|
||||
)
|
||||
|
||||
assert client.client is mock_async_azure_openai
|
||||
assert client.model == azure_openai_unit_test_env["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"]
|
||||
assert client.assistant_id is None
|
||||
assert client.assistant_name == "TestAssistant"
|
||||
assert not client._should_delete_assistant # type: ignore
|
||||
|
||||
|
||||
def test_azure_assistants_client_init_validation_fail() -> None:
|
||||
"""Test AzureOpenAIAssistantsClient initialization with validation failure."""
|
||||
with pytest.raises(ValueError):
|
||||
# Force failure by providing invalid deployment name type - this should cause validation to fail
|
||||
AzureOpenAIAssistantsClient(deployment_name=123, api_key="valid-key") # type: ignore
|
||||
|
||||
|
||||
@pytest.mark.parametrize("exclude_list", [["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"]], indirect=True)
|
||||
def test_azure_assistants_client_init_missing_deployment_name(azure_openai_unit_test_env: dict[str, str]) -> None:
|
||||
"""Test AzureOpenAIAssistantsClient initialization with missing deployment name."""
|
||||
with pytest.raises(ValueError):
|
||||
AzureOpenAIAssistantsClient(api_key=azure_openai_unit_test_env.get("AZURE_OPENAI_API_KEY", "test-key"))
|
||||
|
||||
|
||||
def test_azure_assistants_client_init_with_default_headers(azure_openai_unit_test_env: dict[str, str]) -> None:
|
||||
"""Test AzureOpenAIAssistantsClient initialization with default headers."""
|
||||
default_headers = {"X-Unit-Test": "test-guid"}
|
||||
|
||||
client = AzureOpenAIAssistantsClient(
|
||||
deployment_name="test_chat_deployment",
|
||||
api_key=azure_openai_unit_test_env["AZURE_OPENAI_API_KEY"],
|
||||
endpoint=azure_openai_unit_test_env["AZURE_OPENAI_ENDPOINT"],
|
||||
default_headers=default_headers,
|
||||
)
|
||||
|
||||
assert client.model == "test_chat_deployment"
|
||||
assert isinstance(client, SupportsChatGetResponse)
|
||||
|
||||
# Assert that the default header we added is present in the client's default headers
|
||||
for key, value in default_headers.items():
|
||||
assert key in client.client.default_headers
|
||||
assert client.client.default_headers[key] == value
|
||||
|
||||
|
||||
async def test_azure_assistants_client_get_assistant_id_or_create_existing_assistant(
|
||||
mock_async_azure_openai: MagicMock,
|
||||
) -> None:
|
||||
"""Test _get_assistant_id_or_create when assistant_id is already provided."""
|
||||
client = create_test_azure_assistants_client(mock_async_azure_openai, assistant_id="existing-assistant-id")
|
||||
|
||||
assistant_id = await client._get_assistant_id_or_create() # type: ignore
|
||||
|
||||
assert assistant_id == "existing-assistant-id"
|
||||
assert not client._should_delete_assistant # type: ignore
|
||||
mock_async_azure_openai.beta.assistants.create.assert_not_called()
|
||||
|
||||
|
||||
async def test_azure_assistants_client_get_assistant_id_or_create_create_new(
|
||||
mock_async_azure_openai: MagicMock,
|
||||
) -> None:
|
||||
"""Test _get_assistant_id_or_create when creating a new assistant."""
|
||||
client = create_test_azure_assistants_client(
|
||||
mock_async_azure_openai, deployment_name="test_chat_deployment", assistant_name="TestAssistant"
|
||||
)
|
||||
|
||||
assistant_id = await client._get_assistant_id_or_create() # type: ignore
|
||||
|
||||
assert assistant_id == "test-assistant-id"
|
||||
assert client._should_delete_assistant # type: ignore
|
||||
mock_async_azure_openai.beta.assistants.create.assert_called_once()
|
||||
|
||||
|
||||
async def test_azure_assistants_client_aclose_should_not_delete(
|
||||
mock_async_azure_openai: MagicMock,
|
||||
) -> None:
|
||||
"""Test close when assistant should not be deleted."""
|
||||
client = create_test_azure_assistants_client(
|
||||
mock_async_azure_openai, assistant_id="assistant-to-keep", should_delete_assistant=False
|
||||
)
|
||||
|
||||
await client.close() # type: ignore
|
||||
|
||||
# Verify assistant deletion was not called
|
||||
mock_async_azure_openai.beta.assistants.delete.assert_not_called()
|
||||
assert not client._should_delete_assistant # type: ignore
|
||||
|
||||
|
||||
async def test_azure_assistants_client_aclose_should_delete(mock_async_azure_openai: MagicMock) -> None:
|
||||
"""Test close method calls cleanup."""
|
||||
client = create_test_azure_assistants_client(
|
||||
mock_async_azure_openai, assistant_id="assistant-to-delete", should_delete_assistant=True
|
||||
)
|
||||
|
||||
await client.close()
|
||||
|
||||
# Verify assistant deletion was called
|
||||
mock_async_azure_openai.beta.assistants.delete.assert_called_once_with("assistant-to-delete")
|
||||
assert not client._should_delete_assistant # type: ignore
|
||||
|
||||
|
||||
async def test_azure_assistants_client_async_context_manager(mock_async_azure_openai: MagicMock) -> None:
|
||||
"""Test async context manager functionality."""
|
||||
client = create_test_azure_assistants_client(
|
||||
mock_async_azure_openai, assistant_id="assistant-to-delete", should_delete_assistant=True
|
||||
)
|
||||
|
||||
# Test context manager
|
||||
async with client:
|
||||
pass # Just test that we can enter and exit
|
||||
|
||||
# Verify cleanup was called on exit
|
||||
mock_async_azure_openai.beta.assistants.delete.assert_called_once_with("assistant-to-delete")
|
||||
|
||||
|
||||
def test_azure_assistants_client_serialize(azure_openai_unit_test_env: dict[str, str]) -> None:
|
||||
"""Test serialization of AzureOpenAIAssistantsClient."""
|
||||
default_headers = {"X-Unit-Test": "test-guid"}
|
||||
|
||||
# Test basic initialization and to_dict
|
||||
client = AzureOpenAIAssistantsClient(
|
||||
deployment_name="test_chat_deployment",
|
||||
assistant_id="test-assistant-id",
|
||||
assistant_name="TestAssistant",
|
||||
thread_id="test-thread-id",
|
||||
api_key=azure_openai_unit_test_env["AZURE_OPENAI_API_KEY"],
|
||||
endpoint=azure_openai_unit_test_env["AZURE_OPENAI_ENDPOINT"],
|
||||
default_headers=default_headers,
|
||||
)
|
||||
|
||||
dumped_settings = client.to_dict()
|
||||
|
||||
assert dumped_settings["model"] == "test_chat_deployment"
|
||||
assert dumped_settings["assistant_id"] == "test-assistant-id"
|
||||
assert dumped_settings["assistant_name"] == "TestAssistant"
|
||||
assert dumped_settings["thread_id"] == "test-thread-id"
|
||||
|
||||
# Assert that the default header we added is present in the dumped_settings default headers
|
||||
for key, value in default_headers.items():
|
||||
assert key in dumped_settings["default_headers"]
|
||||
assert dumped_settings["default_headers"][key] == value
|
||||
# Assert that the 'User-Agent' header is not present in the dumped_settings default headers
|
||||
assert "User-Agent" not in dumped_settings["default_headers"]
|
||||
|
||||
|
||||
@tool(approval_mode="never_require")
|
||||
def get_weather(
|
||||
location: Annotated[str, Field(description="The location to get the weather for.")],
|
||||
) -> str:
|
||||
"""Get the weather for a given location."""
|
||||
return f"The weather in {location} is sunny with a high of 25°C."
|
||||
|
||||
|
||||
def test_azure_assistants_client_entra_id_authentication() -> None:
|
||||
"""Test credential authentication path with sync credential."""
|
||||
mock_credential = MagicMock()
|
||||
mock_provider = MagicMock(return_value="token-string")
|
||||
|
||||
with (
|
||||
patch("agent_framework_azure_ai._deprecated_azure_openai.load_settings") as mock_load_settings,
|
||||
patch(
|
||||
"agent_framework_azure_ai._deprecated_azure_openai.resolve_credential_to_token_provider",
|
||||
return_value=mock_provider,
|
||||
) as mock_resolve,
|
||||
patch("agent_framework_azure_ai._deprecated_azure_openai.AsyncAzureOpenAI") as mock_azure_client,
|
||||
patch("agent_framework.openai.OpenAIAssistantsClient.__init__", return_value=None),
|
||||
):
|
||||
mock_load_settings.return_value = {
|
||||
"chat_deployment_name": "test-deployment",
|
||||
"responses_deployment_name": None,
|
||||
"api_key": None,
|
||||
"token_endpoint": "https://cognitiveservices.azure.com/.default",
|
||||
"api_version": "2024-05-01-preview",
|
||||
"endpoint": "https://test-endpoint.openai.azure.com",
|
||||
"base_url": None,
|
||||
}
|
||||
|
||||
client = AzureOpenAIAssistantsClient(
|
||||
deployment_name="test-deployment",
|
||||
endpoint="https://test-endpoint.openai.azure.com",
|
||||
credential=mock_credential,
|
||||
token_endpoint="https://cognitiveservices.azure.com/.default",
|
||||
)
|
||||
|
||||
# Verify credential was resolved to a token provider
|
||||
mock_resolve.assert_called_once_with(mock_credential, "https://cognitiveservices.azure.com/.default")
|
||||
|
||||
# Verify client was created with the token provider
|
||||
mock_azure_client.assert_called_once()
|
||||
call_args = mock_azure_client.call_args[1]
|
||||
assert call_args["azure_ad_token_provider"] is mock_provider
|
||||
|
||||
assert client is not None
|
||||
assert isinstance(client, AzureOpenAIAssistantsClient)
|
||||
|
||||
|
||||
def test_azure_assistants_client_no_authentication_error() -> None:
|
||||
"""Test authentication validation error when no auth provided."""
|
||||
with patch("agent_framework_azure_ai._deprecated_azure_openai.load_settings") as mock_load_settings:
|
||||
mock_load_settings.return_value = {
|
||||
"chat_deployment_name": "test-deployment",
|
||||
"responses_deployment_name": None,
|
||||
"api_key": None,
|
||||
"token_endpoint": None,
|
||||
"api_version": "2024-05-01-preview",
|
||||
"endpoint": "https://test-endpoint.openai.azure.com",
|
||||
"base_url": None,
|
||||
}
|
||||
|
||||
# Test missing authentication raises error
|
||||
with pytest.raises(ValueError, match="api_key, credential, or a client"):
|
||||
AzureOpenAIAssistantsClient(
|
||||
deployment_name="test-deployment",
|
||||
endpoint="https://test-endpoint.openai.azure.com",
|
||||
# No authentication provided at all
|
||||
)
|
||||
|
||||
|
||||
def test_azure_assistants_client_callable_credential() -> None:
|
||||
"""Test callable token provider as credential."""
|
||||
mock_provider = MagicMock(return_value="my-token")
|
||||
|
||||
with (
|
||||
patch("agent_framework_azure_ai._deprecated_azure_openai.load_settings") as mock_load_settings,
|
||||
patch(
|
||||
"agent_framework_azure_ai._deprecated_azure_openai.resolve_credential_to_token_provider",
|
||||
return_value=mock_provider,
|
||||
),
|
||||
patch("agent_framework_azure_ai._deprecated_azure_openai.AsyncAzureOpenAI") as mock_azure_client,
|
||||
patch("agent_framework.openai.OpenAIAssistantsClient.__init__", return_value=None),
|
||||
):
|
||||
mock_load_settings.return_value = {
|
||||
"chat_deployment_name": "test-deployment",
|
||||
"responses_deployment_name": None,
|
||||
"api_key": None,
|
||||
"token_endpoint": "https://cognitiveservices.azure.com/.default",
|
||||
"api_version": "2024-05-01-preview",
|
||||
"endpoint": "https://test-endpoint.openai.azure.com",
|
||||
"base_url": None,
|
||||
}
|
||||
|
||||
client = AzureOpenAIAssistantsClient(
|
||||
deployment_name="test-deployment",
|
||||
endpoint="https://test-endpoint.openai.azure.com",
|
||||
credential=mock_provider,
|
||||
token_endpoint="https://cognitiveservices.azure.com/.default",
|
||||
)
|
||||
|
||||
# Verify client was created with the token provider
|
||||
mock_azure_client.assert_called_once()
|
||||
call_args = mock_azure_client.call_args[1]
|
||||
assert call_args["azure_ad_token_provider"] is mock_provider
|
||||
|
||||
assert client is not None
|
||||
assert isinstance(client, AzureOpenAIAssistantsClient)
|
||||
|
||||
|
||||
def test_azure_assistants_client_base_url_configuration() -> None:
|
||||
"""Test base_url client parameter path."""
|
||||
with (
|
||||
patch("agent_framework_azure_ai._deprecated_azure_openai.load_settings") as mock_load_settings,
|
||||
patch("agent_framework_azure_ai._deprecated_azure_openai.AsyncAzureOpenAI") as mock_azure_client,
|
||||
patch("agent_framework.openai.OpenAIAssistantsClient.__init__", return_value=None),
|
||||
):
|
||||
mock_load_settings.return_value = {
|
||||
"chat_deployment_name": "test-deployment",
|
||||
"responses_deployment_name": None,
|
||||
"api_key": SecretString("test-api-key"),
|
||||
"token_endpoint": None,
|
||||
"api_version": "2024-05-01-preview",
|
||||
"endpoint": None,
|
||||
"base_url": "https://custom-base-url.com",
|
||||
}
|
||||
|
||||
client = AzureOpenAIAssistantsClient(
|
||||
deployment_name="test-deployment", api_key="test-api-key", base_url="https://custom-base-url.com"
|
||||
)
|
||||
|
||||
# base_url path
|
||||
mock_azure_client.assert_called_once()
|
||||
call_args = mock_azure_client.call_args[1]
|
||||
assert call_args["base_url"] == "https://custom-base-url.com"
|
||||
assert "azure_endpoint" not in call_args
|
||||
|
||||
assert client is not None
|
||||
assert isinstance(client, AzureOpenAIAssistantsClient)
|
||||
|
||||
|
||||
def test_azure_assistants_client_azure_endpoint_configuration() -> None:
|
||||
"""Test azure_endpoint client parameter path."""
|
||||
with (
|
||||
patch("agent_framework_azure_ai._deprecated_azure_openai.load_settings") as mock_load_settings,
|
||||
patch("agent_framework_azure_ai._deprecated_azure_openai.AsyncAzureOpenAI") as mock_azure_client,
|
||||
patch("agent_framework.openai.OpenAIAssistantsClient.__init__", return_value=None),
|
||||
):
|
||||
mock_load_settings.return_value = {
|
||||
"chat_deployment_name": "test-deployment",
|
||||
"responses_deployment_name": None,
|
||||
"api_key": SecretString("test-api-key"),
|
||||
"token_endpoint": None,
|
||||
"api_version": "2024-05-01-preview",
|
||||
"endpoint": "https://test-endpoint.openai.azure.com",
|
||||
"base_url": None,
|
||||
}
|
||||
|
||||
client = AzureOpenAIAssistantsClient(
|
||||
deployment_name="test-deployment",
|
||||
api_key="test-api-key",
|
||||
endpoint="https://test-endpoint.openai.azure.com",
|
||||
)
|
||||
|
||||
# azure_endpoint path
|
||||
mock_azure_client.assert_called_once()
|
||||
call_args = mock_azure_client.call_args[1]
|
||||
assert call_args["azure_endpoint"] == "https://test-endpoint.openai.azure.com"
|
||||
assert "base_url" not in call_args
|
||||
|
||||
assert client is not None
|
||||
assert isinstance(client, AzureOpenAIAssistantsClient)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,219 +0,0 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from functools import wraps
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
from agent_framework.azure import AzureOpenAIEmbeddingClient
|
||||
from agent_framework.openai import OpenAIEmbeddingOptions
|
||||
from azure.identity.aio import AzureCliCredential
|
||||
from openai.types import CreateEmbeddingResponse
|
||||
from openai.types import Embedding as OpenAIEmbedding
|
||||
from openai.types.create_embedding_response import Usage
|
||||
|
||||
pytestmark = pytest.mark.filterwarnings("ignore:AzureOpenAIEmbeddingClient is deprecated\\..*:DeprecationWarning")
|
||||
|
||||
|
||||
def _make_openai_response(
|
||||
embeddings: list[list[float]],
|
||||
model: str = "text-embedding-3-small",
|
||||
prompt_tokens: int = 5,
|
||||
total_tokens: int = 5,
|
||||
) -> CreateEmbeddingResponse:
|
||||
"""Helper to create a mock OpenAI embeddings response."""
|
||||
data = [OpenAIEmbedding(embedding=emb, index=i, object="embedding") for i, emb in enumerate(embeddings)]
|
||||
return CreateEmbeddingResponse(
|
||||
data=data,
|
||||
model=model,
|
||||
object="list",
|
||||
usage=Usage(prompt_tokens=prompt_tokens, total_tokens=total_tokens),
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def azure_embedding_unit_test_env(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""Clear ambient Azure OpenAI embedding env vars for deterministic unit tests."""
|
||||
for key in (
|
||||
"AZURE_OPENAI_ENDPOINT",
|
||||
"AZURE_OPENAI_API_KEY",
|
||||
"AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME",
|
||||
"AZURE_OPENAI_BASE_URL",
|
||||
"AZURE_OPENAI_TOKEN_ENDPOINT",
|
||||
):
|
||||
monkeypatch.delenv(key, raising=False)
|
||||
|
||||
|
||||
def test_azure_construction_with_deployment_name(azure_embedding_unit_test_env: None) -> None:
|
||||
client = AzureOpenAIEmbeddingClient(
|
||||
deployment_name="text-embedding-3-small",
|
||||
api_key="test-key",
|
||||
endpoint="https://test.openai.azure.com/",
|
||||
)
|
||||
assert client.model == "text-embedding-3-small"
|
||||
|
||||
|
||||
def test_azure_construction_with_existing_client(azure_embedding_unit_test_env: None) -> None:
|
||||
mock_client = MagicMock()
|
||||
client = AzureOpenAIEmbeddingClient(
|
||||
deployment_name="my-deployment",
|
||||
async_client=mock_client,
|
||||
)
|
||||
assert client.model == "my-deployment"
|
||||
assert client.client is mock_client
|
||||
|
||||
|
||||
def test_azure_construction_missing_deployment_name_raises(azure_embedding_unit_test_env: None) -> None:
|
||||
with pytest.raises(ValueError, match="deployment name is required"):
|
||||
AzureOpenAIEmbeddingClient(
|
||||
api_key="test-key",
|
||||
endpoint="https://test.openai.azure.com/",
|
||||
)
|
||||
|
||||
|
||||
def test_azure_construction_missing_credentials_raises(azure_embedding_unit_test_env: None) -> None:
|
||||
with pytest.raises(ValueError, match="api_key, credential, or a client"):
|
||||
AzureOpenAIEmbeddingClient(
|
||||
deployment_name="test",
|
||||
endpoint="https://test.openai.azure.com/",
|
||||
)
|
||||
|
||||
|
||||
async def test_azure_get_embeddings(azure_embedding_unit_test_env: None) -> None:
|
||||
mock_response = _make_openai_response(
|
||||
embeddings=[[0.1, 0.2]],
|
||||
)
|
||||
mock_async_client = MagicMock()
|
||||
mock_async_client.embeddings = MagicMock()
|
||||
mock_async_client.embeddings.create = AsyncMock(return_value=mock_response)
|
||||
|
||||
client = AzureOpenAIEmbeddingClient(
|
||||
deployment_name="text-embedding-3-small",
|
||||
async_client=mock_async_client,
|
||||
)
|
||||
|
||||
result = await client.get_embeddings(["hello"])
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0].vector == [0.1, 0.2]
|
||||
|
||||
|
||||
def test_azure_otel_provider_name(azure_embedding_unit_test_env: None) -> None:
|
||||
mock_client = MagicMock()
|
||||
client = AzureOpenAIEmbeddingClient(
|
||||
deployment_name="test",
|
||||
async_client=mock_client,
|
||||
)
|
||||
assert client.OTEL_PROVIDER_NAME == "azure.ai.openai"
|
||||
|
||||
|
||||
skip_if_azure_openai_integration_tests_disabled = pytest.mark.skipif(
|
||||
os.getenv("AZURE_OPENAI_ENDPOINT", "") in ("", "https://test-endpoint.com")
|
||||
or (
|
||||
os.getenv("AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME", "") == ""
|
||||
and os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME", "") == ""
|
||||
),
|
||||
reason="No Azure OpenAI endpoint or embedding deployment provided; skipping integration tests.",
|
||||
)
|
||||
|
||||
|
||||
def _with_azure_openai_debug() -> Any:
|
||||
def decorator(func: Any) -> Any:
|
||||
@wraps(func)
|
||||
async def wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||
try:
|
||||
return await func(*args, **kwargs)
|
||||
except Exception as exc:
|
||||
model = os.getenv("AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME") or os.getenv(
|
||||
"AZURE_OPENAI_DEPLOYMENT_NAME", "<unset>"
|
||||
)
|
||||
api_version = os.getenv("AZURE_OPENAI_API_VERSION", "<unset>")
|
||||
endpoint = os.getenv("AZURE_OPENAI_ENDPOINT", "<unset>")
|
||||
debug_message = f"Azure OpenAI debug: endpoint={endpoint}, model={model}, api_version={api_version}"
|
||||
if hasattr(exc, "add_note"):
|
||||
exc.add_note(debug_message)
|
||||
elif exc.args:
|
||||
exc.args = (f"{exc.args[0]}\n{debug_message}", *exc.args[1:])
|
||||
else:
|
||||
exc.args = (debug_message,)
|
||||
raise
|
||||
|
||||
return wrapper
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
def _get_azure_embedding_deployment_name() -> str:
|
||||
return os.getenv("AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME") or os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"]
|
||||
|
||||
|
||||
def _create_azure_openai_embedding_client(
|
||||
*,
|
||||
api_key: str | None = None,
|
||||
credential: AzureCliCredential | None = None,
|
||||
) -> AzureOpenAIEmbeddingClient:
|
||||
resolved_api_key = (
|
||||
api_key if api_key is not None else None if credential is not None else os.getenv("AZURE_OPENAI_API_KEY")
|
||||
)
|
||||
return AzureOpenAIEmbeddingClient(
|
||||
deployment_name=_get_azure_embedding_deployment_name(),
|
||||
api_key=resolved_api_key,
|
||||
endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
|
||||
api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
|
||||
credential=credential,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.flaky
|
||||
@pytest.mark.integration
|
||||
@skip_if_azure_openai_integration_tests_disabled
|
||||
@_with_azure_openai_debug()
|
||||
async def test_integration_azure_openai_get_embeddings() -> None:
|
||||
"""End-to-end test of Azure OpenAI embedding generation."""
|
||||
async with AzureCliCredential() as credential:
|
||||
client = _create_azure_openai_embedding_client(credential=credential)
|
||||
|
||||
result = await client.get_embeddings(["hello world"])
|
||||
|
||||
assert len(result) == 1
|
||||
assert isinstance(result[0].vector, list)
|
||||
assert len(result[0].vector) > 0
|
||||
assert all(isinstance(v, float) for v in result[0].vector)
|
||||
assert result[0].model_id is not None
|
||||
assert result.usage is not None
|
||||
assert result.usage["input_token_count"] > 0
|
||||
|
||||
|
||||
@pytest.mark.flaky
|
||||
@pytest.mark.integration
|
||||
@skip_if_azure_openai_integration_tests_disabled
|
||||
@_with_azure_openai_debug()
|
||||
async def test_integration_azure_openai_get_embeddings_multiple() -> None:
|
||||
"""Test Azure OpenAI embedding generation for multiple inputs."""
|
||||
async with AzureCliCredential() as credential:
|
||||
client = _create_azure_openai_embedding_client(credential=credential)
|
||||
|
||||
result = await client.get_embeddings(["hello", "world", "test"])
|
||||
|
||||
assert len(result) == 3
|
||||
dims = [len(e.vector) for e in result]
|
||||
assert all(d == dims[0] for d in dims)
|
||||
|
||||
|
||||
@pytest.mark.flaky
|
||||
@pytest.mark.integration
|
||||
@skip_if_azure_openai_integration_tests_disabled
|
||||
@_with_azure_openai_debug()
|
||||
async def test_integration_azure_openai_get_embeddings_with_dimensions() -> None:
|
||||
"""Test Azure OpenAI embedding generation with custom dimensions."""
|
||||
async with AzureCliCredential() as credential:
|
||||
client = _create_azure_openai_embedding_client(credential=credential)
|
||||
|
||||
options: OpenAIEmbeddingOptions = {"dimensions": 256}
|
||||
result = await client.get_embeddings(["hello world"], options=options)
|
||||
|
||||
assert len(result) == 1
|
||||
assert len(result[0].vector) == 256
|
||||
@@ -1,542 +0,0 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
from functools import wraps
|
||||
from pathlib import Path
|
||||
from typing import Annotated, Any
|
||||
|
||||
import pytest
|
||||
from agent_framework import (
|
||||
Agent,
|
||||
AgentResponse,
|
||||
ChatResponse,
|
||||
Content,
|
||||
Message,
|
||||
SupportsChatGetResponse,
|
||||
tool,
|
||||
)
|
||||
from agent_framework.azure import AzureOpenAIResponsesClient
|
||||
from azure.identity import AzureCliCredential
|
||||
from pydantic import BaseModel
|
||||
from pytest import param
|
||||
|
||||
pytestmark = pytest.mark.filterwarnings("ignore:AzureOpenAIResponsesClient is deprecated\\..*:DeprecationWarning")
|
||||
|
||||
skip_if_azure_integration_tests_disabled = pytest.mark.skipif(
|
||||
os.getenv("AZURE_OPENAI_ENDPOINT", "") in ("", "https://test-endpoint.com"),
|
||||
reason="No real AZURE_OPENAI_ENDPOINT provided; skipping integration tests.",
|
||||
)
|
||||
|
||||
|
||||
def _with_azure_openai_debug() -> Any:
|
||||
def decorator(func: Any) -> Any:
|
||||
@wraps(func)
|
||||
async def wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||
try:
|
||||
return await func(*args, **kwargs)
|
||||
except Exception as exc:
|
||||
model = os.getenv("AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME") or os.getenv(
|
||||
"AZURE_OPENAI_DEPLOYMENT_NAME", "<unset>"
|
||||
)
|
||||
api_version = os.getenv("AZURE_OPENAI_API_VERSION", "<unset>")
|
||||
endpoint = os.getenv("AZURE_OPENAI_ENDPOINT", "<unset>")
|
||||
debug_message = f"Azure OpenAI debug: endpoint={endpoint}, model={model}, api_version={api_version}"
|
||||
if hasattr(exc, "add_note"):
|
||||
exc.add_note(debug_message)
|
||||
elif exc.args:
|
||||
exc.args = (f"{exc.args[0]}\n{debug_message}", *exc.args[1:])
|
||||
else:
|
||||
exc.args = (debug_message,)
|
||||
raise
|
||||
|
||||
return wrapper
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class OutputStruct(BaseModel):
|
||||
"""A structured output for testing purposes."""
|
||||
|
||||
location: str
|
||||
weather: str
|
||||
|
||||
|
||||
@tool(approval_mode="never_require")
|
||||
async def get_weather(location: Annotated[str, "The location as a city name"]) -> str:
|
||||
"""Get the current weather in a given location."""
|
||||
# Implementation of the tool to get weather
|
||||
return f"The weather in {location} is sunny and 72°F."
|
||||
|
||||
|
||||
async def create_vector_store(
|
||||
client: AzureOpenAIResponsesClient,
|
||||
) -> tuple[str, Content]:
|
||||
"""Create a vector store with sample documents for testing."""
|
||||
file = await client.client.files.create(
|
||||
file=("todays_weather.txt", b"The weather today is sunny with a high of 75F."),
|
||||
purpose="assistants",
|
||||
)
|
||||
vector_store = await client.client.vector_stores.create(
|
||||
name="knowledge_base",
|
||||
expires_after={"anchor": "last_active_at", "days": 1},
|
||||
)
|
||||
result = await client.client.vector_stores.files.create_and_poll(vector_store_id=vector_store.id, file_id=file.id)
|
||||
if result.last_error is not None:
|
||||
raise Exception(f"Vector store file processing failed with status: {result.last_error.message}")
|
||||
|
||||
return file.id, Content.from_hosted_vector_store(vector_store_id=vector_store.id)
|
||||
|
||||
|
||||
async def delete_vector_store(client: AzureOpenAIResponsesClient, file_id: str, vector_store_id: str) -> None:
|
||||
"""Delete the vector store after tests."""
|
||||
|
||||
await client.client.vector_stores.delete(vector_store_id=vector_store_id)
|
||||
await client.client.files.delete(file_id=file_id)
|
||||
|
||||
|
||||
def test_init(azure_openai_unit_test_env: dict[str, str]) -> None:
|
||||
# Test successful initialization
|
||||
azure_responses_client = AzureOpenAIResponsesClient(credential=AzureCliCredential())
|
||||
|
||||
assert azure_responses_client.model == azure_openai_unit_test_env["AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME"]
|
||||
assert isinstance(azure_responses_client, SupportsChatGetResponse)
|
||||
|
||||
|
||||
def test_init_validation_fail() -> None:
|
||||
# Test successful initialization
|
||||
with pytest.raises(ValueError):
|
||||
AzureOpenAIResponsesClient(api_key="34523", deployment_name={"test": "dict"}) # type: ignore
|
||||
|
||||
|
||||
def test_init_model_id_constructor(azure_openai_unit_test_env: dict[str, str]) -> None:
|
||||
# Test successful initialization
|
||||
model_id = "test_model_id"
|
||||
azure_responses_client = AzureOpenAIResponsesClient(deployment_name=model_id)
|
||||
|
||||
assert azure_responses_client.model == model_id
|
||||
assert isinstance(azure_responses_client, SupportsChatGetResponse)
|
||||
|
||||
|
||||
def test_init_model_id_kwarg(azure_openai_unit_test_env: dict[str, str]) -> None:
|
||||
"""Test that model_id kwarg correctly sets the deployment name (issue #4299)."""
|
||||
azure_responses_client = AzureOpenAIResponsesClient(model_id="gpt-4o")
|
||||
|
||||
assert azure_responses_client.model == "gpt-4o"
|
||||
assert isinstance(azure_responses_client, SupportsChatGetResponse)
|
||||
|
||||
|
||||
def test_init_model_id_kwarg_does_not_override_deployment_name(
|
||||
azure_openai_unit_test_env: dict[str, str],
|
||||
) -> None:
|
||||
"""Test that deployment_name takes precedence over model_id kwarg (issue #4299)."""
|
||||
azure_responses_client = AzureOpenAIResponsesClient(deployment_name="my-deployment", model_id="gpt-4o")
|
||||
|
||||
assert azure_responses_client.model == "my-deployment"
|
||||
assert isinstance(azure_responses_client, SupportsChatGetResponse)
|
||||
|
||||
|
||||
def test_init_model_id_kwarg_none(azure_openai_unit_test_env: dict[str, str]) -> None:
|
||||
"""Test that model_id=None does not override the env-var deployment name."""
|
||||
azure_responses_client = AzureOpenAIResponsesClient(model_id=None)
|
||||
|
||||
assert azure_responses_client.model == azure_openai_unit_test_env["AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME"]
|
||||
|
||||
|
||||
def test_init_with_default_header(azure_openai_unit_test_env: dict[str, str]) -> None:
|
||||
default_headers = {"X-Unit-Test": "test-guid"}
|
||||
|
||||
# Test successful initialization
|
||||
azure_responses_client = AzureOpenAIResponsesClient(
|
||||
default_headers=default_headers,
|
||||
)
|
||||
|
||||
assert azure_responses_client.model == azure_openai_unit_test_env["AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME"]
|
||||
assert isinstance(azure_responses_client, SupportsChatGetResponse)
|
||||
|
||||
# Assert that the default header we added is present in the client's default headers
|
||||
for key, value in default_headers.items():
|
||||
assert key in azure_responses_client.client.default_headers
|
||||
assert azure_responses_client.client.default_headers[key] == value
|
||||
|
||||
|
||||
@pytest.mark.parametrize("exclude_list", [["AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME"]], indirect=True)
|
||||
def test_init_with_empty_model_id(azure_openai_unit_test_env: dict[str, str]) -> None:
|
||||
with pytest.raises(ValueError):
|
||||
AzureOpenAIResponsesClient()
|
||||
|
||||
|
||||
def test_serialize(azure_openai_unit_test_env: dict[str, str]) -> None:
|
||||
default_headers = {"X-Unit-Test": "test-guid"}
|
||||
|
||||
settings = {
|
||||
"deployment_name": azure_openai_unit_test_env["AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME"],
|
||||
"api_key": azure_openai_unit_test_env["AZURE_OPENAI_API_KEY"],
|
||||
"default_headers": default_headers,
|
||||
}
|
||||
|
||||
azure_responses_client = AzureOpenAIResponsesClient.from_dict(settings)
|
||||
dumped_settings = azure_responses_client.to_dict()
|
||||
assert dumped_settings["deployment_name"] == azure_openai_unit_test_env["AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME"]
|
||||
assert "api_key" not in dumped_settings
|
||||
# Assert that the default header we added is present in the dumped_settings default headers
|
||||
for key, value in default_headers.items():
|
||||
assert key in dumped_settings["default_headers"]
|
||||
assert dumped_settings["default_headers"][key] == value
|
||||
# Assert that the 'User-Agent' header is not present in the dumped_settings default headers
|
||||
assert "User-Agent" not in dumped_settings["default_headers"]
|
||||
|
||||
|
||||
# region Integration Tests
|
||||
|
||||
|
||||
@pytest.mark.flaky
|
||||
@pytest.mark.integration
|
||||
@skip_if_azure_integration_tests_disabled
|
||||
@pytest.mark.parametrize(
|
||||
"option_name,option_value,needs_validation",
|
||||
[
|
||||
# Simple ChatOptions - just verify they don't fail
|
||||
param("max_tokens", 500, False, id="max_tokens"),
|
||||
param("seed", 123, False, id="seed"),
|
||||
param("user", "test-user-id", False, id="user"),
|
||||
param("metadata", {"test_key": "test_value"}, False, id="metadata"),
|
||||
param("frequency_penalty", 0.5, False, id="frequency_penalty"),
|
||||
param("presence_penalty", 0.3, False, id="presence_penalty"),
|
||||
param("stop", ["END"], False, id="stop"),
|
||||
param("allow_multiple_tool_calls", True, False, id="allow_multiple_tool_calls"),
|
||||
param("tool_choice", "none", True, id="tool_choice_none"),
|
||||
# OpenAIResponsesOptions - just verify they don't fail
|
||||
param("safety_identifier", "user-hash-abc123", False, id="safety_identifier"),
|
||||
param("truncation", "auto", False, id="truncation"),
|
||||
param("prompt_cache_key", "test-cache-key", False, id="prompt_cache_key"),
|
||||
param("max_tool_calls", 3, False, id="max_tool_calls"),
|
||||
# Complex options requiring output validation
|
||||
param("tools", [get_weather], True, id="tools_function"),
|
||||
param("tool_choice", "auto", True, id="tool_choice_auto"),
|
||||
param(
|
||||
"tool_choice",
|
||||
{"mode": "required", "required_function_name": "get_weather"},
|
||||
True,
|
||||
id="tool_choice_required",
|
||||
),
|
||||
param("response_format", OutputStruct, True, id="response_format_pydantic"),
|
||||
param(
|
||||
"response_format",
|
||||
{
|
||||
"type": "json_schema",
|
||||
"json_schema": {
|
||||
"name": "WeatherDigest",
|
||||
"strict": True,
|
||||
"schema": {
|
||||
"title": "WeatherDigest",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"location": {"type": "string"},
|
||||
"conditions": {"type": "string"},
|
||||
"temperature_c": {"type": "number"},
|
||||
"advisory": {"type": "string"},
|
||||
},
|
||||
"required": [
|
||||
"location",
|
||||
"conditions",
|
||||
"temperature_c",
|
||||
"advisory",
|
||||
],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
},
|
||||
},
|
||||
True,
|
||||
id="response_format_runtime_json_schema",
|
||||
),
|
||||
],
|
||||
)
|
||||
@_with_azure_openai_debug()
|
||||
async def test_integration_options(
|
||||
option_name: str,
|
||||
option_value: Any,
|
||||
needs_validation: bool,
|
||||
) -> None:
|
||||
"""Parametrized test covering all ChatOptions and OpenAIResponsesOptions.
|
||||
|
||||
Tests both streaming and non-streaming modes for each option to ensure
|
||||
they don't cause failures. Options marked with needs_validation also
|
||||
check that the feature actually works correctly.
|
||||
"""
|
||||
client = AzureOpenAIResponsesClient(credential=AzureCliCredential())
|
||||
# Need at least 2 iterations for tool_choice tests: one to get function call, one to get final response
|
||||
client.function_invocation_configuration["max_iterations"] = 2
|
||||
|
||||
# Prepare test message
|
||||
if option_name == "tools" or option_name == "tool_choice":
|
||||
# Use weather-related prompt for tool tests
|
||||
messages = [Message(role="user", text="What is the weather in Seattle?")]
|
||||
elif option_name == "response_format":
|
||||
# Use prompt that works well with structured output
|
||||
messages = [
|
||||
Message(role="user", text="The weather in Seattle is sunny"),
|
||||
Message(role="user", text="What is the weather in Seattle?"),
|
||||
]
|
||||
else:
|
||||
# Generic prompt for simple options
|
||||
messages = [Message(role="user", text="Say 'Hello World' briefly.")]
|
||||
|
||||
# Build options dict
|
||||
options: dict[str, Any] = {option_name: option_value}
|
||||
|
||||
# Add tools if testing tool_choice to avoid errors
|
||||
if option_name == "tool_choice":
|
||||
options["tools"] = [get_weather]
|
||||
|
||||
# Test streaming mode
|
||||
response = await client.get_response(messages=messages, stream=True, options=options).get_final_response()
|
||||
|
||||
assert response is not None
|
||||
assert isinstance(response, ChatResponse)
|
||||
assert response.text is not None, f"No text in response for option '{option_name}'"
|
||||
assert len(response.text) > 0, f"Empty response for option '{option_name}'"
|
||||
|
||||
# Validate based on option type
|
||||
if needs_validation:
|
||||
if option_name == "tools" or option_name == "tool_choice":
|
||||
# Should have called the weather function
|
||||
text = response.text.lower()
|
||||
assert "sunny" in text or "seattle" in text, f"Tool not invoked for {option_name}"
|
||||
elif option_name == "response_format":
|
||||
if option_value == OutputStruct:
|
||||
# Should have structured output
|
||||
assert response.value is not None, "No structured output"
|
||||
assert isinstance(response.value, OutputStruct)
|
||||
assert "seattle" in response.value.location.lower()
|
||||
else:
|
||||
# Runtime JSON schema
|
||||
assert response.value is None, "No structured output, can't parse any json."
|
||||
response_value = json.loads(response.text)
|
||||
assert isinstance(response_value, dict)
|
||||
assert "location" in response_value
|
||||
assert "seattle" in response_value["location"].lower()
|
||||
|
||||
|
||||
@pytest.mark.flaky
|
||||
@pytest.mark.integration
|
||||
@skip_if_azure_integration_tests_disabled
|
||||
@_with_azure_openai_debug()
|
||||
async def test_integration_web_search() -> None:
|
||||
client = AzureOpenAIResponsesClient(credential=AzureCliCredential())
|
||||
response = await client.get_response(
|
||||
messages=[
|
||||
Message(
|
||||
role="user",
|
||||
text="What is the current weather? Do not ask for my current location.",
|
||||
)
|
||||
],
|
||||
options={
|
||||
"tools": [
|
||||
AzureOpenAIResponsesClient.get_web_search_tool(user_location={"country": "US", "city": "Seattle"})
|
||||
]
|
||||
},
|
||||
stream=True,
|
||||
).get_final_response()
|
||||
|
||||
assert response.text is not None
|
||||
|
||||
|
||||
@pytest.mark.flaky
|
||||
@pytest.mark.integration
|
||||
@skip_if_azure_integration_tests_disabled
|
||||
@_with_azure_openai_debug()
|
||||
async def test_integration_client_file_search() -> None:
|
||||
"""Test Azure responses client with file search tool."""
|
||||
azure_responses_client = AzureOpenAIResponsesClient(credential=AzureCliCredential())
|
||||
file_id, vector_store = await create_vector_store(azure_responses_client)
|
||||
try:
|
||||
# Test that the client will use the file search tool
|
||||
response = await azure_responses_client.get_response(
|
||||
messages=[
|
||||
Message(
|
||||
role="user",
|
||||
text="What is the weather today? Do a file search to find the answer.",
|
||||
)
|
||||
],
|
||||
options={
|
||||
"tools": [
|
||||
AzureOpenAIResponsesClient.get_file_search_tool(vector_store_ids=[vector_store.vector_store_id])
|
||||
],
|
||||
"tool_choice": "auto",
|
||||
},
|
||||
)
|
||||
|
||||
assert "sunny" in response.text.lower()
|
||||
assert "75" in response.text
|
||||
finally:
|
||||
await delete_vector_store(azure_responses_client, file_id, vector_store.vector_store_id)
|
||||
|
||||
|
||||
@pytest.mark.flaky
|
||||
@pytest.mark.integration
|
||||
@skip_if_azure_integration_tests_disabled
|
||||
@_with_azure_openai_debug()
|
||||
async def test_integration_client_file_search_streaming() -> None:
|
||||
"""Test Azure responses client with file search tool and streaming."""
|
||||
azure_responses_client = AzureOpenAIResponsesClient(credential=AzureCliCredential())
|
||||
file_id, vector_store = await create_vector_store(azure_responses_client)
|
||||
# Test that the client will use the file search tool
|
||||
try:
|
||||
response_stream = azure_responses_client.get_response(
|
||||
messages=[
|
||||
Message(
|
||||
role="user",
|
||||
text="What is the weather today? Do a file search to find the answer.",
|
||||
)
|
||||
],
|
||||
stream=True,
|
||||
options={
|
||||
"tools": [
|
||||
AzureOpenAIResponsesClient.get_file_search_tool(vector_store_ids=[vector_store.vector_store_id])
|
||||
],
|
||||
"tool_choice": "auto",
|
||||
},
|
||||
)
|
||||
|
||||
full_response = await response_stream.get_final_response()
|
||||
assert "sunny" in full_response.text.lower()
|
||||
assert "75" in full_response.text
|
||||
finally:
|
||||
await delete_vector_store(azure_responses_client, file_id, vector_store.vector_store_id)
|
||||
|
||||
|
||||
@pytest.mark.flaky
|
||||
@pytest.mark.integration
|
||||
@skip_if_azure_integration_tests_disabled
|
||||
@_with_azure_openai_debug()
|
||||
async def test_integration_client_agent_hosted_mcp_tool() -> None:
|
||||
"""Integration test for MCP tool with Azure Response Agent using Microsoft Learn MCP."""
|
||||
client = AzureOpenAIResponsesClient(credential=AzureCliCredential())
|
||||
response = await client.get_response(
|
||||
messages=[Message(role="user", text="How to create an Azure storage account using az cli?")],
|
||||
options={
|
||||
# this needs to be high enough to handle the full MCP tool response.
|
||||
"max_tokens": 5000,
|
||||
"tools": AzureOpenAIResponsesClient.get_mcp_tool(
|
||||
name="Microsoft Learn MCP",
|
||||
url="https://learn.microsoft.com/api/mcp",
|
||||
),
|
||||
},
|
||||
)
|
||||
assert isinstance(response, ChatResponse)
|
||||
# MCP server may return empty response intermittently - skip test rather than fail
|
||||
if not response.text:
|
||||
pytest.skip("MCP server returned empty response - service-side issue")
|
||||
# Should contain Azure-related content since it's asking about Azure CLI
|
||||
assert any(term in response.text.lower() for term in ["azure", "storage", "account", "cli"])
|
||||
|
||||
|
||||
@pytest.mark.flaky
|
||||
@pytest.mark.integration
|
||||
@skip_if_azure_integration_tests_disabled
|
||||
@_with_azure_openai_debug()
|
||||
async def test_integration_client_agent_hosted_code_interpreter_tool():
|
||||
"""Test Azure Responses Client agent with code interpreter tool."""
|
||||
client = AzureOpenAIResponsesClient(credential=AzureCliCredential())
|
||||
|
||||
response = await client.get_response(
|
||||
messages=[
|
||||
Message(
|
||||
role="user",
|
||||
text="Calculate the sum of numbers from 1 to 10 using Python code.",
|
||||
)
|
||||
],
|
||||
options={
|
||||
"tools": [AzureOpenAIResponsesClient.get_code_interpreter_tool()],
|
||||
},
|
||||
)
|
||||
# Should contain calculation result (sum of 1-10 = 55) or code execution content
|
||||
contains_relevant_content = any(
|
||||
term in response.text.lower() for term in ["55", "sum", "code", "python", "calculate", "10"]
|
||||
)
|
||||
assert contains_relevant_content or len(response.text.strip()) > 10
|
||||
|
||||
|
||||
@pytest.mark.flaky
|
||||
@pytest.mark.integration
|
||||
@skip_if_azure_integration_tests_disabled
|
||||
@_with_azure_openai_debug()
|
||||
async def test_integration_client_agent_existing_session():
|
||||
"""Test Azure Responses Client agent with existing session to continue conversations across agent instances."""
|
||||
# First conversation - capture the session
|
||||
preserved_session = None
|
||||
|
||||
async with Agent(
|
||||
client=AzureOpenAIResponsesClient(credential=AzureCliCredential()),
|
||||
instructions="You are a helpful assistant with good memory.",
|
||||
) as first_agent:
|
||||
# Start a conversation and capture the session
|
||||
session = first_agent.create_session()
|
||||
first_response = await first_agent.run(
|
||||
"My hobby is photography. Remember this.", session=session, options={"store": True}
|
||||
)
|
||||
|
||||
assert isinstance(first_response, AgentResponse)
|
||||
assert first_response.text is not None
|
||||
|
||||
# Preserve the session for reuse
|
||||
preserved_session = session
|
||||
|
||||
# Second conversation - reuse the session in a new agent instance
|
||||
if preserved_session:
|
||||
async with Agent(
|
||||
client=AzureOpenAIResponsesClient(credential=AzureCliCredential()),
|
||||
instructions="You are a helpful assistant with good memory.",
|
||||
) as second_agent:
|
||||
# Reuse the preserved session
|
||||
second_response = await second_agent.run(
|
||||
"What is my hobby?", session=preserved_session, options={"store": True}
|
||||
)
|
||||
|
||||
assert isinstance(second_response, AgentResponse)
|
||||
assert second_response.text is not None
|
||||
assert "photography" in second_response.text.lower()
|
||||
|
||||
|
||||
@pytest.mark.flaky
|
||||
@pytest.mark.integration
|
||||
@skip_if_azure_integration_tests_disabled
|
||||
@_with_azure_openai_debug()
|
||||
async def test_azure_openai_responses_client_tool_rich_content_image() -> None:
|
||||
"""Test that Azure OpenAI Responses client can handle tool results containing images."""
|
||||
image_path = Path(__file__).parent.parent / "assets" / "sample_image.jpg"
|
||||
image_bytes = image_path.read_bytes()
|
||||
|
||||
@tool(approval_mode="never_require")
|
||||
def get_test_image() -> Content:
|
||||
"""Return a test image for analysis."""
|
||||
return Content.from_data(data=image_bytes, media_type="image/jpeg")
|
||||
|
||||
client = AzureOpenAIResponsesClient(credential=AzureCliCredential())
|
||||
client.function_invocation_configuration["max_iterations"] = 2
|
||||
|
||||
for streaming in [False, True]:
|
||||
messages = [
|
||||
Message(
|
||||
role="user",
|
||||
text="Call the get_test_image tool and describe what you see.",
|
||||
)
|
||||
]
|
||||
options: dict[str, Any] = {"tools": [get_test_image], "tool_choice": "auto"}
|
||||
|
||||
if streaming:
|
||||
response = await client.get_response(messages=messages, stream=True, options=options).get_final_response()
|
||||
else:
|
||||
response = await client.get_response(messages=messages, options=options)
|
||||
|
||||
assert response is not None
|
||||
assert isinstance(response, ChatResponse)
|
||||
assert response.text is not None
|
||||
assert len(response.text) > 0
|
||||
# sample_image.jpg contains a photo of a house; the model should mention it.
|
||||
assert "house" in response.text.lower(), f"Model did not describe the house image. Response: {response.text}"
|
||||
@@ -1,131 +0,0 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import warnings
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from agent_framework import SupportsChatGetResponse
|
||||
|
||||
warnings.filterwarnings(
|
||||
"ignore",
|
||||
message=r"RawAzureAIClient is deprecated\..*",
|
||||
category=DeprecationWarning,
|
||||
)
|
||||
|
||||
from agent_framework.azure import AzureOpenAIResponsesClient # noqa: E402
|
||||
from azure.identity import AzureCliCredential # noqa: E402
|
||||
|
||||
pytestmark = pytest.mark.filterwarnings("ignore:AzureOpenAIResponsesClient is deprecated\\..*:DeprecationWarning")
|
||||
|
||||
|
||||
def test_init_with_project_client(azure_openai_unit_test_env: dict[str, str]) -> None:
|
||||
"""Test initialization with an existing AIProjectClient."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from openai import AsyncOpenAI
|
||||
|
||||
# Create a mock AIProjectClient that returns a mock AsyncOpenAI client
|
||||
mock_openai_client = MagicMock(spec=AsyncOpenAI)
|
||||
mock_openai_client.default_headers = {}
|
||||
|
||||
mock_project_client = MagicMock()
|
||||
mock_project_client.get_openai_client.return_value = mock_openai_client
|
||||
|
||||
with patch(
|
||||
"agent_framework_azure_ai._deprecated_azure_openai.AzureOpenAIResponsesClient._create_client_from_project",
|
||||
return_value=mock_openai_client,
|
||||
):
|
||||
azure_responses_client = AzureOpenAIResponsesClient(
|
||||
project_client=mock_project_client,
|
||||
deployment_name="gpt-4o",
|
||||
)
|
||||
|
||||
assert azure_responses_client.model == "gpt-4o"
|
||||
assert azure_responses_client.client is mock_openai_client
|
||||
assert isinstance(azure_responses_client, SupportsChatGetResponse)
|
||||
|
||||
|
||||
def test_init_with_project_endpoint(azure_openai_unit_test_env: dict[str, str]) -> None:
|
||||
"""Test initialization with a project endpoint and credential."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from openai import AsyncOpenAI
|
||||
|
||||
mock_openai_client = MagicMock(spec=AsyncOpenAI)
|
||||
mock_openai_client.default_headers = {}
|
||||
|
||||
with patch(
|
||||
"agent_framework_azure_ai._deprecated_azure_openai.AzureOpenAIResponsesClient._create_client_from_project",
|
||||
return_value=mock_openai_client,
|
||||
):
|
||||
azure_responses_client = AzureOpenAIResponsesClient(
|
||||
project_endpoint="https://test-project.services.ai.azure.com",
|
||||
deployment_name="gpt-4o",
|
||||
credential=AzureCliCredential(),
|
||||
)
|
||||
|
||||
assert azure_responses_client.model == "gpt-4o"
|
||||
assert azure_responses_client.client is mock_openai_client
|
||||
assert isinstance(azure_responses_client, SupportsChatGetResponse)
|
||||
|
||||
|
||||
def test_create_client_from_project_with_project_client() -> None:
|
||||
"""Test _create_client_from_project with an existing project client."""
|
||||
from openai import AsyncOpenAI
|
||||
|
||||
mock_openai_client = MagicMock(spec=AsyncOpenAI)
|
||||
mock_project_client = MagicMock()
|
||||
mock_project_client.get_openai_client.return_value = mock_openai_client
|
||||
|
||||
result = AzureOpenAIResponsesClient._create_client_from_project(
|
||||
project_client=mock_project_client,
|
||||
project_endpoint=None,
|
||||
credential=None,
|
||||
)
|
||||
|
||||
assert result is mock_openai_client
|
||||
mock_project_client.get_openai_client.assert_called_once()
|
||||
|
||||
|
||||
def test_create_client_from_project_with_endpoint() -> None:
|
||||
"""Test _create_client_from_project with a project endpoint."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from openai import AsyncOpenAI
|
||||
|
||||
mock_openai_client = MagicMock(spec=AsyncOpenAI)
|
||||
mock_credential = MagicMock()
|
||||
|
||||
with patch("agent_framework_azure_ai._deprecated_azure_openai.AIProjectClient") as MockAIProjectClient:
|
||||
mock_instance = MockAIProjectClient.return_value
|
||||
mock_instance.get_openai_client.return_value = mock_openai_client
|
||||
|
||||
result = AzureOpenAIResponsesClient._create_client_from_project(
|
||||
project_client=None,
|
||||
project_endpoint="https://test-project.services.ai.azure.com",
|
||||
credential=mock_credential,
|
||||
)
|
||||
|
||||
assert result is mock_openai_client
|
||||
MockAIProjectClient.assert_called_once()
|
||||
mock_instance.get_openai_client.assert_called_once()
|
||||
|
||||
|
||||
def test_create_client_from_project_missing_endpoint() -> None:
|
||||
"""Test _create_client_from_project raises error when endpoint is missing."""
|
||||
with pytest.raises(ValueError, match="project endpoint is required"):
|
||||
AzureOpenAIResponsesClient._create_client_from_project(
|
||||
project_client=None,
|
||||
project_endpoint=None,
|
||||
credential=MagicMock(),
|
||||
)
|
||||
|
||||
|
||||
def test_create_client_from_project_missing_credential() -> None:
|
||||
"""Test _create_client_from_project raises error when credential is missing."""
|
||||
with pytest.raises(ValueError, match="credential is required"):
|
||||
AzureOpenAIResponsesClient._create_client_from_project(
|
||||
project_client=None,
|
||||
project_endpoint="https://test-project.services.ai.azure.com",
|
||||
credential=None,
|
||||
)
|
||||
@@ -1,773 +0,0 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import os
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from agent_framework import (
|
||||
Agent,
|
||||
tool,
|
||||
)
|
||||
from azure.ai.agents.models import (
|
||||
Agent as AzureAgent,
|
||||
)
|
||||
from azure.ai.agents.models import (
|
||||
CodeInterpreterToolDefinition,
|
||||
)
|
||||
from pydantic import BaseModel
|
||||
|
||||
from agent_framework_azure_ai import (
|
||||
AzureAIAgentClient,
|
||||
AzureAIAgentsProvider,
|
||||
AzureAISettings,
|
||||
)
|
||||
from agent_framework_azure_ai._shared import (
|
||||
from_azure_ai_agent_tools,
|
||||
to_azure_ai_agent_tools,
|
||||
)
|
||||
|
||||
skip_if_azure_ai_integration_tests_disabled = pytest.mark.skipif(
|
||||
os.getenv("AZURE_AI_PROJECT_ENDPOINT", "") in ("", "https://test-project.cognitiveservices.azure.com/"),
|
||||
reason="No real AZURE_AI_PROJECT_ENDPOINT provided; skipping integration tests.",
|
||||
)
|
||||
|
||||
# region Provider Initialization Tests
|
||||
|
||||
|
||||
def test_provider_init_with_agents_client(mock_agents_client: MagicMock) -> None:
|
||||
"""Test AzureAIAgentsProvider initialization with existing AgentsClient."""
|
||||
provider = AzureAIAgentsProvider(agents_client=mock_agents_client)
|
||||
|
||||
assert provider._agents_client is mock_agents_client # type: ignore
|
||||
assert provider._should_close_client is False # type: ignore
|
||||
|
||||
|
||||
def test_provider_init_with_credential(
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
mock_azure_credential: MagicMock,
|
||||
) -> None:
|
||||
"""Test AzureAIAgentsProvider initialization with credential."""
|
||||
with patch("agent_framework_azure_ai._agent_provider.AgentsClient") as mock_client_class:
|
||||
mock_client_instance = MagicMock()
|
||||
mock_client_class.return_value = mock_client_instance
|
||||
|
||||
provider = AzureAIAgentsProvider(credential=mock_azure_credential)
|
||||
|
||||
mock_client_class.assert_called_once()
|
||||
assert provider._agents_client is mock_client_instance # type: ignore
|
||||
assert provider._should_close_client is True # type: ignore
|
||||
|
||||
|
||||
def test_provider_init_with_explicit_endpoint(mock_azure_credential: MagicMock) -> None:
|
||||
"""Test AzureAIAgentsProvider initialization with explicit endpoint."""
|
||||
with patch("agent_framework_azure_ai._agent_provider.AgentsClient") as mock_client_class:
|
||||
mock_client_instance = MagicMock()
|
||||
mock_client_class.return_value = mock_client_instance
|
||||
|
||||
provider = AzureAIAgentsProvider(
|
||||
project_endpoint="https://custom-endpoint.com/",
|
||||
credential=mock_azure_credential,
|
||||
)
|
||||
|
||||
mock_client_class.assert_called_once()
|
||||
call_kwargs = mock_client_class.call_args.kwargs
|
||||
assert call_kwargs["endpoint"] == "https://custom-endpoint.com/"
|
||||
assert provider._should_close_client is True # type: ignore
|
||||
|
||||
|
||||
def test_provider_init_missing_endpoint_raises(
|
||||
mock_azure_credential: MagicMock,
|
||||
) -> None:
|
||||
"""Test AzureAIAgentsProvider raises error when endpoint is missing."""
|
||||
# Mock load_settings to return a dict with None for project_endpoint
|
||||
with patch("agent_framework_azure_ai._agent_provider.load_settings") as mock_load_settings:
|
||||
mock_load_settings.return_value = {"project_endpoint": None, "model_deployment_name": "test-model"}
|
||||
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
AzureAIAgentsProvider(credential=mock_azure_credential)
|
||||
|
||||
assert "project endpoint is required" in str(exc_info.value).lower()
|
||||
|
||||
|
||||
def test_provider_init_missing_credential_raises(azure_ai_unit_test_env: dict[str, str]) -> None:
|
||||
"""Test AzureAIAgentsProvider raises error when credential is missing."""
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
AzureAIAgentsProvider()
|
||||
|
||||
assert "credential is required" in str(exc_info.value).lower()
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
# region Context Manager Tests
|
||||
|
||||
|
||||
async def test_provider_context_manager_closes_client(mock_agents_client: MagicMock) -> None:
|
||||
"""Test that context manager closes client when it was created by provider."""
|
||||
with patch("agent_framework_azure_ai._agent_provider.AgentsClient") as mock_client_class:
|
||||
mock_client_instance = AsyncMock()
|
||||
mock_client_class.return_value = mock_client_instance
|
||||
|
||||
with patch.object(AzureAIAgentsProvider, "__init__", lambda self: None): # type: ignore
|
||||
provider = AzureAIAgentsProvider.__new__(AzureAIAgentsProvider)
|
||||
provider._agents_client = mock_client_instance # type: ignore
|
||||
provider._should_close_client = True # type: ignore
|
||||
provider._settings = AzureAISettings(project_endpoint="https://test.com") # type: ignore
|
||||
|
||||
async with provider:
|
||||
pass
|
||||
|
||||
mock_client_instance.close.assert_called_once()
|
||||
|
||||
|
||||
async def test_provider_context_manager_does_not_close_external_client(mock_agents_client: MagicMock) -> None:
|
||||
"""Test that context manager does not close externally provided client."""
|
||||
mock_agents_client.close = AsyncMock()
|
||||
|
||||
provider = AzureAIAgentsProvider(agents_client=mock_agents_client)
|
||||
|
||||
async with provider:
|
||||
pass
|
||||
|
||||
mock_agents_client.close.assert_not_called()
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
# region create_agent Tests
|
||||
|
||||
|
||||
async def test_create_agent_basic(
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
mock_agents_client: MagicMock,
|
||||
) -> None:
|
||||
"""Test creating a basic agent."""
|
||||
mock_agent = MagicMock(spec=AzureAgent)
|
||||
mock_agent.id = "test-agent-id"
|
||||
mock_agent.name = "TestAgent"
|
||||
mock_agent.description = "A test agent"
|
||||
mock_agent.instructions = "Be helpful"
|
||||
mock_agent.model = "gpt-4"
|
||||
mock_agent.temperature = 0.7
|
||||
mock_agent.top_p = 0.9
|
||||
mock_agent.tools = []
|
||||
mock_agents_client.create_agent = AsyncMock(return_value=mock_agent)
|
||||
|
||||
provider = AzureAIAgentsProvider(agents_client=mock_agents_client)
|
||||
|
||||
agent = await provider.create_agent(
|
||||
name="TestAgent",
|
||||
instructions="Be helpful",
|
||||
description="A test agent",
|
||||
)
|
||||
|
||||
assert isinstance(agent, Agent)
|
||||
assert agent.name == "TestAgent"
|
||||
assert agent.id == "test-agent-id"
|
||||
mock_agents_client.create_agent.assert_called_once()
|
||||
|
||||
|
||||
async def test_create_agent_with_model(
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
mock_agents_client: MagicMock,
|
||||
) -> None:
|
||||
"""Test creating an agent with explicit model."""
|
||||
mock_agent = MagicMock(spec=AzureAgent)
|
||||
mock_agent.id = "test-agent-id"
|
||||
mock_agent.name = "TestAgent"
|
||||
mock_agent.description = None
|
||||
mock_agent.instructions = None
|
||||
mock_agent.model = "custom-model"
|
||||
mock_agent.temperature = None
|
||||
mock_agent.top_p = None
|
||||
mock_agent.tools = []
|
||||
mock_agents_client.create_agent = AsyncMock(return_value=mock_agent)
|
||||
|
||||
provider = AzureAIAgentsProvider(agents_client=mock_agents_client)
|
||||
|
||||
await provider.create_agent(name="TestAgent", model="custom-model")
|
||||
|
||||
call_kwargs = mock_agents_client.create_agent.call_args.kwargs
|
||||
assert call_kwargs["model"] == "custom-model"
|
||||
|
||||
|
||||
async def test_create_agent_with_tools(
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
mock_agents_client: MagicMock,
|
||||
) -> None:
|
||||
"""Test creating an agent with tools."""
|
||||
mock_agent = MagicMock(spec=AzureAgent)
|
||||
mock_agent.id = "test-agent-id"
|
||||
mock_agent.name = "TestAgent"
|
||||
mock_agent.description = None
|
||||
mock_agent.instructions = None
|
||||
mock_agent.model = "gpt-4"
|
||||
mock_agent.temperature = None
|
||||
mock_agent.top_p = None
|
||||
mock_agent.tools = []
|
||||
mock_agents_client.create_agent = AsyncMock(return_value=mock_agent)
|
||||
|
||||
provider = AzureAIAgentsProvider(agents_client=mock_agents_client)
|
||||
|
||||
@tool(approval_mode="never_require")
|
||||
def get_weather(city: str) -> str:
|
||||
"""Get weather for a city."""
|
||||
return f"Weather in {city}"
|
||||
|
||||
await provider.create_agent(name="TestAgent", tools=get_weather)
|
||||
|
||||
call_kwargs = mock_agents_client.create_agent.call_args.kwargs
|
||||
assert "tools" in call_kwargs
|
||||
assert len(call_kwargs["tools"]) > 0
|
||||
|
||||
|
||||
async def test_create_agent_with_response_format(
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
mock_agents_client: MagicMock,
|
||||
) -> None:
|
||||
"""Test creating an agent with structured response format via default_options."""
|
||||
|
||||
class WeatherResponse(BaseModel):
|
||||
temperature: float
|
||||
description: str
|
||||
|
||||
mock_agent = MagicMock(spec=AzureAgent)
|
||||
mock_agent.id = "test-agent-id"
|
||||
mock_agent.name = "TestAgent"
|
||||
mock_agent.description = None
|
||||
mock_agent.instructions = None
|
||||
mock_agent.model = "gpt-4"
|
||||
mock_agent.temperature = None
|
||||
mock_agent.top_p = None
|
||||
mock_agent.tools = []
|
||||
mock_agents_client.create_agent = AsyncMock(return_value=mock_agent)
|
||||
|
||||
provider = AzureAIAgentsProvider(agents_client=mock_agents_client)
|
||||
|
||||
await provider.create_agent(
|
||||
name="TestAgent",
|
||||
default_options={"response_format": WeatherResponse},
|
||||
)
|
||||
|
||||
call_kwargs = mock_agents_client.create_agent.call_args.kwargs
|
||||
assert "response_format" in call_kwargs
|
||||
|
||||
|
||||
async def test_create_agent_missing_model_raises(
|
||||
mock_agents_client: MagicMock,
|
||||
) -> None:
|
||||
"""Test that create_agent raises error when model is not specified."""
|
||||
# Create provider with mocked settings that has no model
|
||||
with patch("agent_framework_azure_ai._agent_provider.load_settings") as mock_load_settings:
|
||||
mock_load_settings.return_value = {"project_endpoint": "https://test.com", "model_deployment_name": None}
|
||||
|
||||
provider = AzureAIAgentsProvider(agents_client=mock_agents_client)
|
||||
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
await provider.create_agent(name="TestAgent")
|
||||
|
||||
assert "model deployment name is required" in str(exc_info.value).lower()
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
# region get_agent Tests
|
||||
|
||||
|
||||
async def test_get_agent_by_id(
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
mock_agents_client: MagicMock,
|
||||
) -> None:
|
||||
"""Test getting an agent by ID."""
|
||||
mock_agent = MagicMock(spec=AzureAgent)
|
||||
mock_agent.id = "existing-agent-id"
|
||||
mock_agent.name = "ExistingAgent"
|
||||
mock_agent.description = "An existing agent"
|
||||
mock_agent.instructions = "Be helpful"
|
||||
mock_agent.model = "gpt-4"
|
||||
mock_agent.temperature = 0.7
|
||||
mock_agent.top_p = 0.9
|
||||
mock_agent.tools = []
|
||||
mock_agents_client.get_agent = AsyncMock(return_value=mock_agent)
|
||||
|
||||
provider = AzureAIAgentsProvider(agents_client=mock_agents_client)
|
||||
|
||||
agent = await provider.get_agent("existing-agent-id")
|
||||
|
||||
assert isinstance(agent, Agent)
|
||||
assert agent.id == "existing-agent-id"
|
||||
mock_agents_client.get_agent.assert_called_once_with("existing-agent-id")
|
||||
|
||||
|
||||
async def test_get_agent_with_function_tools(
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
mock_agents_client: MagicMock,
|
||||
) -> None:
|
||||
"""Test getting an agent that has function tools requires tool implementations."""
|
||||
mock_function_tool = MagicMock()
|
||||
mock_function_tool.type = "function"
|
||||
mock_function_tool.function = MagicMock()
|
||||
mock_function_tool.function.name = "get_weather"
|
||||
|
||||
mock_agent = MagicMock(spec=AzureAgent)
|
||||
mock_agent.id = "agent-with-tools"
|
||||
mock_agent.name = "AgentWithTools"
|
||||
mock_agent.description = None
|
||||
mock_agent.instructions = None
|
||||
mock_agent.model = "gpt-4"
|
||||
mock_agent.temperature = None
|
||||
mock_agent.top_p = None
|
||||
mock_agent.tools = [mock_function_tool]
|
||||
mock_agents_client.get_agent = AsyncMock(return_value=mock_agent)
|
||||
|
||||
provider = AzureAIAgentsProvider(agents_client=mock_agents_client)
|
||||
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
await provider.get_agent("agent-with-tools")
|
||||
|
||||
assert "get_weather" in str(exc_info.value)
|
||||
|
||||
|
||||
async def test_get_agent_with_provided_function_tools(
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
mock_agents_client: MagicMock,
|
||||
) -> None:
|
||||
"""Test getting an agent with function tools when implementations are provided."""
|
||||
mock_function_tool = MagicMock()
|
||||
mock_function_tool.type = "function"
|
||||
mock_function_tool.function = MagicMock()
|
||||
mock_function_tool.function.name = "get_weather"
|
||||
|
||||
mock_agent = MagicMock(spec=AzureAgent)
|
||||
mock_agent.id = "agent-with-tools"
|
||||
mock_agent.name = "AgentWithTools"
|
||||
mock_agent.description = None
|
||||
mock_agent.instructions = None
|
||||
mock_agent.model = "gpt-4"
|
||||
mock_agent.temperature = None
|
||||
mock_agent.top_p = None
|
||||
mock_agent.tools = [mock_function_tool]
|
||||
mock_agents_client.get_agent = AsyncMock(return_value=mock_agent)
|
||||
|
||||
@tool(approval_mode="never_require")
|
||||
def get_weather(city: str) -> str:
|
||||
"""Get weather for a city."""
|
||||
return f"Weather in {city}"
|
||||
|
||||
provider = AzureAIAgentsProvider(agents_client=mock_agents_client)
|
||||
|
||||
agent = await provider.get_agent("agent-with-tools", tools=get_weather)
|
||||
|
||||
assert isinstance(agent, Agent)
|
||||
assert agent.id == "agent-with-tools"
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
# region as_agent Tests
|
||||
|
||||
|
||||
def test_as_agent_wraps_without_http(
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
mock_agents_client: MagicMock,
|
||||
) -> None:
|
||||
"""Test as_agent wraps Agent object without making HTTP calls."""
|
||||
mock_agent = MagicMock(spec=AzureAgent)
|
||||
mock_agent.id = "wrap-agent-id"
|
||||
mock_agent.name = "WrapAgent"
|
||||
mock_agent.description = "Wrapped agent"
|
||||
mock_agent.instructions = "Be helpful"
|
||||
mock_agent.model = "gpt-4"
|
||||
mock_agent.temperature = 0.5
|
||||
mock_agent.top_p = 0.8
|
||||
mock_agent.tools = []
|
||||
|
||||
provider = AzureAIAgentsProvider(agents_client=mock_agents_client)
|
||||
|
||||
agent = provider.as_agent(mock_agent)
|
||||
|
||||
assert isinstance(agent, Agent)
|
||||
assert agent.id == "wrap-agent-id"
|
||||
assert agent.name == "WrapAgent"
|
||||
# Ensure no HTTP calls were made
|
||||
mock_agents_client.get_agent.assert_not_called()
|
||||
mock_agents_client.create_agent.assert_not_called()
|
||||
|
||||
|
||||
def test_as_agent_with_function_tools_validates(
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
mock_agents_client: MagicMock,
|
||||
) -> None:
|
||||
"""Test as_agent validates that function tool implementations are provided."""
|
||||
mock_function_tool = MagicMock()
|
||||
mock_function_tool.type = "function"
|
||||
mock_function_tool.function = MagicMock()
|
||||
mock_function_tool.function.name = "my_function"
|
||||
|
||||
mock_agent = MagicMock(spec=AzureAgent)
|
||||
mock_agent.id = "agent-id"
|
||||
mock_agent.name = "Agent"
|
||||
mock_agent.description = None
|
||||
mock_agent.instructions = None
|
||||
mock_agent.model = "gpt-4"
|
||||
mock_agent.temperature = None
|
||||
mock_agent.top_p = None
|
||||
mock_agent.tools = [mock_function_tool]
|
||||
|
||||
provider = AzureAIAgentsProvider(agents_client=mock_agents_client)
|
||||
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
provider.as_agent(mock_agent)
|
||||
|
||||
assert "my_function" in str(exc_info.value)
|
||||
|
||||
|
||||
def test_as_agent_with_hosted_tools(
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
mock_agents_client: MagicMock,
|
||||
) -> None:
|
||||
"""Test as_agent excludes hosted tools from local tools (they stay on the server agent)."""
|
||||
mock_code_interpreter = MagicMock()
|
||||
mock_code_interpreter.type = "code_interpreter"
|
||||
|
||||
mock_agent = MagicMock(spec=AzureAgent)
|
||||
mock_agent.id = "agent-id"
|
||||
mock_agent.name = "Agent"
|
||||
mock_agent.description = None
|
||||
mock_agent.instructions = None
|
||||
mock_agent.model = "gpt-4"
|
||||
mock_agent.temperature = None
|
||||
mock_agent.top_p = None
|
||||
mock_agent.tools = [mock_code_interpreter]
|
||||
|
||||
provider = AzureAIAgentsProvider(agents_client=mock_agents_client)
|
||||
|
||||
agent = provider.as_agent(mock_agent)
|
||||
|
||||
assert isinstance(agent, Agent)
|
||||
# Hosted tools (code_interpreter, file_search, etc.) are already on the server agent
|
||||
# and should NOT be in local tools to avoid re-sending them at run time
|
||||
tools = agent.default_options.get("tools") or []
|
||||
assert not any(isinstance(t, dict) and t.get("type") == "code_interpreter" for t in tools)
|
||||
|
||||
|
||||
def test_as_agent_with_dict_function_tools_validates(
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
mock_agents_client: MagicMock,
|
||||
) -> None:
|
||||
"""Test as_agent validates dict-format function tools require implementations."""
|
||||
# Dict-based function tool (as returned by some Azure AI SDK operations)
|
||||
dict_function_tool = { # type: ignore
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "dict_based_function",
|
||||
"description": "A function defined as dict",
|
||||
"parameters": {"type": "object", "properties": {}},
|
||||
},
|
||||
}
|
||||
|
||||
mock_agent = MagicMock(spec=AzureAgent)
|
||||
mock_agent.id = "agent-id"
|
||||
mock_agent.name = "Agent"
|
||||
mock_agent.description = None
|
||||
mock_agent.instructions = None
|
||||
mock_agent.model = "gpt-4"
|
||||
mock_agent.temperature = None
|
||||
mock_agent.top_p = None
|
||||
mock_agent.tools = [dict_function_tool]
|
||||
|
||||
provider = AzureAIAgentsProvider(agents_client=mock_agents_client)
|
||||
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
provider.as_agent(mock_agent)
|
||||
|
||||
assert "dict_based_function" in str(exc_info.value)
|
||||
|
||||
|
||||
def test_as_agent_with_dict_function_tools_provided(
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
mock_agents_client: MagicMock,
|
||||
) -> None:
|
||||
"""Test as_agent succeeds when dict-format function tools have implementations provided."""
|
||||
dict_function_tool = { # type: ignore
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "dict_based_function",
|
||||
"description": "A function defined as dict",
|
||||
"parameters": {"type": "object", "properties": {}},
|
||||
},
|
||||
}
|
||||
|
||||
mock_agent = MagicMock(spec=AzureAgent)
|
||||
mock_agent.id = "agent-id"
|
||||
mock_agent.name = "Agent"
|
||||
mock_agent.description = None
|
||||
mock_agent.instructions = None
|
||||
mock_agent.model = "gpt-4"
|
||||
mock_agent.temperature = None
|
||||
mock_agent.top_p = None
|
||||
mock_agent.tools = [dict_function_tool]
|
||||
|
||||
@tool
|
||||
def dict_based_function() -> str:
|
||||
"""A function implementation."""
|
||||
return "result"
|
||||
|
||||
provider = AzureAIAgentsProvider(agents_client=mock_agents_client)
|
||||
|
||||
agent = provider.as_agent(mock_agent, tools=dict_based_function)
|
||||
|
||||
assert isinstance(agent, Agent)
|
||||
assert agent.id == "agent-id"
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
# region Tool Conversion Tests - to_azure_ai_agent_tools
|
||||
|
||||
|
||||
def test_to_azure_ai_agent_tools_empty() -> None:
|
||||
"""Test converting empty tools list."""
|
||||
result = to_azure_ai_agent_tools(None)
|
||||
assert result == []
|
||||
|
||||
result = to_azure_ai_agent_tools([])
|
||||
assert result == []
|
||||
|
||||
|
||||
def test_to_azure_ai_agent_tools_function() -> None:
|
||||
"""Test converting FunctionTool to Azure tool definition."""
|
||||
|
||||
@tool(approval_mode="never_require")
|
||||
def get_weather(city: str) -> str:
|
||||
"""Get weather for a city."""
|
||||
return f"Weather in {city}"
|
||||
|
||||
result = to_azure_ai_agent_tools([get_weather])
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0]["type"] == "function"
|
||||
assert result[0]["function"]["name"] == "get_weather"
|
||||
|
||||
|
||||
def test_to_azure_ai_agent_tools_code_interpreter() -> None:
|
||||
"""Test converting code_interpreter dict tool."""
|
||||
tool = AzureAIAgentClient.get_code_interpreter_tool()
|
||||
|
||||
result = to_azure_ai_agent_tools([tool])
|
||||
|
||||
assert len(result) == 1
|
||||
assert isinstance(result[0], CodeInterpreterToolDefinition)
|
||||
|
||||
|
||||
def test_to_azure_ai_agent_tools_file_search() -> None:
|
||||
"""Test converting file_search dict tool with vector stores."""
|
||||
tool = AzureAIAgentClient.get_file_search_tool(vector_store_ids=["vs-123"])
|
||||
run_options: dict[str, Any] = {}
|
||||
|
||||
result = to_azure_ai_agent_tools([tool], run_options)
|
||||
|
||||
assert len(result) == 1
|
||||
assert "tool_resources" in run_options
|
||||
|
||||
|
||||
def test_to_azure_ai_agent_tools_web_search_bing_grounding(monkeypatch: Any) -> None:
|
||||
"""Test converting web_search dict tool for Bing Grounding."""
|
||||
# Use a properly formatted connection ID as required by Azure SDK
|
||||
valid_conn_id = (
|
||||
"/subscriptions/test-sub/resourceGroups/test-rg/"
|
||||
"providers/Microsoft.CognitiveServices/accounts/test-account/"
|
||||
"projects/test-project/connections/test-connection"
|
||||
)
|
||||
tool = AzureAIAgentClient.get_web_search_tool(bing_connection_id=valid_conn_id)
|
||||
|
||||
result = to_azure_ai_agent_tools([tool])
|
||||
|
||||
assert len(result) > 0
|
||||
|
||||
|
||||
def test_to_azure_ai_agent_tools_web_search_custom(monkeypatch: Any) -> None:
|
||||
"""Test converting web_search dict tool for Custom Bing Search."""
|
||||
tool = AzureAIAgentClient.get_web_search_tool(
|
||||
bing_custom_connection_id="custom-conn-id",
|
||||
bing_custom_instance_id="my-instance",
|
||||
)
|
||||
|
||||
result = to_azure_ai_agent_tools([tool])
|
||||
|
||||
assert len(result) > 0
|
||||
|
||||
|
||||
def test_to_azure_ai_agent_tools_web_search_missing_config(monkeypatch: Any) -> None:
|
||||
"""Test converting web_search dict tool without bing config returns empty."""
|
||||
monkeypatch.delenv("BING_CONNECTION_ID", raising=False)
|
||||
monkeypatch.delenv("BING_CUSTOM_CONNECTION_ID", raising=False)
|
||||
monkeypatch.delenv("BING_CUSTOM_INSTANCE_NAME", raising=False)
|
||||
tool = {"type": "web_search"}
|
||||
|
||||
result = to_azure_ai_agent_tools([tool])
|
||||
|
||||
# web_search without bing connection is passed through as dict
|
||||
assert len(result) == 1
|
||||
|
||||
|
||||
def test_to_azure_ai_agent_tools_mcp() -> None:
|
||||
"""Test converting MCP dict tool."""
|
||||
tool = AzureAIAgentClient.get_mcp_tool(
|
||||
name="my mcp server",
|
||||
url="https://mcp.example.com",
|
||||
)
|
||||
|
||||
result = to_azure_ai_agent_tools([tool])
|
||||
|
||||
assert len(result) > 0
|
||||
|
||||
|
||||
def test_to_azure_ai_agent_tools_dict_passthrough() -> None:
|
||||
"""Test that dict tools are passed through."""
|
||||
tool = {"type": "custom_tool", "config": {"key": "value"}}
|
||||
|
||||
result = to_azure_ai_agent_tools([tool])
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0] == tool
|
||||
|
||||
|
||||
def test_to_azure_ai_agent_tools_unsupported_type() -> None:
|
||||
"""Test that unsupported tool types pass through unchanged."""
|
||||
|
||||
class UnsupportedTool:
|
||||
pass
|
||||
|
||||
unsupported = UnsupportedTool()
|
||||
result = to_azure_ai_agent_tools([unsupported]) # type: ignore
|
||||
assert len(result) == 1
|
||||
assert result[0] is unsupported # Passed through unchanged
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
# region Tool Conversion Tests - from_azure_ai_agent_tools
|
||||
|
||||
|
||||
def test_from_azure_ai_agent_tools_empty() -> None:
|
||||
"""Test converting empty tools list."""
|
||||
result = from_azure_ai_agent_tools(None)
|
||||
assert result == []
|
||||
|
||||
result = from_azure_ai_agent_tools([])
|
||||
assert result == []
|
||||
|
||||
|
||||
def test_from_azure_ai_agent_tools_code_interpreter() -> None:
|
||||
"""Test converting CodeInterpreterToolDefinition."""
|
||||
tool = CodeInterpreterToolDefinition()
|
||||
|
||||
result = from_azure_ai_agent_tools([tool])
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0] == {"type": "code_interpreter"}
|
||||
|
||||
|
||||
def test_from_azure_ai_agent_tools_code_interpreter_dict() -> None:
|
||||
"""Test converting code_interpreter dict."""
|
||||
tool = {"type": "code_interpreter"}
|
||||
|
||||
result = from_azure_ai_agent_tools([tool])
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0] == {"type": "code_interpreter"}
|
||||
|
||||
|
||||
def test_from_azure_ai_agent_tools_file_search_dict() -> None:
|
||||
"""Test converting file_search dict with vector store IDs."""
|
||||
tool = {
|
||||
"type": "file_search",
|
||||
"file_search": {"vector_store_ids": ["vs-123", "vs-456"]},
|
||||
}
|
||||
|
||||
result = from_azure_ai_agent_tools([tool])
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0]["type"] == "file_search"
|
||||
assert result[0]["vector_store_ids"] == ["vs-123", "vs-456"]
|
||||
|
||||
|
||||
def test_from_azure_ai_agent_tools_bing_grounding_dict() -> None:
|
||||
"""Test converting bing_grounding dict."""
|
||||
tool = {
|
||||
"type": "bing_grounding",
|
||||
"bing_grounding": {"connection_id": "conn-123"},
|
||||
}
|
||||
|
||||
result = from_azure_ai_agent_tools([tool])
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0]["type"] == "bing_grounding"
|
||||
assert result[0]["connection_id"] == "conn-123"
|
||||
|
||||
|
||||
def test_from_azure_ai_agent_tools_bing_custom_search_dict() -> None:
|
||||
"""Test converting bing_custom_search dict."""
|
||||
tool = {
|
||||
"type": "bing_custom_search",
|
||||
"bing_custom_search": {
|
||||
"connection_id": "custom-conn",
|
||||
"instance_name": "my-instance",
|
||||
},
|
||||
}
|
||||
|
||||
result = from_azure_ai_agent_tools([tool])
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0]["type"] == "bing_custom_search"
|
||||
assert result[0]["connection_id"] == "custom-conn"
|
||||
assert result[0]["instance_name"] == "my-instance"
|
||||
|
||||
|
||||
def test_from_azure_ai_agent_tools_mcp_dict() -> None:
|
||||
"""Test that mcp dict is skipped (hosted on Azure, no local handling needed)."""
|
||||
tool = {
|
||||
"type": "mcp",
|
||||
"mcp": {
|
||||
"server_label": "my_server",
|
||||
"server_url": "https://mcp.example.com",
|
||||
"allowed_tools": ["tool1"],
|
||||
},
|
||||
}
|
||||
|
||||
result = from_azure_ai_agent_tools([tool])
|
||||
|
||||
# MCP tools are hosted on Azure agent, skipped in conversion
|
||||
assert len(result) == 0
|
||||
|
||||
|
||||
def test_from_azure_ai_agent_tools_function_dict() -> None:
|
||||
"""Test converting function tool dict (returned as-is)."""
|
||||
tool: dict[str, Any] = {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_weather",
|
||||
"description": "Get weather",
|
||||
"parameters": {},
|
||||
},
|
||||
}
|
||||
|
||||
result = from_azure_ai_agent_tools([tool])
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0] == tool
|
||||
|
||||
|
||||
def test_from_azure_ai_agent_tools_unknown_dict() -> None:
|
||||
"""Test converting unknown tool type dict."""
|
||||
tool = {"type": "unknown_tool", "config": "value"}
|
||||
|
||||
result = from_azure_ai_agent_tools([tool])
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0] == tool
|
||||
|
||||
|
||||
# endregion
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,682 +0,0 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from agent_framework import Agent, FunctionTool
|
||||
from agent_framework._mcp import MCPTool
|
||||
from azure.ai.projects.models import (
|
||||
AgentVersionDetails,
|
||||
PromptAgentDefinition,
|
||||
)
|
||||
from azure.ai.projects.models import (
|
||||
FunctionTool as AzureFunctionTool,
|
||||
)
|
||||
|
||||
from agent_framework_azure_ai import AzureAIProjectAgentProvider
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_project_client() -> MagicMock:
|
||||
"""Fixture that provides a mock AIProjectClient."""
|
||||
mock_client = MagicMock()
|
||||
|
||||
# Mock agents property
|
||||
mock_client.agents = MagicMock()
|
||||
mock_client.agents.create_version = AsyncMock()
|
||||
|
||||
# Mock conversations property
|
||||
mock_client.conversations = MagicMock()
|
||||
mock_client.conversations.create = AsyncMock()
|
||||
|
||||
# Mock telemetry property
|
||||
mock_client.telemetry = MagicMock()
|
||||
mock_client.telemetry.get_application_insights_connection_string = AsyncMock()
|
||||
|
||||
# AIProjectClient.get_openai_client() is a sync accessor, even on the aio client.
|
||||
mock_client.get_openai_client = MagicMock(return_value=MagicMock())
|
||||
|
||||
# Mock close method
|
||||
mock_client.close = AsyncMock()
|
||||
|
||||
return mock_client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_azure_credential() -> MagicMock:
|
||||
"""Fixture that provides a mock Azure credential."""
|
||||
return MagicMock()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def azure_ai_unit_test_env(monkeypatch: pytest.MonkeyPatch) -> dict[str, str]:
|
||||
"""Fixture that sets up Azure AI environment variables for unit testing."""
|
||||
env_vars = {
|
||||
"AZURE_AI_PROJECT_ENDPOINT": "https://test-project.cognitiveservices.azure.com/",
|
||||
"AZURE_AI_MODEL_DEPLOYMENT_NAME": "test-model-deployment",
|
||||
}
|
||||
for key, value in env_vars.items():
|
||||
monkeypatch.setenv(key, value)
|
||||
return env_vars
|
||||
|
||||
|
||||
def test_provider_init_with_project_client(mock_project_client: MagicMock) -> None:
|
||||
"""Test AzureAIProjectAgentProvider initialization with existing project_client."""
|
||||
provider = AzureAIProjectAgentProvider(project_client=mock_project_client)
|
||||
|
||||
assert provider._project_client is mock_project_client # type: ignore
|
||||
assert not provider._should_close_client # type: ignore
|
||||
|
||||
|
||||
def test_provider_init_with_credential_and_endpoint(
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
mock_azure_credential: MagicMock,
|
||||
) -> None:
|
||||
"""Test AzureAIProjectAgentProvider initialization with credential and endpoint."""
|
||||
with patch("agent_framework_azure_ai._project_provider.AIProjectClient") as mock_ai_project_client:
|
||||
mock_client = MagicMock()
|
||||
mock_ai_project_client.return_value = mock_client
|
||||
|
||||
provider = AzureAIProjectAgentProvider(
|
||||
project_endpoint=azure_ai_unit_test_env["AZURE_AI_PROJECT_ENDPOINT"],
|
||||
credential=mock_azure_credential,
|
||||
)
|
||||
|
||||
assert provider._project_client is mock_client # type: ignore
|
||||
assert provider._should_close_client # type: ignore
|
||||
|
||||
# Verify AIProjectClient was called with correct parameters
|
||||
mock_ai_project_client.assert_called_once()
|
||||
|
||||
|
||||
def test_provider_init_missing_endpoint() -> None:
|
||||
"""Test AzureAIProjectAgentProvider initialization when endpoint is missing."""
|
||||
with patch("agent_framework_azure_ai._project_provider.load_settings") as mock_load_settings:
|
||||
mock_load_settings.return_value = {"project_endpoint": None, "model_deployment_name": "test-model"}
|
||||
|
||||
with pytest.raises(ValueError, match="Azure AI project endpoint is required"):
|
||||
AzureAIProjectAgentProvider(credential=MagicMock())
|
||||
|
||||
|
||||
def test_provider_init_missing_credential(azure_ai_unit_test_env: dict[str, str]) -> None:
|
||||
"""Test AzureAIProjectAgentProvider initialization when credential is missing."""
|
||||
with pytest.raises(ValueError, match="Azure credential is required when project_client is not provided"):
|
||||
AzureAIProjectAgentProvider(
|
||||
project_endpoint=azure_ai_unit_test_env["AZURE_AI_PROJECT_ENDPOINT"],
|
||||
)
|
||||
|
||||
|
||||
async def test_provider_create_agent(
|
||||
mock_project_client: MagicMock,
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
) -> None:
|
||||
"""Test AzureAIProjectAgentProvider.create_agent method."""
|
||||
with patch("agent_framework_azure_ai._project_provider.load_settings") as mock_load_settings:
|
||||
mock_load_settings.return_value = {
|
||||
"project_endpoint": azure_ai_unit_test_env["AZURE_AI_PROJECT_ENDPOINT"],
|
||||
"model_deployment_name": azure_ai_unit_test_env["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
|
||||
}
|
||||
|
||||
provider = AzureAIProjectAgentProvider(project_client=mock_project_client)
|
||||
|
||||
# Mock agent creation response
|
||||
mock_agent_version = MagicMock(spec=AgentVersionDetails)
|
||||
mock_agent_version.id = "agent-id"
|
||||
mock_agent_version.name = "test-agent"
|
||||
mock_agent_version.version = "1.0"
|
||||
mock_agent_version.description = "Test Agent"
|
||||
mock_agent_version.definition = MagicMock(spec=PromptAgentDefinition)
|
||||
mock_agent_version.definition.model = "gpt-4"
|
||||
mock_agent_version.definition.instructions = "Test instructions"
|
||||
mock_agent_version.definition.temperature = 0.7
|
||||
mock_agent_version.definition.top_p = 0.9
|
||||
mock_agent_version.definition.tools = []
|
||||
|
||||
mock_project_client.agents.create_version = AsyncMock(return_value=mock_agent_version)
|
||||
|
||||
agent = await provider.create_agent(
|
||||
name="test-agent",
|
||||
model="gpt-4",
|
||||
instructions="Test instructions",
|
||||
description="Test Agent",
|
||||
)
|
||||
|
||||
assert isinstance(agent, Agent)
|
||||
assert agent.name == "test-agent"
|
||||
mock_project_client.agents.create_version.assert_called_once()
|
||||
|
||||
|
||||
async def test_provider_create_agent_with_env_model(
|
||||
mock_project_client: MagicMock,
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
) -> None:
|
||||
"""Test AzureAIProjectAgentProvider.create_agent uses model from env var."""
|
||||
with patch("agent_framework_azure_ai._project_provider.load_settings") as mock_load_settings:
|
||||
mock_load_settings.return_value = {
|
||||
"project_endpoint": azure_ai_unit_test_env["AZURE_AI_PROJECT_ENDPOINT"],
|
||||
"model_deployment_name": azure_ai_unit_test_env["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
|
||||
}
|
||||
|
||||
provider = AzureAIProjectAgentProvider(project_client=mock_project_client)
|
||||
|
||||
# Mock agent creation response
|
||||
mock_agent_version = MagicMock(spec=AgentVersionDetails)
|
||||
mock_agent_version.id = "agent-id"
|
||||
mock_agent_version.name = "test-agent"
|
||||
mock_agent_version.version = "1.0"
|
||||
mock_agent_version.description = None
|
||||
mock_agent_version.definition = MagicMock(spec=PromptAgentDefinition)
|
||||
mock_agent_version.definition.model = azure_ai_unit_test_env["AZURE_AI_MODEL_DEPLOYMENT_NAME"]
|
||||
mock_agent_version.definition.instructions = None
|
||||
mock_agent_version.definition.temperature = None
|
||||
mock_agent_version.definition.top_p = None
|
||||
mock_agent_version.definition.tools = []
|
||||
|
||||
mock_project_client.agents.create_version = AsyncMock(return_value=mock_agent_version)
|
||||
|
||||
# Call without model parameter - should use env var
|
||||
agent = await provider.create_agent(name="test-agent")
|
||||
|
||||
assert isinstance(agent, Agent)
|
||||
# Verify the model from env var was used
|
||||
call_args = mock_project_client.agents.create_version.call_args
|
||||
assert call_args[1]["definition"].model == azure_ai_unit_test_env["AZURE_AI_MODEL_DEPLOYMENT_NAME"]
|
||||
|
||||
|
||||
async def test_provider_create_agent_missing_model(mock_project_client: MagicMock) -> None:
|
||||
"""Test AzureAIProjectAgentProvider.create_agent raises when model is missing."""
|
||||
with patch("agent_framework_azure_ai._project_provider.load_settings") as mock_load_settings:
|
||||
mock_load_settings.return_value = {"project_endpoint": "https://test.com", "model_deployment_name": None}
|
||||
|
||||
provider = AzureAIProjectAgentProvider(project_client=mock_project_client)
|
||||
|
||||
with pytest.raises(ValueError, match="Model deployment name is required"):
|
||||
await provider.create_agent(name="test-agent")
|
||||
|
||||
|
||||
async def test_provider_create_agent_with_rai_config(
|
||||
mock_project_client: MagicMock,
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
) -> None:
|
||||
"""Test AzureAIProjectAgentProvider.create_agent passes rai_config from default_options."""
|
||||
with patch("agent_framework_azure_ai._project_provider.load_settings") as mock_load_settings:
|
||||
mock_load_settings.return_value = {
|
||||
"project_endpoint": azure_ai_unit_test_env["AZURE_AI_PROJECT_ENDPOINT"],
|
||||
"model_deployment_name": azure_ai_unit_test_env["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
|
||||
}
|
||||
|
||||
provider = AzureAIProjectAgentProvider(project_client=mock_project_client)
|
||||
|
||||
# Mock agent creation response
|
||||
mock_agent_version = MagicMock(spec=AgentVersionDetails)
|
||||
mock_agent_version.id = "agent-id"
|
||||
mock_agent_version.name = "test-agent"
|
||||
mock_agent_version.version = "1.0"
|
||||
mock_agent_version.description = None
|
||||
mock_agent_version.definition = MagicMock(spec=PromptAgentDefinition)
|
||||
mock_agent_version.definition.model = "gpt-4"
|
||||
mock_agent_version.definition.instructions = None
|
||||
mock_agent_version.definition.temperature = None
|
||||
mock_agent_version.definition.top_p = None
|
||||
mock_agent_version.definition.tools = []
|
||||
|
||||
mock_project_client.agents.create_version = AsyncMock(return_value=mock_agent_version)
|
||||
|
||||
# Create a mock RaiConfig-like object
|
||||
mock_rai_config = MagicMock()
|
||||
mock_rai_config.rai_policy_name = "policy-name"
|
||||
|
||||
# Call create_agent with rai_config in default_options
|
||||
await provider.create_agent(
|
||||
name="test-agent",
|
||||
model="gpt-4",
|
||||
default_options={"rai_config": mock_rai_config},
|
||||
)
|
||||
|
||||
# Verify rai_config was passed to PromptAgentDefinition
|
||||
call_args = mock_project_client.agents.create_version.call_args
|
||||
definition = call_args[1]["definition"]
|
||||
assert definition.rai_config is mock_rai_config
|
||||
|
||||
|
||||
async def test_provider_create_agent_with_reasoning(
|
||||
mock_project_client: MagicMock,
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
) -> None:
|
||||
"""Test AzureAIProjectAgentProvider.create_agent passes reasoning from default_options."""
|
||||
with patch("agent_framework_azure_ai._project_provider.load_settings") as mock_load_settings:
|
||||
mock_load_settings.return_value = {
|
||||
"project_endpoint": azure_ai_unit_test_env["AZURE_AI_PROJECT_ENDPOINT"],
|
||||
"model_deployment_name": azure_ai_unit_test_env["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
|
||||
}
|
||||
|
||||
provider = AzureAIProjectAgentProvider(project_client=mock_project_client)
|
||||
|
||||
# Mock agent creation response
|
||||
mock_agent_version = MagicMock(spec=AgentVersionDetails)
|
||||
mock_agent_version.id = "agent-id"
|
||||
mock_agent_version.name = "test-agent"
|
||||
mock_agent_version.version = "1.0"
|
||||
mock_agent_version.description = None
|
||||
mock_agent_version.definition = MagicMock(spec=PromptAgentDefinition)
|
||||
mock_agent_version.definition.model = "gpt-5.2"
|
||||
mock_agent_version.definition.instructions = None
|
||||
mock_agent_version.definition.temperature = None
|
||||
mock_agent_version.definition.top_p = None
|
||||
mock_agent_version.definition.tools = []
|
||||
|
||||
mock_project_client.agents.create_version = AsyncMock(return_value=mock_agent_version)
|
||||
|
||||
# Create a mock Reasoning-like object
|
||||
mock_reasoning = MagicMock()
|
||||
mock_reasoning.effort = "medium"
|
||||
mock_reasoning.summary = "concise"
|
||||
|
||||
# Call create_agent with reasoning in default_options
|
||||
await provider.create_agent(
|
||||
name="test-agent",
|
||||
model="gpt-5.2",
|
||||
default_options={"reasoning": mock_reasoning},
|
||||
)
|
||||
|
||||
# Verify reasoning was passed to PromptAgentDefinition
|
||||
call_args = mock_project_client.agents.create_version.call_args
|
||||
definition = call_args[1]["definition"]
|
||||
assert definition.reasoning is mock_reasoning
|
||||
|
||||
|
||||
async def test_provider_get_agent_with_name(mock_project_client: MagicMock) -> None:
|
||||
"""Test AzureAIProjectAgentProvider.get_agent with name parameter."""
|
||||
provider = AzureAIProjectAgentProvider(project_client=mock_project_client)
|
||||
|
||||
# Mock agent response
|
||||
mock_agent_version = MagicMock(spec=AgentVersionDetails)
|
||||
mock_agent_version.id = "agent-id"
|
||||
mock_agent_version.name = "test-agent"
|
||||
mock_agent_version.version = "1.0"
|
||||
mock_agent_version.description = "Test Agent"
|
||||
mock_agent_version.definition = MagicMock(spec=PromptAgentDefinition)
|
||||
mock_agent_version.definition.model = "gpt-4"
|
||||
mock_agent_version.definition.instructions = "Test instructions"
|
||||
mock_agent_version.definition.temperature = None
|
||||
mock_agent_version.definition.top_p = None
|
||||
mock_agent_version.definition.tools = []
|
||||
|
||||
mock_agent_object = MagicMock()
|
||||
mock_agent_object.versions.latest = mock_agent_version
|
||||
|
||||
mock_project_client.agents = AsyncMock()
|
||||
mock_project_client.agents.get.return_value = mock_agent_object
|
||||
|
||||
agent = await provider.get_agent(name="test-agent")
|
||||
|
||||
assert isinstance(agent, Agent)
|
||||
assert agent.name == "test-agent"
|
||||
mock_project_client.agents.get.assert_called_with(agent_name="test-agent")
|
||||
|
||||
|
||||
async def test_provider_get_agent_with_reference(mock_project_client: MagicMock) -> None:
|
||||
"""Test AzureAIProjectAgentProvider.get_agent with reference parameter."""
|
||||
provider = AzureAIProjectAgentProvider(project_client=mock_project_client)
|
||||
|
||||
# Mock agent response
|
||||
mock_agent_version = MagicMock(spec=AgentVersionDetails)
|
||||
mock_agent_version.id = "agent-id"
|
||||
mock_agent_version.name = "test-agent"
|
||||
mock_agent_version.version = "1.0"
|
||||
mock_agent_version.description = "Test Agent"
|
||||
mock_agent_version.definition = MagicMock(spec=PromptAgentDefinition)
|
||||
mock_agent_version.definition.model = "gpt-4"
|
||||
mock_agent_version.definition.instructions = "Test instructions"
|
||||
mock_agent_version.definition.temperature = None
|
||||
mock_agent_version.definition.top_p = None
|
||||
mock_agent_version.definition.tools = []
|
||||
|
||||
mock_project_client.agents = AsyncMock()
|
||||
mock_project_client.agents.get_version.return_value = mock_agent_version
|
||||
|
||||
agent_reference = {"name": "test-agent", "version": "1.0"}
|
||||
agent = await provider.get_agent(reference=agent_reference)
|
||||
|
||||
assert isinstance(agent, Agent)
|
||||
assert agent.name == "test-agent"
|
||||
mock_project_client.agents.get_version.assert_called_with(agent_name="test-agent", agent_version="1.0")
|
||||
|
||||
|
||||
async def test_provider_get_agent_missing_parameters(mock_project_client: MagicMock) -> None:
|
||||
"""Test AzureAIProjectAgentProvider.get_agent raises when no identifier provided."""
|
||||
provider = AzureAIProjectAgentProvider(project_client=mock_project_client)
|
||||
|
||||
with pytest.raises(ValueError, match="Either name or reference must be provided"):
|
||||
await provider.get_agent()
|
||||
|
||||
|
||||
async def test_provider_get_agent_missing_function_tools(mock_project_client: MagicMock) -> None:
|
||||
"""Test AzureAIProjectAgentProvider.get_agent raises when required tools are missing."""
|
||||
provider = AzureAIProjectAgentProvider(project_client=mock_project_client)
|
||||
|
||||
# Mock agent with function tools
|
||||
mock_agent_version = MagicMock(spec=AgentVersionDetails)
|
||||
mock_agent_version.id = "agent-id"
|
||||
mock_agent_version.name = "test-agent"
|
||||
mock_agent_version.version = "1.0"
|
||||
mock_agent_version.description = None
|
||||
mock_agent_version.definition = MagicMock(spec=PromptAgentDefinition)
|
||||
mock_agent_version.definition.tools = [
|
||||
AzureFunctionTool(name="test_tool", parameters=[], strict=True, description="Test tool")
|
||||
]
|
||||
|
||||
mock_agent_object = MagicMock()
|
||||
mock_agent_object.versions.latest = mock_agent_version
|
||||
|
||||
mock_project_client.agents = AsyncMock()
|
||||
mock_project_client.agents.get.return_value = mock_agent_object
|
||||
|
||||
with pytest.raises(
|
||||
ValueError, match="The following prompt agent definition required tools were not provided: test_tool"
|
||||
):
|
||||
await provider.get_agent(name="test-agent")
|
||||
|
||||
|
||||
def test_provider_as_agent(mock_project_client: MagicMock) -> None:
|
||||
"""Test AzureAIProjectAgentProvider.as_agent method."""
|
||||
provider = AzureAIProjectAgentProvider(project_client=mock_project_client)
|
||||
|
||||
# Create mock agent version
|
||||
mock_agent_version = MagicMock(spec=AgentVersionDetails)
|
||||
mock_agent_version.id = "agent-id"
|
||||
mock_agent_version.name = "test-agent"
|
||||
mock_agent_version.version = "1.0"
|
||||
mock_agent_version.description = "Test Agent"
|
||||
mock_agent_version.definition = MagicMock(spec=PromptAgentDefinition)
|
||||
mock_agent_version.definition.model = "gpt-4"
|
||||
mock_agent_version.definition.instructions = "Test instructions"
|
||||
mock_agent_version.definition.temperature = 0.7
|
||||
mock_agent_version.definition.top_p = 0.9
|
||||
mock_agent_version.definition.tools = []
|
||||
|
||||
with patch("agent_framework_azure_ai._project_provider.AzureAIClient") as mock_azure_ai_client:
|
||||
agent = provider.as_agent(mock_agent_version)
|
||||
|
||||
assert isinstance(agent, Agent)
|
||||
assert agent.name == "test-agent"
|
||||
assert agent.description == "Test Agent"
|
||||
|
||||
# Verify AzureAIClient was called with correct parameters
|
||||
mock_azure_ai_client.assert_called_once()
|
||||
call_kwargs = mock_azure_ai_client.call_args[1]
|
||||
assert call_kwargs["project_client"] is mock_project_client
|
||||
assert call_kwargs["agent_name"] == "test-agent"
|
||||
assert call_kwargs["agent_version"] == "1.0"
|
||||
assert call_kwargs["agent_description"] == "Test Agent"
|
||||
assert call_kwargs["model_deployment_name"] == "gpt-4"
|
||||
|
||||
|
||||
def test_provider_merge_tools_skips_function_tool_dicts(mock_project_client: MagicMock) -> None:
|
||||
"""Test that _merge_tools skips function tool dicts but keeps other hosted tools."""
|
||||
provider = AzureAIProjectAgentProvider(project_client=mock_project_client)
|
||||
|
||||
# Create a mock FunctionTool to provide as implementation
|
||||
mock_ai_function = create_mock_ai_function("my_function", "My function description")
|
||||
|
||||
# Definition tools include a function tool (dict) and an MCP tool
|
||||
definition_tools = [
|
||||
{"type": "function", "name": "my_function", "parameters": {}}, # Should be skipped
|
||||
{"type": "mcp", "server_label": "my_mcp", "server_url": "http://localhost:8080"}, # Should be converted
|
||||
]
|
||||
|
||||
# Call _merge_tools with user-provided function implementation
|
||||
merged = provider._merge_tools(definition_tools, [mock_ai_function]) # type: ignore
|
||||
|
||||
# Should have 2 items: the converted MCP dict and the user-provided FunctionTool
|
||||
assert len(merged) == 2
|
||||
|
||||
# Check that the function tool dict was NOT included (it was skipped)
|
||||
function_dicts = [t for t in merged if isinstance(t, dict) and t.get("type") == "function"]
|
||||
assert len(function_dicts) == 0
|
||||
|
||||
# Check that the MCP tool was converted to dict
|
||||
mcp_tools = [t for t in merged if isinstance(t, dict) and t.get("type") == "mcp"]
|
||||
assert len(mcp_tools) == 1
|
||||
assert mcp_tools[0]["server_label"] == "my_mcp"
|
||||
|
||||
# Check that the user-provided FunctionTool was included
|
||||
ai_functions = [t for t in merged if isinstance(t, FunctionTool)]
|
||||
assert len(ai_functions) == 1
|
||||
assert ai_functions[0].name == "my_function"
|
||||
|
||||
|
||||
async def test_provider_context_manager(mock_project_client: MagicMock) -> None:
|
||||
"""Test AzureAIProjectAgentProvider async context manager."""
|
||||
with patch("agent_framework_azure_ai._project_provider.AIProjectClient") as mock_ai_project_client:
|
||||
mock_client = MagicMock()
|
||||
mock_client.close = AsyncMock()
|
||||
mock_ai_project_client.return_value = mock_client
|
||||
|
||||
with patch("agent_framework_azure_ai._project_provider.load_settings") as mock_load_settings:
|
||||
mock_load_settings.return_value = {
|
||||
"project_endpoint": "https://test.com",
|
||||
"model_deployment_name": "test-model",
|
||||
}
|
||||
|
||||
async with AzureAIProjectAgentProvider(credential=MagicMock()) as provider:
|
||||
assert provider._project_client is mock_client # type: ignore
|
||||
|
||||
# Should call close after exiting context
|
||||
mock_client.close.assert_called_once()
|
||||
|
||||
|
||||
async def test_provider_context_manager_with_provided_client(mock_project_client: MagicMock) -> None:
|
||||
"""Test AzureAIProjectAgentProvider context manager doesn't close provided client."""
|
||||
mock_project_client.close = AsyncMock()
|
||||
|
||||
async with AzureAIProjectAgentProvider(project_client=mock_project_client) as provider:
|
||||
assert provider._project_client is mock_project_client # type: ignore
|
||||
|
||||
# Should NOT call close when client was provided
|
||||
mock_project_client.close.assert_not_called()
|
||||
|
||||
|
||||
async def test_provider_close_method(mock_project_client: MagicMock) -> None:
|
||||
"""Test AzureAIProjectAgentProvider.close method."""
|
||||
with patch("agent_framework_azure_ai._project_provider.AIProjectClient") as mock_ai_project_client:
|
||||
mock_client = MagicMock()
|
||||
mock_client.close = AsyncMock()
|
||||
mock_ai_project_client.return_value = mock_client
|
||||
|
||||
with patch("agent_framework_azure_ai._project_provider.load_settings") as mock_load_settings:
|
||||
mock_load_settings.return_value = {
|
||||
"project_endpoint": "https://test.com",
|
||||
"model_deployment_name": "test-model",
|
||||
}
|
||||
|
||||
provider = AzureAIProjectAgentProvider(credential=MagicMock())
|
||||
await provider.close()
|
||||
|
||||
mock_client.close.assert_called_once()
|
||||
|
||||
|
||||
def test_create_text_format_config_sets_strict_for_pydantic_models() -> None:
|
||||
"""Test that create_text_format_config sets strict=True for Pydantic models."""
|
||||
from pydantic import BaseModel
|
||||
|
||||
from agent_framework_azure_ai._shared import create_text_format_config
|
||||
|
||||
class TestSchema(BaseModel):
|
||||
subject: str
|
||||
summary: str
|
||||
|
||||
result = create_text_format_config(TestSchema)
|
||||
|
||||
# Verify strict=True is set
|
||||
assert result["strict"] is True
|
||||
assert result["name"] == "TestSchema"
|
||||
assert "schema" in result
|
||||
|
||||
|
||||
class MockMCPTool(MCPTool): # pyright: ignore[reportGeneralTypeIssues]
|
||||
"""A mock MCPTool subclass for testing that passes isinstance checks.
|
||||
|
||||
Note: This intentionally does NOT call super().__init__() because MCPTool's
|
||||
constructor requires MCP server connection parameters that aren't needed for
|
||||
unit testing. We only need isinstance(obj, MCPTool) to return True.
|
||||
"""
|
||||
|
||||
def __init__(self, functions: list[FunctionTool] | None = None) -> None:
|
||||
self.name = "MockMCPTool"
|
||||
self.description = "A mock MCP tool for testing"
|
||||
self.is_connected = False
|
||||
self._mock_functions = functions or []
|
||||
self._connect_called = False
|
||||
|
||||
@property
|
||||
def functions(self) -> list[FunctionTool]:
|
||||
return self._mock_functions
|
||||
|
||||
async def connect(self, *, reset: bool = False) -> None:
|
||||
self._connect_called = True
|
||||
self.is_connected = True
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_mcp_tool() -> MockMCPTool:
|
||||
"""Fixture that provides a mock MCPTool."""
|
||||
mock_functions = [
|
||||
create_mock_ai_function("mcp_function_1", "First MCP function"),
|
||||
create_mock_ai_function("mcp_function_2", "Second MCP function"),
|
||||
]
|
||||
return MockMCPTool(functions=mock_functions)
|
||||
|
||||
|
||||
def create_mock_ai_function(name: str, description: str = "A mock function") -> FunctionTool:
|
||||
"""Create a real FunctionTool for testing."""
|
||||
|
||||
def mock_func(arg: str) -> str:
|
||||
return f"Result from {name}: {arg}"
|
||||
|
||||
return FunctionTool(func=mock_func, name=name, description=description, approval_mode="never_require")
|
||||
|
||||
|
||||
async def test_provider_create_agent_with_mcp_tool(
|
||||
mock_project_client: MagicMock,
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
mock_mcp_tool: "MockMCPTool",
|
||||
) -> None:
|
||||
"""Test that create_agent connects MCP tools and passes discovered functions to Azure AI."""
|
||||
|
||||
# Patch normalize_tools to return tools as-is in a list (avoids callable check)
|
||||
def mock_normalize_tools(tools):
|
||||
if tools is None:
|
||||
return []
|
||||
if isinstance(tools, list):
|
||||
return tools
|
||||
return [tools]
|
||||
|
||||
with (
|
||||
patch("agent_framework_azure_ai._project_provider.load_settings") as mock_load_settings,
|
||||
patch("agent_framework_azure_ai._project_provider.to_azure_ai_tools") as mock_to_azure_tools,
|
||||
patch("agent_framework_azure_ai._project_provider.normalize_tools", side_effect=mock_normalize_tools),
|
||||
):
|
||||
mock_load_settings.return_value = {
|
||||
"project_endpoint": azure_ai_unit_test_env["AZURE_AI_PROJECT_ENDPOINT"],
|
||||
"model_deployment_name": azure_ai_unit_test_env["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
|
||||
}
|
||||
mock_to_azure_tools.return_value = [{"type": "function", "name": "mcp_function_1"}]
|
||||
|
||||
provider = AzureAIProjectAgentProvider(project_client=mock_project_client)
|
||||
|
||||
# Mock agent creation response
|
||||
mock_agent_version = MagicMock(spec=AgentVersionDetails)
|
||||
mock_agent_version.id = "agent-id"
|
||||
mock_agent_version.name = "test-agent"
|
||||
mock_agent_version.version = "1.0"
|
||||
mock_agent_version.description = "Test Agent"
|
||||
mock_agent_version.definition = MagicMock(spec=PromptAgentDefinition)
|
||||
mock_agent_version.definition.model = "gpt-4"
|
||||
mock_agent_version.definition.instructions = "Test instructions"
|
||||
mock_agent_version.definition.tools = []
|
||||
|
||||
mock_project_client.agents.create_version = AsyncMock(return_value=mock_agent_version)
|
||||
|
||||
# Call create_agent with MCP tool
|
||||
await provider.create_agent(
|
||||
name="test-agent",
|
||||
model="gpt-4",
|
||||
instructions="Test instructions",
|
||||
tools=mock_mcp_tool,
|
||||
)
|
||||
|
||||
# Verify MCP tool was connected
|
||||
assert mock_mcp_tool._connect_called is True
|
||||
assert mock_mcp_tool.is_connected is True
|
||||
|
||||
# Verify to_azure_ai_tools was called with the discovered MCP functions
|
||||
mock_to_azure_tools.assert_called_once()
|
||||
tools_passed = mock_to_azure_tools.call_args[0][0]
|
||||
assert len(tools_passed) == 2
|
||||
assert tools_passed[0].name == "mcp_function_1"
|
||||
assert tools_passed[1].name == "mcp_function_2"
|
||||
|
||||
|
||||
async def test_provider_create_agent_with_mcp_and_regular_tools(
|
||||
mock_project_client: MagicMock,
|
||||
azure_ai_unit_test_env: dict[str, str],
|
||||
mock_mcp_tool: "MockMCPTool",
|
||||
) -> None:
|
||||
"""Test that create_agent handles both MCP tools and regular FunctionTools."""
|
||||
# Create a regular FunctionTool
|
||||
regular_function = create_mock_ai_function("regular_function", "A regular function")
|
||||
|
||||
# Patch normalize_tools to return tools as-is in a list (avoids callable check)
|
||||
def mock_normalize_tools(tools):
|
||||
if tools is None:
|
||||
return []
|
||||
if isinstance(tools, list):
|
||||
return tools
|
||||
return [tools]
|
||||
|
||||
with (
|
||||
patch("agent_framework_azure_ai._project_provider.load_settings") as mock_load_settings,
|
||||
patch("agent_framework_azure_ai._project_provider.to_azure_ai_tools") as mock_to_azure_tools,
|
||||
patch("agent_framework_azure_ai._project_provider.normalize_tools", side_effect=mock_normalize_tools),
|
||||
):
|
||||
mock_load_settings.return_value = {
|
||||
"project_endpoint": azure_ai_unit_test_env["AZURE_AI_PROJECT_ENDPOINT"],
|
||||
"model_deployment_name": azure_ai_unit_test_env["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
|
||||
}
|
||||
mock_to_azure_tools.return_value = []
|
||||
|
||||
provider = AzureAIProjectAgentProvider(project_client=mock_project_client)
|
||||
|
||||
# Mock agent creation response
|
||||
mock_agent_version = MagicMock(spec=AgentVersionDetails)
|
||||
mock_agent_version.id = "agent-id"
|
||||
mock_agent_version.name = "test-agent"
|
||||
mock_agent_version.version = "1.0"
|
||||
mock_agent_version.description = None
|
||||
mock_agent_version.definition = MagicMock(spec=PromptAgentDefinition)
|
||||
mock_agent_version.definition.model = "gpt-4"
|
||||
mock_agent_version.definition.instructions = None
|
||||
mock_agent_version.definition.tools = []
|
||||
|
||||
mock_project_client.agents.create_version = AsyncMock(return_value=mock_agent_version)
|
||||
|
||||
# Pass both MCP tool and regular function
|
||||
await provider.create_agent(
|
||||
name="test-agent",
|
||||
model="gpt-4",
|
||||
tools=[mock_mcp_tool, regular_function],
|
||||
)
|
||||
|
||||
# Verify to_azure_ai_tools was called with:
|
||||
# - The regular FunctionTool (1)
|
||||
# - The 2 discovered MCP functions
|
||||
mock_to_azure_tools.assert_called_once()
|
||||
tools_passed = mock_to_azure_tools.call_args[0][0]
|
||||
assert len(tools_passed) == 3 # 1 regular + 2 MCP functions
|
||||
|
||||
# Verify the regular function is in the list
|
||||
tool_names = [t.name for t in tools_passed]
|
||||
assert "regular_function" in tool_names
|
||||
assert "mcp_function_1" in tool_names
|
||||
assert "mcp_function_2" in tool_names
|
||||
@@ -1,494 +0,0 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import os
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from agent_framework import (
|
||||
FunctionTool,
|
||||
)
|
||||
from agent_framework.exceptions import IntegrationInvalidRequestException
|
||||
from azure.ai.agents.models import CodeInterpreterToolDefinition
|
||||
from pydantic import BaseModel
|
||||
|
||||
from agent_framework_azure_ai import AzureAIAgentClient
|
||||
from agent_framework_azure_ai._shared import (
|
||||
_convert_response_format, # type: ignore
|
||||
_convert_sdk_tool, # type: ignore
|
||||
_extract_project_connection_id, # type: ignore
|
||||
create_text_format_config,
|
||||
from_azure_ai_agent_tools,
|
||||
from_azure_ai_tools,
|
||||
to_azure_ai_agent_tools,
|
||||
to_azure_ai_tools,
|
||||
)
|
||||
from agent_framework_azure_ai._shared import (
|
||||
_prepare_mcp_tool_dict_for_azure_ai as _prepare_mcp_tool_for_azure_ai, # type: ignore
|
||||
)
|
||||
|
||||
|
||||
def test_extract_project_connection_id_direct() -> None:
|
||||
"""Test extracting project_connection_id from direct key."""
|
||||
result = _extract_project_connection_id({"project_connection_id": "my-connection"})
|
||||
assert result == "my-connection"
|
||||
|
||||
|
||||
def test_extract_project_connection_id_from_connection_name() -> None:
|
||||
"""Test extracting project_connection_id from connection.name structure."""
|
||||
result = _extract_project_connection_id({"connection": {"name": "my-connection"}})
|
||||
assert result == "my-connection"
|
||||
|
||||
|
||||
def test_extract_project_connection_id_none() -> None:
|
||||
"""Test returns None when no connection info."""
|
||||
assert _extract_project_connection_id(None) is None
|
||||
assert _extract_project_connection_id({}) is None
|
||||
|
||||
|
||||
def test_to_azure_ai_agent_tools_empty() -> None:
|
||||
"""Test converting empty/None tools list."""
|
||||
assert to_azure_ai_agent_tools(None) == []
|
||||
assert to_azure_ai_agent_tools([]) == []
|
||||
|
||||
|
||||
def test_to_azure_ai_agent_tools_function_tool() -> None:
|
||||
"""Test converting FunctionTool to tool definition."""
|
||||
|
||||
def my_func(arg: str) -> str:
|
||||
"""My function."""
|
||||
return arg
|
||||
|
||||
func_tool = FunctionTool(func=my_func, name="my_func", description="My function.") # type: ignore
|
||||
result = to_azure_ai_agent_tools([func_tool]) # type: ignore
|
||||
assert len(result) == 1
|
||||
assert result[0]["type"] == "function"
|
||||
assert result[0]["function"]["name"] == "my_func"
|
||||
|
||||
|
||||
def test_to_azure_ai_agent_tools_code_interpreter() -> None:
|
||||
"""Test converting code_interpreter dict tool."""
|
||||
tool = AzureAIAgentClient.get_code_interpreter_tool()
|
||||
result = to_azure_ai_agent_tools([tool])
|
||||
assert len(result) == 1
|
||||
assert isinstance(result[0], CodeInterpreterToolDefinition)
|
||||
|
||||
|
||||
def test_to_azure_ai_agent_tools_web_search_missing_connection() -> None:
|
||||
"""Test web search tool raises without connection info."""
|
||||
# Clear any environment variables that could provide connection info
|
||||
with patch.dict(
|
||||
os.environ,
|
||||
{"BING_CONNECTION_ID": "", "BING_CUSTOM_CONNECTION_ID": "", "BING_CUSTOM_INSTANCE_NAME": ""},
|
||||
clear=False,
|
||||
):
|
||||
# Also need to unset the keys if they exist
|
||||
env_backup = {}
|
||||
for key in ["BING_CONNECTION_ID", "BING_CUSTOM_CONNECTION_ID", "BING_CUSTOM_INSTANCE_NAME"]:
|
||||
env_backup[key] = os.environ.pop(key, None)
|
||||
try:
|
||||
# get_web_search_tool now raises ValueError when no connection info is available
|
||||
with pytest.raises(ValueError, match="Azure AI Agents requires a Bing connection"):
|
||||
AzureAIAgentClient.get_web_search_tool()
|
||||
finally:
|
||||
# Restore environment
|
||||
for key, value in env_backup.items():
|
||||
if value is not None:
|
||||
os.environ[key] = value
|
||||
|
||||
|
||||
def test_to_azure_ai_agent_tools_dict_passthrough() -> None:
|
||||
"""Test dict tools pass through unchanged."""
|
||||
tool_dict = {"type": "custom", "config": "value"}
|
||||
result = to_azure_ai_agent_tools([tool_dict])
|
||||
assert result[0] == tool_dict
|
||||
|
||||
|
||||
def test_to_azure_ai_agent_tools_unsupported_type() -> None:
|
||||
"""Test unsupported tool type passes through unchanged."""
|
||||
|
||||
class UnsupportedTool:
|
||||
pass
|
||||
|
||||
unsupported = UnsupportedTool()
|
||||
result = to_azure_ai_agent_tools([unsupported]) # type: ignore
|
||||
assert len(result) == 1
|
||||
assert result[0] is unsupported # Passed through unchanged
|
||||
|
||||
|
||||
def test_from_azure_ai_agent_tools_empty() -> None:
|
||||
"""Test converting empty/None tools list."""
|
||||
assert from_azure_ai_agent_tools(None) == []
|
||||
assert from_azure_ai_agent_tools([]) == []
|
||||
|
||||
|
||||
def test_from_azure_ai_agent_tools_code_interpreter() -> None:
|
||||
"""Test converting CodeInterpreterToolDefinition."""
|
||||
tool = CodeInterpreterToolDefinition()
|
||||
result = from_azure_ai_agent_tools([tool])
|
||||
assert len(result) == 1
|
||||
assert result[0] == {"type": "code_interpreter"}
|
||||
|
||||
|
||||
def test_convert_sdk_tool_code_interpreter() -> None:
|
||||
"""Test _convert_sdk_tool with code_interpreter type."""
|
||||
tool = MagicMock()
|
||||
tool.type = "code_interpreter"
|
||||
result = _convert_sdk_tool(tool)
|
||||
assert result == {"type": "code_interpreter"}
|
||||
|
||||
|
||||
def test_convert_sdk_tool_function_returns_none() -> None:
|
||||
"""Test _convert_sdk_tool with function type returns None."""
|
||||
tool = MagicMock()
|
||||
tool.type = "function"
|
||||
result = _convert_sdk_tool(tool)
|
||||
assert result is None
|
||||
|
||||
|
||||
def test_convert_sdk_tool_mcp_returns_none() -> None:
|
||||
"""Test _convert_sdk_tool with mcp type returns None."""
|
||||
tool = MagicMock()
|
||||
tool.type = "mcp"
|
||||
result = _convert_sdk_tool(tool)
|
||||
assert result is None
|
||||
|
||||
|
||||
def test_convert_sdk_tool_file_search() -> None:
|
||||
"""Test _convert_sdk_tool with file_search type."""
|
||||
tool = MagicMock()
|
||||
tool.type = "file_search"
|
||||
tool.file_search = MagicMock()
|
||||
tool.file_search.vector_store_ids = ["vs-1", "vs-2"]
|
||||
result = _convert_sdk_tool(tool)
|
||||
assert result["type"] == "file_search"
|
||||
assert result["vector_store_ids"] == ["vs-1", "vs-2"]
|
||||
|
||||
|
||||
def test_convert_sdk_tool_bing_grounding() -> None:
|
||||
"""Test _convert_sdk_tool with bing_grounding type."""
|
||||
tool = MagicMock()
|
||||
tool.type = "bing_grounding"
|
||||
tool.bing_grounding = MagicMock()
|
||||
tool.bing_grounding.connection_id = "conn-123"
|
||||
result = _convert_sdk_tool(tool)
|
||||
assert result["type"] == "bing_grounding"
|
||||
assert result["connection_id"] == "conn-123"
|
||||
|
||||
|
||||
def test_convert_sdk_tool_bing_custom_search() -> None:
|
||||
"""Test _convert_sdk_tool with bing_custom_search type."""
|
||||
tool = MagicMock()
|
||||
tool.type = "bing_custom_search"
|
||||
tool.bing_custom_search = MagicMock()
|
||||
tool.bing_custom_search.connection_id = "conn-123"
|
||||
tool.bing_custom_search.instance_name = "my-instance"
|
||||
result = _convert_sdk_tool(tool)
|
||||
assert result["type"] == "bing_custom_search"
|
||||
assert result["connection_id"] == "conn-123"
|
||||
assert result["instance_name"] == "my-instance"
|
||||
|
||||
|
||||
def test_to_azure_ai_tools_empty() -> None:
|
||||
"""Test converting empty/None tools list."""
|
||||
assert to_azure_ai_tools(None) == []
|
||||
assert to_azure_ai_tools([]) == []
|
||||
|
||||
|
||||
def test_to_azure_ai_tools_code_interpreter_with_file_ids() -> None:
|
||||
"""Test converting code_interpreter dict tool with file inputs."""
|
||||
tool = {
|
||||
"type": "code_interpreter",
|
||||
"file_ids": ["file-123"],
|
||||
}
|
||||
result = to_azure_ai_tools([tool])
|
||||
assert len(result) == 1
|
||||
assert result[0]["type"] == "code_interpreter"
|
||||
|
||||
|
||||
def test_to_azure_ai_tools_function_tool() -> None:
|
||||
"""Test converting FunctionTool."""
|
||||
|
||||
def my_func(arg: str) -> str:
|
||||
"""My function."""
|
||||
return arg
|
||||
|
||||
func_tool = FunctionTool(func=my_func, name="my_func", description="My function.") # type: ignore
|
||||
result = to_azure_ai_tools([func_tool]) # type: ignore
|
||||
assert len(result) == 1
|
||||
assert result[0]["type"] == "function"
|
||||
assert result[0]["name"] == "my_func"
|
||||
|
||||
|
||||
def test_to_azure_ai_tools_file_search() -> None:
|
||||
"""Test converting file_search dict tool."""
|
||||
tool = {
|
||||
"type": "file_search",
|
||||
"vector_store_ids": ["vs-123"],
|
||||
"max_num_results": 10,
|
||||
}
|
||||
result = to_azure_ai_tools([tool])
|
||||
assert len(result) == 1
|
||||
assert result[0]["type"] == "file_search"
|
||||
assert result[0]["vector_store_ids"] == ["vs-123"]
|
||||
assert result[0]["max_num_results"] == 10
|
||||
|
||||
|
||||
def test_to_azure_ai_tools_web_search_with_location() -> None:
|
||||
"""Test converting web_search dict tool with user location."""
|
||||
tool = {
|
||||
"type": "web_search_preview",
|
||||
"user_location": {
|
||||
"city": "Seattle",
|
||||
"country": "US",
|
||||
"region": "WA",
|
||||
"timezone": "PST",
|
||||
},
|
||||
}
|
||||
result = to_azure_ai_tools([tool])
|
||||
assert len(result) == 1
|
||||
assert result[0]["type"] == "web_search_preview"
|
||||
|
||||
|
||||
def test_to_azure_ai_tools_image_generation() -> None:
|
||||
"""Test converting image_generation dict tool."""
|
||||
tool = {
|
||||
"type": "image_generation",
|
||||
"model": "gpt-image-1",
|
||||
"size": "1024x1024",
|
||||
"quality": "high",
|
||||
}
|
||||
result = to_azure_ai_tools([tool])
|
||||
assert len(result) == 1
|
||||
assert result[0]["type"] == "image_generation"
|
||||
assert result[0]["model"] == "gpt-image-1"
|
||||
|
||||
|
||||
def test_prepare_mcp_tool_basic() -> None:
|
||||
"""Test basic MCP tool conversion."""
|
||||
tool = {"type": "mcp", "server_label": "my_tool", "server_url": "http://localhost:8080"}
|
||||
result = _prepare_mcp_tool_for_azure_ai(tool)
|
||||
assert result["server_label"] == "my_tool"
|
||||
assert "http://localhost:8080" in result["server_url"]
|
||||
|
||||
|
||||
def test_prepare_mcp_tool_with_description() -> None:
|
||||
"""Test MCP tool with description."""
|
||||
tool = {
|
||||
"type": "mcp",
|
||||
"server_label": "my_tool",
|
||||
"server_url": "http://localhost:8080",
|
||||
"server_description": "My MCP server",
|
||||
}
|
||||
result = _prepare_mcp_tool_for_azure_ai(tool)
|
||||
assert result["server_description"] == "My MCP server"
|
||||
|
||||
|
||||
def test_prepare_mcp_tool_with_headers() -> None:
|
||||
"""Test MCP tool with headers (no project_connection_id)."""
|
||||
tool = {
|
||||
"type": "mcp",
|
||||
"server_label": "my_tool",
|
||||
"server_url": "http://localhost:8080",
|
||||
"headers": {"X-Api-Key": "secret"},
|
||||
}
|
||||
result = _prepare_mcp_tool_for_azure_ai(tool)
|
||||
assert result["headers"] == {"X-Api-Key": "secret"}
|
||||
|
||||
|
||||
def test_prepare_mcp_tool_project_connection_takes_precedence() -> None:
|
||||
"""Test project_connection_id takes precedence over headers."""
|
||||
tool = {
|
||||
"type": "mcp",
|
||||
"server_label": "my_tool",
|
||||
"server_url": "http://localhost:8080",
|
||||
"headers": {"X-Api-Key": "secret"},
|
||||
"project_connection_id": "my-conn",
|
||||
}
|
||||
result = _prepare_mcp_tool_for_azure_ai(tool)
|
||||
assert result["project_connection_id"] == "my-conn"
|
||||
assert "headers" not in result
|
||||
|
||||
|
||||
def test_prepare_mcp_tool_approval_mode_always() -> None:
|
||||
"""Test MCP tool with always_require approval mode."""
|
||||
tool = {
|
||||
"type": "mcp",
|
||||
"server_label": "my_tool",
|
||||
"server_url": "http://localhost:8080",
|
||||
"require_approval": "always",
|
||||
}
|
||||
result = _prepare_mcp_tool_for_azure_ai(tool)
|
||||
assert result["require_approval"] == "always"
|
||||
|
||||
|
||||
def test_prepare_mcp_tool_approval_mode_never() -> None:
|
||||
"""Test MCP tool with never_require approval mode."""
|
||||
tool = {
|
||||
"type": "mcp",
|
||||
"server_label": "my_tool",
|
||||
"server_url": "http://localhost:8080",
|
||||
"require_approval": "never",
|
||||
}
|
||||
result = _prepare_mcp_tool_for_azure_ai(tool)
|
||||
assert result["require_approval"] == "never"
|
||||
|
||||
|
||||
def test_prepare_mcp_tool_approval_mode_dict() -> None:
|
||||
"""Test MCP tool with dict approval mode."""
|
||||
tool = {
|
||||
"type": "mcp",
|
||||
"server_label": "my_tool",
|
||||
"server_url": "http://localhost:8080",
|
||||
"require_approval": {"always": {"tool_names": ["sensitive_tool", "dangerous_tool"]}},
|
||||
}
|
||||
result = _prepare_mcp_tool_for_azure_ai(tool)
|
||||
# The approval mode is passed through
|
||||
assert "require_approval" in result
|
||||
|
||||
|
||||
def test_create_text_format_config_pydantic_model() -> None:
|
||||
"""Test creating text format config from Pydantic model."""
|
||||
|
||||
class MySchema(BaseModel):
|
||||
name: str
|
||||
value: int
|
||||
|
||||
result = create_text_format_config(MySchema)
|
||||
assert result["type"] == "json_schema"
|
||||
assert result["name"] == "MySchema"
|
||||
assert result["strict"] is True
|
||||
|
||||
|
||||
def test_create_text_format_config_json_schema_mapping() -> None:
|
||||
"""Test creating text format config from json_schema mapping."""
|
||||
config = {
|
||||
"type": "json_schema",
|
||||
"json_schema": {
|
||||
"name": "MyResponse",
|
||||
"schema": {"type": "object", "properties": {"name": {"type": "string"}}},
|
||||
},
|
||||
}
|
||||
result = create_text_format_config(config)
|
||||
assert result["type"] == "json_schema"
|
||||
assert result["name"] == "MyResponse"
|
||||
|
||||
|
||||
def test_create_text_format_config_json_object() -> None:
|
||||
"""Test creating text format config for json_object type."""
|
||||
result = create_text_format_config({"type": "json_object"})
|
||||
assert result["type"] == "json_object"
|
||||
|
||||
|
||||
def test_create_text_format_config_text() -> None:
|
||||
"""Test creating text format config for text type."""
|
||||
result = create_text_format_config({"type": "text"})
|
||||
assert result["type"] == "text"
|
||||
|
||||
|
||||
def test_create_text_format_config_invalid_raises() -> None:
|
||||
"""Test invalid response_format raises error."""
|
||||
with pytest.raises(IntegrationInvalidRequestException):
|
||||
create_text_format_config({"type": "invalid"})
|
||||
|
||||
|
||||
def test_convert_response_format_with_format_key() -> None:
|
||||
"""Test _convert_response_format with nested format key."""
|
||||
config = {"format": {"type": "json_object"}}
|
||||
result = _convert_response_format(config)
|
||||
assert result["type"] == "json_object"
|
||||
|
||||
|
||||
def test_convert_response_format_json_schema_missing_schema_raises() -> None:
|
||||
"""Test json_schema without schema raises error."""
|
||||
with pytest.raises(IntegrationInvalidRequestException, match="requires a schema"):
|
||||
_convert_response_format({"type": "json_schema", "json_schema": {}})
|
||||
|
||||
|
||||
def test_convert_response_format_raw_json_schema_with_properties() -> None:
|
||||
"""Test raw JSON schema with properties is wrapped in json_schema envelope."""
|
||||
result = _convert_response_format({"type": "object", "properties": {"x": {"type": "string"}}, "title": "MyOutput"})
|
||||
|
||||
assert result["type"] == "json_schema"
|
||||
assert result["name"] == "MyOutput"
|
||||
assert result["strict"] is True
|
||||
assert result["schema"]["additionalProperties"] is False
|
||||
assert "title" not in result["schema"]
|
||||
|
||||
|
||||
def test_convert_response_format_raw_json_schema_no_title() -> None:
|
||||
"""Test raw JSON schema without title defaults name to 'response'."""
|
||||
result = _convert_response_format({"type": "object", "properties": {"x": {"type": "string"}}})
|
||||
|
||||
assert result["name"] == "response"
|
||||
|
||||
|
||||
def test_convert_response_format_raw_json_schema_with_anyof() -> None:
|
||||
"""Test raw JSON schema with anyOf keyword is detected."""
|
||||
result = _convert_response_format({"anyOf": [{"type": "string"}, {"type": "number"}]})
|
||||
|
||||
assert result["type"] == "json_schema"
|
||||
assert result["strict"] is True
|
||||
|
||||
|
||||
def test_from_azure_ai_tools_mcp_approval_mode_always() -> None:
|
||||
"""Test from_azure_ai_tools converts MCP require_approval='always' to dict."""
|
||||
tools = [
|
||||
{
|
||||
"type": "mcp",
|
||||
"server_label": "my_mcp",
|
||||
"server_url": "http://localhost:8080",
|
||||
"require_approval": "always",
|
||||
}
|
||||
]
|
||||
result = from_azure_ai_tools(tools)
|
||||
assert len(result) == 1
|
||||
assert result[0]["type"] == "mcp"
|
||||
assert result[0]["require_approval"] == "always"
|
||||
|
||||
|
||||
def test_from_azure_ai_tools_mcp_approval_mode_never() -> None:
|
||||
"""Test from_azure_ai_tools converts MCP require_approval='never' to dict."""
|
||||
tools = [
|
||||
{
|
||||
"type": "mcp",
|
||||
"server_label": "my_mcp",
|
||||
"server_url": "http://localhost:8080",
|
||||
"require_approval": "never",
|
||||
}
|
||||
]
|
||||
result = from_azure_ai_tools(tools)
|
||||
assert len(result) == 1
|
||||
assert result[0]["type"] == "mcp"
|
||||
assert result[0]["require_approval"] == "never"
|
||||
|
||||
|
||||
def test_from_azure_ai_tools_mcp_approval_mode_dict_always() -> None:
|
||||
"""Test from_azure_ai_tools converts MCP dict require_approval with 'always' key."""
|
||||
tools = [
|
||||
{
|
||||
"type": "mcp",
|
||||
"server_label": "my_mcp",
|
||||
"server_url": "http://localhost:8080",
|
||||
"require_approval": {"always": {"tool_names": ["sensitive_tool", "dangerous_tool"]}},
|
||||
}
|
||||
]
|
||||
result = from_azure_ai_tools(tools)
|
||||
assert len(result) == 1
|
||||
assert result[0]["type"] == "mcp"
|
||||
assert result[0]["require_approval"] == {"always": {"tool_names": ["sensitive_tool", "dangerous_tool"]}}
|
||||
|
||||
|
||||
def test_from_azure_ai_tools_mcp_approval_mode_dict_never() -> None:
|
||||
"""Test from_azure_ai_tools converts MCP dict require_approval with 'never' key."""
|
||||
tools = [
|
||||
{
|
||||
"type": "mcp",
|
||||
"server_label": "my_mcp",
|
||||
"server_url": "http://localhost:8080",
|
||||
"require_approval": {"never": {"tool_names": ["safe_tool"]}},
|
||||
}
|
||||
]
|
||||
result = from_azure_ai_tools(tools)
|
||||
assert len(result) == 1
|
||||
assert result[0]["type"] == "mcp"
|
||||
assert result[0]["require_approval"] == {"never": {"tool_names": ["safe_tool"]}}
|
||||
Reference in New Issue
Block a user