import { PineconeClient } from "@pinecone-database/[email protected]";
/**
* @param name The name of the index to be created. The maximum length is 45 characters.
*
* @param dimension The dimensions of the vectors to be inserted in the index.
*
* @param metric _(Optional)_ The distance metric to be used for similarity search.
*
* @param pods _(Optional)_ The number of pods for the index to use, including replicas.
*
* @param replicas _(Optional)_ The number of replicas.
*
* @param pod_type _(Optional)_ The pod type for the index.
* `s1`: Best storage capacity.
* `p1`: Faster queries.
* `p2`: Lowest latency and highest throughput.
*
* @param pod_size _(Optional)_ The size of the pod. You can increase (but not decrease) the pod size on running indexes.
* If `pod_type` is not set, this will be ignored.
*
* @param metadata_config _(Optional)_ Configuration for the behavior of Pinecone's internal metadata index.
* By default, all metadata is indexed; when `metadata_config` is present, only specified metadata fields are indexed.
*
* @param source_collection _(Optional)_ The name of the collection to create an index from.
*/
type Pinecone = {
apiKey: string;
environment: string;
};
export async function main(
auth: Pinecone,
name: string,
dimension: number,
metric?: "" | "euclidean" | "cosine" | "dotproduct",
pods?: number,
replicas?: number,
pod_type?: "" | "s1" | "p1" | "p2",
pod_size?: "" | "x1" | "x2" | "x4" | "x8",
metadata_config?: Record<string, string>,
source_collection?: number,
) {
const client = new PineconeClient();
await client.init(auth);
const createRequest = removeObjectEmptyFields({
name,
dimension,
metric,
pods,
replicas,
pod_type: pod_type ? `${pod_type}.${pod_size || "x1"}` : undefined,
metadata_config,
source_collection,
});
return await client.createIndex({ createRequest });
}
function removeObjectEmptyFields(
object?: Record<string, any>,
removeEmptyArraysAndObjects = true,
createNewObject = true,
) {
if (!object || typeof object !== "object") return {}
const obj = createNewObject ? { ...object } : object
const emptyValues = [undefined, null, ""]
for (const key in obj) {
const value = obj[key]
if (emptyValues.includes(value)) {
delete obj[key]
} else if (typeof value === "object") {
if (Object.keys(value).length) {
obj[key] = removeObjectEmptyFields(value, removeEmptyArraysAndObjects, false)
}
if (!Object.keys(value).length && removeEmptyArraysAndObjects) {
delete obj[key]
}
}
}
return obj
}
Submitted by hugo989 7 days ago