0
Create Vectorize Index
One script reply has been approved by the moderators Verified

Creates and returns a new Vectorize Index.

Created by hugo697 149 days ago Viewed 3600 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 149 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Create Vectorize Index
8
 * Creates and returns a new Vectorize Index.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  body: {
14
    config: (
15
      | {
16
          preset:
17
            | "@cf/baai/bge-small-en-v1.5"
18
            | "@cf/baai/bge-base-en-v1.5"
19
            | "@cf/baai/bge-large-en-v1.5"
20
            | "openai/text-embedding-ada-002"
21
            | "cohere/embed-multilingual-v2.0";
22
          [k: string]: unknown;
23
        }
24
      | {
25
          dimensions: number;
26
          metric: "cosine" | "euclidean" | "dot-product";
27
          [k: string]: unknown;
28
        }
29
    ) & { [k: string]: unknown };
30
    description?: string;
31
    name: string;
32
    [k: string]: unknown;
33
  }
34
) {
35
  const url = new URL(
36
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/vectorize/indexes`
37
  );
38

39
  const response = await fetch(url, {
40
    method: "POST",
41
    headers: {
42
      "X-AUTH-EMAIL": auth.email,
43
      "X-AUTH-KEY": auth.key,
44
      "Content-Type": "application/json",
45
      Authorization: "Bearer " + auth.token,
46
    },
47
    body: JSON.stringify(body),
48
  });
49
  if (!response.ok) {
50
    const text = await response.text();
51
    throw new Error(`${response.status} ${text}`);
52
  }
53
  return await response.json();
54
}
55