0

Create Index

by
Published Apr 18, 2023
Script pinecone Verified

The script

Submitted by hugo989 Bun
Verified 6 days ago
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

Other submissions
  • Submitted by adam186 Deno
    Created 398 days ago
    1
    import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
    2
    import { PineconeClient } from "npm:@pinecone-database/pinecone";
    3
    
    
    4
    /**
    5
     * @param name The name of the index to be created. The maximum length is 45 characters.
    6
     *
    7
     * @param dimension The dimensions of the vectors to be inserted in the index.
    8
     *
    9
     * @param metric _(Optional)_ The distance metric to be used for similarity search.
    10
     *
    11
     * @param pods _(Optional)_ The number of pods for the index to use, including replicas.
    12
     *
    13
     * @param replicas _(Optional)_ The number of replicas.
    14
     *
    15
     * @param pod_type _(Optional)_ The pod type for the index.
    16
     * `s1`: Best storage capacity.
    17
     * `p1`: Faster queries.
    18
     * `p2`: Lowest latency and highest throughput.
    19
     *
    20
     * @param pod_size _(Optional)_ The size of the pod. You can increase (but not decrease) the pod size on running indexes.
    21
     * If `pod_type` is not set, this will be ignored.
    22
     *
    23
     * @param metadata_config _(Optional)_ Configuration for the behavior of Pinecone's internal metadata index.
    24
     * By default, all metadata is indexed; when `metadata_config` is present, only specified metadata fields are indexed.
    25
     *
    26
     * @param source_collection _(Optional)_ The name of the collection to create an index from.
    27
     */
    28
    type Pinecone = {
    29
      apiKey: string;
    30
      environment: string;
    31
    };
    32
    export async function main(
    33
      auth: Pinecone,
    34
      name: string,
    35
      dimension: number,
    36
      metric?: "" | "euclidean" | "cosine" | "dotproduct",
    37
      pods?: number,
    38
      replicas?: number,
    39
      pod_type?: "" | "s1" | "p1" | "p2",
    40
      pod_size?: "" | "x1" | "x2" | "x4" | "x8",
    41
      metadata_config?: Record<string, string>,
    42
      source_collection?: number,
    43
    ) {
    44
      const client = new PineconeClient();
    45
      await client.init(auth);
    46
    
    
    47
      const createRequest = removeObjectEmptyFields({
    48
        name,
    49
        dimension,
    50
        metric,
    51
        pods,
    52
        replicas,
    53
        pod_type: pod_type ? `${pod_type}.${pod_size || "x1"}` : undefined,
    54
        metadata_config,
    55
        source_collection,
    56
      });
    57
      return await client.createIndex({ createRequest });
    58
    }
    59