Skip to content

Commit

Permalink
Add Ollama test file
Browse files Browse the repository at this point in the history
  • Loading branch information
standsleeping committed Jul 20, 2024
1 parent d48b5da commit 2a95e0c
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tests/providers/test_ollama_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pytest
from unittest.mock import patch, MagicMock
from aimodels.providers.ollama_interface import OllamaInterface
from httpx import ConnectError


@pytest.fixture(autouse=True)
def set_api_url_var(monkeypatch):
"""Fixture to set environment variables for tests."""
monkeypatch.setenv("OLLAMA_API_URL", "http://localhost:11434")


def test_completion():
"""Test that completions request successfully."""

user_greeting = "Howdy!"
message_history = [{"role": "user", "content": user_greeting}]
selected_model = "best-model-ever"
chosen_temperature = 0.77
response_text_content = "mocked-text-response-from-ollama-model"

interface = OllamaInterface()
mock_response = {"message": {"content": response_text_content}}

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

mock_chat.assert_called_with(
model=selected_model,
messages=message_history,
options={"temperature": chosen_temperature},
)

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


def test_connection_error():
"""Test that any issues with connections raise a RuntimeError."""

interface = OllamaInterface()
mock_connect_error = ConnectError(
"arbitrary-connect-error-message", request=MagicMock()
)

with patch.object(interface.ollama_client, "chat", side_effect=mock_connect_error):
with pytest.raises(RuntimeError) as excinfo:
interface.chat_completion_create(messages=[], model="any-model-we-want")

assert str(excinfo.value) == interface._OLLAMA_STATUS_ERROR_MESSAGE

0 comments on commit 2a95e0c

Please sign in to comment.