Add support for OpenRouter as an embedding provider#2452
Add support for OpenRouter as an embedding provider#2452devin-ai-integration[bot] wants to merge 3 commits intomainfrom
Conversation
Co-Authored-By: Joe Moura <joao@crewai.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Co-Authored-By: Joe Moura <joao@crewai.com>
|
Disclaimer: This review was made by a crew of AI Agents. Code Review Comment for PR #2452OverviewThis pull request introduces Positive Aspects
Issues and Recommendations1. Missing Type HintsThe @staticmethod
def _configure_openrouter(
config: Dict[str, Any],
model_name: str
) -> OpenAIEmbeddingFunction:2. Error Handling ImprovementIncorporating error handling for cases where the API key is absent is crucial. A clear error message will assist users in troubleshooting issues effectively. @staticmethod
def _configure_openrouter(config: Dict[str, Any], model_name: str) -> 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")3. DocumentationAdding a docstring to the @staticmethod
def _configure_openrouter(config: Dict[str, Any], model_name: str) -> OpenAIEmbeddingFunction:
"""
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.
"""4. Test Coverage EnhancementTo ensure thorough validation of the OpenRouter functionality, additional test cases should be created to cover scenarios involving environment variables. def test_openrouter_embedder_configuration_with_env_var():
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:
embedder_config = {
"provider": "openrouter",
"config": {
"model": "test-model",
},
}
result = configurator.configure_embedder(embedder_config)
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",
)5. Error Case TestingAn additional test case should address situations with missing API keys to confirm that relevant exceptions are raised appropriately. def test_openrouter_embedder_configuration_missing_api_key():
configurator = EmbeddingConfigurator()
embedder_config = {
"provider": "openrouter",
"config": {
"model": "test-model",
},
}
with pytest.raises(ValueError, match="OpenRouter API key must be provided"):
configurator.configure_embedder(embedder_config)Historical ContextIt would be beneficial to reference previous PRs that involved similar integration efforts, as they can provide a framework for best practices and lessons learned. A search for related PRs that modified similar configurations or functionality can shed light on consistency across the codebase and further solidify the integration of OpenRouter. If you would like, I can perform a search for related pull requests to provide more context and references for future development. Summary of Recommendations
Implementing these recommendations will significantly increase code reliability, maintainability, and usability while ensuring comprehensive validation of the embedding provider integration. Thank you for your contributions, and I look forward to seeing these enhancements! |
…, and tests Co-Authored-By: Joe Moura <joao@crewai.com>
|
OpenRouter does not support embedding models |
Add support for OpenRouter as an embedding provider
Fixes #2451
Description
This PR adds support for OpenRouter as an embedding provider in CrewAI. Users can now configure their agents to use OpenRouter for embeddings.
Usage Example
Testing
Link to Devin run: https://app.devin.ai/sessions/90f238089f664da2ba9a4bd79c061e3e
Requested by: Joe Moura (joao@crewai.com)