From c8aed0e224a379e216e02b1ebb6cd8ddf7716dfd Mon Sep 17 00:00:00 2001 From: tedspare Date: Wed, 1 Mar 2023 16:38:36 -0500 Subject: [PATCH] Upgrade text model to gpt-3.5-turbo (ChatGPT) --- pages/api/generate.ts | 4 ++-- utils/OpenAIStream.ts | 13 ++++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/pages/api/generate.ts b/pages/api/generate.ts index 9f2af4a8..c8439ca8 100644 --- a/pages/api/generate.ts +++ b/pages/api/generate.ts @@ -18,8 +18,8 @@ const handler = async (req: Request): Promise => { } const payload: OpenAIStreamPayload = { - model: "text-davinci-003", - prompt, + model: "gpt-3.5-turbo", + messages: [{ role: "user", content: prompt }], temperature: 0.7, top_p: 1, frequency_penalty: 0, diff --git a/utils/OpenAIStream.ts b/utils/OpenAIStream.ts index 31a51797..b0b0d73a 100644 --- a/utils/OpenAIStream.ts +++ b/utils/OpenAIStream.ts @@ -4,9 +4,16 @@ import { ReconnectInterval, } from "eventsource-parser"; +export type ChatGPTAgent = "user" | "system"; + +export interface ChatGPTMessage { + role: ChatGPTAgent; + content: string; +} + export interface OpenAIStreamPayload { model: string; - prompt: string; + messages: ChatGPTMessage[]; temperature: number; top_p: number; frequency_penalty: number; @@ -22,7 +29,7 @@ export async function OpenAIStream(payload: OpenAIStreamPayload) { let counter = 0; - const res = await fetch("https://api.openai.com/v1/completions", { + const res = await fetch("https://api.openai.com/v1/chat/completions", { headers: { "Content-Type": "application/json", Authorization: `Bearer ${process.env.OPENAI_API_KEY ?? ""}`, @@ -44,7 +51,7 @@ export async function OpenAIStream(payload: OpenAIStreamPayload) { } try { const json = JSON.parse(data); - const text = json.choices[0].text; + const text = json.choices[0].delta?.content || ""; if (counter < 2 && (text.match(/\n/) || []).length) { // this is a prefix character (i.e., "\n\n"), do nothing return;