Skip to content

Instantly share code, notes, and snippets.

@tazarov
Last active December 13, 2023 13:19
Show Gist options
  • Save tazarov/c7c4d8d66fb10d4d98a2cefdc304cf43 to your computer and use it in GitHub Desktop.
Save tazarov/c7c4d8d66fb10d4d98a2cefdc304cf43 to your computer and use it in GitHub Desktop.
Chroma and LlamaIndex both offer embedding functions which are wrappers on top of popular embedding models. Unfortunately Chroma and LI's embedding functions are not compatible with each other. Below we offer an adapters to convert LI embedding function to Chroma one.
from llama_index.embeddings import OpenAIEmbedding
from llama_index.embeddings.base import BaseEmbedding
import chromadb
from chromadb.api.types import EmbeddingFunction
class LlamaIndexEmbeddingAdapter(EmbeddingFunction):
def __init__(self,ef:BaseEmbedding):
self.ef = ef
def __call__(self, input: Documents) -> Embeddings:
return [node.embedding for node in self.ef(input)]
# Make sure you have `OPENAI_API_KEY` as env var.
embed_model = OpenAIEmbedding(embed_batch_size=10)
client = chromadb.Client()
col = client.get_or_create_collection("test_collection",embedding_function=LlamaIndexEmbeddingAdapter(embed_model))
col.add(ids=["1"],documents=["this is a test document"])
# your embeddings should be 1536-dimensional vectors (produced by ADA model)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment