mirror of
https://github.com/deepset-ai/haystack.git
synced 2025-06-26 22:00:13 +00:00

* feat: Add TTFT support in OpenAI generators * pylint fixes * correct disable --------- Co-authored-by: Vladimir Blagojevic <dovlex@gmail.com>
332 lines
14 KiB
Python
332 lines
14 KiB
Python
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
#
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
import logging
|
|
import os
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
from openai import OpenAIError
|
|
|
|
from haystack.components.generators.chat import OpenAIChatGenerator
|
|
from haystack.components.generators.utils import print_streaming_chunk
|
|
from haystack.dataclasses import ChatMessage, StreamingChunk
|
|
from haystack.utils.auth import Secret
|
|
|
|
|
|
@pytest.fixture
|
|
def chat_messages():
|
|
return [
|
|
ChatMessage.from_system("You are a helpful assistant"),
|
|
ChatMessage.from_user("What's the capital of France"),
|
|
]
|
|
|
|
|
|
class TestOpenAIChatGenerator:
|
|
def test_init_default(self, monkeypatch):
|
|
monkeypatch.setenv("OPENAI_API_KEY", "test-api-key")
|
|
component = OpenAIChatGenerator()
|
|
assert component.client.api_key == "test-api-key"
|
|
assert component.model == "gpt-4o-mini"
|
|
assert component.streaming_callback is None
|
|
assert not component.generation_kwargs
|
|
assert component.client.timeout == 30
|
|
assert component.client.max_retries == 5
|
|
|
|
def test_init_fail_wo_api_key(self, monkeypatch):
|
|
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
|
|
with pytest.raises(ValueError, match="None of the .* environment variables are set"):
|
|
OpenAIChatGenerator()
|
|
|
|
def test_init_with_parameters(self, monkeypatch):
|
|
monkeypatch.setenv("OPENAI_TIMEOUT", "100")
|
|
monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
|
|
component = OpenAIChatGenerator(
|
|
api_key=Secret.from_token("test-api-key"),
|
|
model="gpt-4o-mini",
|
|
streaming_callback=print_streaming_chunk,
|
|
api_base_url="test-base-url",
|
|
generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
|
|
timeout=40.0,
|
|
max_retries=1,
|
|
)
|
|
assert component.client.api_key == "test-api-key"
|
|
assert component.model == "gpt-4o-mini"
|
|
assert component.streaming_callback is print_streaming_chunk
|
|
assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"}
|
|
assert component.client.timeout == 40.0
|
|
assert component.client.max_retries == 1
|
|
|
|
def test_init_with_parameters_and_env_vars(self, monkeypatch):
|
|
monkeypatch.setenv("OPENAI_TIMEOUT", "100")
|
|
monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
|
|
component = OpenAIChatGenerator(
|
|
api_key=Secret.from_token("test-api-key"),
|
|
model="gpt-4o-mini",
|
|
streaming_callback=print_streaming_chunk,
|
|
api_base_url="test-base-url",
|
|
generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
|
|
)
|
|
assert component.client.api_key == "test-api-key"
|
|
assert component.model == "gpt-4o-mini"
|
|
assert component.streaming_callback is print_streaming_chunk
|
|
assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"}
|
|
assert component.client.timeout == 100.0
|
|
assert component.client.max_retries == 10
|
|
|
|
def test_to_dict_default(self, monkeypatch):
|
|
monkeypatch.setenv("OPENAI_API_KEY", "test-api-key")
|
|
component = OpenAIChatGenerator()
|
|
data = component.to_dict()
|
|
assert data == {
|
|
"type": "haystack.components.generators.chat.openai.OpenAIChatGenerator",
|
|
"init_parameters": {
|
|
"api_key": {"env_vars": ["OPENAI_API_KEY"], "strict": True, "type": "env_var"},
|
|
"model": "gpt-4o-mini",
|
|
"organization": None,
|
|
"streaming_callback": None,
|
|
"api_base_url": None,
|
|
"generation_kwargs": {},
|
|
},
|
|
}
|
|
|
|
def test_to_dict_with_parameters(self, monkeypatch):
|
|
monkeypatch.setenv("ENV_VAR", "test-api-key")
|
|
component = OpenAIChatGenerator(
|
|
api_key=Secret.from_env_var("ENV_VAR"),
|
|
model="gpt-4o-mini",
|
|
streaming_callback=print_streaming_chunk,
|
|
api_base_url="test-base-url",
|
|
generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
|
|
)
|
|
data = component.to_dict()
|
|
assert data == {
|
|
"type": "haystack.components.generators.chat.openai.OpenAIChatGenerator",
|
|
"init_parameters": {
|
|
"api_key": {"env_vars": ["ENV_VAR"], "strict": True, "type": "env_var"},
|
|
"model": "gpt-4o-mini",
|
|
"organization": None,
|
|
"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"},
|
|
},
|
|
}
|
|
|
|
def test_to_dict_with_lambda_streaming_callback(self, monkeypatch):
|
|
monkeypatch.setenv("OPENAI_API_KEY", "test-api-key")
|
|
component = OpenAIChatGenerator(
|
|
model="gpt-4o-mini",
|
|
streaming_callback=lambda x: x,
|
|
api_base_url="test-base-url",
|
|
generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
|
|
)
|
|
data = component.to_dict()
|
|
assert data == {
|
|
"type": "haystack.components.generators.chat.openai.OpenAIChatGenerator",
|
|
"init_parameters": {
|
|
"api_key": {"env_vars": ["OPENAI_API_KEY"], "strict": True, "type": "env_var"},
|
|
"model": "gpt-4o-mini",
|
|
"organization": None,
|
|
"api_base_url": "test-base-url",
|
|
"streaming_callback": "chat.test_openai.<lambda>",
|
|
"generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"},
|
|
},
|
|
}
|
|
|
|
def test_from_dict(self, monkeypatch):
|
|
monkeypatch.setenv("OPENAI_API_KEY", "fake-api-key")
|
|
data = {
|
|
"type": "haystack.components.generators.chat.openai.OpenAIChatGenerator",
|
|
"init_parameters": {
|
|
"api_key": {"env_vars": ["OPENAI_API_KEY"], "strict": True, "type": "env_var"},
|
|
"model": "gpt-4o-mini",
|
|
"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"},
|
|
},
|
|
}
|
|
component = OpenAIChatGenerator.from_dict(data)
|
|
assert component.model == "gpt-4o-mini"
|
|
assert component.streaming_callback is print_streaming_chunk
|
|
assert component.api_base_url == "test-base-url"
|
|
assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"}
|
|
assert component.api_key == Secret.from_env_var("OPENAI_API_KEY")
|
|
|
|
def test_from_dict_fail_wo_env_var(self, monkeypatch):
|
|
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
|
|
data = {
|
|
"type": "haystack.components.generators.chat.openai.OpenAIChatGenerator",
|
|
"init_parameters": {
|
|
"api_key": {"env_vars": ["OPENAI_API_KEY"], "strict": True, "type": "env_var"},
|
|
"model": "gpt-4o-mini",
|
|
"organization": None,
|
|
"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"},
|
|
},
|
|
}
|
|
with pytest.raises(ValueError, match="None of the .* environment variables are set"):
|
|
OpenAIChatGenerator.from_dict(data)
|
|
|
|
def test_run(self, chat_messages, mock_chat_completion):
|
|
component = OpenAIChatGenerator(api_key=Secret.from_token("test-api-key"))
|
|
response = component.run(chat_messages)
|
|
|
|
# check that the component returns the correct ChatMessage response
|
|
assert isinstance(response, dict)
|
|
assert "replies" in response
|
|
assert isinstance(response["replies"], list)
|
|
assert len(response["replies"]) == 1
|
|
assert [isinstance(reply, ChatMessage) for reply in response["replies"]]
|
|
|
|
def test_run_with_params(self, chat_messages, mock_chat_completion):
|
|
component = OpenAIChatGenerator(
|
|
api_key=Secret.from_token("test-api-key"), generation_kwargs={"max_tokens": 10, "temperature": 0.5}
|
|
)
|
|
response = component.run(chat_messages)
|
|
|
|
# check that the component calls the OpenAI API with the correct parameters
|
|
_, kwargs = mock_chat_completion.call_args
|
|
assert kwargs["max_tokens"] == 10
|
|
assert kwargs["temperature"] == 0.5
|
|
|
|
# check that the component returns the correct response
|
|
assert isinstance(response, dict)
|
|
assert "replies" in response
|
|
assert isinstance(response["replies"], list)
|
|
assert len(response["replies"]) == 1
|
|
assert [isinstance(reply, ChatMessage) for reply in response["replies"]]
|
|
|
|
def test_run_with_params_streaming(self, chat_messages, mock_chat_completion_chunk):
|
|
streaming_callback_called = False
|
|
|
|
def streaming_callback(chunk: StreamingChunk) -> None:
|
|
nonlocal streaming_callback_called
|
|
streaming_callback_called = True
|
|
|
|
component = OpenAIChatGenerator(
|
|
api_key=Secret.from_token("test-api-key"), streaming_callback=streaming_callback
|
|
)
|
|
response = component.run(chat_messages)
|
|
|
|
# check we called the streaming callback
|
|
assert streaming_callback_called
|
|
|
|
# check that the component still returns the correct response
|
|
assert isinstance(response, dict)
|
|
assert "replies" in response
|
|
assert isinstance(response["replies"], list)
|
|
assert len(response["replies"]) == 1
|
|
assert [isinstance(reply, ChatMessage) for reply in response["replies"]]
|
|
assert "Hello" in response["replies"][0].content # see mock_chat_completion_chunk
|
|
|
|
@patch("haystack.components.generators.chat.openai.datetime")
|
|
def test_run_with_streaming_callback_in_run_method(self, mock_datetime, chat_messages, mock_chat_completion_chunk):
|
|
streaming_callback_called = False
|
|
|
|
def streaming_callback(chunk: StreamingChunk) -> None:
|
|
nonlocal streaming_callback_called
|
|
streaming_callback_called = True
|
|
|
|
component = OpenAIChatGenerator(api_key=Secret.from_token("test-api-key"))
|
|
response = component.run(chat_messages, streaming_callback=streaming_callback)
|
|
|
|
# check we called the streaming callback
|
|
assert streaming_callback_called
|
|
|
|
# check that the component still returns the correct response
|
|
assert isinstance(response, dict)
|
|
assert "replies" in response
|
|
assert isinstance(response["replies"], list)
|
|
assert len(response["replies"]) == 1
|
|
assert [isinstance(reply, ChatMessage) for reply in response["replies"]]
|
|
assert "Hello" in response["replies"][0].content # see mock_chat_completion_chunk
|
|
|
|
assert hasattr(response["replies"][0], "meta")
|
|
assert isinstance(response["replies"][0].meta, dict)
|
|
assert (
|
|
response["replies"][0].meta["completion_start_time"]
|
|
== mock_datetime.now.return_value.isoformat.return_value
|
|
)
|
|
|
|
def test_check_abnormal_completions(self, caplog):
|
|
caplog.set_level(logging.INFO)
|
|
component = OpenAIChatGenerator(api_key=Secret.from_token("test-api-key"))
|
|
messages = [
|
|
ChatMessage.from_assistant(
|
|
"", meta={"finish_reason": "content_filter" if i % 2 == 0 else "length", "index": i}
|
|
)
|
|
for i, _ in enumerate(range(4))
|
|
]
|
|
|
|
for m in messages:
|
|
component._check_finish_reason(m)
|
|
|
|
# check truncation warning
|
|
message_template = (
|
|
"The completion for index {index} has been truncated before reaching a natural stopping point. "
|
|
"Increase the max_tokens parameter to allow for longer completions."
|
|
)
|
|
|
|
for index in [1, 3]:
|
|
assert caplog.records[index].message == message_template.format(index=index)
|
|
|
|
# check content filter warning
|
|
message_template = "The completion for index {index} has been truncated due to the content filter."
|
|
for index in [0, 2]:
|
|
assert caplog.records[index].message == message_template.format(index=index)
|
|
|
|
@pytest.mark.skipif(
|
|
not os.environ.get("OPENAI_API_KEY", None),
|
|
reason="Export an env var called OPENAI_API_KEY containing the OpenAI API key to run this test.",
|
|
)
|
|
@pytest.mark.integration
|
|
def test_live_run(self):
|
|
chat_messages = [ChatMessage.from_user("What's the capital of France")]
|
|
component = OpenAIChatGenerator(generation_kwargs={"n": 1})
|
|
results = component.run(chat_messages)
|
|
assert len(results["replies"]) == 1
|
|
message: ChatMessage = results["replies"][0]
|
|
assert "Paris" in message.content
|
|
assert "gpt-4o-mini" in message.meta["model"]
|
|
assert message.meta["finish_reason"] == "stop"
|
|
|
|
@pytest.mark.skipif(
|
|
not os.environ.get("OPENAI_API_KEY", None),
|
|
reason="Export an env var called OPENAI_API_KEY containing the OpenAI API key to run this test.",
|
|
)
|
|
@pytest.mark.integration
|
|
def test_live_run_wrong_model(self, chat_messages):
|
|
component = OpenAIChatGenerator(model="something-obviously-wrong")
|
|
with pytest.raises(OpenAIError):
|
|
component.run(chat_messages)
|
|
|
|
@pytest.mark.skipif(
|
|
not os.environ.get("OPENAI_API_KEY", None),
|
|
reason="Export an env var called OPENAI_API_KEY containing the OpenAI API key to run this test.",
|
|
)
|
|
@pytest.mark.integration
|
|
def test_live_run_streaming(self):
|
|
class Callback:
|
|
def __init__(self):
|
|
self.responses = ""
|
|
self.counter = 0
|
|
|
|
def __call__(self, chunk: StreamingChunk) -> None:
|
|
self.counter += 1
|
|
self.responses += chunk.content if chunk.content else ""
|
|
|
|
callback = Callback()
|
|
component = OpenAIChatGenerator(streaming_callback=callback)
|
|
results = component.run([ChatMessage.from_user("What's the capital of France?")])
|
|
|
|
assert len(results["replies"]) == 1
|
|
message: ChatMessage = results["replies"][0]
|
|
assert "Paris" in message.content
|
|
|
|
assert "gpt-4o-mini" in message.meta["model"]
|
|
assert message.meta["finish_reason"] == "stop"
|
|
|
|
assert callback.counter > 1
|
|
assert "Paris" in callback.responses
|