How to use DSPy with Pinecone
Quick answer
Use
dspy to define your AI tasks declaratively and combine it with pinecone for vector storage and retrieval by initializing both clients and implementing a retrieval function that queries Pinecone vectors within your dspy workflow. This enables seamless integration of LLM predictions with Pinecone's vector search capabilities.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)Pinecone API keypip install dspy pinecone-client openai
Setup
Install the required packages and set environment variables for your OpenAI and Pinecone API keys.
- Install packages:
pip install dspy pinecone-client openai - Set environment variables:
OPENAI_API_KEYandPINECONE_API_KEY
pip install dspy pinecone-client openai Step by step
This example shows how to define a simple dspy signature for question answering, initialize Pinecone to store and query embeddings, and integrate Pinecone retrieval into the dspy prediction workflow.
import os
import dspy
from openai import OpenAI
import pinecone
# Initialize OpenAI client for DSPy
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Configure DSPy to use OpenAI
lm = dspy.LM("openai/gpt-4o-mini", api_key=os.environ["OPENAI_API_KEY"])
dspy.configure(lm=lm)
# Initialize Pinecone client
import pinecone
pc = pinecone.Pinecone(api_key=os.environ["PINECONE_API_KEY"])
index_name = "example-index"
# Create or connect to Pinecone index
if index_name not in pc.list_indexes():
pc.create_index(index_name, dimension=1536) # dimension must match embedding size
index = pc.Index(index_name)
# Define DSPy signature
class QA(dspy.Signature):
question: str = dspy.InputField()
answer: str = dspy.OutputField()
# Define a retrieval function using Pinecone
def embed_text(text: str) -> list[float]:
response = client.embeddings.create(model="text-embedding-3-small", input=text)
return response.data[0].embedding
# Define a DSPy predict function that uses Pinecone retrieval
@dspy.Predict(QA)
def qa_with_retrieval(question: str) -> str:
# Embed the question
q_vector = embed_text(question)
# Query Pinecone for top 3 relevant documents
results = index.query(vector=q_vector, top_k=3, include_metadata=True)
# Combine retrieved texts
context = "\n".join([match['metadata']['text'] for match in results['matches']])
# Prompt with context
prompt = f"Context:\n{context}\n\nQuestion: {question}\nAnswer:"
# Use DSPy LM to generate answer
answer = lm(prompt)
return answer
# Example usage
if __name__ == "__main__":
# Example: Upsert some documents to Pinecone
docs = [
("doc1", embed_text("Python is a programming language."), {"text": "Python is a programming language."}),
("doc2", embed_text("Pinecone is a vector database."), {"text": "Pinecone is a vector database."}),
("doc3", embed_text("DSPy is a declarative LLM programming library."), {"text": "DSPy is a declarative LLM programming library."})
]
index.upsert(items=docs)
# Run the QA
result = qa_with_retrieval(question="What is Pinecone?")
print("Answer:", result) output
Answer: Pinecone is a vector database.
Common variations
You can adapt this integration by:
- Using async DSPy calls with
async defandawaitfor embedding and LM calls. - Changing the embedding model to a different OpenAI or custom model.
- Using other Pinecone index configurations or namespaces for multi-tenant setups.
- Integrating DSPy with other vector databases by replacing the Pinecone client calls.
Troubleshooting
If you see errors like Index does not exist, ensure you have created the Pinecone index with the correct dimension matching your embedding model.
If embeddings are empty or incorrect, verify your OpenAI API key and model name.
For connection issues, check your Pinecone environment and API key validity.
Key Takeaways
- Use
dspyto declaratively define AI tasks and integrate with Pinecone for vector search. - Initialize both OpenAI and Pinecone clients with environment API keys for secure access.
- Embed queries with OpenAI embeddings and retrieve relevant vectors from Pinecone before LLM generation.
- Adjust embedding dimensions and Pinecone index settings to match your use case.
- Handle common errors by verifying index existence, API keys, and environment configurations.