Create completion

Creates a completion for the provided prompt and parameters.

Script openai Verified

by adam186 ยท 12/12/2022

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 376 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
  • Submitted by adam186 Deno
    Created 1009 days ago
    1
    import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
    2
    import {
    3
      Configuration,
    4
      CreateCompletionRequest,
    5
      OpenAIApi,
    6
    } from "npm:[email protected]";
    7
    
    
    8
    /**
    9
     * You can read about the parameters at
    10
     * https://beta.openai.com/docs/api-reference/completions/create
    11
     */
    12
    type Openai = {
    13
      api_key: string;
    14
      organization_id: string;
    15
    };
    16
    export async function main(
    17
      auth: Openai,
    18
      model = "text-davinci-003",
    19
      prompt?: string,
    20
      suffix?: string,
    21
      max_tokens?: number,
    22
      temperature?: number,
    23
      top_p?: number,
    24
      n?: number,
    25
      stream?: boolean,
    26
      logprobs?: number,
    27
      echo?: boolean,
    28
      stop?: string,
    29
      presence_penalty?: number,
    30
      frequency_penalty?: number,
    31
      best_of?: number,
    32
      logit_bias?: object,
    33
    ) {
    34
      const configuration = new Configuration({
    35
        apiKey: auth.api_key,
    36
        organization: auth.organization_id,
    37
      });
    38
      const openai = new OpenAIApi(configuration);
    39
    
    
    40
      const request = removeObjectEmptyFields({
    41
        model,
    42
        prompt,
    43
        suffix,
    44
        max_tokens,
    45
        temperature,
    46
        top_p,
    47
        n,
    48
        stream,
    49
        logprobs,
    50
        echo,
    51
        stop,
    52
        presence_penalty,
    53
        frequency_penalty,
    54
        best_of,
    55
        logit_bias,
    56
      }) as CreateCompletionRequest;
    57
      const response = await openai.createCompletion(request);
    58
      return response.data;
    59
    }
    60