0
Query Index
One script reply has been approved by the moderators Verified
Created by adam186 384 days ago Viewed 5849 times
0
Submitted by adam186 Deno
Verified 384 days ago
1
import { removeObjectEmptyFields } from "https://deno.land/x/windmill_helpers@v1.1.1/mod.ts";
2
import { PineconeClient } from "npm:@pinecone-database/pinecone";
3
import { QueryOperationRequest } from "npm:@pinecone-database/pinecone/0.0.12/dist/pinecone-generated-ts-fetch/index.js";
4

5
/**
6
 *
7
 * @param topK The number of results to return for each query.
8
 *
9
 * @param vector _(Conditionally Optional)_ The query vector. This should be the same length as the dimension
10
 * of the index being queried.
11
 * **Each query request can contain only one of the parameters "id" or "vector".**
12
 *
13
 * @param id _(Conditionally Optional)_ The unique ID of the vector to be used as a query vector.
14
 * **Each query request can contain only one of the parameters "vector" or "id".**
15
 *
16
 * @param namespace _(Optional)_ The namespace to query.
17
 *
18
 * @param includeValues _(Optional)_ Indicates whether vector values are included in the response.
19
 * Defaults to `false`.
20
 *
21
 * @param includeMetadata _(Optional)_ Indicates whether metadata is included in the response as well as the ids.
22
 * Defaults to `false`.
23
 *
24
 * @param filter _(Optional)_ The filter to apply. You can use vector metadata to limit your search.
25
 * See https://www.pinecone.io/docs/metadata-filtering/.
26
 */
27
type Pinecone = {
28
  apiKey: string;
29
  environment: string;
30
};
31
export async function main(
32
  auth: Pinecone,
33
  index_name: string,
34
  topK: number,
35
  vector?: number[],
36
  id?: string,
37
  namespace?: string,
38
  include_values?: boolean,
39
  include_metadata?: boolean,
40
  filter?: object,
41
  raw?: boolean,
42
) {
43
  const client = new PineconeClient();
44
  await client.init(auth);
45
  const index = client.Index(index_name);
46

47
  const queryRequest: QueryOperationRequest = removeObjectEmptyFields({
48
    topK,
49
    vector,
50
    id,
51
    namespace,
52
    includeValues: include_values,
53
    includeMetadata: include_metadata,
54
    filter,
55
  });
56
  return await index[raw ? "queryRaw" : "query"]({ queryRequest });
57
}
58