Skip to content

An interactive research tool for exploring recursive reasoning in Large Language Models through iterative self-reflection. Watch as LLMs refine their thinking across multiple iterations without external feedback.

License

Notifications You must be signed in to change notification settings

chheplo/llm-reflection-lab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

25 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿง  LLM Reflection Lab

An interactive research tool for exploring recursive reasoning in Large Language Models through iterative self-reflection. Watch as LLMs refine their thinking across multiple iterations without external feedback.

Python Streamlit License

๐ŸŽฏ Overview

This project implements a thinking loop system where LLMs iteratively improve their responses through self-reflection. Unlike traditional single-shot prompting, this approach allows models to:

  • ๐Ÿ”„ Reflect on their previous reasoning
  • ๐ŸŽฏ Identify gaps and assumptions
  • ๐Ÿ“ˆ Refine their answers progressively
  • ๐Ÿงฉ Explore different reasoning paths
Screenshot 2025-08-15 at 10 49 42โ€ฏAM ## โœจ Features

Core Functionality

  • Multi-Model Support: Works with Ollama, vLLM, and OpenRouter APIs
  • Reasoning Extraction: Captures explicit reasoning from <think> tags or native fields
  • ๐ŸŽฏ YOLO Mode: Run iterations until convergence is detected automatically
    • Configurable convergence threshold (80-99%)
    • Choose similarity comparison mode: "Response Only" (default) or "Reasoning + Response"
  • ๐Ÿ“š Prompt Templates: Pre-configured epistemic approaches
    • Socratic Method, Empirical-Scientific, Dialectical Synthesis, Systems Thinking, and more
    • Easy template switching via dropdown in prompt editor
  • Customizable Prompts: Edit system prompts and reflection templates via UI
  • Auto-Save: Experiments saved automatically in JSON format
  • ๐Ÿ“„ Export Options:
    • PDF Reports: Professional reports with visualizations, charts, and complete appendix
    • HTML Reports: Interactive web-based reports
    • Smart Filenames: AI-generated descriptive filenames based on question content

๐Ÿ“Š Interactive Visualizations

  • ๐Ÿ•ธ๏ธ Concept Evolution Graph: Network showing how concepts emerge and connect
  • ๐Ÿ”ฅ Similarity Heatmap: Matrix of iteration similarities to identify convergence
  • ๐Ÿ“ˆ Confidence Tracking: Evolution of certainty/uncertainty markers
  • ๐Ÿ“Š Complexity Metrics: Vocabulary diversity and logical connector usage
  • ๐ŸŒŠ Topic Flow Sankey: How topics persist or change between iterations
  • โ†”๏ธ Convergence Timeline: Tracks exploration vs exploitation phases
Screenshot 2025-08-15 at 10 51 21โ€ฏAM

๐Ÿš€ Quick Start

Prerequisites

  • Python 3.10+
  • One of: Ollama, vLLM server, or OpenRouter API key

Installation

Using uv (Recommended)

# Clone the repository
git clone https://github.com/chheplo/llm-reflection-lab.git
cd llm-reflection-lab

# Install with uv
uv sync

Using pip

# Clone the repository
git clone https://github.com/chheplo/llm-reflection-lab.git
cd llm-reflection-lab

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

Running the Application

# With uv
uv run streamlit run app.py

# With pip
streamlit run app.py

Alternative: Run with minimal UI and headless mode

# With uv (minimal toolbar, headless server, no usage stats)
uv run streamlit run app.py --client.toolbarMode=minimal --server.headless true --browser.gatherUsageStats false

# With pip
streamlit run app.py --client.toolbarMode=minimal --server.headless true --browser.gatherUsageStats false

The app will open at http://localhost:8501

๐ŸŽฎ Usage

1. Configure Your Model

For Ollama (Local)

  • Install Ollama
  • Pull a model: ollama pull gpt-oss:20b
  • Start Ollama: ollama serve
  • Select "Ollama (Local)" in the app

For vLLM

  • Start your vLLM server
  • Enter the server URL and API key
  • Click "Load Available Models"

For OpenRouter

  • Get an API key from OpenRouter
  • Select "OpenRouter" and enter your key
  • Choose from available models

2. Run an Experiment

  1. Enter a Question: Complex questions work best
  2. Set Iterations: Choose 3-10 iterations (or more!)
  3. Optional - Enable YOLO Mode:
    • Toggle "๐ŸŽฏ YOLO Mode" to run until convergence
    • Adjust convergence threshold (0.80-0.99)
    • Iterations continue until consecutive responses are similar enough
  4. Start Loop: Click to begin the thinking process
  5. Watch Evolution: See reasoning improve in real-time
  6. Explore Visualizations: Click visualization buttons for insights

3. Customize Prompts

