LlamaIndex Embeddings Langchain Integration

0.5.0 · active · verified Thu Apr 16

The `llama-index-embeddings-langchain` library provides an integration layer to use LangChain's embedding models within the LlamaIndex framework. It acts as a wrapper, allowing users to leverage the wide array of embedding models available in LangChain for LlamaIndex's indexing and retrieval functionalities. The current version is 0.5.0, and as part of the broader LlamaIndex ecosystem, it typically sees active development and frequent releases in conjunction with LlamaIndex core.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate a LangChain embedding model, specifically a HuggingFace one, into LlamaIndex. It involves initializing the LangChain model, wrapping it with `LangchainEmbedding`, and then setting it as the global embedding model for LlamaIndex. This allows any LlamaIndex component (e.g., VectorStoreIndex) to use this embedding model.

import os
from llama_index.embeddings.langchain import LangchainEmbedding
from llama_index.core import Settings
from langchain_community.embeddings import HuggingFaceEmbeddings

# Ensure the necessary LangChain package is installed
# pip install langchain-community

# 1. Initialize a LangChain embedding model
# Using a local model for demonstration, no API key needed
lc_embed_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")

# 2. Wrap the LangChain embedding model with LlamaIndex's LangchainEmbedding wrapper
embed_model = LangchainEmbedding(lc_embed_model)

# 3. Set the global embedding model for LlamaIndex
Settings.embed_model = embed_model

# 4. Example usage: get an embedding
text = "This is a test sentence for embedding."
embedding = Settings.embed_model.get_text_embedding(text)

print(f"Embedding length: {len(embedding)}")
print(f"First 10 dimensions of embedding: {embedding[:10]}")

# You can also use it directly without setting global settings
# direct_embedding = embed_model.get_text_embedding("Another sentence.")
# print(f"Direct embedding length: {len(direct_embedding)}")

view raw JSON →