Building RAG Applications with LangChain, Pinecone, and OpenAI: A Complete Tutorial
Step-by-step guide to building production-ready Retrieval Augmented Generation (RAG) applications using LangChain, Pinecone vector database, and OpenAI embeddings.
Retrieval Augmented Generation (RAG) has emerged as the most practical approach for building AI applications that can reason over your own data. In this technical deep-dive, we'll build a production-ready RAG system from scratch.
Why RAG Over Fine-Tuning?
Fine-tuning LLMs is expensive, time-consuming, and the model can still hallucinate. RAG provides real-time access to your data, is more cost-effective, and allows you to update your knowledge base without retraining.
Architecture Overview
Our RAG system consists of: 1) Document ingestion pipeline, 2) Vector embedding generation, 3) Pinecone vector storage, 4) Query processing with semantic search, 5) LLM response generation with retrieved context.
Setting Up the Environment
pip install langchain langchain-openai pinecone-client python-dotenv tiktokenCreate your .env file with: OPENAI_API_KEY, PINECONE_API_KEY, and PINECONE_ENVIRONMENT.
Document Loading and Chunking
The quality of your RAG system depends heavily on how you chunk documents. We use recursive character splitting with overlap:
from langchain.text_splitter import RecursiveCharacterTextSplitter
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
length_function=len,
separators=["\n\n", "\n", " ", ""]
)Creating Embeddings with OpenAI
We use text-embedding-3-small for cost efficiency or text-embedding-3-large for better accuracy:
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(
model="text-embedding-3-small",
dimensions=1536
)Pinecone Vector Store Setup
from pinecone import Pinecone, ServerlessSpec
pc = Pinecone(api_key=os.getenv("PINECONE_API_KEY"))
pc.create_index(
name="rag-index",
dimension=1536,
metric="cosine",
spec=ServerlessSpec(cloud="aws", region="us-east-1")
)Building the Retrieval Chain
from langchain.chains import RetrievalQA
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4-turbo-preview", temperature=0)
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=vectorstore.as_retriever(search_kwargs={"k": 5}),
return_source_documents=True
)Optimizing Retrieval Quality
Key optimizations include: Hybrid search (combining keyword and semantic), re-ranking retrieved documents, metadata filtering, and query expansion using HyDE (Hypothetical Document Embeddings).
Production Considerations
For production deployments, implement: caching for repeated queries, async processing for document ingestion, monitoring with LangSmith, and rate limiting for API calls.
NyxaLabs RAG Implementation Services
We've built RAG systems processing millions of documents for enterprise clients. Our expertise spans document processing, vector optimization, and LLM orchestration. Contact us for your AI project.