Language Agents with Verbal Reinforcement Learning - Implementation of Reflexion paper
Project description
Reflexion Agent
Language Agents with Verbal Reinforcement Learning
Architecture • Installation • Quick Start • API Reference
A Python implementation of Reflexion, a framework that equips language agents with the ability to reflect on their failures and learn from them.
Architecture
Reflexion Architecture
│
├── ReflexionAgent (Main Agent)
│ ├── Solves tasks using solve function
│ ├── Evaluates results using evaluate function
│ └── Learns from failures via reflection
│
├── Reflector (Reflection Generator)
│ ├── Analyzes failed attempts
│ ├── Extracts key insights
│ └── Generates improvement suggestions
│
├── ReflexionMemory (Reflection Storage)
│ ├── Stores reflections by task
│ ├── Retrieves relevant reflections
│ └── Formats for prompt inclusion
│
└── Types (Data Structures)
├── Task - Problem to solve
├── Attempt - Single solution attempt
├── Reflection - Failure analysis
└── EvaluationResult - Success/failure status
Core Workflow
┌─────────────────────────────────────────────────────────────────┐
│ Reflexion Execution Loop │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Task ──▶ Solve ──▶ Evaluate ──▶ Success? ──▶ Return Answer │
│ │ │ │
│ │ └── No ──▶ Reflect ──▶ Store Memory │
│ │ │ │
│ └────────────────────────────────────┘ │
│ (Retry with reflections) │
│ │
└─────────────────────────────────────────────────────────────────┘
Installation
pip install reflexion-agent
Quick Start
from reflexion import ReflexionAgent, Task, EvaluationResult, EvaluationStatus
# Define your solve function (typically uses an LLM)
def solve_fn(task, reflections):
context = ""
if reflections:
context = f"\nPrevious insights:\n{reflections[0].to_prompt()}"
# Your LLM call here
# response = llm.generate(f"{task.question}{context}")
return "your_answer"
# Define your evaluation function
def evaluate_fn(task, answer):
# Check if answer is correct
if answer == "expected_answer":
return EvaluationResult(status=EvaluationStatus.SUCCESS, score=1.0)
return EvaluationResult(
status=EvaluationStatus.FAILURE,
score=0.0,
feedback="Answer is incorrect"
)
# Create and run agent
agent = ReflexionAgent(solve_fn, evaluate_fn, max_attempts=3)
result = agent.run(Task(question="What is 2+2?"))
print(f"Success: {result.success}")
print(f"Answer: {result.final_answer}")
print(f"Attempts: {result.total_attempts}")
CLI Usage
# Run a single task
reflexion run "What is the capital of France?"
# Start interactive mode
reflexion interactive
# Export trajectory
reflexion run "Solve this" --export trajectory.json
API Reference
ReflexionAgent
| Method | Description |
|---|---|
run(task) |
Run Reflexion process for a task |
run_with_context(question, context) |
Convenience method with string input |
get_memory() |
Get the reflection memory |
get_reflections(task_id) |
Get stored reflections |
clear_memory() |
Clear all stored reflections |
EvaluationStatus
SUCCESS- Task completed correctlyFAILURE- Task failedPARTIAL- Partially correct
Academic Reference
Implementation of the Reflexion paper:
Reflexion: Language Agents with Verbal Reinforcement Learning
Noah Shinn, Federico Cassano, Ashwin Gopinath, Karthik R. Narasimhan, Shunyu Yao
NeurIPS 2023
@inproceedings{shinn2023reflexion,
title={Reflexion: Language Agents with Verbal Reinforcement Learning},
author={Shinn, Noah and Cassano, Federico and Gopinath, Ashwin and Narasimhan, Karthik R and Yao, Shunyu},
booktitle={Advances in Neural Information Processing Systems (NeurIPS)},
year={2023}
}
License
MIT License
Made with ❤️ by AI Agent Research Team
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file reflexion_agent-0.1.0.tar.gz.
File metadata
- Download URL: reflexion_agent-0.1.0.tar.gz
- Upload date:
- Size: 14.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4f49b5f11a31ba705c7cb3610296bc137431543a6517cbab56e235162358fe36
|
|
| MD5 |
227cabaa5128e38dd4bd295574616aa9
|
|
| BLAKE2b-256 |
0d5c806110cbc5a511fbc4337d5b87b7ae9023ffbe7b9449c2706a76448df002
|
File details
Details for the file reflexion_agent-0.1.0-py3-none-any.whl.
File metadata
- Download URL: reflexion_agent-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f66423a31516431b20d66104d396128e1dcfe28dc0e1c7a4cd43138757f39d14
|
|
| MD5 |
0b68da87438361e05e3e363ebe866a5e
|
|
| BLAKE2b-256 |
cae06cf2b4455e9db21dc49bd0aa4d8372a14cc0f61d31009d562033504976ea
|