Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
f7533e0d
Unverified
Commit
f7533e0d
authored
Jun 16, 2023
by
Lee Robinson
Committed by
GitHub
Jun 16, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
docs: Add streaming AI example. (#51382)
Addresses
https://github.com/vercel/feedback/issues/32707
.
parent
04cd1fd4
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
0 deletions
+54
-0
10-router-handlers.mdx
...ilding-your-application/01-routing/10-router-handlers.mdx
+54
-0
No files found.
docs/02-app/01-building-your-application/01-routing/10-router-handlers.mdx
View file @
f7533e0d
...
@@ -388,6 +388,60 @@ export async function GET(request, { params }) {
...
@@ -388,6 +388,60 @@ export async function GET(request, { params }) {
### Streaming
### Streaming
Streaming is commonly used in combination with Large Language Models (LLMs), such an OpenAI, for AI-generated content. Learn more about the [AI SDK](https://sdk.vercel.ai/docs).
```ts filename="app/api/completion/route.ts" switcher
import { Configuration, OpenAIApi } from '
openai
-
edge
'
import { OpenAIStream, StreamingTextResponse } from '
ai
'
const config = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
})
const openai = new OpenAIApi(config)
export const runtime = '
edge
'
export async function POST(req: Request) {
const { prompt } = await req.json()
const response = await openai.createCompletion({
model: '
text
-
davinci
-
003
',
stream: true,
temperature: 0.6,
prompt: '
What
is
Next
.
js
?
',
})
const stream = OpenAIStream(response)
return new StreamingTextResponse(stream)
}
```
```js filename="app/api/completion/route.js" switcher
import { Configuration, OpenAIApi } from '
openai
-
edge
'
import { OpenAIStream, StreamingTextResponse } from '
ai
'
const config = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
})
const openai = new OpenAIApi(config)
export const runtime = '
edge
'
export async function POST(req) {
const { prompt } = await req.json()
const response = await openai.createCompletion({
model: '
text
-
davinci
-
003
',
stream: true,
temperature: 0.6,
prompt: '
What
is
Next
.
js
?
',
})
const stream = OpenAIStream(response)
return new StreamingTextResponse(stream)
}
```
These abstractions use the Web APIs to create a stream. You can also use the underlying Web APIs directly.
```ts filename="app/api/route.ts" switcher
```ts filename="app/api/route.ts" switcher
// https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#convert_async_iterator_to_stream
// https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#convert_async_iterator_to_stream
function iteratorToStream(iterator: any) {
function iteratorToStream(iterator: any) {
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment