0

Creates a model response for the given chat conversation.

by
Published Oct 17, 2025
Script groqai Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Groq = {
3
  api_key: string;
4
};
5
/**
6
 * Creates a model response for the given chat conversation.
7
 *
8
 */
9
export async function main(
10
  auth: Groq,
11
  body: {
12
    frequency_penalty?: number;
13
    function_call?: "none" | "auto" | "required" | { name: string };
14
    functions?: { description?: string; name: string; parameters?: {} }[];
15
    logit_bias?: {};
16
    logprobs?: false | true;
17
    max_tokens?: number;
18
    messages:
19
      | { content: string; name?: string; role: "system" }
20
      | {
21
          content:
22
            | string
23
            | { text: string; type: "text" }
24
            | {
25
                image_url: { detail?: "auto" | "low" | "high"; url: string };
26
                type: "image_url";
27
              }[];
28
          name?: string;
29
          role: "user";
30
        }
31
      | {
32
          content?: string;
33
          function_call?: { arguments?: string; name?: string };
34
          name?: string;
35
          role: "assistant";
36
          tool_calls?: {
37
            function: { arguments: string; name: string };
38
            id: string;
39
            type: "function";
40
          }[];
41
        }
42
      | { content: string; role: "tool"; tool_call_id: string }
43
      | { content: string; name: string; role: "function" }[];
44
    model: string;
45
    n?: number;
46
    parallel_tool_calls?: false | true;
47
    presence_penalty?: number;
48
    response_format?: { type?: "text" | "json_object" };
49
    seed?: number;
50
    stop?: string | string[];
51
    stream?: false | true;
52
    temperature?: number;
53
    tool_choice?:
54
      | "none"
55
      | "auto"
56
      | "required"
57
      | { function: { name: string }; type: "function" };
58
    tools?: {
59
      function: { description?: string; name: string; parameters?: {} };
60
      type: "function";
61
    }[];
62
    top_logprobs?: number;
63
    top_p?: number;
64
    user?: string;
65
  },
66
) {
67
  const url = new URL(`https://api.groq.com/openai/v1/chat/completions`);
68

69
  const response = await fetch(url, {
70
    method: "POST",
71
    headers: {
72
      "Content-Type": "application/json",
73
      Authorization: "Bearer " + auth.api_key,
74
    },
75
    body: JSON.stringify(body),
76
  });
77
  if (!response.ok) {
78
    const text = await response.text();
79
    throw new Error(`${response.status} ${text}`);
80
  }
81
  return await response.json();
82
}
83