Skip to content

About

A multi-agent Python system that generates, critiques, and iteratively improves its own responses using self-reflection and autonomous quality optimization.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

10 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ€– Self-Improving AI Agent

Autonomous Response Quality Optimizer β€” A multi-agent system that generates, critiques, and iteratively improves its own output until a high-quality result is achieved.


🧠 What It Does

This system demonstrates core AI concepts like self-reflection, iterative improvement, and autonomous quality optimization using a pipeline of three specialized agents:

User Prompt
    β”‚
    β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  GENERATOR   β”‚  ← Creates initial response
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    CRITIC    β”‚  ← Evaluates quality (0–10) across 5 dimensions
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β”œβ”€β”€ Score β‰₯ threshold? β†’ STOP βœ…
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   IMPROVER   β”‚  ← Refines response based on critic feedback
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       └── Loop back to CRITIC (up to N iterations)

πŸ“ Project Structure

self-improving-ai-agent/
β”œβ”€β”€ main.py                    ← Entry point β€” run this
β”œβ”€β”€ agents/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ generator_agent.py     ← Produces initial response
β”‚   β”œβ”€β”€ critic_agent.py        ← Scores and critiques responses
β”‚   └── improver_agent.py      ← Applies targeted improvements
β”œβ”€β”€ core/
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── loop_controller.py     ← Orchestrates the full pipeline
β”œβ”€β”€ utils/
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── logger.py              ← Colorful terminal output
β”œβ”€β”€ output/
β”‚   └── history.json           ← Auto-generated improvement log
β”œβ”€β”€ requirements.txt
└── README.md

πŸš€ Quick Start

1. Clone / Download the project

cd self-improving-ai-agent

2. Install dependencies

Rule-Based Mode (no API key needed β€” works out of the box):

# No installation needed! Python standard library only.
python main.py --mode rule --demo

API Mode (optional β€” for real AI-powered improvement):

pip install anthropic          # For Claude
# OR
pip install openai             # For OpenAI

export ANTHROPIC_API_KEY=your_key_here
python main.py --mode api

πŸ’» Usage

# Interactive mode (prompts for mode and input)
python main.py

# Rule-based mode with a demo prompt
python main.py --mode rule --demo

# API mode with custom settings
python main.py --mode api --iterations 5 --threshold 8.5

# Force 2 iterations, stop at score 7.0
python main.py --mode rule --iterations 2 --threshold 7.0

Command-Line Arguments

Argument Type Default Description
--mode rule or api Interactive Operating mode
--iterations int 3 Max improvement cycles
--threshold float 8.0 Quality score to stop early
--demo flag off Use built-in demo prompt

πŸ“Š Scoring System

The Critic Agent evaluates responses across 5 dimensions, each scored 0–10:

Dimension Weight What it measures
Completeness 30% Coverage, depth, word count
Clarity 25% Readability, sentence variety, absence of vague language
Logic 20% Structure, transitions, sound reasoning
Specificity 15% Examples, statistics, concrete details
Tone 10% Professional language, appropriate register

Final Score = Weighted average of all dimensions

Grade Scale:

  • A+ (9.0–10.0) β€” Exceptional
  • A (8.0–8.9) β€” High Quality ← default stop threshold
  • B (7.0–7.9) β€” Good
  • C (6.0–6.9) β€” Acceptable
  • D (5.0–5.9) β€” Needs Work
  • F (0–4.9) β€” Poor

πŸ“‹ Example Output

══════════════════════════════════════════════════════════════════════
  πŸ€–  SELF-IMPROVING AI AGENT  v1.0
  Autonomous Response Quality Optimizer
══════════════════════════════════════════════════════════════════════

──────────────────────────────────────────────────────────────────────
  STARTING IMPROVEMENT LOOP
