Edits history of script submission #20976 for ' Create chat completion (togetherai)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Togetherai = {
      api_key: string;
    };
    /**
     * Create chat completion
     * Query a chat model.
     */
    export async function main(
      auth: Togetherai,
      body: {
        messages: {
          role: "system" | "user" | "assistant" | "tool";
          content:
            | string
            | { type: "text"; text: string }
            | ({ type: "image_url"; image_url: { url: string } }[] & string);
        }[];
        model: string;
        max_tokens?: number;
        stop?: string[];
        temperature?: number;
        top_p?: number;
        top_k?: number;
        repetition_penalty?: number;
        stream?: false | true;
        logprobs?: number;
        echo?: false | true;
        n?: number;
        min_p?: number;
        presence_penalty?: number;
        frequency_penalty?: number;
        logit_bias?: {};
        seed?: number;
        function_call?: "none" | "auto" | { name: string };
        response_format?: { type?: string; schema?: {} };
        tools?: {
          type?: string;
          function?: { description?: string; name?: string; parameters?: {} };
        }[];
        tool_choice?:
          | string
          | {
              index: number;
              id: string;
              type: "function";
              function: { name: string; arguments: string };
            };
        safety_model?: string;
      },
    ) {
      const url = new URL(`https://api.together.xyz/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