chore: import upstream snapshot with attribution
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
Docker image release / Build base image (push) Has been cancelled
Sync docs with Docusaurus / sync (push) Has been cancelled
Tests / Check if changed (push) Has been cancelled
Tests / format (push) Has been cancelled
Tests / check-imports (push) Has been cancelled
Tests / Unit / macos-latest (push) Has been cancelled
Tests / Unit / ubuntu-latest (push) Has been cancelled
Tests / Unit / windows-latest (push) Has been cancelled
Tests / mypy (push) Has been cancelled
Tests / Integration / ubuntu-latest (push) Has been cancelled
Tests / Integration / macos-latest (push) Has been cancelled
Tests / Integration / windows-latest (push) Has been cancelled
Tests / notify-slack-on-failure (push) Has been cancelled
Tests / Mark tests as completed (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
Docker image release / Build base image (push) Has been cancelled
Sync docs with Docusaurus / sync (push) Has been cancelled
Tests / Check if changed (push) Has been cancelled
Tests / format (push) Has been cancelled
Tests / check-imports (push) Has been cancelled
Tests / Unit / macos-latest (push) Has been cancelled
Tests / Unit / ubuntu-latest (push) Has been cancelled
Tests / Unit / windows-latest (push) Has been cancelled
Tests / mypy (push) Has been cancelled
Tests / Integration / ubuntu-latest (push) Has been cancelled
Tests / Integration / macos-latest (push) Has been cancelled
Tests / Integration / windows-latest (push) Has been cancelled
Tests / notify-slack-on-failure (push) Has been cancelled
Tests / Mark tests as completed (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
@@ -0,0 +1,385 @@
|
||||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import os
|
||||
from unittest.mock import AsyncMock, MagicMock, Mock, patch
|
||||
|
||||
import pytest
|
||||
from openai import APIError, OpenAIError
|
||||
|
||||
import haystack.components.embedders.azure_document_embedder as azure_document_embedder_module
|
||||
from haystack import Document
|
||||
from haystack.components.embedders import AzureOpenAIDocumentEmbedder
|
||||
from haystack.utils.auth import Secret
|
||||
from haystack.utils.azure import default_azure_ad_token_provider
|
||||
|
||||
|
||||
class TestAzureOpenAIDocumentEmbedder:
|
||||
def test_init_default(self, monkeypatch):
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake-api-key")
|
||||
embedder = AzureOpenAIDocumentEmbedder(azure_endpoint="https://example-resource.azure.openai.com/")
|
||||
assert embedder.azure_deployment == "text-embedding-ada-002"
|
||||
assert embedder.model == "text-embedding-ada-002"
|
||||
assert embedder.dimensions is None
|
||||
assert embedder.organization is None
|
||||
assert embedder.prefix == ""
|
||||
assert embedder.suffix == ""
|
||||
assert embedder.batch_size == 32
|
||||
assert embedder.progress_bar is True
|
||||
assert embedder.meta_fields_to_embed == []
|
||||
assert embedder.embedding_separator == "\n"
|
||||
assert embedder.timeout is None
|
||||
assert embedder.max_retries is None
|
||||
assert embedder.default_headers == {}
|
||||
assert embedder.azure_ad_token_provider is None
|
||||
assert embedder.http_client_kwargs is None
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is None
|
||||
|
||||
def test_init_with_0_max_retries(self, monkeypatch):
|
||||
"""Tests that the max_retries init param is set correctly if equal 0"""
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake-api-key")
|
||||
embedder = AzureOpenAIDocumentEmbedder(
|
||||
azure_endpoint="https://example-resource.azure.openai.com/", max_retries=0
|
||||
)
|
||||
assert embedder.azure_deployment == "text-embedding-ada-002"
|
||||
assert embedder.model == "text-embedding-ada-002"
|
||||
assert embedder.dimensions is None
|
||||
assert embedder.organization is None
|
||||
assert embedder.prefix == ""
|
||||
assert embedder.suffix == ""
|
||||
assert embedder.batch_size == 32
|
||||
assert embedder.progress_bar is True
|
||||
assert embedder.meta_fields_to_embed == []
|
||||
assert embedder.embedding_separator == "\n"
|
||||
assert embedder.default_headers == {}
|
||||
assert embedder.azure_ad_token_provider is None
|
||||
assert embedder.max_retries == 0
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is None
|
||||
|
||||
def test_to_dict(self, monkeypatch):
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake-api-key")
|
||||
component = AzureOpenAIDocumentEmbedder(azure_endpoint="https://example-resource.azure.openai.com/")
|
||||
data = component.to_dict()
|
||||
assert data == {
|
||||
"type": "haystack.components.embedders.azure_document_embedder.AzureOpenAIDocumentEmbedder",
|
||||
"init_parameters": {
|
||||
"api_key": {"env_vars": ["AZURE_OPENAI_API_KEY"], "strict": False, "type": "env_var"},
|
||||
"azure_ad_token": {"env_vars": ["AZURE_OPENAI_AD_TOKEN"], "strict": False, "type": "env_var"},
|
||||
"api_version": "2023-05-15",
|
||||
"azure_deployment": "text-embedding-ada-002",
|
||||
"dimensions": None,
|
||||
"azure_endpoint": "https://example-resource.azure.openai.com/",
|
||||
"organization": None,
|
||||
"prefix": "",
|
||||
"suffix": "",
|
||||
"batch_size": 32,
|
||||
"progress_bar": True,
|
||||
"meta_fields_to_embed": [],
|
||||
"embedding_separator": "\n",
|
||||
"max_retries": None,
|
||||
"timeout": None,
|
||||
"default_headers": {},
|
||||
"azure_ad_token_provider": None,
|
||||
"http_client_kwargs": None,
|
||||
"raise_on_failure": False,
|
||||
},
|
||||
}
|
||||
|
||||
def test_to_dict_with_parameters(self, monkeypatch):
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake-api-key")
|
||||
component = AzureOpenAIDocumentEmbedder(
|
||||
azure_endpoint="https://example-resource.azure.openai.com/",
|
||||
azure_deployment="text-embedding-ada-002",
|
||||
dimensions=768,
|
||||
organization="HaystackCI",
|
||||
timeout=60.0,
|
||||
max_retries=10,
|
||||
prefix="prefix ",
|
||||
suffix=" suffix",
|
||||
default_headers={"x-custom-header": "custom-value"},
|
||||
azure_ad_token_provider=default_azure_ad_token_provider,
|
||||
http_client_kwargs={"proxy": "http://example.com:3128", "verify": False},
|
||||
raise_on_failure=True,
|
||||
)
|
||||
data = component.to_dict()
|
||||
assert data == {
|
||||
"type": "haystack.components.embedders.azure_document_embedder.AzureOpenAIDocumentEmbedder",
|
||||
"init_parameters": {
|
||||
"api_key": {"env_vars": ["AZURE_OPENAI_API_KEY"], "strict": False, "type": "env_var"},
|
||||
"azure_ad_token": {"env_vars": ["AZURE_OPENAI_AD_TOKEN"], "strict": False, "type": "env_var"},
|
||||
"api_version": "2023-05-15",
|
||||
"azure_deployment": "text-embedding-ada-002",
|
||||
"dimensions": 768,
|
||||
"azure_endpoint": "https://example-resource.azure.openai.com/",
|
||||
"organization": "HaystackCI",
|
||||
"prefix": "prefix ",
|
||||
"suffix": " suffix",
|
||||
"batch_size": 32,
|
||||
"progress_bar": True,
|
||||
"meta_fields_to_embed": [],
|
||||
"embedding_separator": "\n",
|
||||
"max_retries": 10,
|
||||
"timeout": 60.0,
|
||||
"default_headers": {"x-custom-header": "custom-value"},
|
||||
"azure_ad_token_provider": "haystack.utils.azure.default_azure_ad_token_provider",
|
||||
"http_client_kwargs": {"proxy": "http://example.com:3128", "verify": False},
|
||||
"raise_on_failure": True,
|
||||
},
|
||||
}
|
||||
|
||||
def test_from_dict(self, monkeypatch):
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake-api-key")
|
||||
data = {
|
||||
"type": "haystack.components.embedders.azure_document_embedder.AzureOpenAIDocumentEmbedder",
|
||||
"init_parameters": {
|
||||
"api_key": {"env_vars": ["AZURE_OPENAI_API_KEY"], "strict": False, "type": "env_var"},
|
||||
"azure_ad_token": {"env_vars": ["AZURE_OPENAI_AD_TOKEN"], "strict": False, "type": "env_var"},
|
||||
"api_version": "2023-05-15",
|
||||
"azure_deployment": "text-embedding-ada-002",
|
||||
"dimensions": None,
|
||||
"azure_endpoint": "https://example-resource.azure.openai.com/",
|
||||
"organization": None,
|
||||
"prefix": "",
|
||||
"suffix": "",
|
||||
"batch_size": 32,
|
||||
"progress_bar": True,
|
||||
"meta_fields_to_embed": [],
|
||||
"embedding_separator": "\n",
|
||||
"max_retries": 5,
|
||||
"timeout": 30.0,
|
||||
"default_headers": {},
|
||||
"azure_ad_token_provider": None,
|
||||
"http_client_kwargs": None,
|
||||
"raise_on_failure": False,
|
||||
},
|
||||
}
|
||||
component = AzureOpenAIDocumentEmbedder.from_dict(data)
|
||||
assert component.azure_deployment == "text-embedding-ada-002"
|
||||
assert component.azure_endpoint == "https://example-resource.azure.openai.com/"
|
||||
assert component.api_version == "2023-05-15"
|
||||
assert component.max_retries == 5
|
||||
assert component.timeout == 30.0
|
||||
assert component.prefix == ""
|
||||
assert component.suffix == ""
|
||||
assert component.default_headers == {}
|
||||
assert component.azure_ad_token_provider is None
|
||||
assert component.http_client_kwargs is None
|
||||
assert component.raise_on_failure is False
|
||||
|
||||
def test_from_dict_with_parameters(self, monkeypatch):
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake-api-key")
|
||||
data = {
|
||||
"type": "haystack.components.embedders.azure_document_embedder.AzureOpenAIDocumentEmbedder",
|
||||
"init_parameters": {
|
||||
"api_key": {"env_vars": ["AZURE_OPENAI_API_KEY"], "strict": False, "type": "env_var"},
|
||||
"azure_ad_token": {"env_vars": ["AZURE_OPENAI_AD_TOKEN"], "strict": False, "type": "env_var"},
|
||||
"api_version": "2023-05-15",
|
||||
"azure_deployment": "text-embedding-ada-002",
|
||||
"dimensions": 768,
|
||||
"azure_endpoint": "https://example-resource.azure.openai.com/",
|
||||
"organization": "HaystackCI",
|
||||
"prefix": "prefix ",
|
||||
"suffix": " suffix",
|
||||
"batch_size": 32,
|
||||
"progress_bar": True,
|
||||
"meta_fields_to_embed": [],
|
||||
"embedding_separator": "\n",
|
||||
"max_retries": 10,
|
||||
"timeout": 60.0,
|
||||
"default_headers": {"x-custom-header": "custom-value"},
|
||||
"azure_ad_token_provider": "haystack.utils.azure.default_azure_ad_token_provider",
|
||||
"http_client_kwargs": {"proxy": "http://example.com:3128", "verify": False},
|
||||
"raise_on_failure": True,
|
||||
},
|
||||
}
|
||||
component = AzureOpenAIDocumentEmbedder.from_dict(data)
|
||||
assert component.azure_deployment == "text-embedding-ada-002"
|
||||
assert component.azure_endpoint == "https://example-resource.azure.openai.com/"
|
||||
assert component.api_version == "2023-05-15"
|
||||
assert component.max_retries == 10
|
||||
assert component.timeout == 60.0
|
||||
assert component.prefix == "prefix "
|
||||
assert component.suffix == " suffix"
|
||||
assert component.default_headers == {"x-custom-header": "custom-value"}
|
||||
assert component.azure_ad_token_provider is not None
|
||||
assert component.http_client_kwargs == {"proxy": "http://example.com:3128", "verify": False}
|
||||
assert component.raise_on_failure is True
|
||||
|
||||
def test_embed_batch_handles_exceptions_gracefully(self, caplog):
|
||||
embedder = AzureOpenAIDocumentEmbedder(
|
||||
azure_endpoint="https://test.openai.azure.com",
|
||||
api_key=Secret.from_token("fake-api-key"),
|
||||
azure_deployment="text-embedding-ada-002",
|
||||
embedding_separator=" | ",
|
||||
)
|
||||
embedder.warm_up()
|
||||
assert embedder.client is not None
|
||||
|
||||
fake_texts_to_embed = {"1": "text1", "2": "text2"}
|
||||
|
||||
with patch.object(
|
||||
embedder.client.embeddings,
|
||||
"create",
|
||||
side_effect=APIError(message="Mocked error", request=Mock(), body=None),
|
||||
):
|
||||
embedder._embed_batch(texts_to_embed=fake_texts_to_embed, batch_size=32)
|
||||
|
||||
assert len(caplog.records) == 1
|
||||
assert "Failed embedding of documents 1, 2 caused by Mocked error" in caplog.text
|
||||
|
||||
def test_embed_batch_raises_exception_on_failure(self):
|
||||
embedder = AzureOpenAIDocumentEmbedder(
|
||||
azure_endpoint="https://test.openai.azure.com",
|
||||
api_key=Secret.from_token("fake-api-key"),
|
||||
azure_deployment="text-embedding-ada-002",
|
||||
raise_on_failure=True,
|
||||
)
|
||||
embedder.warm_up()
|
||||
assert embedder.client is not None
|
||||
fake_texts_to_embed = {"1": "text1", "2": "text2"}
|
||||
with patch.object(
|
||||
embedder.client.embeddings,
|
||||
"create",
|
||||
side_effect=APIError(message="Mocked error", request=Mock(), body=None),
|
||||
):
|
||||
with pytest.raises(APIError, match="Mocked error"):
|
||||
embedder._embed_batch(texts_to_embed=fake_texts_to_embed, batch_size=2)
|
||||
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.skipif(
|
||||
not os.environ.get("AZURE_OPENAI_API_KEY", None) and not os.environ.get("AZURE_OPENAI_ENDPOINT", None),
|
||||
reason=(
|
||||
"Please export env variables called AZURE_OPENAI_API_KEY containing "
|
||||
"the Azure OpenAI key, AZURE_OPENAI_ENDPOINT containing "
|
||||
"the Azure OpenAI endpoint URL to run this test."
|
||||
),
|
||||
)
|
||||
def test_run(self):
|
||||
docs = [
|
||||
Document(content="I love cheese", meta={"topic": "Cuisine"}),
|
||||
Document(content="A transformer is a deep learning architecture", meta={"topic": "ML"}),
|
||||
]
|
||||
# the default model is text-embedding-ada-002 even if we don't specify it, but let's be explicit
|
||||
embedder = AzureOpenAIDocumentEmbedder(
|
||||
azure_deployment="text-embedding-ada-002",
|
||||
meta_fields_to_embed=["topic"],
|
||||
embedding_separator=" | ",
|
||||
organization="HaystackCI",
|
||||
)
|
||||
|
||||
result = embedder.run(documents=docs)
|
||||
documents_with_embeddings = result["documents"]
|
||||
metadata = result["meta"]
|
||||
|
||||
assert isinstance(documents_with_embeddings, list)
|
||||
assert len(documents_with_embeddings) == len(docs)
|
||||
for doc, new_doc in zip(docs, documents_with_embeddings, strict=True):
|
||||
assert doc.embedding is None
|
||||
assert new_doc is not doc
|
||||
assert isinstance(new_doc, Document)
|
||||
assert isinstance(new_doc.embedding, list)
|
||||
assert len(new_doc.embedding) == 1536
|
||||
assert all(isinstance(x, float) for x in new_doc.embedding)
|
||||
|
||||
assert metadata["usage"]["prompt_tokens"] == 15
|
||||
assert metadata["usage"]["total_tokens"] == 15
|
||||
assert "ada" in metadata["model"]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_azure_clients(monkeypatch):
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake")
|
||||
sync_cls = MagicMock(name="AzureOpenAI")
|
||||
async_cls = MagicMock(name="AsyncAzureOpenAI")
|
||||
async_cls.return_value.close = AsyncMock()
|
||||
monkeypatch.setattr(azure_document_embedder_module, "AzureOpenAI", sync_cls)
|
||||
monkeypatch.setattr(azure_document_embedder_module, "AsyncAzureOpenAI", async_cls)
|
||||
return sync_cls, async_cls
|
||||
|
||||
|
||||
class TestComponentLifecycle:
|
||||
def test_warm_up_uses_default_timeout_and_max_retries(self, monkeypatch):
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake-api-key")
|
||||
embedder = AzureOpenAIDocumentEmbedder(azure_endpoint="https://example-resource.azure.openai.com/")
|
||||
embedder.warm_up()
|
||||
assert embedder.client is not None
|
||||
assert embedder.client.max_retries == 5
|
||||
assert embedder.client.timeout == 30.0
|
||||
|
||||
def test_warm_up_uses_timeout_and_max_retries_from_parameters(self, monkeypatch):
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake-api-key")
|
||||
embedder = AzureOpenAIDocumentEmbedder(
|
||||
azure_endpoint="https://example-resource.azure.openai.com/", timeout=40.0, max_retries=1
|
||||
)
|
||||
embedder.warm_up()
|
||||
assert embedder.client is not None
|
||||
assert embedder.client.max_retries == 1
|
||||
assert embedder.client.timeout == 40.0
|
||||
|
||||
def test_warm_up_uses_timeout_and_max_retries_from_env_vars(self, monkeypatch):
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake-api-key")
|
||||
monkeypatch.setenv("OPENAI_TIMEOUT", "100")
|
||||
monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
|
||||
embedder = AzureOpenAIDocumentEmbedder(azure_endpoint="https://example-resource.azure.openai.com/")
|
||||
embedder.warm_up()
|
||||
assert embedder.client is not None
|
||||
assert embedder.client.max_retries == 10
|
||||
assert embedder.client.timeout == 100.0
|
||||
|
||||
def test_key_resolved_at_warm_up_not_init(self, monkeypatch):
|
||||
monkeypatch.delenv("AZURE_OPENAI_API_KEY", raising=False)
|
||||
monkeypatch.delenv("AZURE_OPENAI_AD_TOKEN", raising=False)
|
||||
embedder = AzureOpenAIDocumentEmbedder(azure_endpoint="https://example-resource.azure.openai.com/")
|
||||
assert embedder.client is None
|
||||
with pytest.raises(OpenAIError):
|
||||
embedder.warm_up()
|
||||
|
||||
def test_sync_lifecycle(self, mock_azure_clients):
|
||||
sync_cls, _ = mock_azure_clients
|
||||
sync_client = sync_cls.return_value
|
||||
embedder = AzureOpenAIDocumentEmbedder(azure_endpoint="https://example-resource.azure.openai.com/")
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is None
|
||||
|
||||
embedder.warm_up()
|
||||
assert embedder.client is sync_cls.return_value
|
||||
assert embedder.async_client is None
|
||||
|
||||
embedder.close()
|
||||
sync_client.close.assert_called_once()
|
||||
assert embedder.client is None
|
||||
|
||||
async def test_async_lifecycle(self, mock_azure_clients):
|
||||
_, async_cls = mock_azure_clients
|
||||
async_client = async_cls.return_value
|
||||
embedder = AzureOpenAIDocumentEmbedder(azure_endpoint="https://example-resource.azure.openai.com/")
|
||||
|
||||
await embedder.warm_up_async()
|
||||
assert embedder.async_client is async_cls.return_value
|
||||
assert embedder.client is None
|
||||
|
||||
await embedder.close_async()
|
||||
async_client.close.assert_awaited_once()
|
||||
assert embedder.async_client is None
|
||||
|
||||
async def test_close_is_safe_without_warm_up(self, mock_azure_clients):
|
||||
embedder = AzureOpenAIDocumentEmbedder(azure_endpoint="https://example-resource.azure.openai.com/")
|
||||
embedder.close()
|
||||
await embedder.close_async()
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is None
|
||||
|
||||
async def test_close_and_close_async_are_independent(self, mock_azure_clients):
|
||||
embedder = AzureOpenAIDocumentEmbedder(azure_endpoint="https://example-resource.azure.openai.com/")
|
||||
embedder.warm_up()
|
||||
await embedder.warm_up_async()
|
||||
|
||||
embedder.close()
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is not None
|
||||
|
||||
await embedder.close_async()
|
||||
assert embedder.async_client is None
|
||||
@@ -0,0 +1,296 @@
|
||||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import os
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
from openai import OpenAIError
|
||||
|
||||
import haystack.components.embedders.azure_text_embedder as azure_text_embedder_module
|
||||
from haystack.components.embedders import AzureOpenAITextEmbedder
|
||||
from haystack.utils.azure import default_azure_ad_token_provider
|
||||
|
||||
|
||||
class TestAzureOpenAITextEmbedder:
|
||||
def test_init_default(self, monkeypatch):
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake-api-key")
|
||||
embedder = AzureOpenAITextEmbedder(azure_endpoint="https://example-resource.azure.openai.com/")
|
||||
|
||||
assert embedder.api_key.resolve_value() == "fake-api-key"
|
||||
assert embedder.azure_deployment == "text-embedding-ada-002"
|
||||
assert embedder.model == "text-embedding-ada-002"
|
||||
assert embedder.dimensions is None
|
||||
assert embedder.organization is None
|
||||
assert embedder.prefix == ""
|
||||
assert embedder.suffix == ""
|
||||
assert embedder.timeout is None
|
||||
assert embedder.max_retries is None
|
||||
assert embedder.default_headers == {}
|
||||
assert embedder.azure_ad_token_provider is None
|
||||
assert embedder.http_client_kwargs is None
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is None
|
||||
|
||||
def test_init_with_zero_max_retries(self, monkeypatch):
|
||||
"""Tests that the max_retries init param is set correctly if equal 0"""
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake-api-key")
|
||||
embedder = AzureOpenAITextEmbedder(azure_endpoint="https://example-resource.azure.openai.com/", max_retries=0)
|
||||
|
||||
assert embedder.api_key.resolve_value() == "fake-api-key"
|
||||
assert embedder.azure_deployment == "text-embedding-ada-002"
|
||||
assert embedder.model == "text-embedding-ada-002"
|
||||
assert embedder.dimensions is None
|
||||
assert embedder.organization is None
|
||||
assert embedder.prefix == ""
|
||||
assert embedder.suffix == ""
|
||||
assert embedder.default_headers == {}
|
||||
assert embedder.azure_ad_token_provider is None
|
||||
assert embedder.max_retries == 0
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is None
|
||||
|
||||
def test_to_dict_default(self, monkeypatch):
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake-api-key")
|
||||
component = AzureOpenAITextEmbedder(azure_endpoint="https://example-resource.azure.openai.com/")
|
||||
data = component.to_dict()
|
||||
assert data == {
|
||||
"type": "haystack.components.embedders.azure_text_embedder.AzureOpenAITextEmbedder",
|
||||
"init_parameters": {
|
||||
"api_key": {"env_vars": ["AZURE_OPENAI_API_KEY"], "strict": False, "type": "env_var"},
|
||||
"azure_ad_token": {"env_vars": ["AZURE_OPENAI_AD_TOKEN"], "strict": False, "type": "env_var"},
|
||||
"azure_deployment": "text-embedding-ada-002",
|
||||
"dimensions": None,
|
||||
"organization": None,
|
||||
"azure_endpoint": "https://example-resource.azure.openai.com/",
|
||||
"api_version": "2023-05-15",
|
||||
"max_retries": None,
|
||||
"timeout": None,
|
||||
"prefix": "",
|
||||
"suffix": "",
|
||||
"default_headers": {},
|
||||
"azure_ad_token_provider": None,
|
||||
"http_client_kwargs": None,
|
||||
},
|
||||
}
|
||||
|
||||
def test_to_dict_with_params(self, monkeypatch):
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake-api-key")
|
||||
component = AzureOpenAITextEmbedder(
|
||||
azure_endpoint="https://example-resource.azure.openai.com/",
|
||||
azure_deployment="text-embedding-ada-002",
|
||||
dimensions=768,
|
||||
organization="HaystackCI",
|
||||
timeout=60.0,
|
||||
max_retries=10,
|
||||
prefix="prefix ",
|
||||
suffix=" suffix",
|
||||
default_headers={"x-custom-header": "custom-value"},
|
||||
azure_ad_token_provider=default_azure_ad_token_provider,
|
||||
http_client_kwargs={"proxy": "http://example.com:3128", "verify": False},
|
||||
)
|
||||
data = component.to_dict()
|
||||
assert data == {
|
||||
"type": "haystack.components.embedders.azure_text_embedder.AzureOpenAITextEmbedder",
|
||||
"init_parameters": {
|
||||
"api_key": {"env_vars": ["AZURE_OPENAI_API_KEY"], "strict": False, "type": "env_var"},
|
||||
"azure_ad_token": {"env_vars": ["AZURE_OPENAI_AD_TOKEN"], "strict": False, "type": "env_var"},
|
||||
"azure_deployment": "text-embedding-ada-002",
|
||||
"dimensions": 768,
|
||||
"organization": "HaystackCI",
|
||||
"azure_endpoint": "https://example-resource.azure.openai.com/",
|
||||
"api_version": "2023-05-15",
|
||||
"max_retries": 10,
|
||||
"timeout": 60.0,
|
||||
"prefix": "prefix ",
|
||||
"suffix": " suffix",
|
||||
"default_headers": {"x-custom-header": "custom-value"},
|
||||
"azure_ad_token_provider": "haystack.utils.azure.default_azure_ad_token_provider",
|
||||
"http_client_kwargs": {"proxy": "http://example.com:3128", "verify": False},
|
||||
},
|
||||
}
|
||||
|
||||
def test_from_dict(self, monkeypatch):
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake-api-key")
|
||||
data = {
|
||||
"type": "haystack.components.embedders.azure_text_embedder.AzureOpenAITextEmbedder",
|
||||
"init_parameters": {
|
||||
"api_key": {"env_vars": ["AZURE_OPENAI_API_KEY"], "strict": False, "type": "env_var"},
|
||||
"azure_ad_token": {"env_vars": ["AZURE_OPENAI_AD_TOKEN"], "strict": False, "type": "env_var"},
|
||||
"azure_deployment": "text-embedding-ada-002",
|
||||
"dimensions": None,
|
||||
"organization": None,
|
||||
"azure_endpoint": "https://example-resource.azure.openai.com/",
|
||||
"api_version": "2023-05-15",
|
||||
"max_retries": 5,
|
||||
"timeout": 30.0,
|
||||
"prefix": "",
|
||||
"suffix": "",
|
||||
"default_headers": {},
|
||||
"http_client_kwargs": None,
|
||||
},
|
||||
}
|
||||
component = AzureOpenAITextEmbedder.from_dict(data)
|
||||
assert component.azure_deployment == "text-embedding-ada-002"
|
||||
assert component.model == "text-embedding-ada-002"
|
||||
assert component.azure_endpoint == "https://example-resource.azure.openai.com/"
|
||||
assert component.api_version == "2023-05-15"
|
||||
assert component.max_retries == 5
|
||||
assert component.timeout == 30.0
|
||||
assert component.prefix == ""
|
||||
assert component.suffix == ""
|
||||
assert component.default_headers == {}
|
||||
assert component.azure_ad_token_provider is None
|
||||
assert component.http_client_kwargs is None
|
||||
|
||||
def test_from_dict_with_parameters(self, monkeypatch):
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake-api-key")
|
||||
data = {
|
||||
"type": "haystack.components.embedders.azure_text_embedder.AzureOpenAITextEmbedder",
|
||||
"init_parameters": {
|
||||
"api_key": {"env_vars": ["AZURE_OPENAI_API_KEY"], "strict": False, "type": "env_var"},
|
||||
"azure_ad_token": {"env_vars": ["AZURE_OPENAI_AD_TOKEN"], "strict": False, "type": "env_var"},
|
||||
"azure_deployment": "text-embedding-ada-002",
|
||||
"dimensions": 768,
|
||||
"organization": "HaystackCI",
|
||||
"azure_endpoint": "https://example-resource.azure.openai.com/",
|
||||
"api_version": "2023-05-15",
|
||||
"max_retries": 10,
|
||||
"timeout": 60.0,
|
||||
"prefix": "prefix ",
|
||||
"suffix": " suffix",
|
||||
"default_headers": {"x-custom-header": "custom-value"},
|
||||
"azure_ad_token_provider": "haystack.utils.azure.default_azure_ad_token_provider",
|
||||
"http_client_kwargs": {"proxy": "http://example.com:3128", "verify": False},
|
||||
},
|
||||
}
|
||||
component = AzureOpenAITextEmbedder.from_dict(data)
|
||||
assert component.azure_deployment == "text-embedding-ada-002"
|
||||
assert component.model == "text-embedding-ada-002"
|
||||
assert component.azure_endpoint == "https://example-resource.azure.openai.com/"
|
||||
assert component.api_version == "2023-05-15"
|
||||
assert component.max_retries == 10
|
||||
assert component.timeout == 60.0
|
||||
assert component.prefix == "prefix "
|
||||
assert component.suffix == " suffix"
|
||||
assert component.default_headers == {"x-custom-header": "custom-value"}
|
||||
assert component.azure_ad_token_provider is not None
|
||||
assert component.http_client_kwargs == {"proxy": "http://example.com:3128", "verify": False}
|
||||
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.skipif(
|
||||
not os.environ.get("AZURE_OPENAI_API_KEY", None) and not os.environ.get("AZURE_OPENAI_ENDPOINT", None),
|
||||
reason=(
|
||||
"Please export env variables called AZURE_OPENAI_API_KEY containing "
|
||||
"the Azure OpenAI key, AZURE_OPENAI_ENDPOINT containing "
|
||||
"the Azure OpenAI endpoint URL to run this test."
|
||||
),
|
||||
)
|
||||
def test_run(self):
|
||||
# the default model is text-embedding-ada-002 even if we don't specify it, but let's be explicit
|
||||
embedder = AzureOpenAITextEmbedder(
|
||||
azure_deployment="text-embedding-ada-002", prefix="prefix ", suffix=" suffix", organization="HaystackCI"
|
||||
)
|
||||
result = embedder.run(text="The food was delicious")
|
||||
|
||||
assert len(result["embedding"]) == 1536
|
||||
assert all(isinstance(x, float) for x in result["embedding"])
|
||||
assert result["meta"]["usage"] == {"prompt_tokens": 6, "total_tokens": 6}
|
||||
assert "ada" in result["meta"]["model"]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_azure_clients(monkeypatch):
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake")
|
||||
sync_cls = MagicMock(name="AzureOpenAI")
|
||||
async_cls = MagicMock(name="AsyncAzureOpenAI")
|
||||
async_cls.return_value.close = AsyncMock()
|
||||
monkeypatch.setattr(azure_text_embedder_module, "AzureOpenAI", sync_cls)
|
||||
monkeypatch.setattr(azure_text_embedder_module, "AsyncAzureOpenAI", async_cls)
|
||||
return sync_cls, async_cls
|
||||
|
||||
|
||||
class TestComponentLifecycle:
|
||||
def test_warm_up_uses_default_timeout_and_max_retries(self, monkeypatch):
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake-api-key")
|
||||
embedder = AzureOpenAITextEmbedder(azure_endpoint="https://example-resource.azure.openai.com/")
|
||||
embedder.warm_up()
|
||||
assert embedder.client is not None
|
||||
assert embedder.client.max_retries == 5
|
||||
assert embedder.client.timeout == 30.0
|
||||
|
||||
def test_warm_up_uses_timeout_and_max_retries_from_parameters(self, monkeypatch):
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake-api-key")
|
||||
embedder = AzureOpenAITextEmbedder(
|
||||
azure_endpoint="https://example-resource.azure.openai.com/", timeout=40.0, max_retries=1
|
||||
)
|
||||
embedder.warm_up()
|
||||
assert embedder.client is not None
|
||||
assert embedder.client.max_retries == 1
|
||||
assert embedder.client.timeout == 40.0
|
||||
|
||||
def test_warm_up_uses_timeout_and_max_retries_from_env_vars(self, monkeypatch):
|
||||
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "fake-api-key")
|
||||
monkeypatch.setenv("OPENAI_TIMEOUT", "100")
|
||||
monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
|
||||
embedder = AzureOpenAITextEmbedder(azure_endpoint="https://example-resource.azure.openai.com/")
|
||||
embedder.warm_up()
|
||||
assert embedder.client is not None
|
||||
assert embedder.client.max_retries == 10
|
||||
assert embedder.client.timeout == 100.0
|
||||
|
||||
def test_key_resolved_at_warm_up_not_init(self, monkeypatch):
|
||||
monkeypatch.delenv("AZURE_OPENAI_API_KEY", raising=False)
|
||||
monkeypatch.delenv("AZURE_OPENAI_AD_TOKEN", raising=False)
|
||||
embedder = AzureOpenAITextEmbedder(azure_endpoint="https://example-resource.azure.openai.com/")
|
||||
assert embedder.client is None
|
||||
with pytest.raises(OpenAIError):
|
||||
embedder.warm_up()
|
||||
|
||||
def test_sync_lifecycle(self, mock_azure_clients):
|
||||
sync_cls, _ = mock_azure_clients
|
||||
sync_client = sync_cls.return_value
|
||||
embedder = AzureOpenAITextEmbedder(azure_endpoint="https://example-resource.azure.openai.com/")
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is None
|
||||
|
||||
embedder.warm_up()
|
||||
assert embedder.client is sync_cls.return_value
|
||||
assert embedder.async_client is None
|
||||
|
||||
embedder.close()
|
||||
sync_client.close.assert_called_once()
|
||||
assert embedder.client is None
|
||||
|
||||
async def test_async_lifecycle(self, mock_azure_clients):
|
||||
_, async_cls = mock_azure_clients
|
||||
async_client = async_cls.return_value
|
||||
embedder = AzureOpenAITextEmbedder(azure_endpoint="https://example-resource.azure.openai.com/")
|
||||
|
||||
await embedder.warm_up_async()
|
||||
assert embedder.async_client is async_cls.return_value
|
||||
assert embedder.client is None
|
||||
|
||||
await embedder.close_async()
|
||||
async_client.close.assert_awaited_once()
|
||||
assert embedder.async_client is None
|
||||
|
||||
async def test_close_is_safe_without_warm_up(self, mock_azure_clients):
|
||||
embedder = AzureOpenAITextEmbedder(azure_endpoint="https://example-resource.azure.openai.com/")
|
||||
embedder.close()
|
||||
await embedder.close_async()
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is None
|
||||
|
||||
async def test_close_and_close_async_are_independent(self, mock_azure_clients):
|
||||
embedder = AzureOpenAITextEmbedder(azure_endpoint="https://example-resource.azure.openai.com/")
|
||||
embedder.warm_up()
|
||||
await embedder.warm_up_async()
|
||||
|
||||
embedder.close()
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is not None
|
||||
|
||||
await embedder.close_async()
|
||||
assert embedder.async_client is None
|
||||
@@ -0,0 +1,119 @@
|
||||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import pytest
|
||||
|
||||
from haystack import Document, Pipeline
|
||||
from haystack.components.embedders import MockDocumentEmbedder, MockTextEmbedder
|
||||
|
||||
|
||||
def _ones(text: str) -> list[float]:
|
||||
"""Module-level embedding function used to test `embedding_fn` and its serialization."""
|
||||
return [1.0, 1.0, 1.0]
|
||||
|
||||
|
||||
class TestMockDocumentEmbedder:
|
||||
@pytest.mark.parametrize(
|
||||
("args", "kwargs", "match"),
|
||||
[
|
||||
(([0.1],), {"embedding_fn": _ones}, "either 'embedding' or 'embedding_fn'"),
|
||||
((), {"dimension": -1}, "must be a positive integer"),
|
||||
],
|
||||
)
|
||||
def test_init_rejects_invalid_config(self, args, kwargs, match):
|
||||
with pytest.raises(ValueError, match=match):
|
||||
MockDocumentEmbedder(*args, **kwargs)
|
||||
|
||||
def test_embeds_documents(self):
|
||||
embedder = MockDocumentEmbedder(dimension=16)
|
||||
result = embedder.run([Document(content="first"), Document(content="second")])
|
||||
embeddings = [doc.embedding for doc in result["documents"]]
|
||||
assert all(len(embedding) == 16 for embedding in embeddings)
|
||||
assert embeddings[0] != embeddings[1]
|
||||
|
||||
def test_consistent_with_text_embedder(self):
|
||||
# the same prepared text yields the same embedding from both mock embedders (shared deterministic algorithm)
|
||||
text_embedding = MockTextEmbedder(dimension=8).run("pizza")["embedding"]
|
||||
doc_embedding = MockDocumentEmbedder(dimension=8).run([Document(content="pizza")])["documents"][0].embedding
|
||||
assert text_embedding == doc_embedding
|
||||
|
||||
def test_fixed_embedding(self):
|
||||
result = MockDocumentEmbedder([0.5, 0.5]).run([Document(content="a"), Document(content="b")])
|
||||
assert all(doc.embedding == [0.5, 0.5] for doc in result["documents"])
|
||||
|
||||
def test_embedding_fn(self):
|
||||
result = MockDocumentEmbedder(embedding_fn=_ones).run([Document(content="a")])
|
||||
assert result["documents"][0].embedding == [1.0, 1.0, 1.0]
|
||||
|
||||
def test_meta_fields_to_embed_affect_embedding(self):
|
||||
document = Document(content="hello", meta={"title": "Greetings"})
|
||||
without_meta = MockDocumentEmbedder(dimension=8).run([document])["documents"][0].embedding
|
||||
with_meta = (
|
||||
MockDocumentEmbedder(dimension=8, meta_fields_to_embed=["title"]).run([document])["documents"][0].embedding
|
||||
)
|
||||
assert without_meta != with_meta
|
||||
|
||||
def test_preserves_document_fields(self):
|
||||
document = Document(content="hello", meta={"title": "Greetings"})
|
||||
embedded = MockDocumentEmbedder(dimension=8).run([document])["documents"][0]
|
||||
assert embedded.id == document.id
|
||||
assert embedded.content == "hello"
|
||||
assert embedded.meta == {"title": "Greetings"}
|
||||
assert embedded.embedding is not None
|
||||
# the original document is not mutated
|
||||
assert document.embedding is None
|
||||
|
||||
def test_empty_documents(self):
|
||||
result = MockDocumentEmbedder().run([])
|
||||
assert result["documents"] == []
|
||||
assert result["meta"]["usage"] == {"prompt_tokens": 0, "total_tokens": 0}
|
||||
|
||||
def test_meta(self):
|
||||
meta = MockDocumentEmbedder(dimension=4).run([Document(content="a b"), Document(content="c")])["meta"]
|
||||
assert meta["model"] == "mock-model"
|
||||
assert meta["usage"] == {"prompt_tokens": 3, "total_tokens": 3}
|
||||
|
||||
@pytest.mark.parametrize("documents", ["not a list", [1, 2, 3]])
|
||||
def test_run_rejects_non_documents(self, documents):
|
||||
with pytest.raises(TypeError, match="expects a list of Documents"):
|
||||
MockDocumentEmbedder().run(documents)
|
||||
|
||||
async def test_run_async(self):
|
||||
embedder = MockDocumentEmbedder(dimension=8)
|
||||
documents = [Document(content="hello")]
|
||||
async_embedding = (await embedder.run_async(documents))["documents"][0].embedding
|
||||
assert async_embedding == embedder.run(documents)["documents"][0].embedding
|
||||
|
||||
def test_warm_up_sets_flag_and_run_auto_warms(self):
|
||||
embedder = MockDocumentEmbedder(dimension=8)
|
||||
assert embedder._is_warmed_up is False
|
||||
embedder.warm_up()
|
||||
assert embedder._is_warmed_up is True
|
||||
# run auto-warms a fresh instance
|
||||
fresh = MockDocumentEmbedder(dimension=8)
|
||||
assert len(fresh.run([Document(content="hello")])["documents"][0].embedding) == 8
|
||||
assert fresh._is_warmed_up is True
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"embedder",
|
||||
[
|
||||
MockDocumentEmbedder(
|
||||
dimension=8, model="m", meta={"k": "v"}, meta_fields_to_embed=["title"], embedding_separator=" | "
|
||||
),
|
||||
MockDocumentEmbedder(embedding_fn=_ones),
|
||||
],
|
||||
ids=["deterministic", "embedding_fn"],
|
||||
)
|
||||
def test_serialization_roundtrip(self, embedder):
|
||||
restored = MockDocumentEmbedder.from_dict(embedder.to_dict())
|
||||
assert isinstance(restored, MockDocumentEmbedder)
|
||||
document = Document(content="hello", meta={"title": "t"})
|
||||
assert restored.run([document])["documents"][0].embedding == embedder.run([document])["documents"][0].embedding
|
||||
|
||||
def test_in_pipeline(self):
|
||||
pipeline = Pipeline()
|
||||
pipeline.add_component("embedder", MockDocumentEmbedder(dimension=8))
|
||||
restored = Pipeline.from_dict(pipeline.to_dict())
|
||||
result = restored.run({"embedder": {"documents": [Document(content="hello")]}})
|
||||
assert len(result["embedder"]["documents"][0].embedding) == 8
|
||||
@@ -0,0 +1,120 @@
|
||||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import math
|
||||
|
||||
import pytest
|
||||
|
||||
from haystack import Pipeline
|
||||
from haystack.components.embedders import MockTextEmbedder
|
||||
from haystack.components.embedders.mock_utils import _l2_normalize
|
||||
|
||||
|
||||
def _ones(text: str) -> list[float]:
|
||||
"""Module-level embedding function used to test `embedding_fn` and its serialization."""
|
||||
return [1.0, 1.0, 1.0]
|
||||
|
||||
|
||||
def test_l2_normalize_handles_zero_vector():
|
||||
# defensive guard in the shared deterministic-embedding helper: a zero vector is returned unchanged
|
||||
assert _l2_normalize([0.0, 0.0]) == [0.0, 0.0]
|
||||
|
||||
|
||||
class TestMockTextEmbedder:
|
||||
@pytest.mark.parametrize(
|
||||
("args", "kwargs", "exception", "match"),
|
||||
[
|
||||
(([0.1, 0.2],), {"embedding_fn": _ones}, ValueError, "either 'embedding' or 'embedding_fn'"),
|
||||
((), {"dimension": 0}, ValueError, "must be a positive integer"),
|
||||
(([],), {}, ValueError, "must not be empty"),
|
||||
((["not", "numbers"],), {}, TypeError, "must be a sequence of numbers"),
|
||||
],
|
||||
)
|
||||
def test_init_rejects_invalid_config(self, args, kwargs, exception, match):
|
||||
with pytest.raises(exception, match=match):
|
||||
MockTextEmbedder(*args, **kwargs)
|
||||
|
||||
def test_deterministic_embedding(self):
|
||||
embedding = MockTextEmbedder(dimension=16).run("hello")["embedding"]
|
||||
assert len(embedding) == 16
|
||||
assert all(isinstance(value, float) for value in embedding)
|
||||
# embeddings are L2-normalized, like real ones
|
||||
assert math.isclose(math.sqrt(sum(value * value for value in embedding)), 1.0, abs_tol=1e-9)
|
||||
|
||||
def test_deterministic_distinguishes_texts(self):
|
||||
embedder = MockTextEmbedder(dimension=8)
|
||||
assert embedder.run("pizza")["embedding"] == embedder.run("pizza")["embedding"]
|
||||
assert embedder.run("pizza")["embedding"] != embedder.run("pasta")["embedding"]
|
||||
# determinism holds across instances and processes (stable hash, not the salted built-in hash)
|
||||
assert (
|
||||
MockTextEmbedder(dimension=8).run("x")["embedding"] == MockTextEmbedder(dimension=8).run("x")["embedding"]
|
||||
)
|
||||
|
||||
def test_fixed_embedding(self):
|
||||
embedder = MockTextEmbedder([0.1, 0.2, 0.3])
|
||||
assert embedder.run("anything")["embedding"] == [0.1, 0.2, 0.3]
|
||||
assert embedder.run("something else")["embedding"] == [0.1, 0.2, 0.3]
|
||||
|
||||
def test_embedding_fn(self):
|
||||
assert MockTextEmbedder(embedding_fn=_ones).run("hello")["embedding"] == [1.0, 1.0, 1.0]
|
||||
|
||||
def test_embedding_fn_invalid_return_raises(self):
|
||||
# embedding_fn deliberately returns a non-vector to exercise the runtime type check
|
||||
embedder = MockTextEmbedder(embedding_fn=lambda text: "not a vector") # type: ignore[arg-type, return-value]
|
||||
with pytest.raises(TypeError, match="must be a sequence of numbers"):
|
||||
embedder.run("hello")
|
||||
|
||||
def test_prefix_suffix_affect_embedding(self):
|
||||
plain = MockTextEmbedder(dimension=8).run("hello")["embedding"]
|
||||
prefixed = MockTextEmbedder(dimension=8, prefix="search: ").run("hello")["embedding"]
|
||||
assert plain != prefixed
|
||||
|
||||
def test_meta(self):
|
||||
meta = MockTextEmbedder(dimension=4).run("a b c")["meta"]
|
||||
assert meta["model"] == "mock-model"
|
||||
assert meta["usage"] == {"prompt_tokens": 3, "total_tokens": 3}
|
||||
# init model and meta are reflected and merged
|
||||
custom = MockTextEmbedder(dimension=4, model="custom", meta={"extra": "value"}).run("hi")["meta"]
|
||||
assert custom["model"] == "custom"
|
||||
assert custom["extra"] == "value"
|
||||
|
||||
def test_run_rejects_non_string(self):
|
||||
# a non-string input is passed on purpose to exercise the runtime type check
|
||||
with pytest.raises(TypeError, match="expects a string"):
|
||||
MockTextEmbedder().run(["not", "a", "string"]) # type: ignore[arg-type]
|
||||
|
||||
async def test_run_async(self):
|
||||
embedder = MockTextEmbedder(dimension=8)
|
||||
assert (await embedder.run_async("hello"))["embedding"] == embedder.run("hello")["embedding"]
|
||||
|
||||
def test_warm_up_sets_flag_and_run_auto_warms(self):
|
||||
embedder = MockTextEmbedder(dimension=8)
|
||||
assert embedder._is_warmed_up is False
|
||||
embedder.warm_up()
|
||||
assert embedder._is_warmed_up is True
|
||||
# run auto-warms a fresh instance
|
||||
fresh = MockTextEmbedder(dimension=8)
|
||||
assert len(fresh.run("hello")["embedding"]) == 8
|
||||
assert fresh._is_warmed_up is True
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"embedder",
|
||||
[
|
||||
MockTextEmbedder(dimension=8, model="m", meta={"k": "v"}, prefix="p", suffix="s"),
|
||||
MockTextEmbedder(embedding_fn=_ones),
|
||||
MockTextEmbedder([0.1, 0.2, 0.3]),
|
||||
],
|
||||
ids=["deterministic", "embedding_fn", "fixed"],
|
||||
)
|
||||
def test_serialization_roundtrip(self, embedder):
|
||||
restored = MockTextEmbedder.from_dict(embedder.to_dict())
|
||||
assert isinstance(restored, MockTextEmbedder)
|
||||
assert restored.run("hello")["embedding"] == embedder.run("hello")["embedding"]
|
||||
|
||||
def test_in_pipeline(self):
|
||||
pipeline = Pipeline()
|
||||
pipeline.add_component("embedder", MockTextEmbedder(dimension=8))
|
||||
restored = Pipeline.from_dict(pipeline.to_dict())
|
||||
result = restored.run({"embedder": {"text": "hello"}})
|
||||
assert len(result["embedder"]["embedding"]) == 8
|
||||
@@ -0,0 +1,431 @@
|
||||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import contextlib
|
||||
import os
|
||||
from unittest.mock import AsyncMock, MagicMock, Mock, patch
|
||||
|
||||
import pytest
|
||||
from openai import APIError
|
||||
|
||||
import haystack.components.embedders.openai_document_embedder as openai_document_embedder_module
|
||||
from haystack import Document
|
||||
from haystack.components.embedders.openai_document_embedder import OpenAIDocumentEmbedder
|
||||
from haystack.utils.auth import Secret
|
||||
|
||||
|
||||
class TestOpenAIDocumentEmbedder:
|
||||
def test_init_default(self, monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_API_KEY", "fake-api-key")
|
||||
embedder = OpenAIDocumentEmbedder()
|
||||
assert embedder.api_key.resolve_value() == "fake-api-key"
|
||||
assert embedder.model == "text-embedding-ada-002"
|
||||
assert embedder.organization is None
|
||||
assert embedder.prefix == ""
|
||||
assert embedder.suffix == ""
|
||||
assert embedder.batch_size == 32
|
||||
assert embedder.progress_bar is True
|
||||
assert embedder.meta_fields_to_embed == []
|
||||
assert embedder.embedding_separator == "\n"
|
||||
assert embedder.timeout is None
|
||||
assert embedder.max_retries is None
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is None
|
||||
|
||||
def test_init_with_parameters(self, monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_TIMEOUT", "100")
|
||||
monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
|
||||
embedder = OpenAIDocumentEmbedder(
|
||||
api_key=Secret.from_token("fake-api-key-2"),
|
||||
model="model",
|
||||
organization="my-org",
|
||||
prefix="prefix",
|
||||
suffix="suffix",
|
||||
batch_size=64,
|
||||
progress_bar=False,
|
||||
meta_fields_to_embed=["test_field"],
|
||||
embedding_separator=" | ",
|
||||
timeout=40.0,
|
||||
max_retries=1,
|
||||
)
|
||||
assert embedder.api_key.resolve_value() == "fake-api-key-2"
|
||||
assert embedder.organization == "my-org"
|
||||
assert embedder.model == "model"
|
||||
assert embedder.prefix == "prefix"
|
||||
assert embedder.suffix == "suffix"
|
||||
assert embedder.batch_size == 64
|
||||
assert embedder.progress_bar is False
|
||||
assert embedder.meta_fields_to_embed == ["test_field"]
|
||||
assert embedder.embedding_separator == " | "
|
||||
assert embedder.timeout == 40.0
|
||||
assert embedder.max_retries == 1
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is None
|
||||
|
||||
def test_init_with_parameters_and_env_vars(self, monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_TIMEOUT", "100")
|
||||
monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
|
||||
embedder = OpenAIDocumentEmbedder(
|
||||
api_key=Secret.from_token("fake-api-key-2"),
|
||||
model="model",
|
||||
organization="my-org",
|
||||
prefix="prefix",
|
||||
suffix="suffix",
|
||||
batch_size=64,
|
||||
progress_bar=False,
|
||||
meta_fields_to_embed=["test_field"],
|
||||
embedding_separator=" | ",
|
||||
)
|
||||
assert embedder.api_key.resolve_value() == "fake-api-key-2"
|
||||
assert embedder.organization == "my-org"
|
||||
assert embedder.model == "model"
|
||||
assert embedder.prefix == "prefix"
|
||||
assert embedder.suffix == "suffix"
|
||||
assert embedder.batch_size == 64
|
||||
assert embedder.progress_bar is False
|
||||
assert embedder.meta_fields_to_embed == ["test_field"]
|
||||
assert embedder.embedding_separator == " | "
|
||||
assert embedder.timeout is None
|
||||
assert embedder.max_retries is None
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is None
|
||||
|
||||
def test_to_dict(self, monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_API_KEY", "fake-api-key")
|
||||
component = OpenAIDocumentEmbedder()
|
||||
data = component.to_dict()
|
||||
assert data == {
|
||||
"type": "haystack.components.embedders.openai_document_embedder.OpenAIDocumentEmbedder",
|
||||
"init_parameters": {
|
||||
"api_key": {"env_vars": ["OPENAI_API_KEY"], "strict": True, "type": "env_var"},
|
||||
"api_base_url": None,
|
||||
"model": "text-embedding-ada-002",
|
||||
"dimensions": None,
|
||||
"organization": None,
|
||||
"http_client_kwargs": None,
|
||||
"prefix": "",
|
||||
"suffix": "",
|
||||
"batch_size": 32,
|
||||
"progress_bar": True,
|
||||
"meta_fields_to_embed": [],
|
||||
"embedding_separator": "\n",
|
||||
"timeout": None,
|
||||
"max_retries": None,
|
||||
"raise_on_failure": False,
|
||||
},
|
||||
}
|
||||
|
||||
def test_to_dict_with_custom_init_parameters(self, monkeypatch):
|
||||
monkeypatch.setenv("ENV_VAR", "fake-api-key")
|
||||
component = OpenAIDocumentEmbedder(
|
||||
api_key=Secret.from_env_var("ENV_VAR", strict=False),
|
||||
model="model",
|
||||
organization="my-org",
|
||||
http_client_kwargs={"proxy": "http://localhost:8080"},
|
||||
prefix="prefix",
|
||||
suffix="suffix",
|
||||
batch_size=64,
|
||||
progress_bar=False,
|
||||
meta_fields_to_embed=["test_field"],
|
||||
embedding_separator=" | ",
|
||||
timeout=10.0,
|
||||
max_retries=2,
|
||||
raise_on_failure=True,
|
||||
)
|
||||
data = component.to_dict()
|
||||
assert data == {
|
||||
"type": "haystack.components.embedders.openai_document_embedder.OpenAIDocumentEmbedder",
|
||||
"init_parameters": {
|
||||
"api_key": {"env_vars": ["ENV_VAR"], "strict": False, "type": "env_var"},
|
||||
"api_base_url": None,
|
||||
"model": "model",
|
||||
"dimensions": None,
|
||||
"organization": "my-org",
|
||||
"http_client_kwargs": {"proxy": "http://localhost:8080"},
|
||||
"prefix": "prefix",
|
||||
"suffix": "suffix",
|
||||
"batch_size": 64,
|
||||
"progress_bar": False,
|
||||
"meta_fields_to_embed": ["test_field"],
|
||||
"embedding_separator": " | ",
|
||||
"timeout": 10.0,
|
||||
"max_retries": 2,
|
||||
"raise_on_failure": True,
|
||||
},
|
||||
}
|
||||
|
||||
def test_prepare_texts_to_embed_w_metadata(self):
|
||||
documents = [
|
||||
Document(id=f"{i}", content=f"document number {i}:\ncontent", meta={"meta_field": f"meta_value {i}"})
|
||||
for i in range(5)
|
||||
]
|
||||
|
||||
embedder = OpenAIDocumentEmbedder(
|
||||
api_key=Secret.from_token("fake-api-key"), meta_fields_to_embed=["meta_field"], embedding_separator=" | "
|
||||
)
|
||||
|
||||
prepared_texts = embedder._prepare_texts_to_embed(documents)
|
||||
|
||||
assert prepared_texts == {
|
||||
"0": "meta_value 0 | document number 0:\ncontent",
|
||||
"1": "meta_value 1 | document number 1:\ncontent",
|
||||
"2": "meta_value 2 | document number 2:\ncontent",
|
||||
"3": "meta_value 3 | document number 3:\ncontent",
|
||||
"4": "meta_value 4 | document number 4:\ncontent",
|
||||
}
|
||||
|
||||
def test_prepare_texts_to_embed_w_suffix(self):
|
||||
documents = [Document(id=f"{i}", content=f"document number {i}") for i in range(5)]
|
||||
|
||||
embedder = OpenAIDocumentEmbedder(
|
||||
api_key=Secret.from_token("fake-api-key"), prefix="my_prefix ", suffix=" my_suffix"
|
||||
)
|
||||
|
||||
prepared_texts = embedder._prepare_texts_to_embed(documents)
|
||||
|
||||
assert prepared_texts == {
|
||||
"0": "my_prefix document number 0 my_suffix",
|
||||
"1": "my_prefix document number 1 my_suffix",
|
||||
"2": "my_prefix document number 2 my_suffix",
|
||||
"3": "my_prefix document number 3 my_suffix",
|
||||
"4": "my_prefix document number 4 my_suffix",
|
||||
}
|
||||
|
||||
def test_run_wrong_input_format(self):
|
||||
embedder = OpenAIDocumentEmbedder(api_key=Secret.from_token("fake-api-key"))
|
||||
|
||||
# wrong formats
|
||||
string_input = "text"
|
||||
list_integers_input = [1, 2, 3]
|
||||
|
||||
with pytest.raises(TypeError, match="OpenAIDocumentEmbedder expects a list of Documents as input"):
|
||||
embedder.run(documents=string_input) # type: ignore[arg-type]
|
||||
|
||||
with pytest.raises(TypeError, match="OpenAIDocumentEmbedder expects a list of Documents as input"):
|
||||
embedder.run(documents=list_integers_input) # type: ignore[arg-type]
|
||||
|
||||
def test_run_on_empty_list(self):
|
||||
embedder = OpenAIDocumentEmbedder(api_key=Secret.from_token("fake-api-key"))
|
||||
|
||||
empty_list_input: list[Document] = []
|
||||
result = embedder.run(documents=empty_list_input)
|
||||
|
||||
assert result["documents"] is not None
|
||||
assert not result["documents"] # empty list
|
||||
|
||||
def test_embed_batch_handles_exceptions_gracefully(self, caplog):
|
||||
embedder = OpenAIDocumentEmbedder(api_key=Secret.from_token("fake_api_key"))
|
||||
embedder.warm_up()
|
||||
assert embedder.client is not None
|
||||
fake_texts_to_embed = {"1": "text1", "2": "text2"}
|
||||
with patch.object(
|
||||
embedder.client.embeddings,
|
||||
"create",
|
||||
side_effect=APIError(message="Mocked error", request=Mock(), body=None),
|
||||
):
|
||||
embedder._embed_batch(texts_to_embed=fake_texts_to_embed, batch_size=2)
|
||||
|
||||
assert len(caplog.records) == 1
|
||||
assert "Failed embedding of documents 1, 2 caused by Mocked error" in caplog.records[0].msg
|
||||
|
||||
def test_run_handles_exceptions_gracefully(self, caplog):
|
||||
embedder = OpenAIDocumentEmbedder(api_key=Secret.from_token("fake_api_key"), batch_size=1)
|
||||
embedder.warm_up()
|
||||
assert embedder.client is not None
|
||||
docs = [
|
||||
Document(content="I love cheese", meta={"topic": "Cuisine"}),
|
||||
Document(content="A transformer is a deep learning architecture", meta={"topic": "ML"}),
|
||||
]
|
||||
|
||||
# Create a successful response for the second call
|
||||
successful_response = Mock()
|
||||
successful_response.data = [
|
||||
Mock(embedding=[0.4, 0.5, 0.6]) # Mock embedding for second doc
|
||||
]
|
||||
successful_response.model = "text-embedding-ada-002"
|
||||
successful_response.usage = {"prompt_tokens": 10, "total_tokens": 10}
|
||||
|
||||
with patch.object(
|
||||
embedder.client.embeddings,
|
||||
"create",
|
||||
side_effect=[
|
||||
APIError(message="Mocked error", request=Mock(), body=None), # First call fails
|
||||
successful_response, # Second call succeeds
|
||||
],
|
||||
):
|
||||
result = embedder.run(documents=docs)
|
||||
assert len(result["documents"]) == 2
|
||||
assert result["documents"][0].embedding is None
|
||||
assert result["documents"][1].embedding == [0.4, 0.5, 0.6]
|
||||
|
||||
def test_embed_batch_raises_exception_on_failure(self):
|
||||
embedder = OpenAIDocumentEmbedder(api_key=Secret.from_token("fake_api_key"), raise_on_failure=True)
|
||||
embedder.warm_up()
|
||||
assert embedder.client is not None
|
||||
fake_texts_to_embed = {"1": "text1", "2": "text2"}
|
||||
with patch.object(
|
||||
embedder.client.embeddings,
|
||||
"create",
|
||||
side_effect=APIError(message="Mocked error", request=Mock(), body=None),
|
||||
):
|
||||
with pytest.raises(APIError, match="Mocked error"):
|
||||
embedder._embed_batch(texts_to_embed=fake_texts_to_embed, batch_size=2)
|
||||
|
||||
@pytest.mark.skipif(os.environ.get("OPENAI_API_KEY", "") == "", reason="OPENAI_API_KEY is not set")
|
||||
@pytest.mark.integration
|
||||
def test_run(self):
|
||||
docs = [
|
||||
Document(content="I love cheese", meta={"topic": "Cuisine"}),
|
||||
Document(content="A transformer is a deep learning architecture", meta={"topic": "ML"}),
|
||||
]
|
||||
|
||||
model = "text-embedding-ada-002"
|
||||
|
||||
embedder = OpenAIDocumentEmbedder(model=model, meta_fields_to_embed=["topic"], embedding_separator=" | ")
|
||||
|
||||
result = embedder.run(documents=docs)
|
||||
assert embedder.client is not None
|
||||
documents_with_embeddings = result["documents"]
|
||||
|
||||
assert isinstance(documents_with_embeddings, list)
|
||||
assert len(documents_with_embeddings) == len(docs)
|
||||
for doc, new_doc in zip(docs, documents_with_embeddings, strict=True):
|
||||
assert doc.embedding is None
|
||||
assert new_doc is not doc
|
||||
assert isinstance(new_doc, Document)
|
||||
assert isinstance(new_doc.embedding, list)
|
||||
assert len(new_doc.embedding) == 1536
|
||||
assert all(isinstance(x, float) for x in new_doc.embedding)
|
||||
|
||||
assert "text" in result["meta"]["model"] and "ada" in result["meta"]["model"], (
|
||||
"The model name does not contain 'text' and 'ada'"
|
||||
)
|
||||
|
||||
assert result["meta"]["usage"] == {"prompt_tokens": 15, "total_tokens": 15}, "Usage information does not match"
|
||||
|
||||
@pytest.mark.skipif(os.environ.get("OPENAI_API_KEY", "") == "", reason="OPENAI_API_KEY is not set")
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_async(self):
|
||||
embedder = OpenAIDocumentEmbedder(
|
||||
model="text-embedding-ada-002", meta_fields_to_embed=["topic"], embedding_separator=" | "
|
||||
)
|
||||
docs = [
|
||||
Document(content="I love cheese", meta={"topic": "Cuisine"}),
|
||||
Document(content="A transformer is a deep learning architecture", meta={"topic": "ML"}),
|
||||
]
|
||||
|
||||
result = await embedder.run_async(documents=docs)
|
||||
assert embedder.async_client is not None
|
||||
documents_with_embeddings = result["documents"]
|
||||
|
||||
assert isinstance(documents_with_embeddings, list)
|
||||
assert len(documents_with_embeddings) == len(docs)
|
||||
for doc, new_doc in zip(docs, documents_with_embeddings, strict=True):
|
||||
assert doc.embedding is None
|
||||
assert new_doc is not doc
|
||||
assert isinstance(new_doc, Document)
|
||||
assert isinstance(new_doc.embedding, list)
|
||||
assert len(new_doc.embedding) == 1536
|
||||
assert all(isinstance(x, float) for x in new_doc.embedding)
|
||||
|
||||
assert "text" in result["meta"]["model"] and "ada" in result["meta"]["model"], (
|
||||
"The model name does not contain 'text' and 'ada'"
|
||||
)
|
||||
|
||||
assert result["meta"]["usage"] == {"prompt_tokens": 15, "total_tokens": 15}, "Usage information does not match"
|
||||
|
||||
# Close async client; suppress RuntimeError if the event loop is already closed
|
||||
with contextlib.suppress(RuntimeError):
|
||||
await embedder.close_async()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_openai_clients(monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_API_KEY", "fake")
|
||||
sync_cls = MagicMock(name="OpenAI")
|
||||
async_cls = MagicMock(name="AsyncOpenAI")
|
||||
async_cls.return_value.close = AsyncMock()
|
||||
monkeypatch.setattr(openai_document_embedder_module, "OpenAI", sync_cls)
|
||||
monkeypatch.setattr(openai_document_embedder_module, "AsyncOpenAI", async_cls)
|
||||
return sync_cls, async_cls
|
||||
|
||||
|
||||
class TestComponentLifecycle:
|
||||
def test_warm_up_uses_default_timeout_and_max_retries(self, monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_API_KEY", "fake-api-key")
|
||||
embedder = OpenAIDocumentEmbedder()
|
||||
embedder.warm_up()
|
||||
assert embedder.client is not None
|
||||
assert embedder.client.max_retries == 5
|
||||
assert embedder.client.timeout == 30.0
|
||||
|
||||
def test_warm_up_uses_timeout_and_max_retries_from_parameters(self):
|
||||
embedder = OpenAIDocumentEmbedder(api_key=Secret.from_token("fake-api-key"), timeout=40.0, max_retries=1)
|
||||
embedder.warm_up()
|
||||
assert embedder.client is not None
|
||||
assert embedder.client.max_retries == 1
|
||||
assert embedder.client.timeout == 40.0
|
||||
|
||||
def test_warm_up_uses_timeout_and_max_retries_from_env_vars(self, monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_TIMEOUT", "100")
|
||||
monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
|
||||
embedder = OpenAIDocumentEmbedder(api_key=Secret.from_token("fake-api-key"))
|
||||
embedder.warm_up()
|
||||
assert embedder.client is not None
|
||||
assert embedder.client.max_retries == 10
|
||||
assert embedder.client.timeout == 100.0
|
||||
|
||||
def test_key_resolved_at_warm_up_not_init(self, monkeypatch):
|
||||
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
|
||||
embedder = OpenAIDocumentEmbedder()
|
||||
with pytest.raises(ValueError, match="None of the .* environment variables are set"):
|
||||
embedder.warm_up()
|
||||
|
||||
def test_sync_lifecycle(self, mock_openai_clients):
|
||||
sync_cls, _ = mock_openai_clients
|
||||
sync_client = sync_cls.return_value
|
||||
embedder = OpenAIDocumentEmbedder()
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is None
|
||||
|
||||
embedder.warm_up()
|
||||
assert embedder.client is sync_cls.return_value
|
||||
assert embedder.async_client is None
|
||||
|
||||
embedder.close()
|
||||
sync_client.close.assert_called_once()
|
||||
assert embedder.client is None
|
||||
|
||||
async def test_async_lifecycle(self, mock_openai_clients):
|
||||
_, async_cls = mock_openai_clients
|
||||
async_client = async_cls.return_value
|
||||
embedder = OpenAIDocumentEmbedder()
|
||||
|
||||
await embedder.warm_up_async()
|
||||
assert embedder.async_client is async_cls.return_value
|
||||
assert embedder.client is None
|
||||
|
||||
await embedder.close_async()
|
||||
async_client.close.assert_awaited_once()
|
||||
assert embedder.async_client is None
|
||||
|
||||
async def test_close_is_safe_without_warm_up(self, mock_openai_clients):
|
||||
embedder = OpenAIDocumentEmbedder()
|
||||
embedder.close()
|
||||
await embedder.close_async()
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is None
|
||||
|
||||
async def test_close_and_close_async_are_independent(self, mock_openai_clients):
|
||||
embedder = OpenAIDocumentEmbedder()
|
||||
embedder.warm_up()
|
||||
await embedder.warm_up_async()
|
||||
|
||||
embedder.close()
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is not None
|
||||
|
||||
await embedder.close_async()
|
||||
assert embedder.async_client is None
|
||||
@@ -0,0 +1,316 @@
|
||||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import contextlib
|
||||
import os
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
from openai.types import CreateEmbeddingResponse, Embedding
|
||||
from openai.types.create_embedding_response import Usage
|
||||
|
||||
import haystack.components.embedders.openai_text_embedder as openai_text_embedder_module
|
||||
from haystack.components.embedders.openai_text_embedder import OpenAITextEmbedder
|
||||
from haystack.utils.auth import Secret
|
||||
|
||||
|
||||
class TestOpenAITextEmbedder:
|
||||
def test_init_default(self, monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_API_KEY", "fake-api-key")
|
||||
embedder = OpenAITextEmbedder()
|
||||
|
||||
assert embedder.api_key.resolve_value() == "fake-api-key"
|
||||
assert embedder.model == "text-embedding-ada-002"
|
||||
assert embedder.api_base_url is None
|
||||
assert embedder.organization is None
|
||||
assert embedder.prefix == ""
|
||||
assert embedder.suffix == ""
|
||||
assert embedder.timeout is None
|
||||
assert embedder.max_retries is None
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is None
|
||||
|
||||
def test_init_with_parameters(self, monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_TIMEOUT", "100")
|
||||
monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
|
||||
embedder = OpenAITextEmbedder(
|
||||
api_key=Secret.from_token("fake-api-key"),
|
||||
model="model",
|
||||
api_base_url="https://my-custom-base-url.com",
|
||||
organization="fake-organization",
|
||||
prefix="prefix",
|
||||
suffix="suffix",
|
||||
timeout=40.0,
|
||||
max_retries=1,
|
||||
)
|
||||
assert embedder.api_key.resolve_value() == "fake-api-key"
|
||||
assert embedder.model == "model"
|
||||
assert embedder.api_base_url == "https://my-custom-base-url.com"
|
||||
assert embedder.organization == "fake-organization"
|
||||
assert embedder.prefix == "prefix"
|
||||
assert embedder.suffix == "suffix"
|
||||
assert embedder.timeout == 40.0
|
||||
assert embedder.max_retries == 1
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is None
|
||||
|
||||
def test_init_with_parameters_and_env_vars(self, monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_TIMEOUT", "100")
|
||||
monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
|
||||
embedder = OpenAITextEmbedder(
|
||||
api_key=Secret.from_token("fake-api-key"),
|
||||
model="model",
|
||||
api_base_url="https://my-custom-base-url.com",
|
||||
organization="fake-organization",
|
||||
prefix="prefix",
|
||||
suffix="suffix",
|
||||
)
|
||||
assert embedder.api_key.resolve_value() == "fake-api-key"
|
||||
assert embedder.model == "model"
|
||||
assert embedder.api_base_url == "https://my-custom-base-url.com"
|
||||
assert embedder.organization == "fake-organization"
|
||||
assert embedder.prefix == "prefix"
|
||||
assert embedder.suffix == "suffix"
|
||||
assert embedder.timeout is None
|
||||
assert embedder.max_retries is None
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is None
|
||||
|
||||
def test_to_dict(self, monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_API_KEY", "fake-api-key")
|
||||
component = OpenAITextEmbedder()
|
||||
data = component.to_dict()
|
||||
assert data == {
|
||||
"type": "haystack.components.embedders.openai_text_embedder.OpenAITextEmbedder",
|
||||
"init_parameters": {
|
||||
"api_key": {"env_vars": ["OPENAI_API_KEY"], "strict": True, "type": "env_var"},
|
||||
"api_base_url": None,
|
||||
"dimensions": None,
|
||||
"model": "text-embedding-ada-002",
|
||||
"organization": None,
|
||||
"http_client_kwargs": None,
|
||||
"prefix": "",
|
||||
"suffix": "",
|
||||
"timeout": None,
|
||||
"max_retries": None,
|
||||
},
|
||||
}
|
||||
|
||||
def test_to_dict_with_custom_init_parameters(self, monkeypatch):
|
||||
monkeypatch.setenv("ENV_VAR", "fake-api-key")
|
||||
component = OpenAITextEmbedder(
|
||||
api_key=Secret.from_env_var("ENV_VAR", strict=False),
|
||||
model="model",
|
||||
api_base_url="https://my-custom-base-url.com",
|
||||
organization="fake-organization",
|
||||
prefix="prefix",
|
||||
suffix="suffix",
|
||||
timeout=10.0,
|
||||
max_retries=2,
|
||||
http_client_kwargs={"proxy": "http://localhost:8080"},
|
||||
)
|
||||
data = component.to_dict()
|
||||
assert data == {
|
||||
"type": "haystack.components.embedders.openai_text_embedder.OpenAITextEmbedder",
|
||||
"init_parameters": {
|
||||
"api_key": {"env_vars": ["ENV_VAR"], "strict": False, "type": "env_var"},
|
||||
"api_base_url": "https://my-custom-base-url.com",
|
||||
"model": "model",
|
||||
"dimensions": None,
|
||||
"organization": "fake-organization",
|
||||
"http_client_kwargs": {"proxy": "http://localhost:8080"},
|
||||
"prefix": "prefix",
|
||||
"suffix": "suffix",
|
||||
"timeout": 10.0,
|
||||
"max_retries": 2,
|
||||
},
|
||||
}
|
||||
|
||||
def test_from_dict(self, monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_API_KEY", "fake-api-key")
|
||||
data = {
|
||||
"type": "haystack.components.embedders.openai_text_embedder.OpenAITextEmbedder",
|
||||
"init_parameters": {
|
||||
"api_key": {"env_vars": ["OPENAI_API_KEY"], "strict": True, "type": "env_var"},
|
||||
"model": "text-embedding-ada-002",
|
||||
"api_base_url": "https://my-custom-base-url.com",
|
||||
"organization": "fake-organization",
|
||||
"http_client_kwargs": None,
|
||||
"prefix": "prefix",
|
||||
"suffix": "suffix",
|
||||
},
|
||||
}
|
||||
component = OpenAITextEmbedder.from_dict(data)
|
||||
assert component.api_key.resolve_value() == "fake-api-key"
|
||||
assert component.model == "text-embedding-ada-002"
|
||||
assert component.api_base_url == "https://my-custom-base-url.com"
|
||||
assert component.organization == "fake-organization"
|
||||
assert component.http_client_kwargs is None
|
||||
assert component.prefix == "prefix"
|
||||
assert component.suffix == "suffix"
|
||||
|
||||
def test_prepare_input(self, monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_API_KEY", "fake-api-key")
|
||||
embedder = OpenAITextEmbedder(dimensions=1536)
|
||||
|
||||
inp = "The food was delicious"
|
||||
prepared_input = embedder._prepare_input(inp)
|
||||
assert prepared_input == {
|
||||
"model": "text-embedding-ada-002",
|
||||
"input": "The food was delicious",
|
||||
"encoding_format": "float",
|
||||
"dimensions": 1536,
|
||||
}
|
||||
|
||||
def test_prepare_output(self, monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_API_KEY", "fake-api-key")
|
||||
|
||||
response = CreateEmbeddingResponse(
|
||||
data=[Embedding(embedding=[0.1, 0.2, 0.3], index=0, object="embedding")],
|
||||
model="text-embedding-ada-002",
|
||||
object="list",
|
||||
usage=Usage(prompt_tokens=6, total_tokens=6),
|
||||
)
|
||||
|
||||
embedder = OpenAITextEmbedder()
|
||||
result = embedder._prepare_output(result=response)
|
||||
assert result == {
|
||||
"embedding": [0.1, 0.2, 0.3],
|
||||
"meta": {"model": "text-embedding-ada-002", "usage": {"prompt_tokens": 6, "total_tokens": 6}},
|
||||
}
|
||||
|
||||
def test_run_wrong_input_format(self):
|
||||
embedder = OpenAITextEmbedder(api_key=Secret.from_token("fake-api-key"))
|
||||
|
||||
list_integers_input = [1, 2, 3]
|
||||
|
||||
with pytest.raises(TypeError, match="OpenAITextEmbedder expects a string as an input"):
|
||||
embedder.run(text=list_integers_input) # type: ignore[arg-type]
|
||||
|
||||
@pytest.mark.skipif(os.environ.get("OPENAI_API_KEY", "") == "", reason="OPENAI_API_KEY is not set")
|
||||
@pytest.mark.integration
|
||||
def test_run(self):
|
||||
model = "text-embedding-ada-002"
|
||||
|
||||
embedder = OpenAITextEmbedder(model=model, prefix="prefix ", suffix=" suffix")
|
||||
result = embedder.run(text="The food was delicious")
|
||||
|
||||
assert len(result["embedding"]) == 1536
|
||||
assert all(isinstance(x, float) for x in result["embedding"])
|
||||
|
||||
assert "text" in result["meta"]["model"] and "ada" in result["meta"]["model"], (
|
||||
"The model name does not contain 'text' and 'ada'"
|
||||
)
|
||||
|
||||
assert result["meta"]["usage"] == {"prompt_tokens": 6, "total_tokens": 6}, "Usage information does not match"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.skipif(os.environ.get("OPENAI_API_KEY", "") == "", reason="OPENAI_API_KEY is not set")
|
||||
@pytest.mark.integration
|
||||
async def test_run_async(self):
|
||||
embedder = OpenAITextEmbedder(model="text-embedding-ada-002", prefix="prefix ", suffix=" suffix")
|
||||
result = await embedder.run_async(text="The food was delicious")
|
||||
|
||||
assert len(result["embedding"]) == 1536
|
||||
assert all(isinstance(x, float) for x in result["embedding"])
|
||||
|
||||
assert "text" in result["meta"]["model"] and "ada" in result["meta"]["model"], (
|
||||
"The model name does not contain 'text' and 'ada'"
|
||||
)
|
||||
|
||||
assert result["meta"]["usage"] == {"prompt_tokens": 6, "total_tokens": 6}, "Usage information does not match"
|
||||
|
||||
# Close async client; suppress RuntimeError if the event loop is already closed
|
||||
with contextlib.suppress(RuntimeError):
|
||||
await embedder.close_async()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_openai_clients(monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_API_KEY", "fake")
|
||||
sync_cls = MagicMock(name="OpenAI")
|
||||
async_cls = MagicMock(name="AsyncOpenAI")
|
||||
async_cls.return_value.close = AsyncMock()
|
||||
monkeypatch.setattr(openai_text_embedder_module, "OpenAI", sync_cls)
|
||||
monkeypatch.setattr(openai_text_embedder_module, "AsyncOpenAI", async_cls)
|
||||
return sync_cls, async_cls
|
||||
|
||||
|
||||
class TestComponentLifecycle:
|
||||
def test_warm_up_uses_default_timeout_and_max_retries(self, monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_API_KEY", "fake-api-key")
|
||||
embedder = OpenAITextEmbedder()
|
||||
embedder.warm_up()
|
||||
assert embedder.client is not None
|
||||
assert embedder.client.max_retries == 5
|
||||
assert embedder.client.timeout == 30.0
|
||||
|
||||
def test_warm_up_uses_timeout_and_max_retries_from_parameters(self):
|
||||
embedder = OpenAITextEmbedder(api_key=Secret.from_token("fake-api-key"), timeout=40.0, max_retries=1)
|
||||
embedder.warm_up()
|
||||
assert embedder.client is not None
|
||||
assert embedder.client.max_retries == 1
|
||||
assert embedder.client.timeout == 40.0
|
||||
|
||||
def test_warm_up_uses_timeout_and_max_retries_from_env_vars(self, monkeypatch):
|
||||
monkeypatch.setenv("OPENAI_TIMEOUT", "100")
|
||||
monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
|
||||
embedder = OpenAITextEmbedder(api_key=Secret.from_token("fake-api-key"))
|
||||
embedder.warm_up()
|
||||
assert embedder.client is not None
|
||||
assert embedder.client.max_retries == 10
|
||||
assert embedder.client.timeout == 100.0
|
||||
|
||||
def test_key_resolved_at_warm_up_not_init(self, monkeypatch):
|
||||
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
|
||||
embedder = OpenAITextEmbedder()
|
||||
with pytest.raises(ValueError, match="None of the .* environment variables are set"):
|
||||
embedder.warm_up()
|
||||
|
||||
def test_sync_lifecycle(self, mock_openai_clients):
|
||||
sync_cls, _ = mock_openai_clients
|
||||
sync_client = sync_cls.return_value
|
||||
embedder = OpenAITextEmbedder()
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is None
|
||||
|
||||
embedder.warm_up()
|
||||
assert embedder.client is sync_cls.return_value
|
||||
assert embedder.async_client is None
|
||||
|
||||
embedder.close()
|
||||
sync_client.close.assert_called_once()
|
||||
assert embedder.client is None
|
||||
|
||||
async def test_async_lifecycle(self, mock_openai_clients):
|
||||
_, async_cls = mock_openai_clients
|
||||
async_client = async_cls.return_value
|
||||
embedder = OpenAITextEmbedder()
|
||||
|
||||
await embedder.warm_up_async()
|
||||
assert embedder.async_client is async_cls.return_value
|
||||
assert embedder.client is None
|
||||
|
||||
await embedder.close_async()
|
||||
async_client.close.assert_awaited_once()
|
||||
assert embedder.async_client is None
|
||||
|
||||
async def test_close_is_safe_without_warm_up(self, mock_openai_clients):
|
||||
embedder = OpenAITextEmbedder()
|
||||
embedder.close()
|
||||
await embedder.close_async()
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is None
|
||||
|
||||
async def test_close_and_close_async_are_independent(self, mock_openai_clients):
|
||||
embedder = OpenAITextEmbedder()
|
||||
embedder.warm_up()
|
||||
await embedder.warm_up_async()
|
||||
|
||||
embedder.close()
|
||||
assert embedder.client is None
|
||||
assert embedder.async_client is not None
|
||||
|
||||
await embedder.close_async()
|
||||
assert embedder.async_client is None
|
||||
Reference in New Issue
Block a user