--- url: "https://web.dev/articles/ai-chatbot-promptapi" titulek: "Build a local and offline-capable chatbot with the Prompt API | web.dev" h1: "Build a local and offline-capable chatbot with the Prompt API Stay organized with collections Save and categorize content based on your preferences." popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/ai-chatbot-promptapi" drobecky: ["web.dev", "Resources"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 01:47:04" znaku: 4170 slov: 597 kodovani: "iso-8859-2" sha256: "b7da5c401df3c4395ae7742deb9b7f85ab04e9a0c4b9ea282283c0fd17901750" --- Build a local and offline-capable chatbot with the Prompt API Stay organized with collections Save and categorize content based on your preferences. Christian Liebel Published: January 13, 2025 *This is the last in a three-part series on LLM chatbots. The previous articles discussed the [power of client-side LLMs](/articles/ai-llms-benefits) and walked you through adding a [WebLLM-powered chatbot to a to-do list application](/articles/ai-chatbot-webllm).* Some newer devices ship with large language and other AI models right on the device. Chrome has proposed [integrating built-in AI APIs into the browser](//developer.chrome.com/docs/ai/built-in), with a [number of APIs](//developer.chrome.com/docs/ai/built-in-apis) in different stages of development. Many of these APIs are going through the standards process, so that websites can use the same implementation and model to achieve the maximum inference performance. The Prompt API is one such AI API. To use it, developers are encouraged to sign up for the [Early Preview Program](//forms.gle/ksWMYFtsUSZiC63m9). Once accepted, you'll receive instructions on how to enable the Prompt API in browsers. The Prompt API is available in an [origin trial for Chrome Extensions](//developer.chrome.com/blog/prompt-api-origin-trial), so you can test this API for real extension users. Shared model access The Prompt API behaves similarly to WebLLM. However, there is no model selection this time (you have to use the LLM that ships with the browser). When enabling built-in AI, Chrome downloads a foundation model into the browser. This model can then be shared across multiple origins and runs with maximum performance. There is a [GitHub issue](//github.com/webmachinelearning/prompt-api/issues/8) where a developer has requested to add a model selection feature. Set up the conversation You can start the message conversation in the exact same way, but the Prompt API also offers a shorthand syntax to specify the system prompt. Start the language model session using the `create()` method on the `LanguageModel` interface: `const session = await LanguageModel.create({ initialPrompt: [ { type: 'system', content: `You are a helpful assistant. You will answer questions related to the user's to-do list. Decline all other requests not related to the user's todos. This is the to-do list in JSON: ${JSON.stringify(todos)}`, }, ], });` Answer your first question Instead of having a configuration object for configuring streaming, the Prompt API offers two separate methods: `prompt()` returns the full string `promptStreaming()` returns an async iterable. In contrast with WebLLM, the Prompt API responds with the full string response, so you don't have to combine the results yourself. If no other origin has triggered the model download before, your first request may take a very long time while the model is downloaded into your browser. If the model is already available, inference starts immediately. `const stream = session.promptStreaming("How many open todos do I have?"); for await (const reply of stream) { console.log(reply); }` Demo [To-do application with Prompt API](https://googlechrome.github.io/samples/todo-ai/dist/todo-ai/browser/) [Source on GitHub](https://github.com/GoogleChrome/samples/tree/gh-pages/todo-ai/) Summary Integrating LLMs into applications can significantly enhance the user experience. While cloud services offer higher-quality models and high inference performance regardless of the user's device, on-device solutions, such as WebLLM and Chrome's Prompt API, are offline-capable, improve privacy, and save cost compared to cloud-based alternatives. Try out these new APIs and make your web applications smarter. Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2025-01-13 UTC.