Welcome to the LangChain Crash Course repository! This repo contains all the code examples you'll need to follow along with the LangChain Master Class for Beginners video. By the end of this course, you'll know how to use LangChain to create your own AI agents, build RAG chatbots, and automate tasks with AI.
LangChain is a framework that simplifies building applications powered by large language models (LLMs). It provides a standardized interface for chains, prompt templates, tools, agents, and memories, making LLM-powered applications more modular and easier to develop.
graph TD
A[LangChain Framework] --> B[Models]
A --> C[Prompt Templates]
A --> D[Chains]
A --> E[Agents]
A --> F[Memory]
A --> G[Tools]
A --> H[Retrievers]
B --> B1[OpenAI]
B --> B2[Anthropic]
B --> B3[Google]
B --> B4[Ollama/Local]
B --> B5[Hugging Face]
E --> G
E --> H
- Setup Environment: Configure your local environment to work with LangChain
- Chat Models: Learn to interact with different LLMs through a unified interface
- Prompt Templates: Master structured prompting techniques
- Chains: Combine models and prompts into reusable pipelines
- RAG (Retrieval-Augmented Generation): Enhance LLM responses with external knowledge
- Agents & Tools: Create autonomous AI systems that can use tools to accomplish tasks
- Python 3.11, 3.12, or 3.13
- Poetry (Follow this Poetry installation tutorial to install Poetry on your system)
- (Optional) Ollama for local LLM inference (Download Ollama)
-
Clone the repository:
git clone https://github.com/bibekgupta3333/langchain-practical-guide cd langchain-practical-guide -
Install dependencies using Poetry:
poetry install
Note: With Poetry 2.0+, the
package-mode = falsesetting inpyproject.tomlmeans--no-rootis no longer needed. -
Set up your environment variables:
- Rename the
.env.examplefile to.envand update the variables inside with your own values. Example:
mv .env.example .env
- Rename the
-
Activate the Poetry virtual environment:
Poetry 2.0+ (Recommended):
poetry env activate
Or manually activate:
source $(poetry env info --path)/bin/activate
Or run directly without activating:
poetry run python 1_chat_models/1_chat_model_basic.py
-
Run the code examples:
python 1_chat_models/1_chat_model_basic.py
If you want to use local LLMs (free, private, offline):
-
Install Ollama from ollama.ai
-
Pull a model:
# For chat models ollama pull llama3.2 ollama pull mistral ollama pull gemma2 # For embeddings ollama pull nomic-embed-text
-
Verify Ollama is running:
ollama list
-
Use in your code:
from langchain_ollama import ChatOllama model = ChatOllama(model="llama3.2")
This repository has been updated to LangChain 1.0+ with several important changes:
- LangChain 1.0+: Upgraded from 0.3.x to 1.0.x with updated import paths
- Ollama Support: Added support for local LLM inference using Ollama
- LangGraph: Integrated for building stateful, multi-actor applications
- LangSmith: Added for observability and debugging
- Python 3.13: Extended Python version support (3.11-3.13)
If you're upgrading from an older version, note these import path changes:
# OLD (LangChain 0.3.x)
from langchain.schema import AIMessage, HumanMessage
from langchain.prompts import ChatPromptTemplate
from langchain.text_splitter import CharacterTextSplitter
from langchain.schema.runnable import RunnableLambda
from langchain.pydantic_v1 import BaseModel
# NEW (LangChain 1.0+)
from langchain_core.messages import AIMessage, HumanMessage
from langchain_core.prompts import ChatPromptTemplate
from langchain_text_splitters import CharacterTextSplitter
from langchain_core.runnables import RunnableLambda
from pydantic import BaseModel
# Chain utilities moved to langchain_classic
from langchain_classic.chains import create_history_aware_retriever, create_retrieval_chain
from langchain_classic.chains.combine_documents import create_stuff_documents_chain- Ollama: Local LLM inference (ChatOllama, OllamaEmbeddings)
- LangChain Ollama: Official LangChain integration for Ollama
- Hugging Face: Enhanced integration via langchain-huggingface
- langchain-google-firestore: Removed due to incompatibility with LangChain 1.0+
- The example file
5_chat_model_save_message_history_firebase.pymay need updates
- The example file
All dependencies have been updated to their latest compatible versions:
langchain: 1.0.7langchain-core: 1.0.5langchain-community: 0.4.1langchain-openai: 1.0.3langchain-anthropic: 1.0.4langchain-google-genai: 3.0.3langgraph: 1.0.3langsmith: 0.4.43langchain-ollama: 1.0.0langchain-huggingface: 1.0.1
Import Error: "cannot import X from langchain.Y"
- Solution: Check the import path changes above. Most imports moved to
langchain_core.*
"Module langchain.pydantic_v1 not found"
- Solution: Use
from pydantic import BaseModel, Fieldinstead
"Module langchain.schema not found"
- Solution: Use
from langchain_core.messages import *instead
Poetry install fails with dependency conflicts
- Solution: Delete
poetry.lockand runpoetry installagain - Or use
poetry updateto update all dependencies
Examples using Google Firestore don't work
- Note:
langchain-google-firestoreis incompatible with LangChain 1.0+ - Alternative: Use other memory providers or wait for an updated version
All example files have been updated with the new import paths and Ollama integration:
Chat Models (1_chat_models/):
1_chat_model_basic.py- Updated to use Ollama2_chat_model_basic_conversation.py- Fixed import paths4_chat_model_conversation_with_user.py- Updated tolangchain_core.messages
Prompt Templates (2_prompt_templates/):
2_prompt_template_with_chat_model.py- Updated tolangchain_core.promptsand Ollama
Chains (3_chains/):
1_chains_basics.py- Updated to Ollama2_chains_under_the_hood.py- Updated to Ollama3_chains_extended.py- Fixedlangchain_core.runnablesimports4_chains_parallel.py- Updated to Ollama5_chains_branching.py- Updated to Ollama
RAG (4_rag/):
1a_rag_basics.py- Updated tolangchain_text_splittersandOllamaEmbeddings1b_rag_basics.py- Updated embeddings to Ollama2a_rag_basics_metadata.py- Updated to Ollama embeddings2b_rag_basics_metadata.py- Updated to Ollama embeddings3_rag_text_splitting_deep_dive.py- Fixed text splitter imports4_rag_embedding_deep_dive.py- Updated embeddings5_rag_retriever_deep_dive.py- Updated retriever imports6_rag_one_off_question.py- Updated tolangchain_classic.chains7_rag_conversational.py- Fixed chain imports tolangchain_classic8_rag_web_scrape_basic.py- Updated embeddings8_rag_web_scrape_firecrawl.py- Updated to Ollama
Agents & Tools (5_agents_and_tools/):
1_agent_and_tools_basics.py- Updated tolangchain_classic.agentsagent_deep_dive/1_agent_react_chat.py- Fixed agent importsagent_deep_dive/2_agent_react_docstore.py- Updated tolangchain_classictools_deep_dive/1_tool_constructor.py- Fixed pydantic importstools_deep_dive/3_tool_base_tool.py- Updated tool imports
Configuration:
.env.example- Added Ollama configuration optionspyproject.toml- Updated to LangChain 1.0+ dependenciespoetry.lock- Regenerated with new dependency versions
Here's a breakdown of the folders and what you'll find in each:
graph LR
A[Chat Models] --> B[Basic Usage]
A --> C[Conversations]
A --> D[Alternative Models]
A --> E[User Interaction]
A --> F[Message History Storage]
A --> G[Advanced Usage]
1_chat_model_basic.py: Introduction to calling LLMs2_chat_model_basic_conversation.py: Managing multi-turn conversations3_chat_model_alternatives.py: Exploring different providers (OpenAI, Anthropic, Google)4_chat_model_conversation_with_user.py: Interactive chat applications5_chat_model_save_message_history_firestore.py: Persisting conversations to databasesadvance/:6_chat_model_streaming.py: Implementing streaming responses for real-time interactions7_chat_model_parameters.py: Exploring model parameters to control response generation8_chat_model_multimodal.py: Working with multimodal models that can process text and images9_chat_model_error_handling.py: Implementing robust error handling in LLM applications
Learn how to interact with models like ChatGPT, Claude, and Gemini through a unified interface.
graph TD
A[Prompt Templates] --> B[Template Creation]
A --> C[Variable Substitution]
A --> D[Integration with Chat Models]
A --> E[Few-shot Examples]
1_prompt_template_basic.py: Creating reusable prompt structures2_prompt_template_with_chat_model.py: Combining templates with chat models
Understand how to systematically design prompts with variables that can be filled in at runtime.
graph LR
A[Chains] --> B[Sequential Processing]
A --> C[Internal Mechanics]
A --> D[Advanced Patterns]
A --> E[Parallel Execution]
A --> F[Conditional Branching]
B --> C
C --> D
D --> E
E --> F
1_chains_basics.py: Fundamentals of chaining operations2_chains_under_the_hood.py: Understanding the internal mechanisms3_chains_extended.py: Advanced chain patterns4_chains_parallel.py: Running multiple operations simultaneously5_chains_branching.py: Creating chains with decision points
Learn how to compose complex workflows by connecting models, prompts, and other components in sequence.
flowchart TD
A[Document Loading] --> B[Text Splitting]
B --> C[Embedding Generation]
C --> D[Vector Storage]
D --> E[Similarity Search]
E --> F[Context Augmentation]
F --> G[LLM Response]
H[User Query] --> E
1a_rag_basics.py&1b_rag_basics.py: Core RAG concepts and implementation2a_rag_basics_metadata.py&2b_rag_basics_metadata.py: Using metadata to enhance retrieval3_rag_text_splitting_deep_dive.py: Advanced document chunking strategies4_rag_embedding_deep_dive.py: Understanding vector embeddings5_rag_retriever_deep_dive.py: Fine-tuning retrieval mechanisms6_rag_one_off_question.py: Single-query RAG implementation7_rag_conversational.py: Chat systems with context retention8_rag_web_scrape_firecrawl.py&8_rag_web_scrape.py: Building knowledge bases from web content
Explore how to enhance LLM responses by incorporating relevant information from external documents, websites, or databases.
graph TD
A[Agent] --> B[Reasoning]
A --> C[Tool Selection]
A --> D[Action Execution]
A --> E[Result Analysis]
B --> C
C --> D
D --> E
E --> B
F[Available Tools] --> C
1_agent_and_tools_basics.py: Introduction to autonomous AI systemsagent_deep_dive/:1_agent_react_chat.py: ReAct pattern for reasoning with chat models2_react_docstore.py: Agents that can search document collections
tools_deep_dive/:1_tool_constructor.py: Creating tools with the constructor approach2_tool_decorator.py: Using decorators to define tools3_tool_base_tool.py: Building tools by extending the BaseTool class
Learn how to create systems that can reason about what actions to take, use tools to gather information or perform tasks, and make decisions based on outcomes.
-
Watch the Video: Start by watching the LangChain Master Class for Beginners video on YouTube at 2X speed for a high-level overview.
-
Run the Code Examples: Follow along with the code examples provided in this repository. Each section in the video corresponds to a folder in this repo.
-
Experiment and Adapt: Modify the examples to explore different parameters, models, or use cases relevant to your projects.
graph LR
A[Basics: Chat Models] --> B[Prompt Engineering]
B --> C[Building Chains]
C --> D[RAG Implementation]
D --> E[Agents & Tools]
E --> F[Complete Applications]
For optimal learning, follow the numbered folders in sequence, as each builds upon concepts introduced in the previous sections.
Each script in this repository contains detailed comments explaining the purpose and functionality of the code. This will help you understand the flow and logic behind each example.
Q: What is LangChain?
A: LangChain is a framework designed to simplify the process of building applications that utilize language models. It provides abstractions for working with LLMs, prompts, chains, agents, memory systems, and tools.
Q: How do I set up my environment?
A: Follow the instructions in the "Getting Started" section above. Ensure you have Python 3.11 installed, install Poetry, clone the repository, install dependencies, rename the .env.example file to .env, and activate the Poetry shell.
Q: Which LLM provider should I use?
A: The examples work with multiple providers:
- OpenAI (GPT-4, GPT-3.5): Best for general-purpose tasks
- Anthropic (Claude): Excellent for reasoning and analysis
- Google (Gemini): Strong multimodal capabilities
- Ollama (Local): Free, private, offline-capable (recommended: llama3, mistral, gemma2)
Choose based on your needs for cost, capabilities, privacy, and whether you need local/offline inference.
Q: I am getting an error when running the examples. What should I do?
A: Ensure all dependencies are installed correctly and your environment variables are set up properly. If the issue persists, seek help in the Skool community or open an issue on GitHub.
Q: Can I contribute to this repository?
A: Yes! Contributions are welcome. Please open an issue or submit a pull request with your changes.
Q: Where can I find more information about LangChain?
A: Check out the official LangChain documentation and join the Skool community for additional resources and support.
This project is licensed under the MIT License.
Click to view references
- LangChain Documentation - Official documentation
- LangChain GitHub Repository - Source code and examples
- OpenAI API Documentation - API reference for OpenAI models
- Anthropic API Documentation - API reference for Claude models
- Google AI Documentation - Documentation for Google AI services
- Brandon Hancock's GitHub - Gen AI course creator