How to Use Weaviate with Langchain for Vector Search
To use
Weaviate with Langchain, install the weaviate-client and langchain Python packages, then create a Weaviate vector store instance in Langchain pointing to your Weaviate server. Use Langchain's Weaviate vector store class to add, query, and manage embeddings seamlessly.Syntax
The main steps to use Weaviate with Langchain are:
- Import the
Weaviatevector store from Langchain. - Initialize the Weaviate client with your server URL and API key if needed.
- Create a Langchain
Weaviatevector store instance by passing the client and index name. - Use the vector store methods like
add_textsto add documents andsimilarity_searchto query.
python
from langchain.vectorstores import Weaviate import weaviate client = weaviate.Client(url="http://localhost:8080", auth_client_secret=None) vector_store = Weaviate(client, index_name="MyIndex", embedding=None) # Add documents vector_store.add_texts(["Hello world", "Langchain with Weaviate"]) # Query results = vector_store.similarity_search("Hello")
Example
This example shows how to connect Langchain to a local Weaviate instance, add text documents, and perform a similarity search.
python
from langchain.vectorstores import Weaviate from langchain.embeddings.openai import OpenAIEmbeddings import weaviate # Initialize Weaviate client client = weaviate.Client(url="http://localhost:8080") # Initialize embeddings (requires OPENAI_API_KEY set in environment) embeddings = OpenAIEmbeddings() # Create Weaviate vector store instance vector_store = Weaviate(client, index_name="LangchainDemo", embedding=embeddings) # Add texts to Weaviate texts = ["Langchain makes building LLM apps easy.", "Weaviate is a vector search engine."] vector_store.add_texts(texts) # Perform similarity search query = "What is Langchain?" results = vector_store.similarity_search(query, k=1) print(results)
Output
[Document(page_content='Langchain makes building LLM apps easy.', metadata={})]
Common Pitfalls
- Not running a Weaviate server or incorrect URL causes connection errors.
- Missing or incorrect API keys if your Weaviate instance requires authentication.
- Forgetting to specify the
embeddingparameter when creating the vector store, which is needed to convert text to vectors. - Not creating the Weaviate schema/index before adding data can cause failures.
python
from langchain.vectorstores import Weaviate import weaviate # Wrong: Missing embedding parameter client = weaviate.Client(url="http://localhost:8080") vector_store = Weaviate(client, index_name="Demo") # Missing embedding causes error # Right: Include embedding from langchain.embeddings.openai import OpenAIEmbeddings embeddings = OpenAIEmbeddings() vector_store = Weaviate(client, index_name="Demo", embedding=embeddings)
Quick Reference
Key points to remember when using Weaviate with Langchain:
- Install
weaviate-clientandlangchainpackages. - Run a Weaviate server accessible to your code.
- Use Langchain's
Weaviatevector store class with an embedding model. - Always create or verify the Weaviate schema before adding data.
- Use
add_textsto add documents andsimilarity_searchto query.
Key Takeaways
Use Langchain's Weaviate vector store class with a Weaviate client and an embedding model.
Ensure your Weaviate server is running and accessible before connecting.
Always specify the embedding parameter to convert text into vectors.
Create the Weaviate schema/index before adding documents to avoid errors.
Use add_texts to add data and similarity_search to find relevant documents.