- Write your applications in LangChain or LangChain.js.
- Use LangSmith to inspect, test, and monitor your chains so you can keep improving them and deploy with confidence.
Setup guide
This guide shows you how to integrate Pinecone Database with LangChain.This guide shows one of many ways to use LangChain and Pinecone together. For more examples, see the following:
Key concepts
ThePineconeVectorStore class from LangChain lets you interact with Pinecone indexes. You must have an existing Pinecone index before you can create a PineconeVectorStore object.
Initialize a vector store
To initialize aPineconeVectorStore object, you must provide the name of the Pinecone index and an Embeddings object initialized through LangChain. You can initialize a PineconeVectorStore object in two ways:
- 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 you can create with LangChain’s CharacterTextSplitter class. The from_texts method accepts a list of strings. As with the first approach, 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
If you get
Pinecone has no attribute 'from_texts' or Pinecone has no attribute 'from_documents', your code is calling these methods on the Pinecone SDK’s Pinecone class instead of LangChain’s vector store class. Import PineconeVectorStore from langchain_pinecone, as shown above.Add more records
After you initialize aPineconeVectorStore object, you can add more records to the underlying Pinecone index (and thus also the linked LangChain object) with the add_documents or add_texts method.
Like from_documents and from_texts, both of these methods handle the embedding of the provided text data and the creation of records in your Pinecone index.
To add documents, use add_documents:
Python
add_texts:
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
similarity_search:
Python
Tutorial
Set up the environment
Install the libraries:Set your Pinecone and OpenAI API keys as environment variables:Then read the keys in Python:
Shell
Shell
Python
Build the knowledge base
Load a sample dataset and prepare it for Pinecone:
-
Load a sample Pinecone dataset into memory:
PythonResponse
-
Reduce the dataset and format it for upserting into Pinecone:
Python
Index the data in Pinecone
Create an index and upsert the prepared data:
-
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:
PythonThe index has aResponse
total_vector_countof0because you haven’t added any vectors yet. -
Upsert the data to Pinecone:
Python
-
After the data is indexed, check the index stats again:
PythonResponse
Initialize a LangChain vector store
Now that you’ve built your Pinecone index, initialize a LangChain vector store that uses it. This step uses the OpenAI API key you set as an environment variable earlier. OpenAI is a paid service, so running the rest of this tutorial may incur a 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 don’t specify a value fortext_field, it defaults to"text".Python -
Query the vector store directly using
vectorstore.similarity_search:PythonResponse
Use Pinecone and LangChain for RAG
In RAG, you pass the query to an LLM as a question, and the LLM must answer it based on the information it gets from the vector store. Follow these steps:
-
Build a RAG chain that retrieves context from the vector store and passes it to the LLM with the question:
PythonResponse
-
To include the sources the LLM uses to answer your question, return the retrieved documents alongside the answer:
PythonResponse