chore: import upstream snapshot with attribution
dotnet-build-and-test / dotnet-test-functions (push) Has been cancelled
dotnet-build-and-test / paths-filter (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Debug, windows-latest, net9.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net8.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-foundry-hosted-it (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test-check (push) Has been cancelled
dotnet-build-and-test / Integration Test Report (push) Has been cancelled
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
dotnet-build-and-test / dotnet-test-functions (push) Has been cancelled
dotnet-build-and-test / paths-filter (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Debug, windows-latest, net9.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net8.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-foundry-hosted-it (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test-check (push) Has been cancelled
dotnet-build-and-test / Integration Test Report (push) Has been cancelled
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 178 KiB |
@@ -0,0 +1,56 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
from pytest import fixture
|
||||
|
||||
|
||||
@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 {}
|
||||
|
||||
|
||||
@fixture
|
||||
def anthropic_unit_test_env(monkeypatch, exclude_list, override_env_param_dict): # type: ignore
|
||||
"""Fixture to set environment variables for AnthropicSettings."""
|
||||
if exclude_list is None:
|
||||
exclude_list = []
|
||||
|
||||
if override_env_param_dict is None:
|
||||
override_env_param_dict = {}
|
||||
|
||||
env_vars = {
|
||||
"ANTHROPIC_API_KEY": "test-api-key-12345",
|
||||
"ANTHROPIC_CHAT_MODEL": "claude-3-5-sonnet-20241022",
|
||||
}
|
||||
|
||||
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
|
||||
def mock_anthropic_client() -> MagicMock:
|
||||
"""Fixture that provides a mock AsyncAnthropic client."""
|
||||
mock_client = MagicMock()
|
||||
mock_client.base_url = "https://api.anthropic.com"
|
||||
|
||||
# Mock beta.messages property
|
||||
mock_client.beta = MagicMock()
|
||||
mock_client.beta.messages = MagicMock()
|
||||
mock_client.beta.messages.create = AsyncMock()
|
||||
|
||||
return mock_client
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,222 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from agent_framework import Agent, ChatMiddlewareLayer, FunctionInvocationLayer
|
||||
from agent_framework._telemetry import get_user_agent
|
||||
from agent_framework.observability import ChatTelemetryLayer
|
||||
|
||||
from agent_framework_anthropic import (
|
||||
AnthropicBedrockClient,
|
||||
AnthropicFoundryClient,
|
||||
AnthropicVertexClient,
|
||||
RawAnthropicBedrockClient,
|
||||
RawAnthropicFoundryClient,
|
||||
RawAnthropicVertexClient,
|
||||
)
|
||||
|
||||
|
||||
def _create_mock_transport(base_url: str) -> MagicMock:
|
||||
transport = MagicMock()
|
||||
transport.base_url = base_url
|
||||
transport.beta = MagicMock()
|
||||
transport.beta.messages = MagicMock()
|
||||
transport.beta.messages.create = AsyncMock()
|
||||
return transport
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("public_client", "raw_client"),
|
||||
[
|
||||
(AnthropicFoundryClient, RawAnthropicFoundryClient),
|
||||
(AnthropicBedrockClient, RawAnthropicBedrockClient),
|
||||
(AnthropicVertexClient, RawAnthropicVertexClient),
|
||||
],
|
||||
)
|
||||
def test_provider_client_wraps_raw_client_with_standard_layer_order(public_client, raw_client) -> None:
|
||||
assert issubclass(public_client, raw_client)
|
||||
mro = public_client.__mro__
|
||||
assert mro.index(FunctionInvocationLayer) < mro.index(ChatMiddlewareLayer)
|
||||
assert mro.index(ChatMiddlewareLayer) < mro.index(ChatTelemetryLayer)
|
||||
assert mro.index(ChatTelemetryLayer) < mro.index(raw_client)
|
||||
|
||||
|
||||
def test_agent_accepts_anthropic_foundry_clients() -> None:
|
||||
mock_transport = _create_mock_transport("https://test-resource.services.ai.azure.com/anthropic/")
|
||||
with patch("agent_framework_anthropic._foundry_client.AsyncAnthropicFoundry", return_value=mock_transport):
|
||||
raw_client = RawAnthropicFoundryClient(
|
||||
model="claude-foundry-test",
|
||||
resource="test-resource",
|
||||
api_key="test-key",
|
||||
)
|
||||
raw_agent = Agent(client=raw_client, instructions="test agent")
|
||||
assert raw_agent.client is raw_client
|
||||
|
||||
with patch("agent_framework_anthropic._foundry_client.AsyncAnthropicFoundry", return_value=mock_transport):
|
||||
client = AnthropicFoundryClient(
|
||||
model="claude-foundry-test",
|
||||
resource="test-resource",
|
||||
api_key="test-key",
|
||||
)
|
||||
agent = Agent(client=client, instructions="test agent")
|
||||
assert agent.client is client
|
||||
|
||||
|
||||
def test_agent_accepts_anthropic_bedrock_clients() -> None:
|
||||
mock_transport = _create_mock_transport("https://bedrock-runtime.us-east-1.amazonaws.com")
|
||||
with patch("agent_framework_anthropic._bedrock_client.AsyncAnthropicBedrock", return_value=mock_transport):
|
||||
raw_client = RawAnthropicBedrockClient(
|
||||
model="claude-bedrock-test",
|
||||
aws_access_key="access-key",
|
||||
aws_secret_key="secret-key",
|
||||
aws_region="us-east-1",
|
||||
)
|
||||
raw_agent = Agent(client=raw_client, instructions="test agent")
|
||||
assert raw_agent.client is raw_client
|
||||
|
||||
with patch("agent_framework_anthropic._bedrock_client.AsyncAnthropicBedrock", return_value=mock_transport):
|
||||
client = AnthropicBedrockClient(
|
||||
model="claude-bedrock-test",
|
||||
aws_access_key="access-key",
|
||||
aws_secret_key="secret-key",
|
||||
aws_region="us-east-1",
|
||||
)
|
||||
agent = Agent(client=client, instructions="test agent")
|
||||
assert agent.client is client
|
||||
|
||||
|
||||
def test_agent_accepts_anthropic_vertex_clients() -> None:
|
||||
mock_transport = _create_mock_transport("https://us-central1-aiplatform.googleapis.com/v1")
|
||||
with patch("agent_framework_anthropic._vertex_client.AsyncAnthropicVertex", return_value=mock_transport):
|
||||
raw_client = RawAnthropicVertexClient(
|
||||
model="claude-vertex-test",
|
||||
region="us-central1",
|
||||
project_id="test-project",
|
||||
)
|
||||
raw_agent = Agent(client=raw_client, instructions="test agent")
|
||||
assert raw_agent.client is raw_client
|
||||
|
||||
with patch("agent_framework_anthropic._vertex_client.AsyncAnthropicVertex", return_value=mock_transport):
|
||||
client = AnthropicVertexClient(
|
||||
model="claude-vertex-test",
|
||||
region="us-central1",
|
||||
project_id="test-project",
|
||||
)
|
||||
agent = Agent(client=client, instructions="test agent")
|
||||
assert agent.client is client
|
||||
|
||||
|
||||
def test_raw_anthropic_foundry_client_creates_sdk_client_from_settings(tmp_path) -> None:
|
||||
env_file = tmp_path / ".env"
|
||||
env_file.write_text(
|
||||
"ANTHROPIC_CHAT_MODEL=claude-foundry-test\n"
|
||||
"ANTHROPIC_FOUNDRY_API_KEY=test-key\n"
|
||||
"ANTHROPIC_FOUNDRY_RESOURCE=test-resource\n"
|
||||
)
|
||||
mock_transport = _create_mock_transport("https://test-resource.services.ai.azure.com/anthropic/")
|
||||
|
||||
with patch(
|
||||
"agent_framework_anthropic._foundry_client.AsyncAnthropicFoundry", return_value=mock_transport
|
||||
) as factory:
|
||||
client = RawAnthropicFoundryClient(env_file_path=str(env_file))
|
||||
|
||||
assert client.model == "claude-foundry-test"
|
||||
assert client.anthropic_client is mock_transport
|
||||
factory.assert_called_once_with(
|
||||
resource="test-resource",
|
||||
api_key="test-key",
|
||||
azure_ad_token_provider=None,
|
||||
default_headers={"User-Agent": get_user_agent()},
|
||||
)
|
||||
|
||||
|
||||
def test_raw_anthropic_foundry_client_creates_sdk_client_from_base_url_settings(tmp_path) -> None:
|
||||
env_file = tmp_path / ".env"
|
||||
env_file.write_text(
|
||||
"ANTHROPIC_CHAT_MODEL=claude-foundry-test\n"
|
||||
"ANTHROPIC_FOUNDRY_API_KEY=test-key\n"
|
||||
"ANTHROPIC_FOUNDRY_BASE_URL=https://test-resource.services.ai.azure.com/anthropic/\n"
|
||||
)
|
||||
mock_transport = _create_mock_transport("https://test-resource.services.ai.azure.com/anthropic/")
|
||||
|
||||
with patch(
|
||||
"agent_framework_anthropic._foundry_client.AsyncAnthropicFoundry", return_value=mock_transport
|
||||
) as factory:
|
||||
client = RawAnthropicFoundryClient(env_file_path=str(env_file))
|
||||
|
||||
assert client.model == "claude-foundry-test"
|
||||
assert client.anthropic_client is mock_transport
|
||||
factory.assert_called_once_with(
|
||||
base_url="https://test-resource.services.ai.azure.com/anthropic/",
|
||||
api_key="test-key",
|
||||
azure_ad_token_provider=None,
|
||||
default_headers={"User-Agent": get_user_agent()},
|
||||
)
|
||||
|
||||
|
||||
def test_raw_anthropic_foundry_client_requires_resource_or_base_url() -> None:
|
||||
with patch("agent_framework_anthropic._foundry_client.load_settings") as mock_load:
|
||||
mock_load.return_value = {
|
||||
"anthropic_foundry_api_key": None,
|
||||
"anthropic_foundry_resource": None,
|
||||
"anthropic_foundry_base_url": None,
|
||||
"anthropic_chat_model": None,
|
||||
}
|
||||
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match=(
|
||||
"Anthropic Foundry requires either `resource`/`ANTHROPIC_FOUNDRY_RESOURCE` "
|
||||
"or `base_url`/`ANTHROPIC_FOUNDRY_BASE_URL`\\."
|
||||
),
|
||||
):
|
||||
RawAnthropicFoundryClient()
|
||||
|
||||
|
||||
def test_raw_anthropic_bedrock_client_creates_sdk_client_from_arguments() -> None:
|
||||
mock_transport = _create_mock_transport("https://bedrock-runtime.us-east-1.amazonaws.com")
|
||||
|
||||
with patch(
|
||||
"agent_framework_anthropic._bedrock_client.AsyncAnthropicBedrock", return_value=mock_transport
|
||||
) as factory:
|
||||
client = RawAnthropicBedrockClient(
|
||||
model="claude-bedrock-test",
|
||||
aws_access_key="access-key",
|
||||
aws_secret_key="secret-key",
|
||||
aws_region="us-east-1",
|
||||
)
|
||||
|
||||
assert client.model == "claude-bedrock-test"
|
||||
assert client.anthropic_client is mock_transport
|
||||
factory.assert_called_once_with(
|
||||
aws_secret_key="secret-key",
|
||||
aws_access_key="access-key",
|
||||
aws_region="us-east-1",
|
||||
aws_profile=None,
|
||||
aws_session_token=None,
|
||||
base_url=None,
|
||||
default_headers={"User-Agent": get_user_agent()},
|
||||
)
|
||||
|
||||
|
||||
def test_raw_anthropic_vertex_client_creates_sdk_client_from_arguments() -> None:
|
||||
mock_transport = _create_mock_transport("https://us-central1-aiplatform.googleapis.com/v1")
|
||||
|
||||
with patch("agent_framework_anthropic._vertex_client.AsyncAnthropicVertex", return_value=mock_transport) as factory:
|
||||
client = RawAnthropicVertexClient(
|
||||
model="claude-vertex-test",
|
||||
region="us-central1",
|
||||
project_id="test-project",
|
||||
)
|
||||
|
||||
assert client.model == "claude-vertex-test"
|
||||
assert client.anthropic_client is mock_transport
|
||||
factory.assert_called_once_with(
|
||||
region="us-central1",
|
||||
project_id="test-project",
|
||||
access_token=None,
|
||||
credentials=None,
|
||||
base_url=None,
|
||||
default_headers={"User-Agent": get_user_agent()},
|
||||
)
|
||||
Reference in New Issue
Block a user