LangChain implementation patterns with templates, scripts, and examples for RAG pipelines
/plugin marketplace add vanman2024/ai-dev-marketplace/plugin install rag-pipeline@ai-dev-marketplaceThis skill is limited to using the following tools:
examples/conversational-retrieval.pyexamples/multi-query-retrieval.pyexamples/self-querying-retrieval.pyscripts/create-vectorstore.shscripts/setup-langchain.shscripts/test-langchain.shtemplates/langgraph-workflow.pytemplates/langsmith-integration.pytemplates/rag-chain.pyComprehensive LangChain implementation patterns for building production-ready RAG (Retrieval-Augmented Generation) pipelines. This skill provides working scripts, templates, and examples for document loading, vector stores, retrieval chains, LangGraph workflows, and LangSmith observability.
langchain-patterns/
├── SKILL.md # This file
├── scripts/
│ ├── setup-langchain.sh # Install dependencies
│ ├── create-vectorstore.sh # Vector store setup
│ └── test-langchain.sh # Validation tests
├── templates/
│ ├── rag-chain.py # Basic RAG chain template
│ ├── langgraph-workflow.py # LangGraph agent workflow
│ └── langsmith-integration.py # Observability template
└── examples/
├── conversational-retrieval.py # Conversational RAG
├── multi-query-retrieval.py # Multi-query retrieval
└── self-querying-retrieval.py # Self-querying retriever
Installs LangChain and common dependencies for RAG applications.
Usage:
bash scripts/setup-langchain.sh [--all|--minimal|--vectorstore]
Options:
--all: Install full suite (LangChain, LangGraph, LangSmith, all vector stores)--minimal: Core LangChain only--vectorstore <name>: Install specific vector store (faiss, chroma, pinecone, qdrant)Environment Variables:
OPENAI_API_KEY: OpenAI API keyANTHROPIC_API_KEY: Anthropic API keyLANGSMITH_API_KEY: LangSmith API key (optional)Creates and populates a vector store from documents.
Usage:
bash scripts/create-vectorstore.sh <store_type> <documents_path> <output_path>
Parameters:
store_type: Vector store type (faiss, chroma, pinecone, qdrant)documents_path: Path to documents directory or fileoutput_path: Where to save the vector storeEnvironment Variables:
EMBEDDING_MODEL: Embedding model to use (default: text-embedding-3-small)CHUNK_SIZE: Text chunk size (default: 1000)CHUNK_OVERLAP: Chunk overlap (default: 200)Validates LangChain installation and configuration.
Usage:
bash scripts/test-langchain.sh [--verbose]
Tests:
Basic RAG chain template with document loading, vector store, and retrieval.
Features:
Usage:
from templates.rag_chain import RAGChain
# Initialize RAG chain
rag = RAGChain(
documents_path="./docs",
vectorstore_path="./vectorstore",
chunk_size=1000,
chunk_overlap=200
)
# Load and index documents
rag.load_documents()
# Query
result = rag.query("What are the main features?")
print(result)
LangGraph workflow template for multi-step agent orchestration.
Features:
Usage:
from templates.langgraph_workflow import create_workflow
# Create workflow
workflow = create_workflow(
llm=llm,
tools=tools,
checkpointer=MemorySaver()
)
# Execute
result = workflow.invoke({
"messages": [HumanMessage(content="Analyze this document")]
})
LangSmith integration template for tracing and evaluation.
Features:
Usage:
from templates.langsmith_integration import LangSmithTracer
# Initialize tracer
tracer = LangSmithTracer(
project_name="rag-pipeline",
tags=["production", "v1"]
)
# Trace chain execution
with tracer.trace("rag-query"):
result = chain.invoke({"query": "..."})
Complete conversational retrieval system with memory.
Features:
Run:
python examples/conversational-retrieval.py --docs ./docs --query "Tell me about RAG"
Multi-query retrieval for better coverage.
Features:
Run:
python examples/multi-query-retrieval.py --docs ./docs --query "What is LangChain?"
Self-querying retriever with metadata filtering.
Features:
Run:
python examples/self-querying-retrieval.py --docs ./docs --query "Recent papers about transformers"
from langchain_community.document_loaders import DirectoryLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_openai import ChatOpenAI
from langchain.chains import RetrievalQA
# Load documents
loader = DirectoryLoader("./docs", glob="**/*.pdf")
documents = loader.load()
# Split
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
chunks = splitter.split_documents(documents)
# Embed and store
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
vectorstore = FAISS.from_documents(chunks, embeddings)
# Create chain
llm = ChatOpenAI(model="gpt-4", temperature=0)
chain = RetrievalQA.from_chain_type(
llm=llm,
retriever=vectorstore.as_retriever()
)
# Query
result = chain.invoke({"query": "What is RAG?"})
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(
memory_key="chat_history",
return_messages=True
)
chain = ConversationalRetrievalChain.from_llm(
llm=llm,
retriever=vectorstore.as_retriever(),
memory=memory
)
# Chat
result1 = chain({"question": "What is LangChain?"})
result2 = chain({"question": "How do I use it?"}) # Uses context
from langgraph.graph import StateGraph, END
from typing import TypedDict, Annotated
class AgentState(TypedDict):
messages: Annotated[list, "messages"]
documents: list
def retrieve(state):
docs = vectorstore.similarity_search(state["messages"][-1])
return {"documents": docs}
def generate(state):
response = llm.invoke(state["messages"] + state["documents"])
return {"messages": state["messages"] + [response]}
workflow = StateGraph(AgentState)
workflow.add_node("retrieve", retrieve)
workflow.add_node("generate", generate)
workflow.set_entry_point("retrieve")
workflow.add_edge("retrieve", "generate")
workflow.add_edge("generate", END)
app = workflow.compile()
| Store | Local | Cloud | Best For |
|---|---|---|---|
| FAISS | ✓ | ✗ | Development, single-machine |
| Chroma | ✓ | ✓ | Local development, small datasets |
| Pinecone | ✗ | ✓ | Production, large scale |
| Qdrant | ✓ | ✓ | Hybrid, metadata filtering |
# Use langchain_community for loaders and stores
from langchain_community.document_loaders import PDFLoader
from langchain_community.vectorstores import FAISS
# Use langchain_openai for OpenAI integrations
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
export OPENAI_API_KEY="sk-..."
export ANTHROPIC_API_KEY="sk-ant-..."
export LANGSMITH_API_KEY="lsv2_pt_..." # Optional
# Save
vectorstore.save_local("./my_vectorstore")
# Load
vectorstore = FAISS.load_local(
"./my_vectorstore",
embeddings,
allow_dangerous_deserialization=True # Only if you trust the source
)
When using this skill:
setup-langchain.sh to install dependenciescreate-vectorstore.sh or use templatetest-langchain.sh to validateThis skill provides everything needed to build production-ready RAG applications with LangChain, from basic retrieval to advanced agent workflows with full observability.
Use when working with Payload CMS projects (payload.config.ts, collections, fields, hooks, access control, Payload API). Use when debugging validation errors, security issues, relationship queries, transactions, or hook behavior.