An Autonomous Search & Self-Reflection Agent powered by LangGraph, LangChain, and Multi-Search Engines.
Languages: 🌐 繁體中文 (Traditional Chinese) | English
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:
- Search & Process: Execute Google or Brave searches and structure results into readable Markdown with source reliability ratings.
- Reflect & Critique: Analyze whether the information is complete, detect contradictions across sources, and identify gaps.
- Iterative Refinement: Automatically generate improved search queries based on critique and iterate until sufficient data is gathered.
- Synthesize & Condense: Produce a comprehensive final answer along with a concise executive quick-summary (< 100 words).
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])
- 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
SimpleCachewith 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.
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| 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 |
- Python 3.10+
- pip package manager
-
Clone the repository:
git clone https://github.com/rowing195/reflexion_search.git cd reflexion_search -
Install dependencies:
pip install -r requirements.txt
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-miniLaunch the default LangGraph application:
python reflexion_LangGragh.pyOpen your browser at http://localhost:7861 to interact with the Gradio interface.
This project is licensed under the MIT License. Copyright © 2026 rowing195.