0

Openai Chat Completions

by
Published Oct 17, 2025
Script deep_infra Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Deepinfra = {
3
  token: string;
4
};
5
/**
6
 * Openai Chat Completions
7
 *
8
 */
9
export async function main(
10
  auth: Deepinfra,
11
  x_deepinfra_source: string,
12
  user_agent: string,
13
  body: {
14
    model: string;
15
    messages:
16
      | { role?: "tool"; content: string; tool_call_id: string }
17
      | {
18
          role?: "assistant";
19
          content?: string;
20
          name?: string;
21
          tool_calls?: {
22
            id: string;
23
            type: string;
24
            function: { name: string; arguments: string };
25
          }[];
26
        }
27
      | {
28
          role?: "user";
29
          content:
30
            | string
31
            | { type: "text"; text: string }
32
            | {
33
                type: "image_url";
34
                image_url: { url: string; detail?: "auto" | "low" | "high" };
35
              }[];
36
          name?: string;
37
        }
38
      | { role?: "system"; content: string; name?: string }[];
39
    stream?: false | true;
40
    temperature?: number;
41
    top_p?: number;
42
    min_p?: number;
43
    top_k?: number;
44
    max_tokens?: number;
45
    stop?: string | string[];
46
    n?: number;
47
    presence_penalty?: number;
48
    frequency_penalty?: number;
49
    tools?: {
50
      type?: string;
51
      function: { name: string; description?: string; parameters?: {} };
52
    }[];
53
    tool_choice?: string;
54
    response_format?: { type?: "text" | "json_object" };
55
    repetition_penalty?: number;
56
    user?: string;
57
    seed?: number;
58
  },
59
) {
60
  const url = new URL(`https://api.deepinfra.com/v1/openai/chat/completions`);
61

62
  const response = await fetch(url, {
63
    method: "POST",
64
    headers: {
65
      "x-deepinfra-source": x_deepinfra_source,
66
      "user-agent": user_agent,
67
      "Content-Type": "application/json",
68
      Authorization: "Bearer " + auth.token,
69
    },
70
    body: JSON.stringify(body),
71
  });
72
  if (!response.ok) {
73
    const text = await response.text();
74
    throw new Error(`${response.status} ${text}`);
75
  }
76
  return await response.json();
77
}
78