AI Model Wiki

    Skip to main content
    45 min
    Lesson 4 of 5

    LangChain Essentials

    From the course Python AI & ML Libraries

    Building AI Applications with LangChain

    Core Components

    from langchain_openai import ChatOpenAI
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_core.output_parsers import StrOutputParser
    
    # Create a chain
    prompt = ChatPromptTemplate.from_messages([
        ("system", "You are a helpful assistant specialized in {topic}."),
        ("user", "{question}"),
    ])
    
    model = ChatOpenAI(model="gpt-4o-mini")
    chain = prompt | model | StrOutputParser()
    
    result = chain.invoke({"topic": "Python", "question": "How do decorators work?"})
    

    RAG Chain

    from langchain_community.vectorstores import Chroma
    from langchain_openai import OpenAIEmbeddings
    from langchain_core.runnables import RunnablePassthrough
    
    vectorstore = Chroma.from_documents(documents, OpenAIEmbeddings())
    retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
    
    rag_chain = (
        {"context": retriever, "question": RunnablePassthrough()}
        | prompt
        | model
        | StrOutputParser()
    )
    
    answer = rag_chain.invoke("What is our refund policy?")
    

    Tools & Agents

    from langchain.agents import create_tool_calling_agent, AgentExecutor
    from langchain_community.tools import DuckDuckGoSearchRun
    
    tools = [DuckDuckGoSearchRun()]
    agent = create_tool_calling_agent(model, tools, prompt)
    executor = AgentExecutor(agent=agent, tools=tools)
    
    result = executor.invoke({"input": "What's the latest news about AI?"})
    

    When to Use LangChain

    • Complex chains with multiple steps
    • RAG pipelines with retrieval and generation
    • Agent workflows with tool integration
    • Prototyping and experimentation (swap components easily)

    Common Pitfall: Selecting Too Many Retrieval Results

    Passing 20 documents as context to an LLM is almost always worse than passing 5. Larger contexts dilute relevance — the model pays equal attention to marginally related documents and the most relevant one. Start with k=3 to k=5 and only increase if evaluation shows improved answer quality:

    retriever = vectorstore.as_retriever(
        search_type="mmr",           # Maximum Marginal Relevance avoids redundant results
        search_kwargs={"k": 4, "fetch_k": 20},
    )
    

    MMR (Maximum Marginal Relevance) is a retrieval strategy that balances relevance with diversity, preventing the retriever from returning five near-identical document chunks.

    Best Practice: Use LCEL Over Legacy Chains

    LangChain's original chain classes (LLMChain, RetrievalQA) are being deprecated in favour of LCEL — the pipe (|) syntax shown earlier in this lesson. LCEL chains are composable, support streaming natively, and produce better error messages. When reading LangChain tutorials, check the date — anything before 2024 likely uses the old chain API.

    Named Product Reference: LangSmith for Observability

    LangChain's observability platform, LangSmith, provides tracing and evaluation for LangChain pipelines. Set LANGCHAIN_TRACING_V2=true and LANGCHAIN_API_KEY as environment variables and every chain invocation is automatically traced — you can inspect which retrieval results were passed to the model and the full prompt. This is invaluable for debugging RAG quality issues in production.

    Key Takeaways

    • •LangChain chains connect prompts, models, and parsers with the pipe operator
    • •RAG chains combine retrieval and generation in a clean interface
    • •Tool-calling agents automate web search and API interactions
    • •LangChain excels at prototyping and complex multi-step workflows
    0 of 5 lessons marked complete

    Progress is kept in this browser only.