Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for mistral interface #12

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions tests/providers/test_mistral_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pytest
from unittest.mock import patch, MagicMock

from mistralai.models.chat_completion import ChatMessage

from aimodels.providers.mistral_interface import MistralInterface


@pytest.fixture(autouse=True)
def set_api_key_env_var(monkeypatch):
"""Fixture to set environment variables for tests."""
monkeypatch.setenv("MISTRAL_API_KEY", "test-api-key")


def test_mistral_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 = MistralInterface()
mock_response = MagicMock()
mock_response.choices = [MagicMock()]
mock_response.choices[0].message = MagicMock()
mock_response.choices[0].message.content = response_text_content

with patch.object(
interface.mistral_client, "chat", return_value=mock_response
) as mock_create:
response = interface.chat_completion_create(
messages=message_history,
model=selected_model,
temperature=chosen_temperature,
)

transformed_message_history = [
ChatMessage(role=message["role"], content=message["content"])
for message in message_history
]

mock_create.assert_called_with(
messages=transformed_message_history,
model=selected_model,
temperature=chosen_temperature,
)

assert response.choices[0].message.content == response_text_content
Loading