Click "โœ๏ธ Prompts" to:

  • Load Templates: Choose from epistemic approaches
    • Default: Standard iterative reasoning
    • Socratic Method: Question-driven inquiry
    • Empirical-Scientific: Evidence-based analysis
    • Dialectical Synthesis: Thesis-antithesis resolution
    • Systems Thinking: Holistic interconnected analysis
    • Iterative Refinement: Precision-focused improvement
  • Edit Prompts: Customize system and reflection prompts
  • Save Changes: Store your customizations

4. Export Results

Click "๐Ÿ“ค Export" to generate reports:

  • PDF Report: Professional document with:
    • Colorful title page with research question
    • Executive summary and key findings
    • Visualization charts (token usage, convergence analysis)
    • Detailed experiment results
    • Complete appendix with all iterations
    • Smart AI-generated filename based on question
  • HTML Report: Web-based interactive report

๐Ÿ“ Project Structure

llm-reflection-lab/
โ”œโ”€โ”€ app.py                 # Main Streamlit application
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ visualizations.py  # Visualization modules
โ”‚   โ”œโ”€โ”€ pdf_export.py      # PDF report generation
โ”‚   โ””โ”€โ”€ prompts.json       # Current active prompts (user customized)
โ”œโ”€โ”€ templates/            # Prompt template library
โ”‚   โ”œโ”€โ”€ default.json      # Standard reasoning template
โ”‚   โ”œโ”€โ”€ socratic-method.json
โ”‚   โ”œโ”€โ”€ empirical-scientific.json
โ”‚   โ”œโ”€โ”€ dialectical-synthesis.json
โ”‚   โ”œโ”€โ”€ systems-thinking.json
โ”‚   โ””โ”€โ”€ iterative-refinement.json
โ”œโ”€โ”€ saves/                # Auto-saved experiments
โ”œโ”€โ”€ pyproject.toml        # Project dependencies (uv)
โ”œโ”€โ”€ requirements.txt      # Project dependencies (pip)
โ””โ”€โ”€ README.md            # This file

๐Ÿ”ฌ How It Works

The Thinking Loop Process

  1. Initial Response: Model answers the question
  2. Self-Reflection: Model reviews its previous answer
  3. Improvement: Model provides refined response
  4. Repeat: Process continues for N iterations (or until convergence in YOLO Mode)

Reasoning Extraction

The system extracts reasoning through:

  • Native reasoning fields (e.g., OpenAI o1 models)
  • <think>...</think> tags in responses
  • Configurable extraction patterns

Convergence Patterns

Through visualizations, you can observe:

  • Convergence: Ideas stabilizing (high similarity)
  • Divergence: Exploring new concepts (low similarity)
  • Phase Transitions: Shifts between exploration/exploitation

YOLO Mode (You Only Loop Once... Until Convergence)

When enabled, YOLO Mode:

  • Automatic Stopping: Detects when consecutive iterations reach similarity threshold
  • Dynamic Duration: Runs as many iterations as needed (up to 100 for safety)
  • Real-time Monitoring: Shows convergence progress chart during execution
  • Efficiency: Stops early when the model's responses stabilize
  • Configurable Threshold: Adjust sensitivity from 80% to 99% similarity
  • Comparison Modes:
    • "Reasoning + Response": Compare full thought process
    • "Response Only": Focus on answer convergence

๐Ÿ“Š Example Insights

From a typical 10-iteration experiment:

  • Iterations 1-2: Initial exploration
  • Iterations 3-6: First convergence cluster
  • Iteration 7: Divergence/pivot point
  • Iterations 8-10: Final convergence

๐Ÿ› ๏ธ Configuration

Environment Variables

  • OPENROUTER_API_KEY: Your OpenRouter API key
  • VLLM_API_KEY: Your vLLM server API key

Prompt Customization

Edit prompts.json or use the UI to modify:

  • System prompts
  • Reflection templates
  • Reasoning extraction patterns

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Development Setup

# Clone your fork
git clone https://github.com/YOUR_USERNAME/llm-reflection-lab.git
cd thinking-loop-experiment

# Install in development mode
uv sync --dev

# Create a feature branch
git checkout -b feature/your-feature

๐Ÿ“„ License

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

๐Ÿ™ Acknowledgments

๐Ÿ“š Citation

If you use this tool in your research, please cite:

@software{llm_reflection_lab,
  title = {LLM Reflection Lab},
  author = {Your Name},
  year = {2024},
  url = {https://github.com/chheplo/llm-reflection-lab}
}

๐Ÿ”— Links


Made with โค๏ธ for the AI research community

About

An interactive research tool for exploring recursive reasoning in Large Language Models through iterative self-reflection. Watch as LLMs refine their thinking across multiple iterations without external feedback.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages