Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create mistral_embedding_function.py #3277

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions chromadb/utils/embedding_functions/mistral_embedding_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import logging
from typing import Union, cast

from chromadb.api.types import Documents, EmbeddingFunction, Embeddings

logger = logging.getLogger(__name__)


class MistralEmbeddingFunction(EmbeddingFunction[Documents]):
"""
This class is used to generate embeddings for a list of texts using the Mistral AI Embeddings API (https://docs.mistral.ai/capabilities/embeddings/)
"""

def __init__(self, api_key: str, model_name: str) -> None:
"""
Initialize the Mistral Embedding Function.

Args:
api_key (str): The API key for Mistral AI.
model_name (str): The name of the model to use for text embeddings. E.g. "mistral-embed" (see https://docs.mistral.ai/getting-started/models/ for available models).
"""
if not api_key:
raise ValueError("Please provide a Mistral API key.")

if not model_name:
raise ValueError("Please provide the model name.")

try:
from mistralai import Mistral
except ImportError:
raise ValueError(
"The Mistral AI python package is not installed. Please install it with `pip install mistralai`"
)

self._client = Mistral(api_key=api_key)
self._model_name = model_name

def __call__(self, input: Union[Documents, str]) -> Embeddings:
"""
Get the embeddings for a list of texts.

Args:
input (Documents): A list of texts to get embeddings for.

Returns:
Embeddings: The embeddings for the texts.

Example:
>>> mistral_ef = MistralEmbeddingFunction(api_key="your_api_key", model_name="mistral-embed")
>>> texts = ["Hello, world!", "How are you?"]
>>> embeddings = mistral_ef(texts)
"""
texts = input if isinstance(input, list) else [input]
embeddingdata = self._client.embeddings.create(
model=self._model_name, inputs=texts
)

vectors = []
for item in embeddingdata.data:
vectors.append(item.embedding)
# Return just the embeddings
return cast(
Embeddings, vectors
)
Loading