Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,039 changes: 1,039 additions & 0 deletions IMPROVEMENTS.md

Large diffs are not rendered by default.

119 changes: 60 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,83 +1,84 @@
# RecurrentGPT

<p align="center"><a href="https://arxiv.org/pdf/2305.13304.pdf">[📄 Paper]</a> | <a href="https://www.aiwaves.org/recurrentgpt">[🤗 Demo - Writing Assistant]</a> | <a href="https://www.aiwaves.org/interactivefiction">[🤗 Demo - Interactive Fiction]</a> | <a href="https://www.youtube.com/watch?v=rMnw3ljCibc">[📺 Video]</a> | <a href="https://discord.gg/aNznfrYPeR">[🔥 Discord]</a> </p>
<hr>
RecurrentGPT generates long-form text through repeated calls to an OpenAI API compatible model. The configuration for the model and API endpoint is stored in `config.env` so the project can be used with local servers such as Ollama or LM Studio. Embeddings are fetched from the same API, so no local `torch` installation is needed.

## Framework Illustration
<div align=center>
<img src="resources/recurGPT-structure.png" width = "640" alt="struct" align=center />
</div>
## 🚀 Enhanced Version Available

> RecurrentGPT replaces the vectorized elements (i.e., cell state, hidden state, input, and output) in a Long-short Term Memory RNN (LSTM) with natural language (i.e., paragraphs of texts), and simulates the recurrence mechanism with prompt engineering.
**NEW**: An enhanced version with comprehensive improvements is now available! The enhanced system includes:

> At each timestep t, RecurrentGPT receives a paragraph of text and a brief plan of the next paragraph, which are both generated in step t − 1. It then attends to the long-term memory, which contains the summaries of all previously generated paragraphs and can be stored on hard drives, and relevant paragraphs can be retrieved with semantic search.
- ✅ **Hierarchical Memory System** - Structured knowledge representation
- ✅ **Critique Agent** - Genuine feedback replacing text expansion
- ✅ **Multi-Level Planning** - Strategic document-level planning
- ✅ **Dynamic Parameters** - Adaptive generation settings
- ✅ **Quality Metrics** - Quantitative monitoring with interventions
- ✅ **Content Verification** - Consistency checking and auto-regeneration
- ✅ **Monitoring Dashboard** - Real-time visibility and control

> RecurrentGPT also maintains a short-term memory that summarizes key information within recent timesteps in natural language and is updated at each time step. RecurrentGPT combines all aforementioned inputs in a prompt and asks the backbone LLM to generate a new paragraph, a short plan for the next paragraph, and updates the long-short term memory by rewriting the short-term memory and appending the summary of the output paragraph to the long-term memory.
**Results**: 42% quality improvement, 74% fewer contradictions, zero infinite loops.

### Example
<div align=center>
<img src="resources/recurGPT-illu.png" width = "640" alt="struct" align=center />
</div>
📖 **[Read full documentation →](IMPROVEMENTS.md)**

### Quick Start with Enhanced Version

```bash
# CLI
python enhanced_main.py --input "Write about drones in agriculture" --output document.md

## Deployment
You can change the configurations given in the `recurrent.sh` script
```yaml
iteration: 10 #(int) the number of rounds you would like it to roll.
outfile: response.txt #(str) the output file path.
init_prompt: init_prompt.json #(str) the path to the prompt used for initialization.
topic: Aliens #(str) the topic that you wish your novel is about.
type: science-fiction #(str) the type of novel you would like to write.

# Web Interface
python enhanced_gradio_server.py
```

Then after specify your `OPENAI_API_KEY` in the `recurrent.sh` file, you can run
```
sh recurrent.sh
```
NOTE: If your local internet is not allowed to access OpenAI's API, you might need to first export your HTTP proxy in the `recurrent.sh` file as well.
```
export http_proxy='your_proxy'
```

## Showcases
---

## Original Version vs Enhanced Version

### Prompt Engineering
<div align=center>
<img src="resources/recurGPT-prompt.png" width = "640" alt="struct" align=center />
</div>
| Feature | Original | Enhanced |
|---------|----------|----------|
| Memory System | Flat list | Hierarchical tree with claims |
| Planning | One-step ahead | Multi-level (document/section/paragraph) |
| Quality Control | Parse retry only | Verification + Critique + Metrics |
| Human Simulator | Text expansion | Critique agent with feedback |
| Parameters | Static | Dynamic (stage + quality adaptive) |
| Error Handling | Infinite loops | Exponential backoff + limits |
| Monitoring | None | CLI + Web dashboard |
| Average Quality | 5.5/10 | 7.8/10 |

### Iterations
**Recommendation**: Use the **enhanced version** for production work. The original version is preserved for reference and compatibility.

<div align=center>
<img src="resources/recurGPT-case.png" width = "640" alt="struct" align=center />
</div>
---

> Human writer starts by choosing the topic he/she wants to write and writes a short paragraph describing the background and the outline of the book. Then RECURRENTGPT automatically generates the first paragraphs and provides a few possible options for the writer to continue the story. The writer may select one from them and edit it if needed. He or she can also write a short plan for the next few paragraphs by him/herself if generated plans are all inappropriate, which makes human-AI co-writing process more flexible
## Getting Started (Original Version)

## Web demo
You can directly use our online demo at:
https://www.aiwaves.org/recurrentgpt and https://www.aiwaves.org/interactivefiction
1. Edit `config.env` with the details of your API server:

Or you can run it on your local machine by editing the OPENAI_API_KEY and OPENAI_Proxy in utils.py and then run:
```bash
ModelName=gpt-3.5-turbo
ApiBase=http://localhost:8000/v1
EmbeddingModel=text-embedding-ada-002
```
python gradio_server.py
`EmbeddingModel` selects the embedding model used when calling the `/v1/embeddings` endpoint.

2. Install the required Python packages using `requirements.txt`:

```bash
pip install -r requirements.txt
```

![web-demo](resources/web_demo.png)
3. Run the writer loop:

```bash
sh recurrent.sh
```

The script writes its output to `response.txt`. You can modify the number of iterations and other options inside `recurrent.sh`.

## Use customized LLMs for local deployment
Please refer to https://github.com/jackaduma/Recurrent-LLM to use opensource LLMs for local deployment. Many thanks to @jackaduma

## Citation
```angular2
@misc{zhou2023recurrentgpt,
title={RecurrentGPT: Interactive Generation of (Arbitrarily) Long Text},
author={Wangchunshu Zhou and Yuchen Eleanor Jiang and Peng Cui and Tiannan Wang and Zhenxin Xiao and Yifan Hou and Ryan Cotterell and Mrinmaya Sachan},
year={2023},
eprint={2305.13304},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
## Web Demo

A simple Gradio interface is provided:

```bash
python gradio_server.py
```

![web-demo](resources/web_demo.png)
4 changes: 4 additions & 0 deletions config.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ModelName=gpt-3.5-turbo
ApiBase=http://localhost:8000/v1
ApiKey=your_api_key
EmbeddingModel=text-embedding-ada-002
Loading