0

Create a Dataset

by
Published Apr 8, 2025

Create a dataset by uploading a file. See ['Dataset Creation'](https://docs.cohere.com/docs/datasets#dataset-creation) for more information.

Script cohere Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Cohere = {
3
  apiKey: string;
4
};
5
type Base64 = string;
6
/**
7
 * Create a Dataset
8
 * Create a dataset by uploading a file. See ['Dataset Creation'](https://docs.cohere.com/docs/datasets#dataset-creation) for more information.
9
 */
10
export async function main(
11
  auth: Cohere,
12
  name: string | undefined,
13
  type:
14
    | "embed-input"
15
    | "embed-result"
16
    | "cluster-result"
17
    | "cluster-outliers"
18
    | "reranker-finetune-input"
19
    | "single-label-classification-finetune-input"
20
    | "chat-finetune-input"
21
    | "multi-label-classification-finetune-input"
22
    | undefined,
23
  keep_original_file: string | undefined,
24
  skip_malformed_input: string | undefined,
25
  keep_fields: string | undefined,
26
  optional_fields: string | undefined,
27
  text_separator: string | undefined,
28
  csv_delimiter: string | undefined,
29
  body: {
30
    data: {
31
      base64: Base64;
32
      type:
33
        | "image/png"
34
        | "image/jpeg"
35
        | "image/gif"
36
        | "application/pdf"
37
        | "appication/json"
38
        | "text/csv"
39
        | "text/plain"
40
        | "audio/mpeg"
41
        | "audio/wav"
42
        | "video/mp4";
43
      name: string;
44
    };
45
    eval_data?: {
46
      base64: Base64;
47
      type:
48
        | "image/png"
49
        | "image/jpeg"
50
        | "image/gif"
51
        | "application/pdf"
52
        | "appication/json"
53
        | "text/csv"
54
        | "text/plain"
55
        | "audio/mpeg"
56
        | "audio/wav"
57
        | "video/mp4";
58
      name: string;
59
    };
60
  },
61
) {
62
  const url = new URL(`https://api.cohere.com/v1/datasets`);
63
  for (const [k, v] of [
64
    ["name", name],
65
    ["type", type],
66
    ["keep_original_file", keep_original_file],
67
    ["skip_malformed_input", skip_malformed_input],
68
    ["keep_fields", keep_fields],
69
    ["optional_fields", optional_fields],
70
    ["text_separator", text_separator],
71
    ["csv_delimiter", csv_delimiter],
72
  ]) {
73
    if (v !== undefined && v !== "" && k !== undefined) {
74
      url.searchParams.append(k, v);
75
    }
76
  }
77
  const formData = new FormData();
78
  for (const [k, v] of Object.entries(body)) {
79
    if (v !== undefined) {
80
      if (["data", "eval_data"].includes(k)) {
81
        const { base64, type, name } = v as {
82
          base64: Base64;
83
          type: string;
84
          name: string;
85
        };
86
        formData.append(
87
          k,
88
          new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
89
            type,
90
          }),
91
          name,
92
        );
93
      } else {
94
        formData.append(k, String(v));
95
      }
96
    }
97
  }
98
  const response = await fetch(url, {
99
    method: "POST",
100
    headers: {
101
      Authorization: "Bearer " + auth.apiKey,
102
    },
103
    body: formData,
104
  });
105
  if (!response.ok) {
106
    const text = await response.text();
107
    throw new Error(`${response.status} ${text}`);
108
  }
109
  return await response.json();
110
}
111