2024-05-09 15:40:36 +02:00
|
|
|
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2023-12-21 16:21:24 +01:00
|
|
|
from datetime import datetime
|
2022-01-26 18:12:55 +01:00
|
|
|
from pathlib import Path
|
2024-03-13 16:59:26 +01:00
|
|
|
from test.tracing.utils import SpyingTracer
|
2024-02-22 12:52:04 +01:00
|
|
|
from typing import Generator
|
2023-12-21 16:21:24 +01:00
|
|
|
from unittest.mock import Mock, patch
|
|
|
|
|
2020-05-04 18:00:07 +02:00
|
|
|
import pytest
|
2023-12-21 16:21:24 +01:00
|
|
|
from openai.types.chat import ChatCompletion, ChatCompletionMessage
|
|
|
|
from openai.types.chat.chat_completion import Choice
|
2021-06-14 17:53:43 +02:00
|
|
|
|
2024-02-22 12:52:04 +01:00
|
|
|
from haystack import tracing
|
2023-11-24 14:48:43 +01:00
|
|
|
from haystack.testing.test_utils import set_all_seeds
|
2020-05-04 18:00:07 +02:00
|
|
|
|
2023-11-24 11:52:55 +01:00
|
|
|
set_all_seeds(0)
|
2022-01-25 20:36:28 +01:00
|
|
|
|
2024-06-26 12:32:05 +02:00
|
|
|
# Tracing is disable by default to avoid failures in CI
|
|
|
|
tracing.disable_tracing()
|
|
|
|
|
2022-08-12 09:27:56 +01:00
|
|
|
|
2023-11-24 11:52:55 +01:00
|
|
|
@pytest.fixture()
|
|
|
|
def mock_tokenizer():
|
2023-01-16 15:36:14 +01:00
|
|
|
"""
|
2023-11-24 11:52:55 +01:00
|
|
|
Tokenizes the string by splitting on spaces.
|
2023-01-16 15:36:14 +01:00
|
|
|
"""
|
2023-11-24 11:52:55 +01:00
|
|
|
tokenizer = Mock()
|
|
|
|
tokenizer.encode = lambda text: text.split()
|
|
|
|
tokenizer.decode = lambda tokens: " ".join(tokens)
|
|
|
|
return tokenizer
|
2023-06-13 14:52:24 +02:00
|
|
|
|
|
|
|
|
2023-11-24 11:52:55 +01:00
|
|
|
@pytest.fixture()
|
|
|
|
def test_files_path():
|
|
|
|
return Path(__file__).parent / "test_files"
|
2023-12-21 08:51:54 +01:00
|
|
|
|
|
|
|
|
2023-12-21 16:21:24 +01:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_chat_completion():
|
|
|
|
"""
|
|
|
|
Mock the OpenAI API completion response and reuse it for tests
|
|
|
|
"""
|
|
|
|
with patch("openai.resources.chat.completions.Completions.create") as mock_chat_completion_create:
|
|
|
|
completion = ChatCompletion(
|
|
|
|
id="foo",
|
|
|
|
model="gpt-4",
|
|
|
|
object="chat.completion",
|
|
|
|
choices=[
|
|
|
|
Choice(
|
|
|
|
finish_reason="stop",
|
|
|
|
logprobs=None,
|
|
|
|
index=0,
|
|
|
|
message=ChatCompletionMessage(content="Hello world!", role="assistant"),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
created=int(datetime.now().timestamp()),
|
|
|
|
usage={"prompt_tokens": 57, "completion_tokens": 40, "total_tokens": 97},
|
|
|
|
)
|
|
|
|
|
|
|
|
mock_chat_completion_create.return_value = completion
|
|
|
|
yield mock_chat_completion_create
|
|
|
|
|
|
|
|
|
2023-12-21 08:51:54 +01:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def request_blocker(request: pytest.FixtureRequest, monkeypatch):
|
|
|
|
"""
|
|
|
|
This fixture is applied automatically to all tests.
|
|
|
|
Those that are not marked as integration will have the requests module
|
|
|
|
monkeypatched to avoid making HTTP requests by mistake.
|
|
|
|
"""
|
|
|
|
marker = request.node.get_closest_marker("integration")
|
|
|
|
if marker is not None:
|
|
|
|
return
|
|
|
|
|
|
|
|
def urlopen_mock(self, method, url, *args, **kwargs):
|
|
|
|
raise RuntimeError(f"The test was about to {method} {self.scheme}://{self.host}{url}")
|
|
|
|
|
|
|
|
monkeypatch.setattr("urllib3.connectionpool.HTTPConnectionPool.urlopen", urlopen_mock)
|
2024-02-22 12:52:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def spying_tracer() -> Generator[SpyingTracer, None, None]:
|
|
|
|
tracer = SpyingTracer()
|
|
|
|
tracing.enable_tracing(tracer)
|
|
|
|
|
|
|
|
yield tracer
|
|
|
|
|
|
|
|
# Make sure to disable tracing after the test to avoid affecting other tests
|
|
|
|
tracing.disable_tracing()
|