Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace openai-edge with openai #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions app/api/completion/route.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import {
ChatCompletionRequestMessage,
Configuration,
OpenAIApi,
} from "openai-edge";
import OpenAI from "openai";
import { ChatCompletionMessageParam } from "openai/resources/chat";
import { OpenAIStream, StreamingTextResponse } from "ai";
import { CompletionRequestBody } from "@/lib/types";

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

export const runtime = "edge";

// This is the instructions that GPT-4 will use to know how to respond. For more information on
// the difference between a system message and a user message, see:
Expand All @@ -30,7 +24,7 @@ Only respond with a poem, don't make the poem too long.`,
// your prompt.
async function buildUserMessage(
req: Request,
): Promise<ChatCompletionRequestMessage> {
): Promise<ChatCompletionMessageParam> {
const body = await req.json();

// We use zod to validate the request body. To change the data that is sent to the API,
Expand All @@ -47,7 +41,7 @@ async function buildUserMessage(

export async function POST(req: Request) {
// Ask OpenAI for a streaming completion given the prompt
const response = await openai.createChatCompletion({
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
stream: true,
temperature: 0,
Expand Down
Loading