Unverified Commit f7533e0d authored by Lee Robinson's avatar Lee Robinson Committed by GitHub

docs: Add streaming AI example. (#51382)

Addresses https://github.com/vercel/feedback/issues/32707.
parent 04cd1fd4
...@@ -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) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment