- Write your applications in LangChain/LangChain.js. Get started quickly by using Templates for reference.
- Use LangSmith to inspect, test, and monitor your chains to constantly improve and deploy with confidence.
- Turn any chain into an API with LangServe.
Setup guide
This guide shows you how to integrate Pinecone, a high-performance vector database, with LangChain, a framework for building applications powered by large language models (LLMs). Pinecone enables developers to build scalable, real-time recommendation and search systems based on vector similarity search. LangChain, on the other hand, provides modules for managing and optimizing the use of language models in applications. Its core philosophy is to facilitate data-aware applications where the language model interacts with other data sources and its environment. By integrating Pinecone with LangChain, you can add knowledge to LLMs via Retrieval Augmented Generation (RAG), greatly enhancing LLM ability for autonomous agents, chatbots, question-answering, and multi-agent systems.This guide demonstrates only one way out of many that you can use LangChain and Pinecone together. For additional examples, see:
Key concepts
ThePineconeVectorStore class provided by LangChain can be used to interact with Pinecone indexes. It’s important to remember that you must have an existing Pinecone index before you can create a PineconeVectorStore object.
Initializing a vector store
To initialize aPineconeVectorStore object, you must provide the name of the Pinecone index and an Embeddings object initialized through LangChain. There are two general approaches to initializing a PineconeVectorStore object:
- Initialize without adding records:
Python
from_existing_index method of LangChain’s PineconeVectorStore class to initialize a vector store.
- Initialize while adding records:
from_documents and from_texts methods of LangChain’s PineconeVectorStore class add records to a Pinecone index and return a PineconeVectorStore object.
The from_documents method accepts a list of LangChain’s Document class objects, which can be created using LangChain’s CharacterTextSplitter class. The from_texts method accepts a list of strings. Similarly to above, you must provide the name of an existing Pinecone index and an Embeddings object.
Both of these methods handle the embedding of the provided text data and the creation of records in your Pinecone index.
Python
Add more records
Once you have initialized aPineconeVectorStore object, you can add more records to the underlying Pinecone index (and thus also the linked LangChain object) using either the add_documents or add_texts methods.
Like their counterparts that also initialize a PineconeVectorStore object, both of these methods also handle the embedding of the provided text data and the creation of records in your Pinecone index.
Python
Python
Perform a similarity search
Asimilarity_search on a PineconeVectorStore object returns a list of LangChain Document objects most similar to the query provided. While the similarity_search uses a Pinecone query to find the most similar results, this method includes additional steps and returns results of a different type.
The similarity_search method accepts raw text and automatically embeds it using the Embedding object provided when you initialized the PineconeVectorStore. You can also provide a k value to determine the number of LangChain Document objects to return. The default value is k=4.
Python
Python
Namespaces
Several methods of thePineconeVectorStore class support using namespaces. You can also initialize your PineconeVectorStore object with a namespace to restrict all further operations to that space.
Python
PineconeVectorStore object without a namespace, you can specify the target namespace within the operation.
Python
Python
Tutorial
1. Set up your environment
Before you begin, install some necessary libraries and set environment variables for your Pinecone and OpenAI API keys:Python
2. Build the knowledge base
-
Load a sample Pinecone dataset into memory:
Python
-
Reduce the dataset and format it for upserting into Pinecone:
Python
3. Index the data in Pinecone
-
Decide whether to use a serverless or pod-based index.
Python
-
Initialize your client connection to Pinecone and create an index. This step uses the Pinecone API key you set as an environment variable earlier.
Python
-
Target the index and check its current stats:
You’ll see that the index has aPython
total_vector_countof0, as you haven’t added any vectors yet. -
Now upsert the data to Pinecone:
Python
-
Once the data is indexed, check the index stats once again:
Python
4. Initialize a LangChain vector store
Now that you’ve built your Pinecone index, you need to initialize a LangChain vector store using the index. This step uses the OpenAI API key you set as an environment variable earlier. Note that OpenAI is a paid service and so running the remainder of this tutorial may incur some small cost.-
Initialize a LangChain embedding object:
Python
-
Initialize the LangChain vector store:
The
text_fieldparameter sets the name of the metadata field that stores the raw text when you upsert records using a LangChain operation such asvectorstore.from_documentsorvectorstore.add_texts. This metadata field is used as thepage_contentin theDocumentobjects retrieved from query-like LangChain operations such asvectorstore.similarity_search. If you do not specify a value fortext_field, it will default to"text".Python -
Now you can query the vector store directly using
vectorstore.similarity_search:Python
5. Use Pinecone and LangChain for RAG
In RAG, you take the query as a question that is to be answered by a LLM, but the LLM must answer the question based on the information it is seeing from the vectorstore.-
To do this, initialize a
RetrievalQAobject like so:Python -
You can also include the sources of information that the LLM is using to answer your question using a slightly different version of
RetrievalQAcalledRetrievalQAWithSourcesChain:Python
6. Clean up
When you no longer need the index, use thedelete_index operation to delete it:
Python