Unverified Commit b149fb70 authored by Dom Sip's avatar Dom Sip Committed by GitHub

docs: Update Route Handlers AI example code snippet (#52127)

changing AI example model from `text-davinci-003` to `gpt-3.5-turbo`
which is in a `chat/route.ts` format.

The alternative but with "completition" style would be a bit longer code
like:

```
// app/api/completion/route.ts

import { Configuration, OpenAIApi } from 'openai-edge'
import { OpenAIStream, StreamingTextResponse } from 'ai'

export const runtime = 'edge'

const apiConfig = new Configuration({
  apiKey: process.env.OPENAI_API_KEY!,
})

const openai = new OpenAIApi(apiConfig)

function buildPrompt(prompt: string) {
  return prompt.split('\n').map((message) => ({
    role: 'user',
    content: message,
  }));
}

export async function POST(req: Request) {
  
  // Extract the `prompt` from the body of the request
  const { prompt } = await req.json();

  // Request the OpenAI API for the response based on the prompt
  const response = await openai.createChatCompletion({
    model: 'gpt-3.5-turbo',
    stream: true,
    messages: buildPrompt(prompt),
    max_tokens: 500,
    temperature: 0.7,
    top_p: 1,
    frequency_penalty: 1,
    presence_penalty: 1,
  })

  // Convert the response into a friendly text-stream
  const stream = OpenAIStream(response)

  // Respond with the stream
  return new StreamingTextResponse(stream)
}

```

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

---------
Co-authored-by: 's avatarLee Robinson <me@leerob.io>
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 71f08ef8
...@@ -390,52 +390,74 @@ export async function GET(request, { params }) { ...@@ -390,52 +390,74 @@ export async function GET(request, { params }) {
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). 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 ```ts filename="app/api/chat/route.ts" switcher
import { Configuration, OpenAIApi } from 'openai-edge' import { Configuration, OpenAIApi } from 'openai-edge'
import { OpenAIStream, StreamingTextResponse } from 'ai' import { OpenAIStream, StreamingTextResponse } from 'ai'
const config = new Configuration({ export const runtime = 'edge'
apiKey: process.env.OPENAI_API_KEY,
const apiConfig = new Configuration({
apiKey: process.env.OPENAI_API_KEY!,
}) })
const openai = new OpenAIApi(config)
export const runtime = 'edge' const openai = new OpenAIApi(apiConfig)
export async function POST(req: Request) { export async function POST(req: Request) {
const { prompt } = await req.json() // Extract the `messages` from the body of the request
const response = await openai.createCompletion({ const { messages } = await req.json()
model: 'text-davinci-003',
// Request the OpenAI API for the response based on the prompt
const response = await openai.createChatCompletion({
model: 'gpt-3.5-turbo',
stream: true, stream: true,
temperature: 0.6, messages: messages,
prompt: 'What is Next.js?', max_tokens: 500,
temperature: 0.7,
top_p: 1,
frequency_penalty: 1,
presence_penalty: 1,
}) })
// Convert the response into a friendly text-stream
const stream = OpenAIStream(response) const stream = OpenAIStream(response)
// Respond with the stream
return new StreamingTextResponse(stream) return new StreamingTextResponse(stream)
} }
``` ```
```js filename="app/api/completion/route.js" switcher ```js filename="app/api/chat/route.js" switcher
import { Configuration, OpenAIApi } from 'openai-edge' import { Configuration, OpenAIApi } from 'openai-edge'
import { OpenAIStream, StreamingTextResponse } from 'ai' import { OpenAIStream, StreamingTextResponse } from 'ai'
const config = new Configuration({ export const runtime = 'edge'
const apiConfig = new Configuration({
apiKey: process.env.OPENAI_API_KEY, apiKey: process.env.OPENAI_API_KEY,
}) })
const openai = new OpenAIApi(config)
export const runtime = 'edge' const openai = new OpenAIApi(apiConfig)
export async function POST(req) { export async function POST(req) {
const { prompt } = await req.json() // Extract the `messages` from the body of the request
const response = await openai.createCompletion({ const { messages } = await req.json()
model: 'text-davinci-003',
// Request the OpenAI API for the response based on the prompt
const response = await openai.createChatCompletion({
model: 'gpt-3.5-turbo',
stream: true, stream: true,
temperature: 0.6, messages: messages,
prompt: 'What is Next.js?', max_tokens: 500,
temperature: 0.7,
top_p: 1,
frequency_penalty: 1,
presence_penalty: 1,
}) })
// Convert the response into a friendly text-stream
const stream = OpenAIStream(response) const stream = OpenAIStream(response)
// Respond with the stream
return new StreamingTextResponse(stream) return new StreamingTextResponse(stream)
} }
``` ```
......
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