-
Notifications
You must be signed in to change notification settings - Fork 901
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
.vscode/ | ||
__pycache__/ | ||
env/ | ||
.env |
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 |
---|---|---|
@@ -1,9 +1,26 @@ | ||
import os | ||
|
||
import groq | ||
from aisuite.provider import Provider | ||
|
||
|
||
class GroqProvider(Provider): | ||
def __init__(self) -> None: | ||
pass | ||
def __init__(self, **config): | ||
""" | ||
Initialize the Groq provider with the given configuration. | ||
Pass the entire configuration dictionary to the Groq client constructor. | ||
""" | ||
# Ensure API key is provided either in config or via environment variable | ||
config.setdefault("api_key", os.getenv("GROQ_API_KEY")) | ||
if not config["api_key"]: | ||
raise ValueError( | ||
" API key is missing. Please provide it in the config or set the OPENAI_API_KEY environment variable." | ||
) | ||
self.client = groq.Groq(**config) | ||
|
||
def chat_completions_create(self, model, messages): | ||
raise ValueError("Groq provider not yet implemented.") | ||
def chat_completions_create(self, model, messages, **kwargs): | ||
return self.client.chat.completions.create( | ||
model=model, | ||
messages=messages, | ||
**kwargs # Pass any additional arguments to the Groq API | ||
) |
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,46 @@ | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
|
||
from aisuite.providers.groq_provider import GroqProvider | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def set_api_key_env_var(monkeypatch): | ||
"""Fixture to set environment variables for tests.""" | ||
monkeypatch.setenv("GROQ_API_KEY", "test-api-key") | ||
|
||
|
||
def test_groq_provider(): | ||
"""High-level test that the provider 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" | ||
|
||
provider = GroqProvider() | ||
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( | ||
provider.client.chat.completions, | ||
"create", | ||
return_value=mock_response, | ||
) as mock_create: | ||
response = provider.chat_completions_create( | ||
messages=message_history, | ||
model=selected_model, | ||
temperature=chosen_temperature, | ||
) | ||
|
||
mock_create.assert_called_with( | ||
messages=message_history, | ||
model=selected_model, | ||
temperature=chosen_temperature, | ||
) | ||
|
||
assert response.choices[0].message.content == response_text_content |