Compare commits

...

3 Commits

Author SHA1 Message Date
Devin AI
cf4d5e3cdd Improve OpenRouter embedding provider with type hints, error handling, and tests
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-03-24 07:11:11 +00:00
Devin AI
5470c56dc6 Fix lint issues: Sort imports in test file
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-03-24 07:06:42 +00:00
Devin AI
de200b9404 Fix #2451: Add support for OpenRouter as an embedding provider
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-03-24 07:04:47 +00:00
2 changed files with 120 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ class EmbeddingConfigurator:
"huggingface": self._configure_huggingface,
"watson": self._configure_watson,
"custom": self._configure_custom,
"openrouter": self._configure_openrouter,
}
def configure_embedder(
@@ -210,6 +211,35 @@ class EmbeddingConfigurator:
return WatsonEmbeddingFunction()
@staticmethod
def _configure_openrouter(config: Dict[str, Any], model_name: str) -> EmbeddingFunction:
"""
Configure OpenRouter embedding provider.
Args:
config (Dict[str, Any]): Configuration dictionary containing the API key and optional settings.
model_name (str): Name of the embedding model to use.
Returns:
OpenAIEmbeddingFunction: Configured OpenRouter embedding function.
Raises:
ValueError: If the API key is not provided in the config or environment.
"""
from chromadb.utils.embedding_functions.openai_embedding_function import (
OpenAIEmbeddingFunction,
)
api_key = config.get("api_key") or os.getenv("OPENROUTER_API_KEY")
if not api_key:
raise ValueError("OpenRouter API key must be provided either in config or OPENROUTER_API_KEY environment variable")
return OpenAIEmbeddingFunction(
api_key=api_key,
api_base=config.get("api_base", "https://openrouter.ai/api/v1"),
model_name=model_name,
)
@staticmethod
def _configure_custom(config):
custom_embedder = config.get("embedder")

View File

@@ -0,0 +1,90 @@
import os
from unittest.mock import MagicMock, patch
import pytest
from crewai.utilities.embedding_configurator import EmbeddingConfigurator
def test_openrouter_embedder_configuration():
# Setup
configurator = EmbeddingConfigurator()
mock_openai_embedding = MagicMock()
with patch(
"chromadb.utils.embedding_functions.openai_embedding_function.OpenAIEmbeddingFunction",
return_value=mock_openai_embedding,
) as mock_embedder:
# Test with provided config
embedder_config = {
"provider": "openrouter",
"config": {
"api_key": "test-key",
"model": "test-model",
},
}
# Execute
result = configurator.configure_embedder(embedder_config)
# Verify
assert result == mock_openai_embedding
mock_embedder.assert_called_once_with(
api_key="test-key",
api_base="https://openrouter.ai/api/v1",
model_name="test-model",
)
def test_openrouter_embedder_configuration_with_env_var():
# Setup
configurator = EmbeddingConfigurator()
mock_openai_embedding = MagicMock()
# Test with API key from environment variable
with patch.dict(os.environ, {"OPENROUTER_API_KEY": "env-key"}), \
patch(
"chromadb.utils.embedding_functions.openai_embedding_function.OpenAIEmbeddingFunction",
return_value=mock_openai_embedding,
) as mock_embedder:
# Config without API key
embedder_config = {
"provider": "openrouter",
"config": {
"model": "test-model",
},
}
# Execute
result = configurator.configure_embedder(embedder_config)
# Verify
assert result == mock_openai_embedding
mock_embedder.assert_called_once_with(
api_key="env-key",
api_base="https://openrouter.ai/api/v1",
model_name="test-model",
)
def test_openrouter_embedder_configuration_missing_api_key():
# Setup
configurator = EmbeddingConfigurator()
# Test without API key
with patch.dict(os.environ, {}, clear=True), \
patch(
"chromadb.utils.embedding_functions.openai_embedding_function.OpenAIEmbeddingFunction",
side_effect=Exception("Should not be called"),
):
# Config without API key
embedder_config = {
"provider": "openrouter",
"config": {
"model": "test-model",
},
}
# Verify error is raised
with pytest.raises(ValueError, match="OpenRouter API key must be provided"):
configurator.configure_embedder(embedder_config)