Edits history of script submission #16299 for ' Creates a model response for the given chat conversation. (groqai)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Groq = {
      api_key: string;
    };
    /**
     * Creates a model response for the given chat conversation.
     *
     */
    export async function main(
      auth: Groq,
      body: {
        frequency_penalty?: number;
        function_call?: "none" | "auto" | "required" | { name: string };
        functions?: { description?: string; name: string; parameters?: {} }[];
        logit_bias?: {};
        logprobs?: false | true;
        max_tokens?: number;
        messages:
          | { content: string; name?: string; role: "system" }
          | {
              content:
                | string
                | { text: string; type: "text" }
                | {
                    image_url: { detail?: "auto" | "low" | "high"; url: string };
                    type: "image_url";
                  }[];
              name?: string;
              role: "user";
            }
          | {
              content?: string;
              function_call?: { arguments?: string; name?: string };
              name?: string;
              role: "assistant";
              tool_calls?: {
                function: { arguments: string; name: string };
                id: string;
                type: "function";
              }[];
            }
          | { content: string; role: "tool"; tool_call_id: string }
          | { content: string; name: string; role: "function" }[];
        model: string;
        n?: number;
        parallel_tool_calls?: false | true;
        presence_penalty?: number;
        response_format?: { type?: "text" | "json_object" };
        seed?: number;
        stop?: string | string[];
        stream?: false | true;
        temperature?: number;
        tool_choice?:
          | "none"
          | "auto"
          | "required"
          | { function: { name: string }; type: "function" };
        tools?: {
          function: { description?: string; name: string; parameters?: {} };
          type: "function";
        }[];
        top_logprobs?: number;
        top_p?: number;
        user?: string;
      },
    ) {
      const url = new URL(`https://api.groq.com/openai/v1/chat/completions`);
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.api_key,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago