Skip to content

Commit

Permalink
Upgrade text model to gpt-3.5-turbo (ChatGPT)
Browse files Browse the repository at this point in the history
  • Loading branch information
tedspare committed Mar 1, 2023
1 parent 2a55bb8 commit c8aed0e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pages/api/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const handler = async (req: Request): Promise<Response> => {
}

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,
Expand Down
13 changes: 10 additions & 3 deletions utils/OpenAIStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 ?? ""}`,
Expand All @@ -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;
Expand Down

0 comments on commit c8aed0e

Please sign in to comment.