-
Notifications
You must be signed in to change notification settings - Fork 618
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from andrewyng/add-openai-tests
Add OpenAI test file
- Loading branch information
Showing
2 changed files
with
73 additions
and
5 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
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,42 @@ | ||
import pytest | ||
from unittest.mock import patch, MagicMock | ||
from aimodels.providers.openai_interface import OpenAIInterface | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def set_api_key_env_var(monkeypatch): | ||
"""Set environment variables for tests.""" | ||
monkeypatch.setenv("OPENAI_API_KEY", "test-api-key") | ||
|
||
|
||
def test_openai_interface(): | ||
"""Test chat completions.""" | ||
|
||
user_greeting = "Hello Hello!" | ||
message_history = [{"role": "user", "content": user_greeting}] | ||
selected_model = "our-favorite-model" | ||
chosen_temperature = 0.9 | ||
response_text_content = "mocked-text-response-from-model" | ||
|
||
interface = OpenAIInterface() | ||
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.openai_client.chat.completions, "create", return_value=mock_response | ||
) as mock_create: | ||
response = interface.chat_completion_create( | ||
messages=message_history, | ||
model=selected_model, | ||
temperature=chosen_temperature, | ||
) | ||
|
||
mock_create.assert_called_with( | ||
model=selected_model, | ||
messages=message_history, | ||
temperature=chosen_temperature, | ||
) | ||
|
||
assert response.choices[0].message.content == response_text_content |