-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[Workers AI]Streaming docs on getting started #19018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| --- | ||
| title: Streaming | ||
| pcx_content_type: get-started | ||
| sidebar: | ||
| order: 4 | ||
| --- | ||
|
|
||
| Streaming allows you to receive partial responses from Workers AI's text generation models in real-time using **Server-Sent Events (SSE)**. By enabling streaming, you can improve user experiences in applications that rely on immediate feedback, such as chatbots or live content generation. | ||
|
|
||
| To enable streaming on Workers AI, set the `stream` parameter to `true` in your request. This changes the response format and MIME type to `text/event-stream`, allowing tokens to be sent incrementally. | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Using streaming with REST API | ||
|
|
||
| Here's an example of enabling streaming with Workers AI using REST API: | ||
|
|
||
| ```bash | ||
| curl -X POST \ | ||
| "https://api.cloudflare.com/client/v4/accounts/<account>/ai/run/@cf/meta/llama-2-7b-chat-int8" \ | ||
| -H "Authorization: Bearer <token>" \ | ||
| -H "Content-Type:application/json" \ | ||
| -d '{ "prompt": "where is new york?", "stream": true }' | ||
| ``` | ||
|
|
||
| **Response:** | ||
|
|
||
| ```plaintext | ||
| data: {"response":"New"} | ||
|
|
||
| data: {"response":" York"} | ||
|
|
||
| data: {"response":" is"} | ||
|
|
||
| data: {"response":" located"} | ||
|
|
||
| data: {"response":" in"} | ||
|
|
||
| data: {"response":" the"} | ||
|
|
||
| ... | ||
|
|
||
| data: [DONE] | ||
| ``` | ||
|
|
||
| The `data: [DONE]` signal indicates the end of the stream. | ||
|
|
||
| ### Streaming in a Worker Script | ||
|
|
||
| You can also use streaming directly within a Cloudflare Worker: | ||
|
|
||
| ```javascript | ||
| import { Ai } from "@cloudflare/ai"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not anymore |
||
|
|
||
| export default { | ||
| async fetch(request, env, ctx) { | ||
| const ai = new Ai(env.AI, { sessionOptions: { ctx: ctx } }); | ||
| const stream = await ai.run("@cf/meta/llama-2-7b-chat-int8", { | ||
| prompt: "where is new york?", | ||
| stream: true, | ||
| }); | ||
| return new Response(stream, { | ||
| headers: { "content-type": "text/event-stream" }, | ||
| }); | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| ### Client-side: Consuming the event stream | ||
|
|
||
| If you want to consume the streamed output in a browser, you can use the following JavaScript code with an HTML page or a frontend framework (e.g., React, Vue): | ||
daisyfaithauma marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```javascript | ||
| const source = new EventSource("/worker-endpoint"); | ||
|
|
||
| source.onmessage = (event) => { | ||
| if (event.data === "[DONE]") { | ||
| // Close the connection to prevent automatic reconnection | ||
| source.close(); | ||
| return; | ||
| } | ||
|
|
||
| const data = JSON.parse(event.data); | ||
| document.getElementById("output").innerHTML += data.response; | ||
| }; | ||
| ``` | ||
|
|
||
| The above code can be easily integrated into simple HTML pages or complex SPAs using frameworks like React, Angular, or Vue. For example, in React, you can manage the `EventSource` connection in a `useEffect` hook and update the state incrementally as data is streamed. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Strike easily here. |
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update models?