This guide demonstrates only one way out of many that you can use LangChain and Pinecone together. For additional examples, see:
1. Set up your environment
Before you begin, install some necessary libraries and set environment variables for your Pinecone and OpenAI API keys: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. Pod-based indexes are the traditional Pinecone architecture; they are available on Pinecone’s (free) starter tier. Serverless is the new Pinecone architecture offering large cost savings, easier scaling, and more — there is no free tier available for Serverless yet, but when signing up, you can get $100 in free credits.
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:
PythonYou’ll see that the index has a
total_vector_count
of0
, 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:
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
RetrievalQA
object 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
RetrievalQA
calledRetrievalQAWithSourcesChain
:Python
6. Clean up
When you no longer need the index, use thedelete_index
operation to delete it:
Python