Skip to content

lookmohan/langgraph-web-researcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 

Repository files navigation

LangGraph Research Assistant πŸ”

A simple iterative research agent built with LangGraph that searches the web and refines answers automatically.

Built for Google Colab | Uses Groq (Free) | Real Web Search | No API Cost for Search


🎯 What Does This Do?

This notebook demonstrates LangGraph - a framework for building AI agents that can:

  • βœ… Loop and retry (not just linear chains)
  • βœ… Make decisions (should I search again?)
  • βœ… Refine queries automatically
  • βœ… Prevent infinite loops (max 3 iterations)

Example Flow:

Question β†’ Search β†’ Bad Answer? β†’ Refine Query β†’ Search Again β†’ Good Answer βœ“

πŸš€ Quick Start (Google Colab)

1️⃣ Open in Colab

Click here β†’ Open In Colab

2️⃣ Get Your Free Groq API Key

  1. Go to groq.com
  2. Sign up (it's free!)
  3. Create an API key
  4. Copy it

3️⃣ Add API Key to Colab

# In Colab: Click πŸ”‘ (Secrets) on left sidebar
# Add: Name = "GROQ", Value = "your-api-key-here"

4️⃣ Run All Cells

Press Runtime > Run all and watch it work!


πŸ“¦ What Gets Installed

# This happens automatically in Cell 1:
!pip install langgraph langchain_groq langchain_community -U ddgs

No OpenAI required! Uses:

  • Groq β†’ Free fast LLM (llama-3.3-70b)
  • DuckDuckGo β†’ Free web search (no API key!)

πŸ”§ How It Works

1. State (Shared Memory)

question: str           # Original question
search_query: str       # Refined search query
search_results: str     # What we found
answer: str            # Final answer
needs_more_info: bool  # Should we loop?
iteration_count: int   # Safety counter (max 3)

2. Nodes (Workers)

  • analyze_question β†’ Creates/refines search query
  • search_web β†’ Searches DuckDuckGo
  • generate_answer β†’ Uses LLM to answer

3. Edges (Flow Control)

START β†’ analyze β†’ search β†’ generate β†’ ⚑ Decision Point
                              ↑              ↓
                              └─────(loop)β”€β”€β”€β”˜  or END

The Magic: Conditional edge decides:

  • If needs_more_info == True AND iterations < 3 β†’ Loop back
  • Otherwise β†’ Stop and return best answer

πŸ’‘ Example Output

❓ Question: How many episodes in One Piece anime?

πŸ” Searching for: How many episodes in One Piece anime?
βœ… Found results (length: 1237 chars)
πŸ€– Generating answer (iteration 1)...
⚠️  Answer incomplete, needs more research
πŸ”„ Looping back for more research (iteration 1/3)

πŸ” Searching for: One Piece anime total episodes count 2024
βœ… Found results (length: 760 chars)
πŸ€– Generating answer (iteration 2)...
βœ… Answer complete!

FINAL ANSWER:
The One Piece anime has released more than 1150 episodes as of November 2024.

Total iterations: 2

🎨 Customize It

Change the Question

question = "What are the latest AI trends?"  # Your question here

Adjust Max Iterations

# In should_continue_research function:
if state["needs_more_info"] and state["iteration_count"] < 5:  # Change 3 to 5

Use Different LLM

llm = ChatGroq(
    model="mixtral-8x7b-32768",  # Faster, cheaper
    # or "llama-3.3-70b-versatile"  # Smarter, slower
)

🧠 Why LangGraph vs LangChain?

Feature LangChain LangGraph
Flow Linear (A→B→C) Graph (loops, branches)
Decisions No Yes
Retries Manual Built-in
Use Case Simple RAG Complex agents

Example:

  • LangChain: Question β†’ Retrieve β†’ Answer βœ… Good for simple Q&A
  • LangGraph: Question β†’ Search β†’ Bad? β†’ Retry β†’ Good! βœ… Better for complex tasks

πŸ“Š Real-World Use Cases

  1. Research Assistant - Searches until it finds complete answer
  2. Customer Support - Routes questions to right department
  3. Code Reviewer - Analyzes β†’ Finds issues β†’ Suggests fixes β†’ Loops
  4. Content Writer - Research β†’ Outline β†’ Write β†’ Self-edit β†’ Publish

πŸ› οΈ Troubleshooting

Error: "NameError: name 'GROQ' is not defined"

  • Solution: Add your Groq API key in Colab Secrets (πŸ”‘ icon on left)

Search returns empty results

  • DuckDuckGo might be rate-limiting
  • Try again in a few seconds

Answer says "NEED_MORE_INFO" 3 times

  • Question might be too specific or recent
  • Try rephrasing the question

πŸ“š Learn More


🀝 Contributing

Found a bug? Have an improvement?

  1. Fork this notebook
  2. Make changes
  3. Share your version!

πŸ“ License

MIT License - Use freely in your projects!


⭐ If this helped you understand LangGraph, give it a star!

Built with ❀️ for beginners learning AI agents


πŸŽ“ Next Steps

After this, try:

  1. Add memory (save conversation history)
  2. Use multiple agents (researcher + writer)
  3. Add human-in-the-loop (ask user for clarification)
  4. Connect to your own data (RAG with iteration)

About

Iterative AI research assistant using LangGraph with web search and automatic query refinement. Built for Google Colab with Groq LLM (free) + DuckDuckGo search.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors