openai-node

Форк
0
/
stream-to-client-next.ts 
38 строк · 1.2 Кб
1
import OpenAI from 'openai';
2
import type { NextApiRequest, NextApiResponse } from 'next';
3

4
// This file demonstrates how to stream from a Next.JS server as
5
// a new-line separated JSON-encoded stream. This file cannot be run
6
// without Next.JS scaffolding.
7

8
export const runtime = 'edge';
9

10
// This endpoint can be called with:
11
//
12
//   curl 127.0.0.1:3000 -N -X POST -H 'Content-Type: text/plain' \
13
//     --data 'Can you explain why dogs are better than cats?'
14
//
15
// Or consumed with fetch:
16
//
17
//   fetch('http://localhost:3000', {
18
//     method: 'POST',
19
//     body: 'Tell me why dogs are better than cats',
20
//   }).then(async res => {
21
//     const runner = ChatCompletionStreamingRunner.fromReadableStream(res)
22
//   })
23
//
24
// See examples/stream-to-client-browser.ts for a more complete example.
25
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
26
  const openai = new OpenAI();
27

28
  const stream = openai.beta.chat.completions.stream({
29
    model: 'gpt-3.5-turbo',
30
    stream: true,
31
    // @ts-ignore
32
    messages: [{ role: 'user', content: await req.text() }],
33
  });
34

35
  return res.send(stream.toReadableStream());
36
  // @ts-ignore -- Or, for the app router:
37
  return new Response(stream.toReadableStream());
38
}
39

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.