A web-based chatbot that explains the ideas of the Second Renaissance movement,
powered by Claude (claude-sonnet-4-6). Drop documents into a folder and the
bot uses them as its knowledge base — no code changes required.
pip install -r requirements.txtRequires Python 3.10+.
cp .env.example .env
# Open .env and replace the placeholder with your real key:
# ANTHROPIC_API_KEY=sk-ant-...Place .txt, .md, or .pdf files in the documents/ folder.
See documents/placeholder.txt for full guidance.
python server.pyOpen http://localhost:8000 in your browser.
2RAI/
├── server.py # FastAPI backend — document loading, chat endpoint
├── config.py # Model, max_tokens, system prompt preamble
├── requirements.txt
├── .env.example # Copy to .env and add your API key
├── documents/ # ← Drop your content here
│ └── placeholder.txt
├── static/
│ └── index.html # Single-page chat UI
└── README.md
Edit config.py to change any of these settings without touching the server code:
| Setting | Default | Description |
|---|---|---|
MODEL |
claude-sonnet-4-6 |
Anthropic model to use |
MAX_TOKENS |
1024 |
Maximum tokens in each response |
DOCUMENTS_FOLDER |
documents |
Path to the documents folder |
SYSTEM_PROMPT_PREAMBLE |
(see file) | Bot persona and instructions |
The documents/ folder is the only place you need to touch to manage content:
- Add a file → corpus reloads automatically within ~1 second
- Remove a file → corpus reloads automatically
- Edit a file → corpus reloads automatically
Supported formats: .txt, .md, .pdf
If a PDF cannot be parsed (scanned images, corrupted files), a warning is logged and the file is skipped — the server keeps running with the remaining documents.
The full document corpus is sent as a cached system-prompt block. The first request after a corpus change pays full input-token cost; subsequent requests within 5 minutes reuse the Anthropic prompt cache (~90 % cheaper for the document portion). Cache usage is visible in the Anthropic console.
- Startup —
server.pyreads every supported file indocuments/, extracts text (usingpdfplumberfor PDFs), and assembles a single corpus string. - File watching —
watchdogmonitors the folder; any change triggers a debounced reload (1 s delay to avoid partial-write races). - Chat endpoint —
POST /chataccepts{ message, history }and calls the Claude API with streaming enabled. The corpus is injected into the system prompt withcache_control: ephemeral. - Frontend — A plain HTML/JS page reads the SSE stream token-by-token and renders a live, markdown-aware response.
| Symptom | Likely cause | Fix |
|---|---|---|
ANTHROPIC_API_KEY … not set on startup |
Missing env var | Add key to .env |
| PDF skipped with warning | Scanned/image-only PDF | Use a text-based PDF or convert to .txt |
| Responses cut off | MAX_TOKENS too low |
Increase in config.py |
| Old content still appearing | Cache TTL (5 min) | Wait or send a new message after the TTL expires |