──────────────────────────────────────────────────────────────────────
  β„Ή Prompt: "Explain machine learning to a complete beginner..."
  β„Ή Mode: RULE  |  Max Iterations: 3  |  Threshold: 8.0/10

  β–Ά PHASE 1  Initial Generation

  [GENERATOR] Creating initial response...
  βœ“ Initial response generated (94 words)

  β–Ά PHASE 2  Initial Evaluation

  [CRITIC] Evaluating response quality...
  βœ“ Evaluation complete β€” Score: 4.8/10 (D)
     Clarity        [β–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘]  4.5/10
     Completeness   [β–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘β–‘]  3.5/10
     Logic          [β–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘]  4.5/10
     Tone           [β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘]  5.0/10
     Specificity    [β–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘β–‘]  3.0/10

  β–Ά ITERATION 1  Improvement Cycle

  [IMPROVER] Applying improvements (iteration 1)...
  βœ“ Improvement applied β€” 94 β†’ 287 words (+193)

  [CRITIC] Re-evaluating improved response...
  βœ“ Evaluation complete β€” Score: 7.2/10 (B)
  β„Ή Score change: 4.8 β†’ 7.2 (+2.40)

  β–Ά ITERATION 2  Improvement Cycle

  [IMPROVER] Applying improvements (iteration 2)...
  βœ“ Improvement applied β€” 287 β†’ 412 words (+125)

  [CRITIC] Re-evaluating improved response...
  βœ“ Evaluation complete β€” Score: 8.1/10 (A)
  β„Ή Score change: 7.2 β†’ 8.1 (+0.90)

  βœ“ Quality threshold reached (8.1 β‰₯ 8.0)! Stopping early.

──────────────────────────────────────────────────────────────────────
  FINAL RESULT
──────────────────────────────────────────────────────────────────────
  βœ“ Best response achieved (Score: 8.1/10):

  [Improved response printed here...]

  β„Ή Total iterations completed: 2
  β„Ή History saved to: output/history.json

πŸ“‚ Output: history.json

Every session saves a complete audit trail:

{
  "session_metadata": {
    "timestamp": "2024-01-15T14:32:10.123456",
    "prompt": "Explain machine learning...",
    "mode": "rule",
    "initial_score": 4.8,
    "final_score": 8.1,
    "total_iterations": 3
  },
  "iterations": [
    {
      "iteration": 0,
      "label": "Initial Response",
      "overall_score": 4.8,
      "scores": {"clarity": 4.5, "completeness": 3.5, ...},
      "weaknesses": ["Response is too brief (94 words)..."],
      "response": "..."
    },
    {
      "iteration": 1,
      "label": "Iteration 1",
      "overall_score": 7.2,
      ...
    }
  ]
}

πŸ”§ Extending the System

Add a new scoring dimension (Critic Agent)

  1. Add key to WEIGHTS dict in critic_agent.py (ensure weights sum to 1.0)
  2. Add scoring logic in _evaluate_rule_based()
  3. Add feedback logic in _generate_feedback()

Add a new improvement strategy (Improver Agent)

  1. Add a case to _apply_fix() matching your new dimension
  2. Implement _fix_<your_dimension>() method

Swap in a different AI model (API mode)

  • Edit _call_anthropic() in both generator_agent.py and critic_agent.py
  • Change the model= parameter to your preferred model

🧩 Architecture: Dual Mode Design

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    LoopController                       β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚  Generator   β”‚  β”‚    Critic    β”‚  β”‚   Improver   β”‚ β”‚
β”‚  β”‚              β”‚  β”‚              β”‚  β”‚              β”‚ β”‚
β”‚  β”‚  Rule Mode:  β”‚  β”‚  Rule Mode:  β”‚  β”‚  Rule Mode:  β”‚ β”‚
β”‚  β”‚  Templates + β”‚  β”‚  Heuristics  β”‚  β”‚  Pattern-    β”‚ β”‚
β”‚  β”‚  heuristics  β”‚  β”‚  + metrics   β”‚  β”‚  based fixes β”‚ β”‚
β”‚  β”‚              β”‚  β”‚              β”‚  β”‚              β”‚ β”‚
β”‚  β”‚  API Mode:   β”‚  β”‚  API Mode:   β”‚  β”‚  API Mode:   β”‚ β”‚
β”‚  β”‚  Claude/GPT  β”‚  β”‚  Structured  β”‚  β”‚  Guided      β”‚ β”‚
β”‚  β”‚  completion  β”‚  β”‚  JSON eval   β”‚  β”‚  rewriting   β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“œ License

MIT License β€” free to use, modify, and distribute.


Built to demonstrate AI concepts: self-reflection, iterative improvement, and autonomous quality optimization.

About

A multi-agent Python system that generates, critiques, and iteratively improves its own responses using self-reflection and autonomous quality optimization.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages