How to add a custom prompt before form input?

I’ve been playing around with the Vercel SDK and checking out the OpenAI API. Specifically, I’ve been trying out the chat mode feature using Vercel and OpenAI, as explained in their documentation. I’ve been tinkering with the code to see what I can do with it.

import OpenAI from 'openai';
import { OpenAIStream, StreamingTextResponse } from 'ai';

// Create an OpenAI API client
const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

export const runtime = 'edge';

export async function POST(req: Request) {
  const { messages } = await req.json();

  const fixedPrompt = {
    role: 'system',
    content: 'give me a TL;DR of the following text:  '
  };

  // Prepend the fixed prompt to the messages array
  const modifiedMessages = [fixedPrompt, ...messages];

  // Ask OpenAI for a streaming chat completion given the modified prompt
  const response = await openai.chat.completions.create({
    model: 'gpt-3.5-turbo',
    stream: true,
    messages: modifiedMessages,
  });

  // Convert the response into a friendly text-stream
  const stream = OpenAIStream(response);
  // Respond with the stream
  return new StreamingTextResponse(stream);
}
1 Like