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

update to use openai typing #21

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
158 changes: 157 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"eventsource-parser": "^0.0.5",
"framer-motion": "^8.4.3",
"next": "latest",
"openai": "^3.1.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-hook-form": "^7.42.0",
Expand Down
5 changes: 3 additions & 2 deletions pages/api/generate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { OpenAIStream, OpenAIStreamPayload } from "../../utils/OpenAIStream";
import { CreateCompletionRequest } from "openai";
import { OpenAIStream } from "../../utils/OpenAIStream";

if (!process.env.OPENAI_API_KEY) {
throw new Error("Missing env var from OpenAI");
Expand All @@ -17,7 +18,7 @@ const handler = async (req: Request): Promise<Response> => {
return new Response("No prompt in the request", { status: 400 });
}

const payload: OpenAIStreamPayload = {
const payload: CreateCompletionRequest = {
model: "text-davinci-003",
prompt,
temperature: 0.7,
Expand Down
15 changes: 2 additions & 13 deletions utils/OpenAIStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,9 @@ import {
ParsedEvent,
ReconnectInterval,
} from "eventsource-parser";
import { CreateCompletionRequest } from "openai";

export interface OpenAIStreamPayload {
model: string;
prompt: string;
temperature: number;
top_p: number;
frequency_penalty: number;
presence_penalty: number;
max_tokens: number;
stream: boolean;
n: number;
}

export async function OpenAIStream(payload: OpenAIStreamPayload) {
export async function OpenAIStream(payload: CreateCompletionRequest) {
const encoder = new TextEncoder();
const decoder = new TextDecoder();

Expand Down