This project implements a minimal AI-powered chatbot using Cloudflare Workers and Workers AI (Llama 3.3). It supports conversational memory via Durable Objects and a simple web UI with Markdown rendering and voice input.
The webpage can be accessed at https://cf-ai-chat.sriram-venkatesh.workers.dev/
- Chatbot API:
/api/chatendpoint for chat interactions, powered by Llama 3.3. - Session Memory: Persists chat history per session using Durable Objects.
- CORS Support: Handles CORS preflight and headers for API endpoints.
- Markdown Output: Responses are formatted in GitHub-Flavored Markdown, including fenced code blocks.
- Web UI: Minimal HTML page for chat, with Markdown rendering and voice input (Web Speech API).
- Session Reset:
/api/resetendpoint to clear session memory.
- Cloudflare Workers
- Workers AI
- Durable Objects
- Clone the repository: git clone https://github.com/Sriramv739/cloudflare-ai-chatbot.git && cd cloudflare-ai-chatbot
- Install dependencies: npm install
- Run on localhost: npx wrangler dev
- POST /api/chat
- Request:
{ sessionId: string, message: string } - Response:
{ reply: string }Handles chat messages, persists session history, and returns AI-generated Markdown-formatted replies.
- Request:
- POST /api/reset
- Request:
{ sessionId: string } - Response:
{ ok: true }Clears session memory for the given session.
- Request:
- GET / Returns the HTML chat UI.
- Type your question and press Send or hit Enter.
- Hold the 🎙️ button to use voice input.
- Session data is temporary—closing the tab erases chat history.
- Demo only—do not share sensitive information.
src/index.ts: Main Worker entry point, API routing, and session management.SessionDO: Durable Object class for session memory.getIndexHtml(): Generates the HTML page for the chat UI.- Helper functions for CORS, JSON responses, Markdown formatting, and code block detection.
- System Prompt: Modify
SYSTEM_PROMPTto change assistant behavior or formatting. - Model: Change the model name in
env.AI.run()to use a different LLM.