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

test: adapt Mistral to OpenAI refactoring #1271

Merged
merged 1 commit into from
Jan 2, 2025
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
58 changes: 37 additions & 21 deletions integrations/mistral/tests/test_mistral_chat_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,24 @@ def test_to_dict_default(self, monkeypatch):
monkeypatch.setenv("MISTRAL_API_KEY", "test-api-key")
component = MistralChatGenerator()
data = component.to_dict()
assert data == {
"type": "haystack_integrations.components.generators.mistral.chat.chat_generator.MistralChatGenerator",
"init_parameters": {
"api_key": {"env_vars": ["MISTRAL_API_KEY"], "strict": True, "type": "env_var"},
"model": "mistral-tiny",
"organization": None,
"streaming_callback": None,
"api_base_url": "https://api.mistral.ai/v1",
"generation_kwargs": {},
},

assert (
data["type"]
== "haystack_integrations.components.generators.mistral.chat.chat_generator.MistralChatGenerator"
)

expected_params = {
"api_key": {"env_vars": ["MISTRAL_API_KEY"], "strict": True, "type": "env_var"},
"model": "mistral-tiny",
"organization": None,
"streaming_callback": None,
"api_base_url": "https://api.mistral.ai/v1",
"generation_kwargs": {},
}

for key, value in expected_params.items():
assert data["init_parameters"][key] == value
Comment on lines +98 to +99
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This avoids the test failure if there are other params in addition to those expected


def test_to_dict_with_parameters(self, monkeypatch):
monkeypatch.setenv("ENV_VAR", "test-api-key")
component = MistralChatGenerator(
Expand All @@ -102,18 +108,23 @@ def test_to_dict_with_parameters(self, monkeypatch):
generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
)
data = component.to_dict()
assert data == {
"type": "haystack_integrations.components.generators.mistral.chat.chat_generator.MistralChatGenerator",
"init_parameters": {
"api_key": {"env_vars": ["ENV_VAR"], "strict": True, "type": "env_var"},
"model": "mistral-small",
"api_base_url": "test-base-url",
"organization": None,
"streaming_callback": "haystack.components.generators.utils.print_streaming_chunk",
"generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"},
},

assert (
data["type"]
== "haystack_integrations.components.generators.mistral.chat.chat_generator.MistralChatGenerator"
)

expected_params = {
"api_key": {"env_vars": ["ENV_VAR"], "strict": True, "type": "env_var"},
"model": "mistral-small",
"api_base_url": "test-base-url",
"streaming_callback": "haystack.components.generators.utils.print_streaming_chunk",
"generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"},
}

for key, value in expected_params.items():
assert data["init_parameters"][key] == value

def test_from_dict(self, monkeypatch):
monkeypatch.setenv("MISTRAL_API_KEY", "fake-api-key")
data = {
Expand Down Expand Up @@ -187,7 +198,12 @@ def test_check_abnormal_completions(self, caplog):
]

for m in messages:
component._check_finish_reason(m)
try:
# Haystack >= 2.9.0
component._check_finish_reason(m.meta)
except AttributeError:
# Haystack < 2.9.0
component._check_finish_reason(m)
Comment on lines +201 to +206
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is ugly, but we can get rid of it after releasing Haystack 2.9.0
(I'll create an issue to track that)


# check truncation warning
message_template = (
Expand Down
Loading