Download projects/elizabeth/scripts/session_writer_example.py from LevelUp2x/adaptai: direct link, hf CLI and curl.
- Browser
- Download file 2.36 kB
-
https://huggingface.co/datasets/LevelUp2x/adaptai/resolve/main/projects/elizabeth/scripts/session_writer_example.py
- Command line
-
hf download hf://datasets/LevelUp2x/adaptai/projects/elizabeth/scripts/session_writer_example.py
-
curl -L -o session_writer_example.py https://huggingface.co/datasets/LevelUp2x/adaptai/resolve/main/projects/elizabeth/scripts/session_writer_example.py
2.36 kB
| #!/usr/bin/env python3 | |
| """ | |
| Example dual-writer for Elizabeth sessions: DragonFly/Redis (fast resume) + Postgres (durable). | |
| Requires: pip install redis psycopg2-binary | |
| """ | |
| import os | |
| import json | |
| import uuid | |
| import time | |
| from datetime import datetime | |
| import psycopg2 | |
| import redis | |
| POSTGRES_DSN = os.getenv('POSTGRES_DSN', 'postgresql://postgres:postgres@localhost:5432/elizabeth') | |
| DFLY_URL = os.getenv('DFLY_URL', os.getenv('REDIS_URL', 'redis://localhost:6379/0')) | |
| def ensure_schema(conn): | |
| with conn.cursor() as cur: | |
| cur.execute(open(os.path.join(os.path.dirname(__file__), 'sql', 'session_store.sql')).read()) | |
| conn.commit() | |
| def start_session(r: redis.Redis, conn): | |
| sid = str(uuid.uuid4()) | |
| now = datetime.utcnow().isoformat() + 'Z' | |
| # Redis: store a hash for session meta | |
| r.hset(f"eliz:session:{sid}", mapping={ | |
| 'created_at': now, | |
| 'updated_at': now, | |
| 'status': 'active', | |
| }) | |
| # Postgres: insert session | |
| with conn.cursor() as cur: | |
| cur.execute( | |
| "INSERT INTO eliz_sessions (id, created_at, updated_at, status) VALUES (%s, NOW(), NOW(), %s)", | |
| (sid, 'active') | |
| ) | |
| conn.commit() | |
| return sid | |
| def add_message(r: redis.Redis, conn, session_id: str, role: str, content: str, meta: dict | None = None): | |
| mid = str(uuid.uuid4()) | |
| now = datetime.utcnow().isoformat() + 'Z' | |
| meta = meta or {} | |
| # Redis: append to a stream for the session | |
| r.xadd(f"eliz:stream:{session_id}", {'id': mid, 'role': role, 'content': content, 'meta': json.dumps(meta), 'ts': now}) | |
| # Postgres: insert message | |
| with conn.cursor() as cur: | |
| cur.execute( | |
| "INSERT INTO eliz_messages (id, session_id, role, content, meta) VALUES (%s, %s, %s, %s, %s)", | |
| (mid, session_id, role, content, json.dumps(meta)) | |
| ) | |
| cur.execute("UPDATE eliz_sessions SET updated_at = NOW() WHERE id = %s", (session_id,)) | |
| conn.commit() | |
| return mid | |
| def main(): | |
| r = redis.from_url(DFLY_URL) | |
| conn = psycopg2.connect(POSTGRES_DSN) | |
| ensure_schema(conn) | |
| sid = start_session(r, conn) | |
| print('Session:', sid) | |
| add_message(r, conn, sid, 'user', 'Hello Elizabeth!') | |
| time.sleep(0.1) | |
| add_message(r, conn, sid, 'assistant', 'Hi! I am live and durable.') | |
| print('Wrote two messages.') | |
| if __name__ == '__main__': | |
| main() | |