mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-08 23:58:34 +00:00
Fix issue #2832: Add VoyageAI embedding function implementation
Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
3
src/crewai/knowledge/embedder/chromadb/__init__.py
Normal file
3
src/crewai/knowledge/embedder/chromadb/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .utils.embedding_functions import VoyageAIEmbeddingFunction
|
||||
|
||||
__all__ = ["VoyageAIEmbeddingFunction"]
|
||||
3
src/crewai/knowledge/embedder/chromadb/utils/__init__.py
Normal file
3
src/crewai/knowledge/embedder/chromadb/utils/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from . import embedding_functions
|
||||
|
||||
__all__ = ["embedding_functions"]
|
||||
@@ -0,0 +1,3 @@
|
||||
from .voyageai_embedding_function import VoyageAIEmbeddingFunction
|
||||
|
||||
__all__ = ["VoyageAIEmbeddingFunction"]
|
||||
@@ -0,0 +1,42 @@
|
||||
import logging
|
||||
from typing import List, Optional
|
||||
|
||||
from chromadb.api.types import Documents, EmbeddingFunction, Embeddings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class VoyageAIEmbeddingFunction(EmbeddingFunction[Documents]):
|
||||
def __init__(self, api_key: str, model_name: str = "voyage-3"):
|
||||
try:
|
||||
import voyageai
|
||||
except ImportError:
|
||||
raise ValueError(
|
||||
"The voyageai python package is not installed. Please install it with `pip install voyageai`"
|
||||
)
|
||||
|
||||
self._api_key = api_key
|
||||
self._model_name = model_name
|
||||
|
||||
def __call__(self, input: Documents) -> Embeddings:
|
||||
try:
|
||||
import voyageai
|
||||
except ImportError:
|
||||
raise ValueError(
|
||||
"The voyageai python package is not installed. Please install it with `pip install voyageai`"
|
||||
)
|
||||
|
||||
if not input:
|
||||
return []
|
||||
|
||||
if isinstance(input, str):
|
||||
input = [input]
|
||||
|
||||
try:
|
||||
embeddings = voyageai.get_embeddings(
|
||||
input, model=self._model_name, api_key=self._api_key
|
||||
)
|
||||
return embeddings
|
||||
except Exception as e:
|
||||
logger.error(f"Error during VoyageAI embedding: {e}")
|
||||
raise e
|
||||
47
tests/utilities/test_embedding_configurator.py
Normal file
47
tests/utilities/test_embedding_configurator.py
Normal file
@@ -0,0 +1,47 @@
|
||||
import os
|
||||
import pytest
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
from crewai.utilities.embedding_configurator import EmbeddingConfigurator
|
||||
|
||||
|
||||
class TestEmbeddingConfigurator:
|
||||
def test_configure_voyageai_embedder(self):
|
||||
"""Test that the VoyageAI embedder is configured correctly."""
|
||||
with patch(
|
||||
"crewai.utilities.embedding_configurator.VoyageAIEmbeddingFunction"
|
||||
) as mock_voyageai:
|
||||
mock_instance = MagicMock()
|
||||
mock_voyageai.return_value = mock_instance
|
||||
|
||||
config = {"api_key": "test-key"}
|
||||
model_name = "voyage-3"
|
||||
|
||||
configurator = EmbeddingConfigurator()
|
||||
embedder = configurator._configure_voyageai(config, model_name)
|
||||
|
||||
mock_voyageai.assert_called_once_with(
|
||||
model_name=model_name, api_key="test-key"
|
||||
)
|
||||
assert embedder == mock_instance
|
||||
|
||||
def test_configure_embedder_with_voyageai(self):
|
||||
"""Test that the embedder configurator correctly handles VoyageAI provider."""
|
||||
with patch(
|
||||
"crewai.utilities.embedding_configurator.VoyageAIEmbeddingFunction"
|
||||
) as mock_voyageai:
|
||||
mock_instance = MagicMock()
|
||||
mock_voyageai.return_value = mock_instance
|
||||
|
||||
embedder_config = {
|
||||
"provider": "voyageai",
|
||||
"config": {"api_key": "test-key", "model": "voyage-3"},
|
||||
}
|
||||
|
||||
configurator = EmbeddingConfigurator()
|
||||
embedder = configurator.configure_embedder(embedder_config)
|
||||
|
||||
mock_voyageai.assert_called_once_with(
|
||||
model_name="voyage-3", api_key="test-key"
|
||||
)
|
||||
assert embedder == mock_instance
|
||||
Reference in New Issue
Block a user