mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-14 18:48:29 +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
|
||||
Reference in New Issue
Block a user