A stateful Telegram persona for private and group chats that remembers people, reads the room, and follows up on scheduled reminders.
Most chatbots forget who they are talking to, answer every message immediately, and become noisy the moment they enter a group chat. Rachel is designed around the opposite goal: feel like a believable participant who knows when to speak, remembers what matters, and carries context across conversations.
The shipped persona is Rachel, a 22-year-old NTU marketing student in Singapore with a backstory, seven conversational moods, twelve tunable personality traits, and a weekly schedule. She waits for a burst of messages to finish, types at a human pace, can stay quiet when a conversation does not concern her, and recalls both general world knowledge and personal facts about the people she meets.
Underneath the character is an asynchronous persona engine: FastAPI hosts two Telethon clients and an in-process scheduler, LangGraph coordinates reactive replies, proactive outreach, and memory workflows, PostgreSQL stores operational state, and Graphiti on Neo4j provides temporal long-term memory. Model and embedding traffic goes through Merge Gateway. Persona and workflow prompts live in source control; traits, weekly activities, memories, and active model selections have persistent stores.
Rachel does not answer directly inside Telegram's message-arrival event. Each unsuppressed incoming turn resets a seven-second reply debounce; a separate three-minute inactivity timer controls conversation finalization. Multi-paragraph answers are emitted as separate Telegram messages, with later paragraphs simulating typing at 22 characters per second. Per-chat locks, shielded in-flight replies, separate watermarks, and message-ID-ordered buffer insertion protect delivery when new messages arrive mid-generation.
After admission, summarization and the first fetcher pass run in parallel. The fetcher assesses evidence, selects and executes tools across bounded passes, and calls finish_context(reason) when done. The responder waits for both branches before generating one final reply. Raw results retain their tool, arguments, pass number, and success, empty, error, or timeout status, so a failed lookup cannot silently become a claim that no facts exist. A configurable pass budget and a 30-second deadline per pass bound retrieval; completed results survive a later timeout, and the final responder handles incomplete evidence naturally.
Reply generation and memory extraction track “already handled” independently. An in-memory reply watermark keeps new user messages visible even when Rachel's later send-time message ID sorts after them; a persisted extraction watermark divides finished transcripts into old context and new material. Advancing extraction state at handoff avoids routine re-extraction after a flush or restart, with a deliberate tradeoff: failed background extraction is not automatically replayed.
General facts and personal memories become Graphiti episodes in separate partitions. Hybrid retrieval uses COMBINED_HYBRID_SEARCH_RRF, returning deduplicated relationship facts and verbatim episodes while excluding lossy node summaries. Graphiti resolves entities and temporal conflicts; the 16 fixed profile slots instead merge deterministically in PostgreSQL JSONB under per-user locks, where new non-empty values replace older values. One source schema drives extraction, rendering, and the admin field list without a database migration for each slot change.
Proactive schedules are stored in PostgreSQL and rebuilt into APScheduler jobs on startup. A conditional database update consumes each attempt before generation or sending, enforcing lifetime limits even when an attempt fails. Reactive and proactive messages share the same per-chat reply lock, typing simulation, history insertion, and silence controls; agent schedule tools receive their chat scope from runtime configuration. Shielded mutations finish both persistence and live job synchronization even if the requesting context pass times out.
The context fetcher can read relevant public HTTP(S) links without a browser or search service. Each redirect destination is validated, DNS results must all be public, and the connection is pinned to a checked address while retaining the original HTTP/TLS host identity. Fetches have explicit time, size, redirect, and per-reply attempt limits. Extracted content and URL errors enter prompts as explicitly untrusted data in concrete human messages, keeping page text out of system instructions.
The main, small, and embedding model roles are stored in PostgreSQL and can be switched from Telegram, REST, or the dashboard. Cache invalidation rebuilds model clients and the Graphiti singleton on subsequent calls. The responder uses strict JSON Schema output with local validation and visible-text recovery; Graphiti embeds schema guidance in its prompt while omitting the HTTP response_format field for route compatibility. Malformed JSON and schema mismatches share three corrective retries after the first attempt, preventing transport retries from multiplying an exhausted format budget.
Prometheus records model calls, terminal errors, retrieval pass counts, and loop outcomes with bounded labels. Phoenix traces reactive replies, proactive outreach, and memory updates as separate roots connected by burst-session and chat metadata. Direct memory lookups receive explicit tool spans with arguments, complete results, and retrieval status; the direct JEV router call records its validated probability and available usage/cost. This connects a visible reply to its routing and retrieval evidence without treating recovered Graphiti retries as terminal failures.
flowchart TB
subgraph TG[Telegram]
U[People and group chats]
A[Administrator]
end
subgraph APP[FastAPI · one asyncio event loop]
RC[Rachel Telethon client]
BOT[Admin Telethon bot]
API[REST API · dashboard · metrics]
STATE[Per-chat buffers · locks · scheduler]
end
subgraph AI[LangGraph workflows]
REPLY[Reactive and proactive reply graphs]
MEM[World-view and user-memory extraction]
end
GW[Merge Gateway<br/>chat · decisions · embeddings]
PG[(PostgreSQL<br/>history · profiles · schedules · models)]
N4J[(Neo4j + Graphiti<br/>temporal memories)]
U -->|Telegram events| RC
RC -->|incoming messages| STATE
STATE -->|debounced turns or outreach intent| REPLY
REPLY -->|typed message bursts| U
STATE -->|finalized transcript| MEM
A <-->|admin commands| BOT
A <-->|dashboard and REST| API
REPLY <-->|traits · profiles · schedule tools| PG
STATE <-->|history · summaries · saved jobs| PG
MEM -->|profiles · watermark| PG
REPLY <-->|hybrid memory search| N4J
MEM -->|fact episodes| N4J
REPLY <-->|model calls| GW
MEM <-->|model and embedding calls| GW
BOT <-->|configuration| PG
API <-->|configuration and history| PG
- Telegram client (
app/telegram/client.py) owns message normalization, per-chat buffers, reply/flush timers, group silence controls, typing simulation, and graceful persistence. - Reply workflow (
app/services/llm.py) gates participation, summarizes the conversation, retrieves only relevant context, and generates a structured response plus a traceable reason. - Proactive scheduler (
app/services/proactive.py) restores cron jobs, claims attempts, coordinates live schedule changes, and initiates outreach through the shared Telegram delivery path. - Memory workflows (
app/services/memory.py,worldview.py,userfacts.py) partition finalized transcripts once, then update general knowledge, personal facts, and structured profiles concurrently. - Graphiti adapter (
app/services/graphiti.py) initializes Neo4j indices, serializes graph writes, performs hybrid retrieval, and adds schema-aware retry behavior around Graphiti's LLM client. - Data layer (
app/models.py,app/repository.py,app/database.py) uses async SQLAlchemy sessions and PostgreSQL upserts for durable chat and control-plane state. - Admin plane (
app/telegram/bot.py,app/routers/admin.py,app/static/index.html) exposes Telegram commands, REST endpoints, a dashboard without a build step, and Prometheus metrics. Architecture rendering uses the checked-in reactive Mermaid source and compiled workflows.
Create schedules in the dashboard's Proactive tab or through the chat agent's schedule tools. Each schedule includes a reachable Telegram chat, an outreach intent, a five-field cron expression, and an IANA timezone (default Asia/Singapore). For example, 0 9 * * * fires daily at 09:00 in that timezone; the persona's weekly activity calendar is a separate store.
Each proactive schedule has repeat_times (a positive integer, or null for unlimited) and a read-only repeat_completed counter. The dashboard accepts a blank repeat limit for unlimited schedules and displays consumed attempts. PostgreSQL atomically checks the limit and commits the increment before generation or sending starts; failures, interruptions, and process crashes still consume that attempt. “Run now” can execute a disabled schedule, but shares the same allowance as cron fires.
Exhausted schedules show Done and are removed from the active scheduler without changing their saved enabled setting. Raising the limit above the consumed count, or clearing it to unlimited, resumes an enabled schedule. Editing or toggling a schedule never resets its counter. Existing schedules migrate to unlimited with zero counted attempts; older executions are not reconstructed. Apply uv run alembic upgrade head before starting the updated application.
Agent tools can create, list, replace, enable/disable, and delete schedules only in the current chat; there is no agent-facing run-now tool. Agent create/update calls require an explicit repeat limit of 1–99999 or null; HTTP accepts positive PostgreSQL bigint limits. Replacing a schedule through the agent enables it and preserves consumed attempts. Disabling or deleting removes future occurrences but does not cancel an execution already underway.
The proactive graph runs context fetcher and responder with the same retrieval bounds as reactive replies, but without a checker, router, summarizer, or mood write. Its fired intent is the current trigger; the conversation history is prior context. Delivery waits behind reactive replies and timed silence; indefinite mute fails the attempt. Jobs coalesce missed occurrences, allow one instance per schedule, and use a 60-second misfire grace period. Shutdown drains active outreach and schedule mutations before disconnecting Telegram.
flowchart LR
S((start)) --> C[checker]
C -->|no new user messages| E((end))
C -->|forced admission| A[admission]
C -->|ordinary turn| R[JEV decision router]
R -->|stay silent| E
R -->|reply| A
A --> SUM[summarizer]
A --> CTX
CTX[context fetcher] -->|collecting| CTX
CTX -->|finish_context or application cap| RESP[one final responder]
SUM -.->|summary and mood at join| RESP
RESP --> E
flowchart LR
%% Invisible layout links
CTX ~~~ F
F ~~~ E
S((start)) --> C[checker]
C -->|no new user messages| E((end))
C -->|forced admission| A[admission]
C -->|ordinary turn| R[JEV decision router]
R -->|stay silent| E
R -->|reply| A
A --> SUM[summarizer]
A --> CTX[context fetcher]
CTX -->|collecting| CTX
CTX -->|finish_context or application cap| RESP[one final responder]
SUM -.->|summary and mood at join| RESP
RESP --> E
F["Finalized conversation<br/>3 min idle or 150 buffered messages<br/>Split once at the extraction watermark<br/>Extract from new turns with earlier context"]
W["World-view facts<br/>Extract durable general facts<br/>Write one episode per fact"]
P["Personal facts<br/>Extract free-form facts per person<br/>Resolve names to Telegram IDs<br/>Write to each user’s partition"]
U["User profiles<br/>Extract fixed profile slots<br/>Use summary and existing profiles<br/>Merge non-empty updates"]
F --> W & P & U
W --> G[Graphiti]
P --> G
G <--> N[Neo4j]
U --> PG[Postgres]
E -.-> F
CTX <-.-> G
CTX <-.-> PG
The checker rejects turns without new user messages even when admission is forced. Otherwise, direct mentions/replies and the spontaneity path can bypass routing. The router sees the summary and latest 15 messages, uses separate DM/group questions, and admits a reply at a validated noul probability of at least 0.7. Errors or invalid answers admit the turn; rejection skips summarization, retrieval, and generation.
After admission, the summarizer and first fetcher pass run in parallel. The fetcher loops directly in the reply graph. The deferred responder waits for both branches to finish. The fetcher uses the existing summary captured when collection starts; the responder receives the current call's updated summary and mood, active traits, Singapore time, and all accumulated observations. Summarizer failure preserves existing state. The responder returns only content and reason; it runs once per completed admitted graph attempt and cannot request retrieval. The whole-graph Telegram retry is separate.
The fetcher independently assesses the current task and evidence, selecting supported calendar, memory, search, URL, and schedule actions. It executes each batch sequentially under a shared 30-second deadline. Raw observations retain successes, empty results, errors and timeouts. A valid completion is exactly one finish_context(reason) call, alone, with a trimmed nonempty reason. A lookup and completion cannot share a batch: the fetcher must review its results on a later pass. Invalid protocol responses become error observations and allow a corrective pass. Completion reasons stay in tracing state and are never factual evidence for the responder.
Set AGENT_MAX_CONTEXT_PASSES to 1–10 (default 8). Every fetcher model attempt counts, including completion, protocol errors and model failures. Immediate completion takes one pass; calendar lookup → completion takes two; search → page read → completion takes three. At the cap, the last tool batch still executes and its results reach the responder without an extra completion call. Completion on the final permitted pass finishes normally. The graph step guard reserves room for the final responder and passes along collected evidence.
Normalized searches and URL reads are reused across passes regardless of outcome. Exact repeated schedule mutations are deduplicated by tool name and validated arguments, including failed and timed-out writes. Read-only schedule listings remain refreshable for reconciliation. Shielded mutations still finish after a pass timeout, but remain unconfirmed until an observation establishes their outcome. Personal lookup uses search_user_info(name, query) against actual participant names and returns both scoped facts and profiles; unknown or ambiguous names become errors.
The proactive graph reuses collection followed by one final responder, without routing, summarization or mood writes. The fired intent is delivered now; old reminder requests are prior context and do not authorize recreating a schedule. Fetcher and responder receive role-specific instructions and a post-history trigger.
Prometheus keeps rachel_agent_loop_outcomes_total{outcome} and rachel_agent_context_passes: first_pass means explicit completion on pass one, retried means explicit completion on a later pass, and capped means application-enforced termination. Aggregate metrics are recorded once at the outer entry point; one workflow trace contains all fetcher passes and the final responder. The read-only workflow prompt listing includes the fetcher system prompt and loop instructions.
The renderer loads the reactive diagram from scripts/graph_llm.mmd and registers reactive, proactive, world-view, and user-memory PNGs. Rendering requires access to the configured Mermaid image service; existing PNGs can be regenerated after changes.
search_web(query, num_results=5) uses the official async Exa SDK to find missing public facts. Set optional EXA_API_KEY in .env to enable requests; startup works without it and searches report a clear configuration error. Queries must be nonblank and result counts range from 1–20.
- The model cannot select the output format.
SEARCH_MODEinapp/services/web_search.pycurrently uses"text"for source highlights with URLs, optional titles, excerpts, and a local truncation flag. Developers can change it to"urls"for source URL strings only. Combined excerpts are capped at 2,000 characters per source. Links remain available when excerpts are missing. No generated answers or summaries are requested. - At most
AGENT_MAX_SEARCH_ATTEMPTSExa attempts per graph run (default 4, 0 disables attempts), across all passes. The fetcher receives the configured limit in its runtime collection state. Trimmed query and result count define an attempt; omitted defaults normalize identically. Failures and timeouts consume attempts and are reused; invalid arguments consume none. Restarting the whole graph starts fresh, so the Telegram retry can repeat searches and URL reads. - A 15-second total deadline and a 15-second SDK network timeout bound each search inside the existing 30-second pass deadline. Clients are created lazily and closed after use. Errors are sanitized; there are no application-level automatic retries.
- The fetcher selects tools once per pass. The fetcher can select search-discovered URLs on a later pass when page contents are needed, then call
finish_context. The final responder can cite the collected source links. Searches do not trigger automatic page fetches and do not replace participant memory.
Search and fetched-page payloads, including titles, excerpts and failures, are explicitly untrusted data supplied to both agents separately from system instructions. Earlier observations survive later failures. The eight-pass default and separate two-URL allowance apply within each graph run.
fetch_url performs GET requests for public HTML, plain text, Markdown, and JSON, including JSON media types ending in +json. HTML is converted to readable Markdown with links and tables; extraction runs off the event loop. Results include the requested/final URL, media type, optional title, source content, and a truncation flag.
- At most two distinct normalized URL attempts per graph run, across all retrieval passes; successful and failed attempts are reused.
- A 10-second overall fetch deadline, at most three redirects, a 2 MiB decoded response limit, and 12,000 characters of returned content.
- Standard HTTP/HTTPS ports only; credentials in URLs, private or special-purpose destinations, and DNS answers containing non-public addresses are rejected. Redirect targets are revalidated and connections use a pinned address.
- Fetches do not use environment proxies or application authentication; cookies do not carry between redirect hops.
This source reader complements Exa search; it does not crawl, execute JavaScript, log in, parse PDFs, or download binary files. URL payloads are explicitly labeled untrusted and supplied separately from system instructions.
When a conversation is idle for three minutes—or its buffer reaches 150 messages—the transcript is persisted and both memory workflows start in the background.
- World view: extract durable, non-personal knowledge, then ingest each fact as a Graphiti episode in the
worldviewpartition. - User memory: fan out into free-form personal facts stored in
user-facts-<user_id>Graphiti partitions and fixed profile slots merged into PostgreSQL JSONB.
History uses the composite key (chat_id, telegram_message_id), since Telegram message IDs are unique only within a chat. Reply context is capped at the latest 40 messages. Profiles use a five-minute cache with write-through after extraction; direct admin edits can remain stale for that TTL. Finalization advances the durable extraction watermark when handing off the snapshot, so failed extraction is not automatically retried. Shutdown flushes buffered history and pending state without starting new extraction.
| Layer | Technology | Purpose |
|---|---|---|
| Runtime | Python 3.11+ · asyncio | One event loop for HTTP, Telegram, and workflow tasks |
| HTTP service | FastAPI · Uvicorn | Lifespan management, admin API, dashboard, health, and metrics |
| Messaging | Telethon / MTProto | Persistent Rachel and admin-bot Telegram clients without webhooks |
| Orchestration | LangGraph · LangChain OpenAI | Stateful reply and memory workflows over an OpenAI-compatible endpoint |
| Scheduling | APScheduler 3 · IANA timezones | In-process cron jobs backed by durable PostgreSQL definitions |
| Model gateway | Merge Gateway | Chat completions, JEV reply decisions, and embeddings |
| Long-term memory | Graphiti · Neo4j 5.26 | Temporal entity graph, conflict handling, and hybrid RRF search |
| Operational data | PostgreSQL 16 · SQLAlchemy 2 · asyncpg | Histories, summaries, moods, watermarks, profiles, traits, activities, outreach, and active models |
| Schema changes | Alembic | Versioned PostgreSQL migrations |
| Structured data | Pydantic 2 · pydantic-settings | Validated model output, API bodies, and environment configuration |
| Public web search | Exa Python SDK (AsyncExa) |
Source links or bounded relevant excerpts |
| Public URL content | HTTPX · Trafilatura | Bounded HTTP retrieval and HTML-to-Markdown extraction |
| Observability | Prometheus · Phoenix OTEL · OpenInference | Model/loop metrics, workflow traces, router decisions, and tool evidence |
| Admin UI | Vanilla HTML/CSS/JavaScript | Single-file dashboard with no frontend build step |
| Packaging | uv · pyproject.toml · uv.lock |
Reproducible dependency installation and command execution |
| Infrastructure | Docker · Docker Compose | Local/production PostgreSQL, Neo4j, and optional app container |
- Human-like reply timing, typing indicators, and multi-message bursts
- Group-aware reply routing, 5% spontaneous participation, timed silence, and indefinite mute controls
- Media placeholders for stickers, GIFs, photos, videos, voice messages, audio, and files
- Seven conversation moods, with successful summarizer updates available in the current reply
- Twelve live-tunable personality sliders and a seeded weekly schedule in Singapore time
- Bounded retrieval across calendar, world-view, user facts/profiles, public web search, public URLs, and schedule tools
- Proactive reminders and check-ins with cron, timezone, lifetime attempt limits, and run status
- Chat-scoped schedule creation and management through conversation
- Temporal world and per-user knowledge graphs with scoped Graphiti group IDs
- Sixteen-slot structured user profiles with deterministic conflict-free merging
- Runtime model catalog and independent main/small/embedding model roles
- Admin control through Telegram, REST, and a browser dashboard
- Read-only inspection of source-controlled workflow prompts
- Per-reply reasoning stored beside the Telegram message history
- Prometheus metrics for model reliability and loop outcomes, plus Phoenix workflow/tool tracing
- Graceful shutdown that drains outreach and schedule mutations, then flushes conversation buffers
- One-off scripts for graph seeding, visualization, login, and destructive graph reset
Possible extensions, rather than committed roadmap items, include durable background-extraction retries and expiry policies for short-lived facts such as “Sarah's interview is on Thursday.” These would address the current handoff watermark tradeoff and distinguish events from enduring preferences. Proactive outreach already exists; a future calendar integration could update or cancel reminders when an external event changes.
Discord or WhatsApp Business adapters could bring the persona and memory workflows to other communities, with new work needed for transport identity, permissions, message ordering, and delivery semantics. Reusable persona packages could bundle prompts, traits, activities, profile schemas, and memory policies for game characters, interactive fiction, education, or community assistants.
Runtime model selection, stored response reasons, retrieval observations in traces, and per-node metrics also provide a foundation for evaluating persona consistency, factual recall, and model reliability. An evaluation harness would need to control datasets and embedding compatibility; it is not included today.
Install these tools before cloning the repository:
| Tool | Version | Install |
|---|---|---|
| Git | Any maintained release | git-scm.com |
| Python | 3.11 or newer | python.org |
| uv | Current stable | docs.astral.sh/uv |
| Docker Desktop / Docker Engine | Compose v2 capable | docs.docker.com |
| Telegram account | Able to receive a login code | Used to create Rachel's persistent Telethon session |
Use a separate BotFather bot for the admin client; Rachel's speaking identity is authorized independently in anon.session and can be a bot token or a user-account login. Node.js is optional and needed only for the dashboard's built-in Node test runner. There is no frontend dependency installation or build step.
Docker must be running before PostgreSQL or Neo4j can start. Verify it with docker ps; a table—even an empty one—means the daemon is reachable.
Clone the GitHub repository and enter its root directory:
git clone https://github.com/GeneralR3d/Rachael.git
cd RachaelAll following commands assume the current directory is the repository root.
Let uv create .venv and install the exact dependency set recorded in uv.lock:
uv sync --frozenuv should finish with the locked dependencies installed in .venv, or report that they are already installed. If Python 3.11+ is missing, run uv python install 3.11 and repeat the command.
Copy the committed template to the local file read by pydantic-settings and Docker Compose:
cp template.env .envNever commit .env; it contains Telegram and gateway credentials.
TELEGRAM_API_ID and TELEGRAM_API_HASH (required)
Identify your Telegram developer application to MTProto. Create an application at my.telegram.org under API development tools.
Example: TELEGRAM_API_ID=1234567 and TELEGRAM_API_HASH=0123456789abcdef0123456789abcdef
TELEGRAM_BOT_TOKEN (required)
Authenticates the separate admin bot. Create it with @BotFather using /newbot.
Example: TELEGRAM_BOT_TOKEN=123456789:AAExampleToken
ADMIN_ID (required)
Whitelists the only Telegram user allowed to operate the admin bot. Obtain your numeric user ID from @userinfobot; leaving the template value blank prevents the integer setting from validating at startup.
Example: ADMIN_ID=987654321
Rachel's own Telegram login is intentionally not stored in .env; step 6 creates anon.session interactively.
ELEVENLABS_API_KEYenables speech synthesis. Leave it unset or blank for text delivery.ELEVENLABS_VOICE_IDdefaults toSEWXl8lPSO01tdGbWECX.ELEVENLABS_MODEL_IDdefaults toeleven_v4.
With a key configured, each eligible reactive reply in a private or group chat has a 15% chance of becoming one captionless voice note. Replies containing URLs or totaling 650 characters or more use text. For voice, ElevenLabs receives [Strong Singapore Accent] followed by the full reply, including every paragraph. Rachel uses text if synthesis fails. Scheduled outreach, commands, and confirmations use text.
Rachel uploads Ogg Opus audio from memory and stores the original reply text and reason in the existing history. Incoming captionless voice notes appear as [voice message].
MERGE_GATEWAY_API_KEY (required)
Authenticates JEV reply routing, summarization, context selection, user-fact extraction, and profile extraction. Create a Gateway credential at gateway.merge.dev.
Example: MERGE_GATEWAY_API_KEY=mg_...
MERGE_GATEWAY_GRAPHITI_API_KEY (optional)
Separately meters the world-view extractor and Graphiti's internal LLM, embedding, and reranking calls. Leave it blank to reuse MERGE_GATEWAY_API_KEY.
Example: MERGE_GATEWAY_GRAPHITI_API_KEY=mg_...
MERGE_GATEWAY_BASE_URL (optional)
Gateway-native base URL used by Graphiti embeddings and the router's /decisions request. Keep the default unless you operate a custom endpoint.
Default: https://api-gateway.merge.dev/v1
MERGE_GATEWAY_OPENAI_BASE_URL (optional)
OpenAI-compatible base URL used by chat completions and reranking. The /v1/openai suffix is required for the hosted Gateway.
Default: https://api-gateway.merge.dev/v1/openai
LLM_MODEL (optional)
Initial model for the chat reply workflows, extractors, and Graphiti's main role. The value must use Gateway's provider/model format.
Default: deepseek/deepseek-v4-flash
DECISIONS_LLM (optional)
Decision model for the router's reply gate. The router asks one should_reply Noul question through /v1/decisions and replies when its probability is at least 0.7. Invalid answers and request errors default to replying.
Default: typesafe/jev-1.13
LLM_SMALL_MODEL (optional; supported by the code but not currently listed in template.env)
Initial smaller model for Graphiti helper and reranker calls.
Default: deepseek/deepseek-v4-flash
LLM_EMBEDDING_MODEL (optional)
Initial embedding model for Graphiti's semantic search.
Default: openai/text-embedding-3-small
AGENT_MAX_CONTEXT_PASSES (optional)
Sets the total retrieval-pass budget for each reactive or proactive reply, including completion calls and failed model attempts. Choose an integer from 1–10; values outside that range fail settings validation.
Default: 8
AGENT_MAX_SEARCH_ATTEMPTS (optional)
Sets the maximum number of distinct Exa searches per reactive or proactive graph run, across all retrieval passes. Choose a nonnegative integer; 0 disables attempts. Failed and timed-out attempts count; identical normalized searches are reused. Restart the application after changing this setting. This budget is separate from AGENT_MAX_CONTEXT_PASSES and the two-URL reading limit.
Default: 4
The database-backed model selector can override the main, small, and embedding values at runtime. Startup seeds missing selections without overwriting existing admin choices. Legacy OPENROUTER_MODEL, OPENROUTER_SMALL_MODEL, and OPENROUTER_EMBEDDING_MODEL names are accepted only as compatibility aliases for model IDs; the commented OPENROUTER_API_KEY, OPENROUTER_GRAPHITI_API_KEY, and OPENROUTER_BASE_URL template entries are retired and have no effect.
Model IDs above are repository defaults, not a guarantee that every Gateway route supports the required features. The summarizer and three memory extractors use function calling with reasoning disabled; the context fetcher binds real tools with medium reasoning; the responder requests strict JSON Schema output with medium reasoning. Preserve these capability requirements when choosing another main model. Graphiti is configured with a small-model reranker, but the current RRF search does not invoke it because its log-probability requirements are incompatible with the Gateway response.
BOT_NAME (optional)
Controls Rachel's display label in prompts and the exact name accepted by natural-language silence commands.
Default: Rachel
USER_NAME (optional)
Loaded for legacy compatibility, but does not currently affect conversation behavior. It may be left blank.
DB_PASSWORD (optional locally; change in production)
Sets the password used by the Compose PostgreSQL container. Keep it above DATABASE_URL because dotenv interpolation is top-down. URL-encode characters such as @, :, /, or # inside the URL.
Local default: rachel
DATABASE_URL (required to match the chosen run mode)
Async SQLAlchemy connection string. For a host-run app talking to the Compose database, use port 5433:
DATABASE_URL=postgresql+asyncpg://rachel:${DB_PASSWORD}@localhost:5433/rachel
For the Compose app service, the compose file overrides this with the internal host db:5432 automatically. The bare default in app/config.py uses localhost port 5432, so keep the template's explicit 5433 URL for a host-run app using the Compose database.
NEO4J_PASSWORD (required)
Must match the password used in NEO4J_AUTH. Set a non-default value, especially outside local development.
Example: NEO4J_PASSWORD=replace-with-a-long-random-password
NEO4J_USER (optional)
Neo4j account name.
Default: neo4j
NEO4J_URI (required to match the chosen run mode)
Use bolt://localhost:7687 when the app runs on the host. The Compose app service overrides it with bolt://neo4j:7687.
WORLDVIEW_PATH (legacy, optional; not in the template)
Defaults to worldview.md, but does not control active fact storage. World knowledge lives in Neo4j, and the bulk import script reads the repository-root worldview.md directly.
PHOENIX_ROOT_URL (optional, commented in the template)
Sets the public base URL used by the Compose Phoenix service when generating links. Supply your own reverse-proxy URL for a server deployment; the Compose default is http://localhost:6006. This setting does not choose the app's trace destination.
Example: PHOENIX_ROOT_URL=https://phoenix.example.com
PHOENIX_COLLECTOR_ENDPOINT and PHOENIX_ENDPOINT (optional, SDK/process settings; not in the template)
The Compose app service supplies both as http://phoenix:6006. For a host-run app, explicitly supply http://localhost:6006 in the process environment as shown below. Phoenix registration happens before application settings load .env, so adding these keys only to .env is insufficient for that early registration.
PYTHONUNBUFFERED (optional, container setting)
The Compose app sets this to 1 so Python logs reach container output promptly. No manual value is needed for the local setup.
Start only the two local infrastructure services while running the Python app on the host:
docker compose up -d db neo4jCheck that both containers reach a healthy state:
docker compose psYou should see rachel-db and rachel-neo4j as healthy. PostgreSQL is available only on 127.0.0.1:5433; Neo4j Bolt is on 127.0.0.1:7687, and the Neo4j browser is at http://localhost:7474. First-time Neo4j startup can take 20–40 seconds.
If a container stays unhealthy, inspect it with docker compose logs db or docker compose logs neo4j and confirm the passwords in .env match the compose configuration.
Apply every Alembic migration to create or upgrade the application schema:
uv run alembic upgrade headAlembic should report each revision and finish without an error. Run this again after pulling migrations. Neo4j does not use Alembic; Graphiti creates its indices and constraints on first access.
Run the one-time interactive login that writes the gitignored anon.session file:
uv run python -m scripts.loginAt the phone-number-or-bot-token prompt, enter the separate token for the bot that should speak as Rachel, or use a user-account phone number and complete Telegram's login challenge. This identity is independent of TELEGRAM_BOT_TOKEN, which belongs to the admin bot. A successful run prints Logged in as: ... Session saved to anon.session. Complete this step before Uvicorn: without an authorized session, startup attempts to authenticate anon.session with the configured admin bot token.
Start FastAPI with hot reload for local development:
uv run uvicorn app.main:app --reloadStartup seeds traits, weekly activities, and missing model selections, starts both Telegram clients, then restores proactive jobs. Prompts are imported from app/prompts.py. The service exposes:
- Dashboard: http://localhost:8000/
- Interactive API docs: http://localhost:8000/docs
- Prometheus metrics: http://localhost:8000/metrics
Uvicorn should report Application startup complete. If startup fails, first verify the database health and URLs, required .env values, and the authorized anon.session. The admin client's bot.session is created from TELEGRAM_BOT_TOKEN on first startup.
For local traces, start the optional Phoenix service:
docker compose up -d phoenixThe Phoenix UI should become available at http://localhost:6006. Start or restart the app with its trace endpoints explicitly set in the process environment:
PHOENIX_COLLECTOR_ENDPOINT=http://localhost:6006 PHOENIX_ENDPOINT=http://localhost:6006 uv run uvicorn app.main:app --reloadNew model and workflow calls should appear under the Rachel project. If trace export fails, check docker compose logs phoenix and confirm the endpoint matches the app's run mode.
Call the lightweight health endpoint:
curl http://localhost:8000/healthExpected response:
{"status":"ok"}The health route confirms HTTP startup; it does not probe Telegram, either datastore, or a model call. Confirm the model metrics are exposed, following the metrics mount's redirect if present:
curl -fsSL http://localhost:8000/metrics/ | rg 'rachel_(llm|agent)'The command should print metric descriptions and histogram samples; model-counter labels appear after calls occur. Use grep -E in place of rg if ripgrep is unavailable. Finally, mention Rachel or reply directly to one of her messages from another Telegram account and wait for the seven-second debounce. An ordinary DM may correctly be suppressed by the router.
Add one curated general fact through the same Graphiti ingestion path used in production:
uv run python -m scripts.add_worldview_fact "Chagee is a bubble tea brand"Bulk-ingest - bullet lines from a repository-root worldview.md file:
uv run python -m scripts.ingest_worldview_mdBulk-ingest - bullet lines from user_fact_<user_id>.md into one user's partition:
uv run python -m scripts.ingest_user_facts_md 123456789Ingestion is intentionally sequential and may take several model round-trips per fact. Re-running a bulk script creates additional raw episodes even when Graphiti deduplicates the derived entities and edges.
Rachel considers each non-empty incoming turn, but speaking behavior depends on context:
- A direct @mention or reply latches forced admission for the whole burst and clears both timed silence and indefinite mute. The no-new-user-message guard still applies.
- Ordinary group messages use the group router; DMs use a separate private-message router that can suppress acknowledgements or already-handled content.
- Untagged turns have a 5% chance to skip the router for spontaneous participation.
Rachel shush,Rachel quiet,Rachel silence,Rachel shut up, orRachel stop talkingsilences a group for 15 minutes.Rachel mutesilences a group indefinitely;Rachel speakresumes normal scheduling.- Silence state is process-local and does not survive an application restart. Messages still persist and feed memory while Rachel is silent.
The single-file dashboard at / exposes prompts, personality traits, model selections, proactive schedules, chats, histories, user profiles and facts, and world-view facts. Chat names resolve through Telegram, with unavailable names falling back to IDs in the UI. Its Architecture tab renders the four workflow diagrams on demand. The HTTP API, dashboard, and metrics have no application authentication; use loopback locally and an authenticated reverse proxy in production.
System prompts are defined in app/prompts.py and are read-only in the dashboard, API, and Telegram admin bot. Edit the source and restart the service to change them. Migration f9c6a1d4b3e8 drops the retired system_prompts table and its stored values. Personality levels remain adjustable.
Only ADMIN_ID can use these command groups:
| Area | Commands |
|---|---|
| Prompts (read-only) | /get_responder_system_prompt, /get_summarizer_system_prompt |
| Chats | /list_user_names, /list_chats, /get_history <chat_id>, /clear_history <chat_id>, /get_summary <chat_id>, /delete_summary <chat_id> |
| User memory | /get_user_facts <user_id>, /add_user_facts <user_id> <fact>, /get_user_profile <user_id>, /delete_user_profile <user_id> |
| Personality | /list_traits, /set_trait <id> <low|medium|high>, /reset_traits |
| Models | /list_models, /list_active_models, /add_model <provider/model>, /delete_model <provider/model>, /set_active_model <main|small|embedding> <provider/model> |
Routes are mounted at the root, with app/routers/admin.py as the schema source of truth. Open http://localhost:8000/docs for request bodies and generated API documentation.
- Prompts:
GET /responder-system-prompt,/summarizer-system-prompt, and/workflow-promptsare read-only. - Users and chats:
GET /users/namesand/list-chats. - Conversation state endpoints include
GETandDELETE /history/{chat_id}and/summary/{chat_id}. History GET defaults to?page=1for the latest 100 stored messages, ordered oldest to newest within the page. IfX-Has-Moreistrue, incrementpagefor older messages. - Profiles:
GET,PUT, andDELETE /user-profile/{user_id};GET /user-profile-fieldsderives fields from the source schema. - Facts:
GETandPOST /user-facts/{user_id}and/worldview-facts. There is no episode edit/delete API; ingestion may take several model round-trips per fact. - Traits:
GET /personality,PATCH /personality/{trait_id}, andPOST /personality/reset. - Models:
GET,POST, andDELETE /models(delete takes amodel_stringquery parameter);GET /active-modelsandPUT /active-models/{role}. - Outreach:
GETandPOST /proactive-schedules;PUTandDELETE /proactive-schedules/{schedule_id};PATCH /proactive-schedules/{schedule_id}/enabled;POST /proactive-schedules/{schedule_id}/run. Responses include attempts consumed, status/error, and next run time. - Diagnostics:
GET /health,GET /architecture(currently HTTP 502), and the/metricsASGI mount.
rachel_llm_calls_total{node}counts logical model calls by workflow node.rachel_llm_errors_total{node,kind}counts final failures by node and bounded error class.rachel_agent_loop_outcomes_total{outcome}recordsfirst_pass,retried, orcappedfor admitted reactive and proactive loops.rachel_agent_context_passesrecords retrieval pass counts in histogram buckets from 1–10.
The model counters cover router, summarizer, context fetcher, responder, the three memory extractors, and internal Graphiti LLM calls. Recovered Graphiti retries are not terminal errors. Embedding/reranking calls and non-LLM infrastructure failures are outside those counters. Metrics use the single-process registry; run one application process.
app/main.py registers the Rachel Phoenix project with HTTP/protobuf export and automatic instrumentation. Workflow roots are telegram_reply, telegram_proactive, and memory_update, grouped where available by burst-session ID and chat metadata. The direct router request has a manual LLM span; direct world-view and personal-memory lookups have explicit tool spans under the context fetcher, while calendar, URL, and schedule tools retain their LangChain spans.
Tool spans include arguments, complete results, retrieval pass/category/status, and errors or deadline cancellations. This includes retrieved personal facts and profiles, so trace storage is another copy of conversation-derived data. The optional Compose Phoenix service stores its data in the phoenix_data volume and exposes its UI only on 127.0.0.1:6006.
Run the server with source reload:
uv run uvicorn app.main:app --reloadApply all pending migrations:
uv run alembic upgrade headCreate a blank migration after changing SQLAlchemy models, then edit the generated revision explicitly:
uv run alembic revision -m "describe the schema change"Inspect the migration graph's head without applying a schema change:
uv run alembic headsThe current head is 0a7e2c9d4b61. Preserve existing revision IDs and ancestry when adding a migration; alembic revision above creates a blank file that must be edited explicitly.
Run a lightweight Python syntax check without starting databases or Telegram:
uv run python -m compileall -q app scripts alembicRun Python regression tests with valid app settings. Model and Telegram boundaries are mocked; placeholder API credentials suffice, and no Telegram messages are sent:
uv run python -m unittest discover -s testsRun just the bounded-agent-loop suite when working on retrieval orchestration:
uv run python -m unittest tests.test_agent_loopRun the dashboard JavaScript checks with Node's built-in test runner; no browser or frontend build is needed:
node --test tests/*.cjsSuccessful test commands finish with no failures; database tests report skips unless configured. TEST_DATABASE_URL is an optional test-only async PostgreSQL URL, for example postgresql+asyncpg://rachel:rachel@localhost:5433/rachel_test. Create a disposable database yourself before setting it; Compose only creates the application database. Integration tests create and remove isolated schemas. Never point this setting at production data.
Tests cover reply routing, responder schemas/fallbacks, prompt boundaries, public URL handling, Telegram lifecycle races, schedule tools and limits, scheduler shutdown, tracing, and migration preservation. Migration 0a7e2c9d4b61 adds the history timestamp if absent while preserving existing timestamps. There is no lint configuration or CI workflow in this checkout. Reference/ is an ignored local copy of the original implementation and may be absent from a fresh clone.
The workflow renderer registers reactive, proactive, world-view, and user-memory diagrams. The following command writes all four PNGs; Mermaid rendering may also require network access:
uv run python -m scripts.draw_graphsA successful render prints wrote <filename> for each graph. In the current checkout it fails on the absent source; use the inline Mermaid in this README to understand the reply flow.
scripts.clear_graph deletes every Neo4j partition, including all personal memories. It requires the exact typed confirmation delete everything unless --yes is supplied. Use the additive seeding helpers above for ordinary imports.
Two deployment guides are included:
DEPLOY.mdruns PostgreSQL and Neo4j in Docker while the app runs on the host under systemd.DEPLOY_DOCKER.mdruns the app and both datastores with Docker Compose.
Use this README and current code when older guide instructions disagree: the host guide contains retired OpenRouter names and omits Neo4j from some commands, and both guides have older prompt-seeding/login descriptions. Both datastores are needed; use the MERGE_GATEWAY_* and LLM_* settings above. The Compose app service is already enabled, injects internal database/Phoenix addresses, and applies Alembic migrations before serving. Its session bind mounts require actual files on the host before container startup.
Run one app process in either topology; multiple workers would duplicate Telegram state and proactive scheduling. Bind host-run Uvicorn to loopback and protect the dashboard, REST API, metrics, and exposed Phoenix UI with nginx basic authentication or an equivalent access layer.
- Telethon sessions, buffers, locks, timers, caches, APScheduler jobs, and metrics are process-local. Multiple app workers or replicas require coordination that is not implemented.
- A multi-burst reply is persisted as one history row keyed to the first Telegram message ID, so an interleaved user message can be ordered differently after a database re-seed.
- Timed silence, indefinite mute state, and reply watermarks are process-local; the durable memory-extraction watermark is persisted.
- Extraction watermarks advance at background-task handoff. Failed extraction is not automatically replayed, and shutdown flushes do not start memory extraction.
- Proactive repeat limits count attempts, including failures and manual runs. They do not guarantee successful delivery; missed occurrences beyond the scheduler grace period are not replayed.
- Diagram rendering requires access to the Mermaid image service; rendering errors surface as HTTP 502 from
/architecture. Checked-in PNGs may be stale. - Switching to an embedding model with a different vector dimensionality does not automatically re-embed existing Neo4j data.
- Graphiti's configured OpenAI reranker requires log probabilities the Gateway does not supply. Current RRF retrieval avoids it; changing search recipes needs compatibility work.
- Admin profile edits can remain stale in the five-minute profile cache until its TTL expires.
- URL reading is limited to public static text/HTML/Markdown/JSON and is subject to the fetch bounds above.
- The admin HTTP surface does not implement its own authentication.
- Linting, CI, and a ready-made Prometheus/Grafana deployment are not yet included.
No license file is currently included. Contact the maintainer before redistributing the code or creating derivative works.