1 | import { PineconeClient } from "@pinecone-database/[email protected]"; |
2 |
|
3 | |
4 | * @param name The name of the index to be created. The maximum length is 45 characters. |
5 | * |
6 | * @param dimension The dimensions of the vectors to be inserted in the index. |
7 | * |
8 | * @param metric _(Optional)_ The distance metric to be used for similarity search. |
9 | * |
10 | * @param pods _(Optional)_ The number of pods for the index to use, including replicas. |
11 | * |
12 | * @param replicas _(Optional)_ The number of replicas. |
13 | * |
14 | * @param pod_type _(Optional)_ The pod type for the index. |
15 | * `s1`: Best storage capacity. |
16 | * `p1`: Faster queries. |
17 | * `p2`: Lowest latency and highest throughput. |
18 | * |
19 | * @param pod_size _(Optional)_ The size of the pod. You can increase (but not decrease) the pod size on running indexes. |
20 | * If `pod_type` is not set, this will be ignored. |
21 | * |
22 | * @param metadata_config _(Optional)_ Configuration for the behavior of Pinecone's internal metadata index. |
23 | * By default, all metadata is indexed; when `metadata_config` is present, only specified metadata fields are indexed. |
24 | * |
25 | * @param source_collection _(Optional)_ The name of the collection to create an index from. |
26 | */ |
27 | type Pinecone = { |
28 | apiKey: string; |
29 | environment: string; |
30 | }; |
31 | export async function main( |
32 | auth: Pinecone, |
33 | name: string, |
34 | dimension: number, |
35 | metric?: "" | "euclidean" | "cosine" | "dotproduct", |
36 | pods?: number, |
37 | replicas?: number, |
38 | pod_type?: "" | "s1" | "p1" | "p2", |
39 | pod_size?: "" | "x1" | "x2" | "x4" | "x8", |
40 | metadata_config?: Record<string, string>, |
41 | source_collection?: number, |
42 | ) { |
43 | const client = new PineconeClient(); |
44 | await client.init(auth); |
45 |
|
46 | const createRequest = removeObjectEmptyFields({ |
47 | name, |
48 | dimension, |
49 | metric, |
50 | pods, |
51 | replicas, |
52 | pod_type: pod_type ? `${pod_type}.${pod_size || "x1"}` : undefined, |
53 | metadata_config, |
54 | source_collection, |
55 | }); |
56 | return await client.createIndex({ createRequest }); |
57 | } |
58 |
|
59 | function removeObjectEmptyFields( |
60 | object?: Record<string, any>, |
61 | removeEmptyArraysAndObjects = true, |
62 | createNewObject = true, |
63 | ) { |
64 | if (!object || typeof object !== "object") return {} |
65 | const obj = createNewObject ? { ...object } : object |
66 | const emptyValues = [undefined, null, ""] |
67 | for (const key in obj) { |
68 | const value = obj[key] |
69 | if (emptyValues.includes(value)) { |
70 | delete obj[key] |
71 | } else if (typeof value === "object") { |
72 | if (Object.keys(value).length) { |
73 | obj[key] = removeObjectEmptyFields(value, removeEmptyArraysAndObjects, false) |
74 | } |
75 | if (!Object.keys(value).length && removeEmptyArraysAndObjects) { |
76 | delete obj[key] |
77 | } |
78 | } |
79 | } |
80 | return obj |
81 | } |
82 |
|