From 29e001c853a6e01667aff08c36bc69264c742529 Mon Sep 17 00:00:00 2001 From: standsleeping Date: Thu, 4 Jul 2024 14:32:59 -0500 Subject: [PATCH 1/3] Test MultiFMClient --- tests/test_multi_fm_client.py | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 tests/test_multi_fm_client.py diff --git a/tests/test_multi_fm_client.py b/tests/test_multi_fm_client.py new file mode 100644 index 00000000..156ce9ab --- /dev/null +++ b/tests/test_multi_fm_client.py @@ -0,0 +1,45 @@ +import pytest +from aimodels.client.multi_fm_client import MultiFMClient, AnthropicInterface + + +def test_get_provider_interface_with_new_instance(): + """Test that get_provider_interface creates a new instance of the interface.""" + client = MultiFMClient() + interface, model_name = client.get_provider_interface("anthropic:some-model:v1") + assert isinstance(interface, AnthropicInterface) + assert model_name == "some-model:v1" + assert client.all_interfaces["anthropic"] == interface + + +def test_get_provider_interface_with_existing_instance(): + """Test that get_provider_interface returns an existing instance of the interface, if already created.""" + client = MultiFMClient() + + # New interface instance + new_instance, _ = client.get_provider_interface("anthropic:some-model:v2") + + # Call twice, get same instance back + same_instance, _ = client.get_provider_interface("anthropic:some-model:v2") + + assert new_instance is same_instance + + +def test_get_provider_interface_with_invalid_format(): + client = MultiFMClient() + + with pytest.raises(ValueError) as exc_info: + client.get_provider_interface("invalid-model-no-colon") + + assert "Expected ':' in model identifier" in str(exc_info.value) + + +def test_get_provider_interface_with_unknown_interface(): + client = MultiFMClient() + + with pytest.raises(Exception) as exc_info: + client.get_provider_interface("unknown-interface:some-model") + + assert ( + "Could not find factory to create interface for provider 'unknown-interface'" + in str(exc_info.value) + ) From f5fbeca85432f4eca540065d62ae03554d2e93f7 Mon Sep 17 00:00:00 2001 From: standsleeping Date: Thu, 4 Jul 2024 14:35:35 -0500 Subject: [PATCH 2/3] First-pass approach to testing interfaces --- tests/test_anthropic_interface.py | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 tests/test_anthropic_interface.py diff --git a/tests/test_anthropic_interface.py b/tests/test_anthropic_interface.py new file mode 100644 index 00000000..54bf645e --- /dev/null +++ b/tests/test_anthropic_interface.py @@ -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 From 1ef852eb522b5b3ea3b901203c9b4725268dac70 Mon Sep 17 00:00:00 2001 From: Kevin Solorio Date: Mon, 8 Jul 2024 22:01:06 -0500 Subject: [PATCH 3/3] add github action for running tests --- .github/workflows/run_pytest.yml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 .github/workflows/run_pytest.yml diff --git a/.github/workflows/run_pytest.yml b/.github/workflows/run_pytest.yml new file mode 100644 index 00000000..0093c348 --- /dev/null +++ b/.github/workflows/run_pytest.yml @@ -0,0 +1,24 @@ +name: Lint + +on: [push, pull_request] + +jobs: + build_and_test: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: [ "3.10", "3.11", "3.12" ] + steps: + - uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install poetry + poetry install + - name: Test with pytest + run: poetry run pytest +