LangChain
RAG Pipeline with LangChain
Build a Retrieval-Augmented Generation pipeline that searches documents with vector embeddings and answers questions with citations — using LangChain.
RAGvector storeembeddingsdocuments
Working Code
from langchain_openai import ChatOpenAIfrom langchain_core.tools import tool
@tooldef search_documents(query: str, top_k: int = 5) -> str: """Search uploaded documents for relevant content.""" results = vector_store.similarity_search(query, k=top_k) return "\n\n".join( f"[{i+1}] {r.metadata.get('source', 'unknown')}: {r.page_content}" for i, r in enumerate(results) )
model = ChatOpenAI(model="gpt-4o")model_with_tools = model.bind_tools([search_documents])response = model_with_tools.invoke([ ("system", "You are a document Q&A assistant. Search documents before answering. Cite sources using [1], [2] format."), ("user", "What does the refund policy say about digital products?"),])Step by Step
1
Install dependencies
Install LangChain and the required tools for this use case.
2
Define your tools
Create the domain-specific tool functions your agent will use to interact with external services.
3
Create the agent and run
Initialize the LangChain agent with your tools, set the system prompt, and execute a query.
Build with other frameworks
Ready to build with LangChain?
Generate a production-ready project with LangChain pre-configured — FastAPI + Next.js, auth, streaming, and more.
Get StartedReady to build your first production AI agent?
Open-source tools, battle-tested patterns, zero boilerplate. Configure your stack and ship in minutes — not months.