Edits history of script submission #12081 for ' Create a Dataset (cohere)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Cohere = {
      apiKey: string;
    };
    type Base64 = string;
    /**
     * Create a Dataset
     * Create a dataset by uploading a file. See ['Dataset Creation'](https://docs.cohere.com/docs/datasets#dataset-creation) for more information.
     */
    export async function main(
      auth: Cohere,
      name: string | undefined,
      type:
        | "embed-input"
        | "embed-result"
        | "cluster-result"
        | "cluster-outliers"
        | "reranker-finetune-input"
        | "single-label-classification-finetune-input"
        | "chat-finetune-input"
        | "multi-label-classification-finetune-input"
        | undefined,
      keep_original_file: string | undefined,
      skip_malformed_input: string | undefined,
      keep_fields: string | undefined,
      optional_fields: string | undefined,
      text_separator: string | undefined,
      csv_delimiter: string | undefined,
      body: {
        data: {
          base64: Base64;
          type:
            | "image/png"
            | "image/jpeg"
            | "image/gif"
            | "application/pdf"
            | "appication/json"
            | "text/csv"
            | "text/plain"
            | "audio/mpeg"
            | "audio/wav"
            | "video/mp4";
          name: string;
        };
        eval_data?: {
          base64: Base64;
          type:
            | "image/png"
            | "image/jpeg"
            | "image/gif"
            | "application/pdf"
            | "appication/json"
            | "text/csv"
            | "text/plain"
            | "audio/mpeg"
            | "audio/wav"
            | "video/mp4";
          name: string;
        };
      },
    ) {
      const url = new URL(`https://api.cohere.com/v1/datasets`);
      for (const [k, v] of [
        ["name", name],
        ["type", type],
        ["keep_original_file", keep_original_file],
        ["skip_malformed_input", skip_malformed_input],
        ["keep_fields", keep_fields],
        ["optional_fields", optional_fields],
        ["text_separator", text_separator],
        ["csv_delimiter", csv_delimiter],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const formData = new FormData();
      for (const [k, v] of Object.entries(body)) {
        if (v !== undefined) {
          if (["data", "eval_data"].includes(k)) {
            const { base64, type, name } = v as {
              base64: Base64;
              type: string;
              name: string;
            };
            formData.append(
              k,
              new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
                type,
              }),
              name,
            );
          } else {
            formData.append(k, String(v));
          }
        }
      }
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: "Bearer " + auth.apiKey,
        },
        body: formData,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 428 days ago