4

I'm trying to build a simple RAG, and I'm stuck at this code:

from langchain.embeddings.huggingface import HuggingFaceEmbeddings
from llama_index import LangchainEmbedding, ServiceContext

embed_model = LangchainEmbedding(
  HuggingFaceEmbeddings(model_name="thenlper/gte-large")
)
service_context = ServiceContext.from_defaults(
    chunk_size=256,
    llm=llm,
    embed_model=embed_model
)
index = VectorStoreIndex.from_documents(documents, service_context=service_context)

where I get ImportError: cannot import name 'LangchainEmbedding' from 'llama_index' How can I solve? Is it related to the fact that I'm working on Colab?

2
  • 1
    always put FULL error message (starting at word "Traceback") in question (not in comments) as text (not screenshot, not link to external portal). There are other useful information in the full error/traceback. Commented Nov 18, 2023 at 23:43
  • 1
    maybe check documentation. It seems it doesn't have from llama_index import LangchainEmbedding but from llama_index.embedings import LangchainEmbedding (See source code) Commented Nov 18, 2023 at 23:47

6 Answers 6

10

INFO 2024:

This answer worked in 2023 when question was asked
but it seems they moved all code and now you have to use other answers.


Not

from llama_index import LangchainEmbedding

but

from llama_index.embeddings import LangchainEmbedding

(See source code for llama_index/embeddings/__ init__.py)

Sign up to request clarification or add additional context in comments.

Comments

9

The other answers didn't work for me while the one below did.

from llama_index.legacy.embeddings.langchain import LangchainEmbedding

2 Comments

explain please why this is better then existing answers? (i didnt downvote)
It works for me. As of 29 June 2024
3

Now this is the right Answer, LangchainEmbedding replace with HuggingFaceEmbeddings. Now its working.

from langchain.embeddings import HuggingFaceEmbeddings
from llama_index import ServiceContext, set_global_service_context

embed_model = HuggingFaceEmbeddings(
    model_name="sentence-transformers/all-mpnet-base-v2"
)

1 Comment

The answer needs bettweb formating following the guideline
2

For a problem with

from llama_index import LangchainEmbedding

or

from llama_index.embedings import LangchainEmbedding

It looks like the syntax has been updated yet again, see:

https://github.com/run-llama/llama_index/blob/main/llama_index/embeddings/__init__.py

As of 2023 Dec 7th, it says:

from llama_index.embeddings.langchain import LangchainEmbedding

Today that works...tomorrow...a brave new world.

Comments

1

With reference to above code. This is working exactly.

from langchain.embeddings.huggingface import HuggingFaceEmbeddings
from llama_index import ServiceContext, set_global_service_context

embed_model = HuggingFaceEmbeddings(
  model_name="thenlper/gte-large"
)

Comments

1

There is an update install langchain embedding separately

!pip install llama-index-embeddings-langchain

Then

from llama_index.embeddings.langchain import LangchainEmbedding

This worked for me check this for more ....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.