Introduction
Chroma is great for local prototyping, but for production RAG you need a managed vector database. Pinecone is the most popular: serverless, auto-scaling, and with a generous free tier. This post connects Streamlit to Pinecone for semantic search and RAG.
Setup
pip install pinecone-client openai streamlit
Create a Pinecone account (free tier: 1 index, 100k vectors), then get your API key.
Store in .streamlit/secrets.toml:
PINECONE_API_KEY = "pcsk_..."
OPENAI_API_KEY = "sk-..."
Step 1: Create an index
from pinecone import Pinecone, ServerlessSpec
pc = Pinecone(api_key=st.secrets["PINECONE_API_KEY"])
# Create index (run once)
pc.create_index(
name="my-docs",
dimension=1536, # OpenAI embedding dimension
metric="cosine", # or "euclidean", "dotproduct"
spec=ServerlessSpec(cloud="aws", region="us-east-1"),
)
dimension must match your embedding model (OpenAI text-embedding-3-small = 1536).
Step 2: Connect and upsert
@st.cache_resource
def get_index():
pc = Pinecone(api_key=st.secrets["PINECONE_API_KEY"])
return pc.Index("my-docs")
index = get_index()
# Upsert vectors
from openai import OpenAI
oai = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
def embed(text):
return oai.embeddings.create(input=text, model="text-embedding-3-small").data[0].embedding
# Upsert a document
index.upsert(vectors=[
{"id": "doc1", "values": embed("Hello world"), "metadata": {"text": "Hello world", "source": "intro.md"}},
{"id": "doc2", "values": embed("Python is great"), "metadata": {"text": "Python is great", "source": "python.md"}},
])
Step 3: Query (search)
def search(query, top_k=5):
query_emb = embed(query)
results = index.query(vector=query_emb, top_k=top_k, include_metadata=True)
return results["matches"]
matches = search("programming language")
for m in matches:
print(f"{m['score']:.3f} | {m['metadata']['text']}")
Returns the most similar documents with similarity scores.
Step 4: Full Streamlit app
import streamlit as st
from pinecone import Pinecone
from openai import OpenAI
st.title("🔍 Semantic Search with Pinecone")
pc = Pinecone(api_key=st.secrets["PINECONE_API_KEY"])
index = pc.Index("my-docs")
oai = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
def embed(text):
return oai.embeddings.create(input=text, model="text-embedding-3-small").data[0].embedding
# Ingest
st.subheader("Add documents")
doc_text = st.text_area("Document text")
doc_id = st.text_input("Document ID")
if st.button("Add") and doc_text and doc_id:
index.upsert(vectors=[{"id": doc_id, "values": embed(doc_text),
"metadata": {"text": doc_text}}])
st.success("Added!")
# Search
st.subheader("Search")
query = st.text_input("Query")
if query:
results = index.query(vector=embed(query), top_k=5, include_metadata=True)
for m in results["matches"]:
st.write(f"**{m['score']:.3f}** — {m['metadata']['text']}")
Step 5: RAG chatbot with Pinecone
from langchain_openai import ChatOpenAI
from langchain_pinecone import PineconeVectorStore
from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
# Vector store from existing Pinecone index
vectorstore = PineconeVectorStore(index_name="my-docs", embedding=OpenAIEmbeddings())
# Retriever
retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
# Chain
llm = ChatOpenAI(model="gpt-4o-mini")
prompt = ChatPromptTemplate.from_template(
"Answer based on context:\n{context}\n\nQuestion: {input}"
)
chain = create_retrieval_chain(retriever, create_stuff_documents_chain(llm, prompt))
# Chat
if prompt := st.chat_input("Ask"):
response = chain.invoke({"input": prompt})
st.write(response["answer"])
with st.expander("Sources"):
for d in response["context"]:
st.write(d.page_content[:200])
Step 6: Metadata filtering
Pinecone supports filtering by metadata:
results = index.query(
vector=embed(query),
top_k=5,
filter={"source": "intro.md"}, # only search this source
include_metadata=True,
)
Great for multi-tenant apps or per-user document stores.
Step 7: Namespaces (multi-tenancy)
# Upsert to a namespace
index.upsert(vectors=[...], namespace="user-123")
# Query a namespace
results = index.query(vector=embed(query), top_k=5, namespace="user-123")
Namespaces isolate data per user/tenant — essential for SaaS apps.
Step 8: Delete and update
# Delete by ID
index.delete(ids=["doc1"])
# Delete by filter
index.delete(filter={"source": "old.md"})
# Delete all in namespace
index.delete(delete_all=True, namespace="user-123")
Pinecone vs alternatives
| Pinecone | Chroma | Weaviate | Qdrant | |
|---|---|---|---|---|
| Hosting | Managed (serverless) | Self-hosted/embedded | Both | Both |
| Free tier | 100k vectors | Unlimited (local) | 14-day trial | 1GB free |
| Scaling | Auto | Manual | Manual/auto | Manual/auto |
| Filtering | Metadata + namespaces | Metadata | Rich (GraphQL) | Rich |
| Best for | Production, scale | Prototyping, local | Complex queries | Self-hosted, performance |
Rule: Chroma for prototyping, Pinecone for production. Migrate when you need scale.
Common mistakes
- Wrong dimension — must match your embedding model. OpenAI
text-embedding-3-small= 1536. - Not caching the index client —
@st.cache_resourcefor the Pinecone client. - Upserting without IDs — IDs must be unique. Use document IDs or UUIDs.
- Ignoring namespaces — for multi-user apps, always use namespaces.
- Not handling rate limits — Pinecone has rate limits on free tier. Add retries.
Key Concepts
- Pinecone = managed vector database (serverless)
- Index = collection of vectors with a fixed dimension
- Upsert = insert or update vectors
- Query = find nearest neighbors by vector similarity
- Namespace = partition for multi-tenancy
- Metadata filter = narrow search by attributes
What to Try Next
- Build a RAG chatbot with Pinecone as the vector store.
- Add auth for per-user document stores (namespaces).
- Combine with Supabase — store metadata in Postgres, vectors in Pinecone.
- Migrate from Chroma to Pinecone for production.
FAQ
Pinecone free tier limits?
1 index, 100k vectors, 1 namespace (on serverless). Enough for a personal project or small app. Paid plans start at $70/mo for production.
Pinecone vs Chroma for production?
Chroma is great for prototyping and local dev. Pinecone is managed, auto-scaling, and handles millions of vectors. Migrate to Pinecone when you need scale or don’t want to manage infrastructure.
Can I use local embeddings with Pinecone?
Yes — generate embeddings locally (Ollama, sentence-transformers) and upsert the vectors. Pinecone just stores and searches vectors; it doesn’t care how they were generated. The dimension must match.