0

I am trying to do a simple request to ChatGPT but get a 200 response with no content

{
"answer": {}
}

Why can i not read the completion (also, how can i stream it?)

import {
    ChatCompletionRequestMessageRoleEnum,
    Configuration,
    OpenAIApi,
} from "openai-edge";
import config from "../../config";
import { NextApiRequest, NextApiResponse } from "next";

const openAIConfig = new Configuration({ apiKey: config.openAIKey });
const openAI = new OpenAIApi(openAIConfig);

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse<any>
) {
    let messages = [
      {
        role: ChatCompletionRequestMessageRoleEnum.System,
        content: "Hello, I'm a chatbot",
      },
      {
        role: ChatCompletionRequestMessageRoleEnum.User,
        content: "Hello, is Pluto really a planet?",
      },
    ];
    const completion = await openAI.createChatCompletion({
      model: "gpt-3.5-turbo",
      stream: false,
      messages,
    });

    res.status(200).send(completion);
}

1 Answer 1

1

I used the wrong convention for the POST.

i created the following file and everything worked

~/app/api/chat/route.ts

import {
  ChatCompletionRequestMessageRoleEnum,
  ChatCompletionRequestMessage,
  Configuration,
  OpenAIApi,
} from "openai-edge";
import config from "../../../config";
import { OpenAIStream, StreamingTextResponse } from "ai";

const openAIConfig = new Configuration({ apiKey: config.openAIKey });
const openAI = new OpenAIApi(openAIConfig);

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

  let setupMessage: ChatCompletionRequestMessage[] = [
    {
      role: ChatCompletionRequestMessageRoleEnum.System,
      content: "You are a helpful chatbot, who thinks they are a dog that can talk.",
    },
    {
      role: ChatCompletionRequestMessageRoleEnum.User,
      content: "Hello, do you like playing in the park?",
    },
  ];
  
  const completion = await openAI.createChatCompletion({
    model: "gpt-3.5-turbo",
    stream: true,
    messages: setupMessage,
  });
  
  const stream = await OpenAIStream(completion);

  return new StreamingTextResponse(stream);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.