2024-09-18 13:30:48 +03:00
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
2024-09-11 17:00:19 +03:00
|
|
|
from langchain_community.embeddings.ollama import OllamaEmbeddings
|
|
|
|
from langchain_openai import AzureOpenAIEmbeddings
|
2024-09-18 13:30:48 +03:00
|
|
|
from quivr_api.modules.dependencies import get_embedding_client
|
|
|
|
|
2024-09-11 17:00:19 +03:00
|
|
|
|
|
|
|
def test_ollama_embedding():
|
|
|
|
with patch("quivr_api.modules.dependencies.settings") as mock_settings:
|
|
|
|
mock_settings.ollama_api_base_url = "http://ollama.example.com"
|
|
|
|
mock_settings.azure_openai_embeddings_url = None
|
2024-09-18 13:30:48 +03:00
|
|
|
|
2024-09-11 17:00:19 +03:00
|
|
|
embedding_client = get_embedding_client()
|
2024-09-18 13:30:48 +03:00
|
|
|
|
2024-09-11 17:00:19 +03:00
|
|
|
assert isinstance(embedding_client, OllamaEmbeddings)
|
|
|
|
assert embedding_client.base_url == "http://ollama.example.com"
|
|
|
|
|
2024-09-18 13:30:48 +03:00
|
|
|
|
2024-09-11 17:00:19 +03:00
|
|
|
def test_azure_embedding():
|
|
|
|
with patch("quivr_api.modules.dependencies.settings") as mock_settings:
|
|
|
|
mock_settings.ollama_api_base_url = None
|
|
|
|
mock_settings.azure_openai_embeddings_url = "https://quivr-test.openai.azure.com/openai/deployments/embedding/embeddings?api-version=2023-05-15"
|
2024-09-18 13:30:48 +03:00
|
|
|
|
2024-09-11 17:00:19 +03:00
|
|
|
embedding_client = get_embedding_client()
|
2024-09-18 13:30:48 +03:00
|
|
|
|
2024-09-11 17:00:19 +03:00
|
|
|
assert isinstance(embedding_client, AzureOpenAIEmbeddings)
|
|
|
|
assert embedding_client.azure_endpoint == "https://quivr-test.openai.azure.com"
|
|
|
|
|
2024-09-18 13:30:48 +03:00
|
|
|
|
2024-09-11 17:00:19 +03:00
|
|
|
def test_openai_embedding():
|
2024-09-18 13:30:48 +03:00
|
|
|
with (
|
|
|
|
patch("quivr_api.modules.dependencies.settings") as mock_settings,
|
|
|
|
patch(
|
|
|
|
"quivr_api.modules.dependencies.OpenAIEmbeddings"
|
|
|
|
) as mock_openai_embeddings,
|
|
|
|
):
|
2024-09-11 17:00:19 +03:00
|
|
|
mock_settings.ollama_api_base_url = None
|
|
|
|
mock_settings.azure_openai_embeddings_url = None
|
2024-09-18 13:30:48 +03:00
|
|
|
|
2024-09-11 17:00:19 +03:00
|
|
|
# Create a mock instance for OpenAIEmbeddings
|
|
|
|
mock_openai_instance = MagicMock()
|
|
|
|
mock_openai_embeddings.return_value = mock_openai_instance
|
2024-09-18 13:30:48 +03:00
|
|
|
|
2024-09-11 17:00:19 +03:00
|
|
|
embedding_client = get_embedding_client()
|
2024-09-18 13:30:48 +03:00
|
|
|
|
2024-09-11 17:00:19 +03:00
|
|
|
assert embedding_client == mock_openai_instance
|