import { PineconeClient } from "@pinecone-database/[email protected]";
import { QueryOperationRequest } from "@pinecone-database/[email protected]/dist/pinecone-generated-ts-fetch/index.js";
/**
*
* @param topK The number of results to return for each query.
*
* @param vector _(Conditionally Optional)_ The query vector. This should be the same length as the dimension
* of the index being queried.
* **Each query request can contain only one of the parameters "id" or "vector".**
*
* @param id _(Conditionally Optional)_ The unique ID of the vector to be used as a query vector.
* **Each query request can contain only one of the parameters "vector" or "id".**
*
* @param namespace _(Optional)_ The namespace to query.
*
* @param includeValues _(Optional)_ Indicates whether vector values are included in the response.
* Defaults to `false`.
*
* @param includeMetadata _(Optional)_ Indicates whether metadata is included in the response as well as the ids.
* Defaults to `false`.
*
* @param filter _(Optional)_ The filter to apply. You can use vector metadata to limit your search.
* See https://www.pinecone.io/docs/metadata-filtering/.
*/
type Pinecone = {
apiKey: string;
environment: string;
};
export async function main(
auth: Pinecone,
index_name: string,
topK: number,
vector?: number[],
id?: string,
namespace?: string,
include_values?: boolean,
include_metadata?: boolean,
filter?: object,
raw?: boolean,
) {
const client = new PineconeClient();
await client.init(auth);
const index = client.Index(index_name);
const queryRequest: QueryOperationRequest = removeObjectEmptyFields({
topK,
vector,
id,
namespace,
includeValues: include_values,
includeMetadata: include_metadata,
filter,
});
return await index[raw ? "queryRaw" : "query"]({ queryRequest });
}
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