Skip to content

A collection of simple, practical examples to learn the basics of LangChain. This repo covers setup, memory management, chaining operations, structured output parsing, and retrieval-based question answering. Perfect for developers starting with LangChain who want to grasp its core concepts and functionalities.

License

Notifications You must be signed in to change notification settings

Nicolasfmc/langchain_basics

Repository files navigation

LangChain Basics

A collection of simple, practical examples demonstrating basic LangChain methods, functions, and concepts. This repository serves as a learning resource for developers getting started with LangChain.

🚀 Getting Started

Prerequisites

  • Python 3.8 or higher
  • pip (Python package installer)

Setting Up Your Environment

  1. Clone the repository:

    git clone https://github.com/nicolasfmc/langchain_basics.git
    cd langchain_basics
  2. Create a virtual environment:

    # Create virtual environment
    python -m venv venv
    
    # Activate virtual environment
    # On Windows:
    venv\Scripts\activate
    
    # On macOS/Linux:
    source venv/bin/activate
  3. Install dependencies:

    pip install -r requirements.txt
  4. Set up environment variables:

    Create a .env file in the root directory and add your API keys:

    OPENAI_API_KEY=your_openai_api_key_here
    # Add other API keys as needed

📚 Examples (Ordered by Complexity)

Beginner Level

  1. Basic - Introduction to basics
  2. Simple Sequential Chain - Basic sequential chain implementation

Intermediate Level

  1. Conversation Buffer Memory - Basic memory management for conversations
  2. Conversation Buffer Window Memory - Windowed memory for chat history
  3. Conversation Summary Memory - Summarized conversation memory
  4. LCEL Basic - The basic of LangChain Expression Language (LCEL)
  5. LCEL Join Chains - LangChain Expression Language (LCEL) + Join chains
  6. Conversation Buffer Custom Chain - Custom chain with buffer memory
  7. JSON Output Parser - Parsing structured JSON outputs
  8. Retrieval QA Tasks - Document retrieval and Q&A
  9. Retrieval QA - Intermediate retrieval-based question answering

🛠️ What You'll Learn

  • Basic LangChain Setup: Environment configuration and basic usage
  • Memory Management: Different types of conversation memory (buffer, window, summary)
  • Chain Operations: Sequential chains and custom chain implementations
  • Output Parsing: Structured data extraction and JSON parsing
  • Retrieval Systems: Document-based question answering and retrieval-augmented generation (RAG)
  • Advanced Patterns: Custom chains and complex workflow implementations

📖 Usage

Each Python file in this repository is a standalone example. To run any example:

python filename.py

For example:

python simple_sequential_chain.py

🔧 Virtual Environment Management

Why Use Virtual Environments?

Virtual environments help you:

  • Keep project dependencies isolated
  • Avoid conflicts between different projects
  • Maintain consistent development environments
  • Easy dependency management

Managing Your Virtual Environment

Activate the environment (do this every time you work on the project):

# Windows
venv\Scripts\activate

# macOS/Linux
source venv/bin/activate

Deactivate when done:

deactivate

Update requirements.txt when you add new packages:

pip freeze > requirements.txt

📦 Dependencies

The requirements.txt file contains all necessary dependencies. Key packages include:

  • langchain - Core LangChain library
  • openai - OpenAI API integration
  • python-dotenv - Environment variable management
  • Additional utilities for specific examples

🤝 Contributing

Feel free to contribute by:

  1. Adding new examples
  2. Improving existing code
  3. Adding documentation
  4. Reporting issues

📄 License

This project is open source and available under the MIT License.

🆘 Troubleshooting

Common Issues:

  1. Import errors: Make sure your virtual environment is activated and dependencies are installed
  2. API key errors: Verify your .env file is properly configured
  3. Module not found: Check that you're running commands from the project root directory

Getting Help:

  • Check the official LangChain documentation
  • Review individual file comments for specific usage instructions
  • Open an issue in this repository for bug reports or questions

Happy coding! 🚀

About

A collection of simple, practical examples to learn the basics of LangChain. This repo covers setup, memory management, chaining operations, structured output parsing, and retrieval-based question answering. Perfect for developers starting with LangChain who want to grasp its core concepts and functionalities.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages