taphra / ai-chat
Taphra AI chat starter: a streaming chat page on Anthropic, OpenAI or Ollama, behind a password, with CSRF checks, rate limits and conversation history.
Requires
- php: ^8.2
- taphra/taphra: ^1.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Taphra's guides are at https://taphra.dev/docs/. Coding agents: read this app's AGENTS.md, and all the guides in one file at https://taphra.dev/llms-full.txt.
A chat page on Taphra whose replies stream in as they are written, with each
visitor's conversation kept for an hour after their last message. It talks
to Anthropic by default; OpenAI and Ollama are one line away in
app/Config/ai.yml.
Every message is a paid call to the AI provider, so the chat is closed to anyone without the password, every form and request carries a CSRF token, and each client IP may send 30 messages an hour.
Requirements
- PHP 8.2 or later, with the
curlextension - Composer
- An API key for your provider (Anthropic by default), or a local Ollama
Quick start
composer create-project taphra/ai-chat my-chat cd my-chat echo "ANTHROPIC_API_KEY=sk-ant-..." >> .env # your key php taphra serve
Open http://127.0.0.1:8000 and sign in with the password that
composer create-project wrote to .env as CHAT_PASSWORD. Change it
there whenever you like (at least 12 characters); without one, the chat
stays closed. app/bootstrap.php reads .env on every request; a variable
set in the real environment takes precedence.
Configuration
app/Config/ai.yml:
default_provider: anthropic providers: anthropic: api_key: "${ANTHROPIC_API_KEY}" model: claude-opus-5-5 max_tokens: 4096
Change default_provider to openai (key in OPENAI_API_KEY) or ollama
(local, no key). ${NAME} is read from the environment; never write a key
into the file.
Requests to the provider (with the messages) are not logged; set
logging.enabled: true in ai.yml to write them to app/tmp/Log/ai.log.
app/Config/app.yml, under chat::
messages_per_hour: messages per client IP per hour (default 30); over it the chat answers 429.max_message_chars: longest message accepted (default 4000).
Behind a proxy or load balancer, list it under trusted_proxies in
app.yml, so the limits see each client's own IP.
Routes
| Method and path | What it does |
|---|---|
GET / |
the chat, or the sign-in form |
POST /login |
signs in with CHAT_PASSWORD (5 wrong tries per IP, then a 15 minute wait) |
POST /logout |
signs out |
POST /chat/stream |
message=...; the reply as Server-Sent Events: data: {"chunk":"..."} lines, then data: [DONE] |
POST /chat/send |
message=...; the reply as JSON, {"reply":"..."} |
POST /chat/clear |
forgets the conversation |
Every POST needs the CSRF token: the forms carry it, and the page's script
sends the <meta name="csrf-token"> value as an X-CSRF-Token header.
Signing in gives the browser a new session id. A GET to a POST route
answers 405.
Customising
-
The assistant's instructions:
app/Prompts/assistant.prompt.php. -
The page:
app/Views/chat/index.tpl.phpandapp/Layouts/default.lyt.php. -
Tools:
app/AiTools/DateTimeTool.class.phpis an example tool. To let the model call tools, run an agent instead of a plain completion inChatController::send():$result = \Taphra\Ai\AI::agent() ->registerTool(new \App\AiTools\DateTimeTool()) ->run($conversation->getMessages()); $reply = $result['content'];
Docker
docker compose up --build
A development container on http://127.0.0.1:8080 (published on your machine
only). It reads ANTHROPIC_API_KEY and CHAT_PASSWORD from .env.
Layout
app/
AiTools/ DateTimeTool (an example tool)
Config/ app.yml (limits), ai.yml (provider), routes.yml
Controllers/ ChatController, AppController (base: CSRF on every POST)
Layouts/ default.lyt.php
Lib/ ChatPassword
Prompts/ assistant.prompt.php
Static/ document root (index.php)
Views/chat/ index.tpl.php (sign-in form, chat page and its script)
bootstrap.php runs on every request and command: .env, mode
composer.json
taphra the command line
An unset APP_ENV means production (no debug pages); php taphra serve sets
APP_ENV=dev for its server.
Licence
This starter, like Taphra, is open-source software under the MIT licence (Copyright 2019-2026 Prodacity LLC); see LICENSE.