Edits history of script submission #12077 for ' Chat with the model (cohere)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Cohere = {
      apiKey: string;
    };
    /**
     * Chat with the model
     * Generates a text response to a user message and streams it down, token by token. To learn how to use the Chat API with streaming follow our [Text Generation guides](https://docs.cohere.com/v2/docs/chat-api).
    
    Follow the [Migration Guide](https://docs.cohere.com/v2/docs/migrating-v1-to-v2) for instructions on moving from API v1 to API v2.
    
     */
    export async function main(
      auth: Cohere,
      body: {
        stream?: false | true;
        model: string;
        messages:
          | { role: "user"; content: string | { type: "text"; text: string }[] }
          | {
              role: "assistant";
              tool_calls?: {
                id?: string;
                type?: "function";
                function?: { name?: string; arguments?: string };
              }[];
              tool_plan?: string;
              content?: string | { type: "text"; text: string }[];
              citations?: {
                start?: number;
                end?: number;
                text?: string;
                sources?:
                  | { id?: string; tool_output?: {} }
                  | { id?: string; document?: {} }[];
              }[];
            }
          | { role: "system"; content: string | { type: "text"; text: string }[] }
          | {
              role: "tool";
              tool_call_id: string;
              content:
                | string
                | { type: "text"; text: string }
                | { type: "document"; document: { data: {}; id?: string } }[];
            }[];
        tools?: {
          type?: "function";
          function?: { name?: string; description?: string; parameters?: {} };
        }[];
        strict_tools?: false | true;
        documents?: string | { data: {}; id?: string }[];
        citation_options?: { mode?: "FAST" | "ACCURATE" | "OFF" };
        response_format?:
          | { type: "text" | "json_object" }
          | { type: "text" | "json_object"; json_schema?: {} };
        safety_mode?: "OFF" | "CONTEXTUAL" | "STRICT";
        max_tokens?: number;
        stop_sequences?: string[];
        temperature?: number;
        seed?: number;
        frequency_penalty?: number;
        presence_penalty?: number;
        k?: number;
        p?: number;
        logprobs?: false | true;
      },
    ) {
      const url = new URL(`https://api.cohere.com/v2/chat`);
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.apiKey,
        },
        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 428 days ago