Advertisement
elliottchong

Untitled

Sep 24th, 2023
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { OpenAIApi, Configuration } from "openai-edge";
  2. import { OpenAIStream, StreamingTextResponse } from "ai";
  3. // /api/completion
  4. const config = new Configuration({
  5.   apiKey: process.env.OPENAI_API_KEY,
  6. });
  7.  
  8. const openai = new OpenAIApi(config);
  9.  
  10. export async function POST(req: Request) {
  11.   // extract the prompt from the body
  12.   const { prompt } = await req.json();
  13.  
  14.   const response = await openai.createChatCompletion({
  15.     model: "gpt-3.5-turbo",
  16.     messages: [
  17.       {
  18.         role: "system",
  19.         content: `You are a helpful AI embedded in a notion text editor app that is used to autocomplete sentences
  20.             The traits of AI include expert knowledge, helpfulness, cleverness, and articulateness.
  21.         AI is a well-behaved and well-mannered individual.
  22.         AI is always friendly, kind, and inspiring, and he is eager to provide vivid and thoughtful responses to the user.`,
  23.       },
  24.       {
  25.         role: "user",
  26.         content: `
  27.         I am writing a piece of text in a notion text editor app.
  28.         Help me complete my train of thought here: ##${prompt}##
  29.         keep the tone of the text consistent with the rest of the text.
  30.         keep the response short and sweet.
  31.         `,
  32.       },
  33.     ],
  34.     stream: true,
  35.   });
  36.   const stream = OpenAIStream(response);
  37.   return new StreamingTextResponse(stream);
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement