-
Notifications
You must be signed in to change notification settings - Fork 615
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First-pass approach to testing interfaces
- Loading branch information
1 parent
e307653
commit 9d9ee83
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import pytest | ||
from unittest.mock import patch, MagicMock | ||
from aimodels.providers.anthropic_interface import AnthropicInterface | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def set_api_key_env_var(monkeypatch): | ||
"""Fixture to set environment variables for tests.""" | ||
monkeypatch.setenv("ANTHROPIC_API_KEY", "test-api-key") | ||
|
||
|
||
def test_anthropic_interface(): | ||
"""High-level test that the interface is initialized and chat completions are requested successfully.""" | ||
|
||
user_greeting = "Hello!" | ||
message_history = [{"role": "user", "content": user_greeting}] | ||
selected_model = "our-favorite-model" | ||
chosen_temperature = 0.75 | ||
response_text_content = "mocked-text-response-from-model" | ||
|
||
interface = AnthropicInterface() | ||
mock_response = MagicMock() | ||
mock_response.content = [MagicMock()] | ||
mock_response.content[0].text = response_text_content | ||
|
||
with patch.object( | ||
interface.anthropic_client.messages, "create", return_value=mock_response | ||
) as mock_create: | ||
response = interface.chat_completion_create( | ||
messages=message_history, | ||
model=selected_model, | ||
temperature=chosen_temperature, | ||
) | ||
|
||
transformed_message_history = [ | ||
{"role": "user", "content": [{"type": "text", "text": user_greeting}]}, | ||
] | ||
|
||
mock_create.assert_called_with( | ||
messages=transformed_message_history, | ||
model=selected_model, | ||
temperature=chosen_temperature, | ||
max_tokens=4096, | ||
) | ||
|
||
assert response.choices[0].message.content == response_text_content |