-
Notifications
You must be signed in to change notification settings - Fork 1
/
openai.ts
48 lines (40 loc) · 1.36 KB
/
openai.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import OpenAI from "openai";
import { OpenAIStream } from "ai";
export type AIMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam;
type Params = {
env: Record<string, any>;
messages: AIMessage[];
onStartCallback: () => void;
onTokenCallback: (token: string) => void;
};
export async function getChatCompletionResponse(params: Params) {
const { env, messages, onStartCallback, onTokenCallback } = params;
try {
const apiKey = env.OPENAI_API_KEY as string | undefined;
const organization = env.OPENAI_API_ORGANIZATION as string | undefined;
if (!apiKey) {
throw new Error("OPENAI_API_KEY is not set");
}
const openai = organization
? new OpenAI({ apiKey, organization })
: new OpenAI({ apiKey });
const openAIParams: OpenAI.Chat.ChatCompletionCreateParams = {
model: "gpt-3.5-turbo",
stream: true,
messages,
};
const openaiResponse = await openai.chat.completions.create(openAIParams);
const stream = OpenAIStream(openaiResponse, {
onStart: async () => onStartCallback(),
onToken: async (token) => onTokenCallback(token),
});
// @ts-ignore
for await (const _ of stream) {
// no-op, just read the stream, onToken callback above will handle the tokens
}
} catch (e) {
console.error("Error while executing OpenAI call", e);
throw e;
}
return null;
}