Skip to main content

LangChain Guide

· 3 min read

LangChain is a framework for developing applications powered by language models. It provides tools and abstractions for building complex LLM applications, with a focus on composability and reusability.

Key Components

  1. Models

    • LLM wrappers (OpenAI, Anthropic, etc.)
    • Chat models
    • Text embedding models
  2. Prompts

    • Template management
    • Few-shot examples
    • Output parsing
  3. Memory

    • Conversation history
    • Vector stores
    • Custom memory types
  4. Chains

    • Sequential operations
    • Conditional logic
    • Error handling
  5. Agents

    • Tool usage
    • Planning
    • Execution

Installation

# Python
pip install langchain
pip install openai # for OpenAI models

# Node.js
npm install langchain
npm install @langchain/openai

Basic Usage

Python Examples

  1. Simple Chain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain

# Initialize LLM
llm = OpenAI(temperature=0.7)

# Create prompt template
prompt = PromptTemplate(
input_variables=["topic"],
template="Write a summary about {topic}."
)

# Create chain
chain = LLMChain(llm=llm, prompt=prompt)

# Run chain
result = chain.run(topic="artificial intelligence")
  1. Chat Models
from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage, SystemMessage

chat = ChatOpenAI()
messages = [
SystemMessage(content="You are a helpful assistant."),
HumanMessage(content="What is LangChain?")
]

response = chat(messages)

Node.js Examples

  1. Simple Chain
import { OpenAI } from '@langchain/openai';
import { PromptTemplate } from '@langchain/core/prompts';
import { LLMChain } from 'langchain/chains';

// Initialize LLM
const llm = new OpenAI({ temperature: 0.7 });

// Create prompt template
const prompt = PromptTemplate.fromTemplate('Write a summary about {topic}.');

// Create chain
const chain = new LLMChain({ llm, prompt });

// Run chain
const result = await chain.call({ topic: 'artificial intelligence' });

Advanced Features

1. RAG Implementation

from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.chains import RetrievalQA

# Create vector store
embeddings = OpenAIEmbeddings()
vectorstore = Chroma(embedding_function=embeddings)

# Split and store documents
text_splitter = CharacterTextSplitter()
docs = text_splitter.split_documents(documents)
vectorstore.add_documents(docs)

# Create QA chain
qa_chain = RetrievalQA.from_chain_type(
llm=OpenAI(),
retriever=vectorstore.as_retriever()
)

# Query
response = qa_chain.run("What is LangChain?")

2. Agents with Tools

from langchain.agents import initialize_agent, Tool
from langchain.tools import DuckDuckGoSearchRun

# Define tools
search = DuckDuckGoSearchRun()
tools = [
Tool(
name="Search",
func=search.run,
description="Search the internet"
)
]

# Create agent
agent = initialize_agent(
tools,
llm,
agent="zero-shot-react-description",
verbose=True
)

# Run agent
agent.run("What is the latest news about AI?")

Best Practices

  1. Prompt Engineering

    • Use clear, specific instructions
    • Include examples when needed
    • Handle edge cases
    • Validate outputs
  2. Chain Design

    • Keep chains modular
    • Implement proper error handling
    • Use appropriate temperature settings
    • Cache results when possible
  3. Memory Management

    • Clear conversation history when needed
    • Use appropriate memory types
    • Handle context length limits
    • Implement token counting
  4. Cost Optimization

    • Monitor API usage
    • Implement caching
    • Use cheaper models when possible
    • Batch requests when appropriate

Common Use Cases

  1. Document Processing

    • Text summarization
    • Information extraction
    • Document Q&A
    • Content generation
  2. Conversational AI

    • Chatbots
    • Virtual assistants
    • Customer support
    • Interactive agents
  3. Data Analysis

    • Data extraction
    • Pattern recognition
    • Report generation
    • Insights discovery
  4. Content Creation

    • Blog post generation
    • Code documentation
    • Technical writing
    • Content translation

Debugging and Testing

  1. Verbose Output
# Enable verbose mode
chain = LLMChain(llm=llm, prompt=prompt, verbose=True)
agent = initialize_agent(tools, llm, verbose=True)
  1. Callbacks
from langchain.callbacks import StdOutCallbackHandler

handler = StdOutCallbackHandler()
chain = LLMChain(
llm=llm,
prompt=prompt,
callbacks=[handler]
)
  1. Error Handling
try:
result = chain.run(input_data)
except Exception as e:
print(f"Error: {str(e)}")

Integration Examples

  1. FastAPI Integration
from fastapi import FastAPI
from langchain.chains import LLMChain

app = FastAPI()
chain = LLMChain(llm=llm, prompt=prompt)

@app.post("/generate")
async def generate_text(data: dict):
try:
result = chain.run(data)
return {"result": result}
except Exception as e:
return {"error": str(e)}
  1. NestJS Integration
@Injectable()
export class LangChainService {
private chain: LLMChain;

constructor() {
const llm = new OpenAI({ temperature: 0.7 });
const prompt = PromptTemplate.fromTemplate(
'Write a summary about {topic}.',
);
this.chain = new LLMChain({ llm, prompt });
}

async generate(input: any) {
try {
return await this.chain.call(input);
} catch (error) {
throw new Error(`LangChain error: ${error.message}`);
}
}
}

Resources