Skip to content

Educational project for building a RAG app LLaMA, via LangChain

License

Notifications You must be signed in to change notification settings

nluninja/langchain-101

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

LangChain Search Agents πŸ”πŸ€–

License: MIT Python 3.8+ LangChain

A collection of intelligent search agents that connect various search engines with Large Language Models (LLMs) using LangChain. These agents can search the web, process results, and provide accurate, context-aware answers to your questions.

🌟 Features

  • 9 Complete Tutorials: From basics to advanced topics
  • Multiple Search Providers: Google Custom Search, DuckDuckGo, Wikipedia
  • Multi-Tool Agents: Combine search, calculator, and more
  • Conversation Memory: Build context-aware chatbots
  • RAG (Retrieval-Augmented Generation): Query your documents
  • Data Analysis: Analyze CSV and SQL databases with natural language
  • Custom Tools: Build your own agent capabilities
  • Streaming Responses: Real-time token-by-token output
  • LLM Integration: Support for OpenAI GPT, Llama, Claude, and other models
  • Environment-based Configuration: Secure API key management
  • Production Ready: Best practices, error handling, type hints

πŸ“š Notebooks & Tutorials

All notebooks are available in the notebooks/ directory and can be run in Google Colab:

# Notebook Description Difficulty Colab
01 Google Search Agent Search with Google Custom Search API ⭐ Beginner Open In Colab
02 DuckDuckGo Search Agent Free search, no API keys! ⭐ Beginner Open In Colab
03 Multi-Tool Agent Search + Calculator + Wikipedia ⭐⭐ Intermediate Open In Colab
04 Conversational Agent Chatbot with memory ⭐⭐ Intermediate Open In Colab
05 RAG Document Q&A Ask questions about your documents ⭐⭐⭐ Advanced Open In Colab
06 Custom Tool Agent Build your own tools ⭐⭐ Intermediate Open In Colab
07 CSV Data Agent Analyze CSV files with natural language ⭐⭐ Intermediate Open In Colab
08 SQL Database Agent Query SQL databases naturally ⭐⭐⭐ Advanced Open In Colab
09 Streaming Agent Real-time streaming responses ⭐⭐ Intermediate Open In Colab

Quick Comparison

Agent Type API Keys Needed Best For Complexity
DuckDuckGo Search LLM only Quick start, learning Low
Google Search LLM + Google Production, quality results Low
Multi-Tool LLM only Versatile Q&A Medium
Conversational LLM only Chatbots, follow-ups Medium
RAG Document LLM only Document analysis High
Custom Tool LLM only Extensibility Medium
CSV Analysis LLM only Data analysis Medium
SQL Database LLM only Business intelligence High
Streaming LLM only Better UX Medium

πŸ“‹ Table of Contents

πŸš€ Installation

Prerequisites

  • Python 3.8 or higher
  • pip package manager

Local Installation

  1. Clone the repository:
git clone https://github.com/nluninja/langchain_agents.git
cd langchain_agents
  1. Create a virtual environment (recommended):
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt

Google Colab

Click the badge in the notebook to open directly in Colab:

Open In Colab

βš™οΈ Configuration

  1. Copy the example environment file:
cp .env.example .env
  1. Edit .env and add your API keys:
# Required: Choose your LLM provider
OPENAI_API_KEY=sk-...
# OR
GROQ_API_KEY=gsk_...
# OR
ANTHROPIC_API_KEY=sk-ant-...

# Required: For Google Custom Search
GOOGLE_API_KEY=AIza...
GOOGLE_CSE_ID=0123456789...

Important: Never commit your .env file to version control!

🎯 Quick Start

Using the Notebook

  1. Open LangChain_GoogleSearch_Llama33.ipynb in Jupyter or Google Colab
  2. Add your API keys to the environment variables
  3. Run all cells
  4. Enter your question when prompted

Python Script Example

from dotenv import load_dotenv
from langchain_agents import SearchAgent

# Load environment variables
load_dotenv()

# Create agent
agent = SearchAgent()

# Ask a question
response = agent.ask("What are the latest developments in AI?")
print(response)

πŸ“š Usage Examples

Basic Question Answering

# Simple factual question
agent.ask("What is the capital of France?")

# Current events
agent.ask("What are today's top news stories?")

# Research question
agent.ask("How does photosynthesis work?")

Advanced Usage

# Use specific search provider
agent = SearchAgent(search_provider="duckduckgo")

# Customize model parameters
agent = SearchAgent(
    model="gpt-4",
    temperature=0.7,
    max_tokens=500
)

# Enable verbose mode for debugging
agent = SearchAgent(verbose=True)

πŸ”‘ API Key Setup

Google Custom Search API

  1. Go to Google Cloud Console
  2. Create a new project or select existing one
  3. Enable "Custom Search API"
  4. Create credentials (API key)
  5. Set up a Custom Search Engine
  6. Copy your API key and Search Engine ID to .env

Free tier: 100 queries per day

OpenAI API

  1. Visit OpenAI Platform
  2. Create an account and add payment method
  3. Generate an API key from the API keys page
  4. Copy to .env as OPENAI_API_KEY

Pricing: Pay per token used

Groq API (Free Llama models)

  1. Visit Groq Console
  2. Sign up for free account
  3. Generate an API key
  4. Copy to .env as GROQ_API_KEY

Free tier: Generous rate limits for Llama models

DuckDuckGo (No API Key Required)

DuckDuckGo search requires no API key! Just install:

pip install duckduckgo-search

πŸ’» Python Examples

The examples/ directory contains standalone Python scripts for common use cases:

basic_search.py

Simple search agent using DuckDuckGo (no API keys required for search):

python examples/basic_search.py

multi_tool_demo.py

Agent with multiple tools (search, calculator, Wikipedia):

python examples/multi_tool_demo.py

conversational_bot.py

Interactive chatbot with conversation memory:

python examples/conversational_bot.py

All examples use .env file for API keys - see Configuration for setup.

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    User     β”‚
β”‚   Question  β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  LangChain  β”‚
β”‚    Agent    β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
       β–Ό             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚Search Tool  β”‚  β”‚   LLM    β”‚
β”‚ (Google/    β”‚  β”‚(GPT/     β”‚
β”‚ DuckDuckGo) β”‚  β”‚Llama/    β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜  β”‚Claude)   β”‚
       β”‚         β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
       β–Ό              β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”‚
β”‚   Search    β”‚      β”‚
β”‚   Results   β”‚      β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜      β”‚
       β”‚             β”‚
       β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
              β–Ό
       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
       β”‚   Answer    β”‚
       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Components

  • LangChain Agent: Orchestrates the workflow and decides when to search
  • Search Tool: Queries search engines and retrieves relevant information
  • LLM: Processes search results and generates natural language answers
  • Prompt Template: Structures the context and question for the LLM

πŸ› οΈ Development

Running Tests

pip install -r requirements-dev.txt
pytest tests/

Code Formatting

black .
isort .
flake8 .

Pre-commit Hooks

pre-commit install
pre-commit run --all-files

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

See CONTRIBUTING.md for detailed guidelines.

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

πŸ“§ Contact

Project Link: https://github.com/nluninja/langchain_agents

⚠️ Disclaimer

This project is for educational purposes. Be mindful of API rate limits and costs. Always follow the terms of service for the APIs you use.


Happy searching! πŸŽ‰

About

Educational project for building a RAG app LLaMA, via LangChain

Topics

Resources

License

Contributing

Stars

Watchers

Forks