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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
export let openAiTextCompletion = async (params: {
/** https://beta.openai.com/account/api-keys */
apiKey: string,
/** Optional. https://beta.openai.com/account/org-settings */
org?: string,
// REST args, see https://beta.openai.com/docs/api-reference/completions/create
prompt: string,
model?: string,
suffix?: string,
max_tokens?: number,
temperature?: number,
top_p?: number,
n?: number,
stream?: false,
logprobs?: number,
echo?: boolean,
stop?: string | string[],
presence_penalty?: number,
frequency_penalty?: number,
best_of?: number,
logit_bias?: { [key: string]: number },
user?: string,
}) => {
if (!params.apiKey) {
throw new Error(
"Please provide 'apiKey' param. See: https://beta.openai.com/account/api-keys "
);
}
const { apiKey, org, ...args } = params;
args.stream = false;
const response = await fetchJSON("https://api.openai.com/v1/completions", {
method: "POST",
headers: {
Authorization: `Bearer ${params.apiKey}`,
...(params.org ? { "OpenAI-Organization": params.org } : {}),
},
body: JSON.stringify(args),
});
return response as Response;
};
interface Response {
id: string;
object: "text-completion";
created: number;
model: string;
choices: {
text: string;
index: number;
logprobs: any;
finish_reason: string;
}[];
usage: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
}
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023