Skip to content

Repository files navigation

Project Logo

🔍 Reflexion Search Agent

An Autonomous Search & Self-Reflection Agent powered by LangGraph, LangChain, and Multi-Search Engines.

Python LangChain LangGraph OpenAI Gradio License: MIT


Languages: 🌐 繁體中文 (Traditional Chinese) | English


📌 Table of Contents


💡 Overview

Reflexion Search Agent implements an intelligent iterative web search system based on the Reflexion agent paradigm. Instead of returning raw search engine results directly, the agent uses a multi-node workflow to:

  1. Search & Process: Execute Google or Brave searches and structure results into readable Markdown with source reliability ratings.
  2. Reflect & Critique: Analyze whether the information is complete, detect contradictions across sources, and identify gaps.
  3. Iterative Refinement: Automatically generate improved search queries based on critique and iterate until sufficient data is gathered.
  4. Synthesize & Condense: Produce a comprehensive final answer along with a concise executive quick-summary (< 100 words).

🏗️ Architecture

The primary implementation (reflexion_LangGragh.py) is powered by LangGraph using a cyclic StateGraph workflow:

flowchart TD
    Start([User Input Query]) --> SearchNode[1. Search Node<br/>Google / Brave Search API]
    SearchNode --> ReflectNode[2. Reflect Node<br/>Analysis & Source Verification]
    ReflectNode --> Condition{Data Sufficient OR<br/>Max Iterations Reached?}
    Condition -- No --> RefineNode[3. Refine Query Node<br/>Generate Improved Query]
    RefineNode --> SearchNode
    Condition -- Yes / Stop --> SummarizeNode[4. Summarize Node<br/>Final Answer & Quick Summary]
    SummarizeNode --> End([Output Answer to Gradio UI])
Loading

🚀 Key Features

  • LangGraph Engine: Graph-based state machine with clear node boundaries and cyclic query refinement.
  • Smart Early-Stopping: Automatically terminates search loops when reflection deems data sufficient, reducing latency and token costs.
  • Multi-Search Engine Fallback: Primary integration with Google Custom Search API, fallback to Brave Search API if primary keys are unavailable or fail.
  • Multi-LLM Provider: Flexible support for both OpenAI and OpenRouter endpoints and models.
  • In-Memory Caching: Implements SimpleCache with configurable TTL to avoid duplicate search and LLM requests.
  • Gradio Web Interface: Interactive single-search and multi-round iterative search interface with real-time status diagnostics and JSON result exports.

📁 Repository Structure

reflexion_search/
├── reflexion_LangGragh.py   # Primary entry point (LangGraph StateGraph Engine)
├── reflexion.py             # Procedural OOP version (See README_old.md)
├── reflexion_simple.py      # Minimalist baseline (See README_old.md)
├── requirements.txt         # Python dependencies
├── .env.example             # Environment variable template
├── README.md                # English Documentation (Primary)
├── README_zh-TW.md          # Traditional Chinese Documentation
└── README_old.md            # Legacy documentation for procedural versions

⚖️ Scripts Comparison

Script Engine / Architecture Search Provider LLM Provider Early Stopping Default Port
reflexion_LangGragh.py (Recommended) LangGraph StateGraph Google CSE + Brave Fallback OpenAI + OpenRouter ✅ Yes 7861
reflexion.py Procedural OOP Google CSE OpenAI ❌ Fixed Rounds 7860
reflexion_simple.py Minimalist Pipeline Google CSE OpenAI ❌ Fixed Rounds Auto

🛠️ Getting Started

Prerequisites

  • Python 3.10+
  • pip package manager

Installation

  1. Clone the repository:

    git clone https://github.com/rowing195/reflexion_search.git
    cd reflexion_search
  2. Install dependencies:

    pip install -r requirements.txt

Environment Variables

Copy .env.example or create a .env file in the root directory:

# LLM Credentials (Provide at least one)
OPENAI_API_KEY=sk-...
OPENROUTER_API_KEY=sk-or-v1-...

# Search Credentials (Provide Google CSE or Brave API)
GOOGLE_API_KEY=AIzaSy...
GOOGLE_CSE_ID=...
BRAVE_API_KEY=BSA...

# Model Options (Optional)
SEARCH_PROCESSOR_MODEL=gpt-4.1
REFLECTION_MODEL=gpt-4.1-mini

Running the Application

Launch the default LangGraph application:

python reflexion_LangGragh.py

Open your browser at http://localhost:7861 to interact with the Gradio interface.


📄 License

This project is licensed under the MIT License. Copyright © 2026 rowing195.

Back to Top

About

Multi-round reflective search agent that combines a search API with LLMs to iteratively refine queries and converge on complete, trustworthy answers.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages