-
Notifications
You must be signed in to change notification settings - Fork 0
/
completion_sse.ts
43 lines (42 loc) · 1.09 KB
/
completion_sse.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
import { fromFetchSSEStream } from "copilot-js/dist/helpers/fromFetchSSEStream";
import { createFilter } from "./filter";
export const callCompletion_sse = ({
text,
precedingText,
followingText,
callback,
apiKey,
}: {
text: string;
precedingText?: string;
followingText?: string;
callback: (output: string) => void;
apiKey: string;
}) => {
const filter = createFilter(callback, !!precedingText);
const controller = new AbortController();
const responsePromise = fetch("https://api.openai.com/v1/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({
model: "text-davinci-003",
stream: true,
prompt: `#instuction${text}\n#output\n${precedingText}`,
max_tokens: 2000,
temperature: 0.5,
suffix: followingText,
n: 1,
}),
signal: controller.signal,
});
return fromFetchSSEStream<{ choices: { text: string }[] }>({
callback: (data) => {
filter(data.choices[0].text);
},
controller,
responsePromise,
});
};