Create chat completion

Creates a model response for the given chat conversation.

Script openai Verified

by adam186 ยท 4/18/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 372 days ago
1
type Openai = {
2
  api_key: string;
3
  organization_id: string;
4
};
5
/**
6
 * Create chat completion
7
 * Creates a model response for the given chat conversation.
8
 */
9
export async function main(
10
  auth: Openai,
11
  body: {
12
    messages: (
13
      | { content: string; role: "system"; name?: string; [k: string]: unknown }
14
      | {
15
          content:
16
            | string
17
            | (
18
                | { type: "text"; text: string; [k: string]: unknown }
19
                | {
20
                    type: "image_url";
21
                    image_url: {
22
                      url: string;
23
                      detail?: "auto" | "low" | "high";
24
                      [k: string]: unknown;
25
                    };
26
                    [k: string]: unknown;
27
                  }
28
              )[];
29
          role: "user";
30
          name?: string;
31
          [k: string]: unknown;
32
        }
33
      | {
34
          content?: string;
35
          role: "assistant";
36
          name?: string;
37
          tool_calls?: {
38
            id: string;
39
            type: "function";
40
            function: { name: string; arguments: string; [k: string]: unknown };
41
            [k: string]: unknown;
42
          }[];
43
          function_call?: {
44
            arguments: string;
45
            name: string;
46
            [k: string]: unknown;
47
          };
48
          [k: string]: unknown;
49
        }
50
      | {
51
          role: "tool";
52
          content: string;
53
          tool_call_id: string;
54
          [k: string]: unknown;
55
        }
56
      | {
57
          role: "function";
58
          content: string;
59
          name: string;
60
          [k: string]: unknown;
61
        }
62
    )[];
63
    model:
64
      | string
65
      | (
66
          | "gpt-4-0125-preview"
67
          | "gpt-4-turbo-preview"
68
          | "gpt-4-1106-preview"
69
          | "gpt-4-vision-preview"
70
          | "gpt-4"
71
          | "gpt-4-0314"
72
          | "gpt-4-0613"
73
          | "gpt-4-32k"
74
          | "gpt-4-32k-0314"
75
          | "gpt-4-32k-0613"
76
          | "gpt-3.5-turbo"
77
          | "gpt-3.5-turbo-16k"
78
          | "gpt-3.5-turbo-0301"
79
          | "gpt-3.5-turbo-0613"
80
          | "gpt-3.5-turbo-1106"
81
          | "gpt-3.5-turbo-0125"
82
          | "gpt-3.5-turbo-16k-0613"
83
        );
84
    frequency_penalty?: number;
85
    logit_bias?: { [k: string]: number };
86
    logprobs?: boolean;
87
    top_logprobs?: number;
88
    max_tokens?: number;
89
    n?: number;
90
    presence_penalty?: number;
91
    response_format?: { type?: "text" | "json_object"; [k: string]: unknown };
92
    seed?: number;
93
    stop?: string | string[];
94
    stream?: boolean;
95
    temperature?: number;
96
    top_p?: number;
97
    tools?: {
98
      type: "function";
99
      function: {
100
        description?: string;
101
        name: string;
102
        parameters?: { [k: string]: unknown };
103
        [k: string]: unknown;
104
      };
105
      [k: string]: unknown;
106
    }[];
107
    tool_choice?:
108
      | ("none" | "auto")
109
      | {
110
          type: "function";
111
          function: { name: string; [k: string]: unknown };
112
          [k: string]: unknown;
113
        };
114
    user?: string;
115
    function_call?: ("none" | "auto") | { name: string; [k: string]: unknown };
116
    functions?: {
117
      description?: string;
118
      name: string;
119
      parameters?: { [k: string]: unknown };
120
      [k: string]: unknown;
121
    }[];
122
    [k: string]: unknown;
123
  }
124
) {
125
  const url = new URL(`https://api.openai.com/v1/chat/completions`);
126

127
  const response = await fetch(url, {
128
    method: "POST",
129
    headers: {
130
      "OpenAI-Organization": auth.organization_id,
131
      "Content-Type": "application/json",
132
      Authorization: "Bearer " + auth.api_key,
133
    },
134
    body: JSON.stringify(body),
135
  });
136
  if (!response.ok) {
137
    const text = await response.text();
138
    throw new Error(`${response.status} ${text}`);
139
  }
140
  return await response.json();
141
}
142

Other submissions
  • Submitted by sindre svendby964 Deno
    Created 1040 days ago
    1
    import type { Resource } from "https://deno.land/x/[email protected]/mod.ts";
    2
    import { removeObjectEmptyFields } from 'https://deno.land/x/[email protected]/mod.ts'
    3
    import { Configuration, CreateChatCompletionRequest, OpenAIApi } from "npm:[email protected]"
    4
    
    
    5
    /**
    6
     * You can read about the parameters at 
    7
     * https://platform.openai.com/docs/api-reference/chat/create
    8
     */
    9
    export async function main(
    10
      auth: Resource<'openai'>,
    11
      messages: {
    12
        role: 'assistant' | 'system' | 'user',
    13
        content: string,
    14
        name?: string
    15
      }[],
    16
      model: 'gpt-3.5-turbo' | 'gpt-3.5-turbo-16k' | 'gpt-4' | 'gpt-4-32k',
    17
      frequency_penalty?: number,
    18
      logit_bias?: object,
    19
      max_tokens?: number,
    20
      n?: number,
    21
      presence_penalty?: number,
    22
      stop?: string,
    23
      stream?: boolean,
    24
      temperature?: number,
    25
      top_p?: number,
    26
      user: string | undefined = Deno.env.get('WM_USERNAME'),
    27
    ) {
    28
      const configuration = new Configuration({
    29
        apiKey: auth.api_key,
    30
        organization: auth.organization_id
    31
      });
    32
      const openai = new OpenAIApi(configuration);
    33
      
    34
      const request: CreateChatCompletionRequest = {
    35
        messages,
    36
        model,
    37
        frequency_penalty,
    38
        logit_bias,
    39
        max_tokens,
    40
        n,
    41
        presence_penalty,
    42
        stop,
    43
        stream,
    44
        temperature,
    45
        top_p,
    46
        user
    47
      }
    48
      const response = await openai.createChatCompletion(
    49
        removeObjectEmptyFields(request)
    50
      )
    51
      return response.data
    52
    }
  • Submitted by adam186 Deno
    Created 1004 days ago
    1
    import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
    2
    import {
    3
      Configuration,
    4
      CreateChatCompletionRequest,
    5
      OpenAIApi,
    6
    } from "npm:[email protected]";
    7
    
    
    8
    /**
    9
     * You can read about the parameters at
    10
     * https://platform.openai.com/docs/api-reference/chat/create
    11
     */
    12
    type Openai = {
    13
      api_key: string;
    14
      organization_id: string;
    15
    };
    16
    export async function main(
    17
      auth: Openai,
    18
      messages: {
    19
        role: "assistant" | "system" | "user";
    20
        content: string;
    21
        name?: string;
    22
      }[],
    23
      model: "gpt-3.5-turbo" | "gpt-3.5-turbo-0301" = "gpt-3.5-turbo",
    24
      frequency_penalty?: number,
    25
      logit_bias?: object,
    26
      max_tokens?: number,
    27
      n?: number,
    28
      presence_penalty?: number,
    29
      stop?: string,
    30
      stream?: boolean,
    31
      temperature?: number,
    32
      top_p?: number,
    33
      user?: string,
    34
    ) {
    35
      const configuration = new Configuration({
    36
        apiKey: auth.api_key,
    37
        organization: auth.organization_id,
    38
      });
    39
      const openai = new OpenAIApi(configuration);
    40
    
    
    41
      const request: CreateChatCompletionRequest = {
    42
        messages,
    43
        model,
    44
        frequency_penalty,
    45
        logit_bias,
    46
        max_tokens,
    47
        n,
    48
        presence_penalty,
    49
        stop,
    50
        stream,
    51
        temperature,
    52
        top_p,
    53
        user,
    54
      };
    55
      const response = await openai.createChatCompletion(
    56
        removeObjectEmptyFields(request),
    57
      );
    58
      return response.data;
    59
    }
    60
    
    
  • Submitted by mydinh2910934 Deno
    Created 975 days ago
    1
    // import * as wmill from 'https://deno.land/x/windmill/index.ts'
    2
    
    
    3
    export async function main(x: string) {
    4
        return x
    5
    }
  • Submitted by mydinh2910934 Deno
    Created 975 days ago
    1
    // import * as wmill from 'https://deno.land/x/windmill/index.ts'
    2
    
    
    3
    export async function main(x: string) {
    4
        return x
    5
    }