|
| 1 | +# LLM Chat Application Template |
| 2 | + |
| 3 | +A simple, ready-to-deploy chat application template powered by Cloudflare Workers AI. This template provides a clean starting point for building AI chat applications with streaming responses. |
| 4 | + |
| 5 | +[](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/templates/tree/main/llm-chat-app-template) |
| 6 | + |
| 7 | +<!-- dash-content-start --> |
| 8 | + |
| 9 | +## Demo |
| 10 | + |
| 11 | +This template demonstrates how to build an AI-powered chat interface using Cloudflare Workers AI with streaming responses. It features: |
| 12 | + |
| 13 | +- Real-time streaming of AI responses using Server-Sent Events (SSE) |
| 14 | +- Easy customization of models and system prompts |
| 15 | +- Support for AI Gateway integration |
| 16 | +- Clean, responsive UI that works on mobile and desktop |
| 17 | + |
| 18 | +## Features |
| 19 | + |
| 20 | +- 💬 Simple and responsive chat interface |
| 21 | +- ⚡ Server-Sent Events (SSE) for streaming responses |
| 22 | +- 🧠 Powered by Cloudflare Workers AI LLMs |
| 23 | +- 🛠️ Built with TypeScript and Cloudflare Workers |
| 24 | +- 📱 Mobile-friendly design |
| 25 | +- 🔄 Maintains chat history on the client |
| 26 | +- 🔎 Built-in Observability logging |
| 27 | +<!-- dash-content-end --> |
| 28 | + |
| 29 | +## Getting Started |
| 30 | + |
| 31 | +### Prerequisites |
| 32 | + |
| 33 | +- [Node.js](https://nodejs.org/) (v18 or newer) |
| 34 | +- [Wrangler CLI](https://developers.cloudflare.com/workers/wrangler/install-and-update/) |
| 35 | +- A Cloudflare account with Workers AI access |
| 36 | + |
| 37 | +### Installation |
| 38 | + |
| 39 | +1. Clone this repository: |
| 40 | + |
| 41 | + ```bash |
| 42 | + git clone https://github.com/cloudflare/templates.git |
| 43 | + cd templates/llm-chat-app |
| 44 | + ``` |
| 45 | + |
| 46 | +2. Install dependencies: |
| 47 | + |
| 48 | + ```bash |
| 49 | + npm install |
| 50 | + ``` |
| 51 | + |
| 52 | +3. Generate Worker type definitions: |
| 53 | + ```bash |
| 54 | + npm run cf-typegen |
| 55 | + ``` |
| 56 | + |
| 57 | +### Development |
| 58 | + |
| 59 | +Start a local development server: |
| 60 | + |
| 61 | +```bash |
| 62 | +npm run dev |
| 63 | +``` |
| 64 | + |
| 65 | +This will start a local server at http://localhost:8787. |
| 66 | + |
| 67 | +Note: Using Workers AI accesses your Cloudflare account even during local development, which will incur usage charges. |
| 68 | + |
| 69 | +### Deployment |
| 70 | + |
| 71 | +Deploy to Cloudflare Workers: |
| 72 | + |
| 73 | +```bash |
| 74 | +npm run deploy |
| 75 | +``` |
| 76 | + |
| 77 | +### Monitor |
| 78 | + |
| 79 | +View real-time logs associated with any deployed Worker: |
| 80 | + |
| 81 | +```bash |
| 82 | +npm wrangler tail |
| 83 | +``` |
| 84 | + |
| 85 | +## Project Structure |
| 86 | + |
| 87 | +``` |
| 88 | +/ |
| 89 | +├── public/ # Static assets |
| 90 | +│ ├── index.html # Chat UI HTML |
| 91 | +│ └── chat.js # Chat UI frontend script |
| 92 | +├── src/ |
| 93 | +│ ├── index.ts # Main Worker entry point |
| 94 | +│ └── types.ts # TypeScript type definitions |
| 95 | +├── test/ # Test files |
| 96 | +├── wrangler.jsonc # Cloudflare Worker configuration |
| 97 | +├── tsconfig.json # TypeScript configuration |
| 98 | +└── README.md # This documentation |
| 99 | +``` |
| 100 | + |
| 101 | +## How It Works |
| 102 | + |
| 103 | +### Backend |
| 104 | + |
| 105 | +The backend is built with Cloudflare Workers and uses the Workers AI platform to generate responses. The main components are: |
| 106 | + |
| 107 | +1. **API Endpoint** (`/api/chat`): Accepts POST requests with chat messages and streams responses |
| 108 | +2. **Streaming**: Uses Server-Sent Events (SSE) for real-time streaming of AI responses |
| 109 | +3. **Workers AI Binding**: Connects to Cloudflare's AI service via the Workers AI binding |
| 110 | + |
| 111 | +### Frontend |
| 112 | + |
| 113 | +The frontend is a simple HTML/CSS/JavaScript application that: |
| 114 | + |
| 115 | +1. Presents a chat interface |
| 116 | +2. Sends user messages to the API |
| 117 | +3. Processes streaming responses in real-time |
| 118 | +4. Maintains chat history on the client side |
| 119 | + |
| 120 | +## Customization |
| 121 | + |
| 122 | +### Changing the Model |
| 123 | + |
| 124 | +To use a different AI model, update the `MODEL_ID` constant in `src/index.ts`. You can find available models in the [Cloudflare Workers AI documentation](https://developers.cloudflare.com/workers-ai/models/). |
| 125 | + |
| 126 | +### Using AI Gateway |
| 127 | + |
| 128 | +The template includes commented code for AI Gateway integration, which provides additional capabilities like rate limiting, caching, and analytics. |
| 129 | + |
| 130 | +To enable AI Gateway: |
| 131 | + |
| 132 | +1. [Create an AI Gateway](https://dash.cloudflare.com/?to=/:account/ai/ai-gateway) in your Cloudflare dashboard |
| 133 | +2. Uncomment the gateway configuration in `src/index.ts` |
| 134 | +3. Replace `YOUR_GATEWAY_ID` with your actual AI Gateway ID |
| 135 | +4. Configure other gateway options as needed: |
| 136 | + - `skipCache`: Set to `true` to bypass gateway caching |
| 137 | + - `cacheTtl`: Set the cache time-to-live in seconds |
| 138 | + |
| 139 | +Learn more about [AI Gateway](https://developers.cloudflare.com/ai-gateway/). |
| 140 | + |
| 141 | +### Modifying the System Prompt |
| 142 | + |
| 143 | +The default system prompt can be changed by updating the `SYSTEM_PROMPT` constant in `src/index.ts`. |
| 144 | + |
| 145 | +### Styling |
| 146 | + |
| 147 | +The UI styling is contained in the `<style>` section of `public/index.html`. You can modify the CSS variables at the top to quickly change the color scheme. |
| 148 | + |
| 149 | +## Resources |
| 150 | + |
| 151 | +- [Cloudflare Workers Documentation](https://developers.cloudflare.com/workers/) |
| 152 | +- [Cloudflare Workers AI Documentation](https://developers.cloudflare.com/workers-ai/) |
| 153 | +- [Workers AI Models](https://developers.cloudflare.com/workers-ai/models/) |
0 commit comments