[Solved] Chromadb + Langchain + SentenceTransformerEmbeddingFunction throwing 'SentenceTransformerEmbeddingFunction' object has no attribute 'embed – Langchain

by
Alexei Petrov
chromadb information-retrieval langchain sentence-transformers

Quick Fix: You can use LangChain’s wrapper for SentenceTransformerEmbeddings. Here’s an example: python from langchain.embeddings import SentenceTransformerEmbeddings embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2") For more details, refer: https://blog.futuresmart.ai/using-langchain-and-open-source-vector-db-chroma-for-semantic-search-with-openais-llm

The Solutions:

Solution 1: Using LangChain’s Wrapper for SentenceTransformer

Rather than using `Chromadb`’s `embedding_functions` for embedding, you should use `LangChain`’s `SentenceTransformerEmbeddings`. This will provide you with a more comprehensive and supported way to integrate SentenceTransformer with LangChain and Chroma. Here’s how you can use `SentenceTransformerEmbeddings`:

from langchain.embeddings import SentenceTransformerEmbeddings

# Initialize SentenceTransformer embeddings with your preferred model
embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")

With this approach, you won’t encounter the 'SentenceTransformerEmbeddingFunction' object has no attribute 'embed_documents' error because you’re utilizing LangChain’s dedicated wrapper for SentenceTransformer, which provides a seamless integration with LangChain and Chroma.

Solution 2: Check the Usage of the `embedding` Variable

The error you are encountering is not related to the code you provided. The issue might lie in how you are using the embedding variable. To resolve this, try the following:

  1. Ensure that you have imported the embed_documents function correctly. It should be imported from langchain.vectorstores.chroma_utils instead of chromadb.utils.embedding_functions.
from langchain.vectorstores.chroma_utils import embed_documents  
  1. Check that you are using the embedding variable correctly. The embed_documents function is a method of the SentenceTransformerEmbeddingFunction class, so you need to call it using the embedding object.
embeddings = embedding.embed_documents(texts)  
  1. Make sure that the model_name you are using is valid for the SentenceTransformer model. Refer to the Hugging Face model hub to verify the correct model name.

  2. Double-check that you have the correct versions of Chromadb and Langchain installed.

  3. If you are still encountering the error, try reinstalling both Chromadb and Langchain to ensure that there are no issues with the installation.

Here is an example of how you can use the SentenceTransformerEmbeddingFunction with Chromadb:

from langchain.vectorstores import Chroma
from langchain.vectorstores.chroma_utils import embed_documents

embedding = SentenceTransformerEmbeddingFunction(model_name="all-MiniLM-L6-v2")

chroma = Chroma()

texts = ["This is the first sentence.", "This is the second sentence."]

embeddings = embedding.embed_documents(texts)

chroma.add(embeddings, texts)  

This code should work without raising the AttributeError. If you are still having issues, please provide more information about how you are using the embedding variable, including the full code.

Q&A

Why it throws the error at SentenceTransformerEmbeddingFunction?

You need to use LangChain’s wrapper for SentenceTransformerEmbeddingFunction

Does the code in the question cause the error?

No, the error in the question irrelevant with code.