Generative Question-Answering with LangChain and Pinecone

Store document embeddings in Pinecone's managed vector database with LangChain, and how it compares with self-hosted pgvector for question answering.

An LLM can't answer questions about documents it has never seen, so a Q&A app retrieves the relevant passages first and sends them to the model along with the question. Retrieval works on embeddings: an embedding model converts each chunk of text into a vector, a list of numbers that represents its meaning, and text on similar topics produces similar vectors. A vector database stores those vectors and, given a question, returns the chunks whose vectors are nearest to it.

In my Q&A post, the vector store is Postgres with pgvector, running next to the app. This post covers Pinecone, a managed vector database, and when it's the better choice.

What is Pinecone?

Pinecone runs the vector database as a service. Its serverless indexes scale with usage, so you don't size servers, tune indexes or plan for growth. The free Starter plan includes up to 2 GB of storage, 1 million read units and 2 million write units a month, and up to five indexes, on AWS in us-east-1. Paid plans start at $20 a month for Builder, and Standard has a $50 monthly minimum.

Create an Index and Store Documents

LangChain's langchain-pinecone package wraps Pinecone in the same vector store interface as every other store. This is adapted from the LangChain Pinecone docs:

import os

from pinecone import Pinecone, ServerlessSpec
from langchain_openai import OpenAIEmbeddings
from langchain_pinecone import PineconeVectorStore

pc = Pinecone(api_key=os.environ["PINECONE_API_KEY"])

if not pc.has_index("docs"):
    pc.create_index(
        name="docs",
        dimension=1536,
        metric="cosine",
        spec=ServerlessSpec(cloud="aws", region="us-east-1"),
    )

embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
vector_store = PineconeVectorStore(index=pc.Index("docs"), embedding=embeddings)

vector_store.add_documents(chunks)
hits = vector_store.similarity_search("What does the report recommend?", k=6)

The index dimension has to match the embedding model: 1536 for text-embedding-3-small. If you change embedding models later, you create a new index and embed everything again.

The Vector Store with pgvector

By default, the vector store in my LangChain examples app is a Postgres table. langchain-postgres creates it with a column for each piece of metadata the app filters or lists by:

engine.init_vectorstore_table(
    table_name=TABLE,
    vector_size=EMBEDDING_SIZE,
    metadata_columns=[Column("source", "TEXT"), Column("page", "INTEGER")],
)

From there, add_documents and similarity_search work exactly as they do with Pinecone, because both classes implement LangChain's vector store interface. The rest of the Q&A code, which numbers the retrieved chunks, adds them to the prompt and asks the model to cite them, doesn't change when you switch stores.

Pinecone or pgvector?

Pineconepgvector
HostingManaged serviceYour Postgres, self-hosted or managed
SetupAPI key and an indexA Postgres database with the extension
ScalingAutomatic, billed by usageYou size the database and tune indexes
Your app dataSeparate system, synced by your codeSame database, so you can join and filter with SQL
Cost to startFree Starter planWhatever your Postgres costs
Where vectors livePinecone's cloudWherever you run Postgres

Pinecone suits large or fast-growing collections, and teams that don't want to run a database. pgvector suits apps that already use Postgres, need vectors next to the rest of their data, or have to keep documents on their own infrastructure. My LangChain examples app supports both. It uses pgvector by default, and setting VECTOR_STORE=pinecone moves Chat with Docs to a Pinecone serverless index, which the app creates on first use.

Deploy on Railway

My LangChain Pinecone template on Railway deploys the app with Pinecone as its vector store, so no database runs on Railway. Set OPENAI_API_KEY and PINECONE_API_KEY, and the app creates the index on first use. For the pgvector version, the LangChain Apps template deploys the same app with Postgres. Either way, set APP_PASSWORD if your API keys are stored on the server.

Deploy on Railway

Frequently Asked Questions

Is Pinecone free? The Starter plan is free, with up to 2 GB of storage, 1 million read units and 2 million write units a month, and five indexes on AWS us-east-1.

Is pgvector a good alternative to Pinecone? For many apps, yes, especially if you already run Postgres. Pinecone saves you running and scaling the database; pgvector keeps vectors next to your other data and on your own infrastructure.

Can I switch vector stores in LangChain without rewriting my app? Mostly. Pinecone, pgvector, Chroma and others share the same add_documents, similarity_search and retriever methods. Setup code and anything that talks to the database directly still changes.

Subscribe to alphasec

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe