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

Creates a model response for the given chat conversation.

Created by adam186 363 days ago Viewed 6794 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 136 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