File size: 6,320 Bytes
4aec76b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# Scripts
This folder contains helper scripts for working with the Redis-backed chat history used by the server.
## 1) `smoke_redis_history.py`
Purpose: quick smoke test that writes two messages to a session and verifies they are readable from a fresh `HistoryStore` instance.
Usage (from repo root):
```bash
# ensure redis is running (Homebrew example)
brew services start redis
# run the smoke script (make `server/` importable via PYTHONPATH)
PYTHONPATH=server venv/bin/python scripts/smoke_redis_history.py
```
You can pass a session id argument to use a different session key:
```bash
PYTHONPATH=server venv/bin/python scripts/smoke_redis_history.py my_session_id
```
Notes: the script sets `config.HISTORY_BACKEND = 'redis'` at runtime; it does not delete the session key after running.
## 2) `inspect_redis_history.py`
Purpose: inspect chat history keys in Redis, pretty-print messages, tail a session, or delete keys. Useful for debugging and manual inspection.
Requirements:
- A Python environment with the `redis` package installed (install with `pip install redis` into the project's `venv`).
- A running Redis server reachable at `redis://localhost:6379/0` (default). The script will attempt to read `llm_system.config.REDIS_URL` if `server/` is on `PYTHONPATH`.
Basic usage (show all `chat_history:*` keys):
```bash
PYTHONPATH=server venv/bin/python scripts/inspect_redis_history.py
```
Show a single session (pretty output):
```bash
PYTHONPATH=server venv/bin/python scripts/inspect_redis_history.py --session smoke_test_session_01
```
Useful flags:
- `--session, -s`: show one session (e.g. `smoke_test_session_01`) instead of listing all keys.
- `--pattern, -p`: key pattern to match (default: `chat_history:*`).
- `--redis-url, -r`: override the Redis URL for this run.
- `--format, -f`: output format, `pretty` (default) or `json` (JSON-lines).
- `--truncate`: truncate message content to this many characters (default: `200`; `0` = no truncation).
- `--limit`: show only the last N messages (default: `0` = all).
- `--tail, -t`: poll a single session and print updates (press Ctrl+C to stop).
- `--delete, -d`: delete the inspected key(s) after printing (use with caution).
- `--no-color`: disable ANSI color output.
Examples:
Pretty, truncated output:
```bash
PYTHONPATH=server venv/bin/python scripts/inspect_redis_history.py --session smoke_test_session_01 --format pretty --truncate 120 --limit 10
```
JSON-lines output (suitable for piping to files or jq):
```bash
PYTHONPATH=server venv/bin/python scripts/inspect_redis_history.py --session smoke_test_session_01 --format json --limit 50 > session.jsonl
```
Tail a session (poll every second):
```bash
PYTHONPATH=server venv/bin/python scripts/inspect_redis_history.py --session smoke_test_session_01 --tail
```
Delete after printing:
```bash
PYTHONPATH=server venv/bin/python scripts/inspect_redis_history.py --session smoke_test_session_01 --delete
```
Notes & safety:
- The script is intentionally lightweight and intended for local/dev use. `--delete` will remove Redis keys immediately and without recovery — use carefully.
- If the `llm_system` package is not importable from the current working directory, the script falls back to `redis://localhost:6379/0`.
## 3) `redis-memory.py`
Purpose: inspect Redis memory usage, set memory limits, and configure eviction policies to prevent unbounded memory growth.
Basic usage (show current memory info):
```bash
PYTHONPATH=server venv/bin/python scripts/redis-memory.py --info
```
Show recommendations for chat history setup:
```bash
PYTHONPATH=server venv/bin/python scripts/redis-memory.py --recommend
```
Useful commands:
Set memory limit to 512 MB:
```bash
PYTHONPATH=server venv/bin/python scripts/redis-memory.py --set-maxmemory 512mb
```
Set eviction policy to `volatile-lru` (recommended for chat history with TTL):
```bash
PYTHONPATH=server venv/bin/python scripts/redis-memory.py --set-policy volatile-lru
```
**Memory Management for Chat History:**
1. **Set session TTL** in `server/llm_system/config.py`:
```python
REDIS_HISTORY_TTL_SECONDS = 2592000 # 30 days; 0 = no expiry
```
Sessions will auto-expire after this duration, freeing memory.
2. **Set maxmemory limit** to prevent unbounded growth:
```bash
PYTHONPATH=server venv/bin/python scripts/redis-memory.py --set-maxmemory 512mb
```
3. **Set eviction policy** to remove least-recently-used sessions:
```bash
PYTHONPATH=server venv/bin/python scripts/redis-memory.py --set-policy volatile-lru
```
This removes keys with TTL set, prioritizing LRU.
4. **Monitor memory usage** regularly:
```bash
PYTHONPATH=server venv/bin/python scripts/redis-memory.py --info
```
**Why these settings?**
- **TTL**: Automatically expires old sessions, preventing memory bloat over time.
- **maxmemory**: Prevents Redis from consuming all system RAM and crashing.
- **volatile-lru**: Removes least-recently-used sessions (with TTL set) first, keeping active sessions in memory.
Example setup (30-day session expiry, 512 MB limit):
```bash
# Set config
cat >> server/llm_system/config.py << 'EOF'
REDIS_HISTORY_TTL_SECONDS = 2592000 # 30 days
EOF
# Set Redis limits
PYTHONPATH=server venv/bin/python scripts/redis-memory.py --set-maxmemory 512mb
PYTHONPATH=server venv/bin/python scripts/redis-memory.py --set-policy volatile-lru
# Verify
PYTHONPATH=server venv/bin/python scripts/redis-memory.py --info
```
## 4) `redis-commander.sh`
Purpose: start/stop/status the redis-commander web UI for visual inspection of Redis data.
Usage (from repo root):
```bash
# Start redis-commander (listening on port 8081)
./scripts/redis-commander.sh start
# Check status
./scripts/redis-commander.sh status
# Stop
./scripts/redis-commander.sh stop
```
Then open http://127.0.0.1:8081 in your browser to explore Redis keys and values visually.
Notes:
- Logs: `/tmp/redis-commander.log`
- PID file: `/tmp/redis-commander.pid`
- Requires `redis-commander` to be installed globally: `npm install -g redis-commander`
---
Want more?
- I can add `--output-file` to `inspect_redis_history.py` to write JSONL to a file, `--cleanup-all` (with confirmation) to remove all `chat_history:*` keys, or a `Makefile` target for convenience. Tell me which and I'll add it. |