2
Create completion
One script reply has been approved by the moderators Verified

Creates a completion for the provided prompt and parameters.

Created by adam186 741 days ago Viewed 16198 times
1
Submitted by hugo697 Typescript (fetch-only)
Verified 387 days ago
1
type Openai = {
2
  api_key: string;
3
  organization_id: string;
4
};
5
/**
6
 * Create completion
7
 * Creates a completion for the provided prompt and parameters.
8
 */
9
export async function main(
10
  auth: Openai,
11
  body: {
12
    model: string | ("gpt-3.5-turbo-instruct" | "davinci-002" | "babbage-002");
13
    prompt: string | string[] | number[] | number[][];
14
    best_of?: number;
15
    echo?: boolean;
16
    frequency_penalty?: number;
17
    logit_bias?: { [k: string]: number };
18
    logprobs?: number;
19
    max_tokens?: number;
20
    n?: number;
21
    presence_penalty?: number;
22
    seed?: number;
23
    stop?: string | string[];
24
    stream?: boolean;
25
    suffix?: string;
26
    temperature?: number;
27
    top_p?: number;
28
    user?: string;
29
    [k: string]: unknown;
30
  }
31
) {
32
  const url = new URL(`https://api.openai.com/v1/completions`);
33

34
  const response = await fetch(url, {
35
    method: "POST",
36
    headers: {
37
      "OpenAI-Organization": auth.organization_id,
38
      "Content-Type": "application/json",
39
      Authorization: "Bearer " + auth.api_key,
40
    },
41
    body: JSON.stringify(body),
42
  });
43
  if (!response.ok) {
44
    const text = await response.text();
45
    throw new Error(`${response.status} ${text}`);
46
  }
47
  return await response.json();
48
}
49

Other submissions