Skip to content

Commit

Permalink
VS-257: Fix Vectorize query options
Browse files Browse the repository at this point in the history
  • Loading branch information
garvit-gupta committed Aug 16, 2024
1 parent 299094f commit 13044c0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/cloudflare/internal/test/vectorize/vectorize-api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const test_vector_search_vector_query = {
async test(_, env) {
const IDX = env["vector-search"];
{
// with returnValues = true, returnMetadata = true
// with returnValues = true, returnMetadata = "indexed"
const results = await IDX.query(new Float32Array(new Array(5).fill(0)), {
topK: 3,
returnValues: true,
Expand Down Expand Up @@ -58,7 +58,7 @@ export const test_vector_search_vector_query = {
}

{
// with returnValues = unset (false), returnMetadata = unset (false)
// with returnValues = unset (false), returnMetadata = unset (none)
const results = await IDX.query(new Float32Array(new Array(5).fill(0)), {
topK: 3,
});
Expand All @@ -85,7 +85,7 @@ export const test_vector_search_vector_query = {
}

{
// with returnValues = unset (false), returnMetadata = unset (false), filter = "Peter Piper picked a peck of pickled peppers"
// with returnValues = unset (false), returnMetadata = unset (none), filter = "Peter Piper picked a peck of pickled peppers"
const results = await IDX.query(new Float32Array(new Array(5).fill(0)), {
topK: 1,
filter: {
Expand Down
4 changes: 2 additions & 2 deletions src/cloudflare/internal/test/vectorize/vectorize-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default {
) {
return Response.json({});
} else if (request.method === "POST" && pathname.endsWith("/query")) {
/** @type {VectorizeQueryOptions<VectorizeMetadataRetrievalLevel> & {vector: number[]}} */
/** @type {VectorizeQueryOptions & {vector: number[]}} */
const body = await request.json();
let returnSet = structuredClone(exampleVectorMatches);
if (
Expand All @@ -114,7 +114,7 @@ export default {
returnSet.forEach((v) => {
delete v.values;
});
if (!body?.returnMetadata)
if (!body?.returnMetadata || body?.returnMetadata === "none")
returnSet.forEach((v) => {
delete v.metadata;
});
Expand Down
16 changes: 15 additions & 1 deletion src/cloudflare/internal/vectorize-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ class VectorizeIndexImpl implements Vectorize {

public async query(
vector: VectorFloatArray | number[],
options: VectorizeQueryOptions<VectorizeMetadataRetrievalLevel>
options?: VectorizeQueryOptions
): Promise<VectorizeMatches> {
if (this.indexVersion === "v2") {
if (options && options.returnMetadata && !isVectorizeMetadataRetrievalLevel(options.returnMetadata) ) {
throw new Error(
`Invalid returnMetadata option. Expected: "none", "indexed" or "all"; got: ${options.returnMetadata}`
);
}
const res = await this._send(Operation.VECTOR_QUERY, `query`, {
method: "POST",
body: JSON.stringify({
Expand All @@ -59,6 +64,11 @@ class VectorizeIndexImpl implements Vectorize {

return await toJson<VectorizeMatches>(res);
} else {
if (options && options.returnMetadata && typeof options.returnMetadata !== 'boolean') {
throw new Error(
`Invalid returnMetadata option. Expected boolean; got: ${options.returnMetadata}`
);
}
const compat = {
queryMetadataOptional: flags.vectorizeQueryMetadataOptional,
};
Expand Down Expand Up @@ -220,6 +230,10 @@ class VectorizeIndexImpl implements Vectorize {
}
}

function isVectorizeMetadataRetrievalLevel(value: unknown): value is VectorizeMetadataRetrievalLevel {
return typeof value === 'string' && (value === 'all' || value === 'indexed' || value === 'none');
}

const maxBodyLogChars = 1_000;
async function toJson<T = unknown>(response: Response): Promise<T> {
const body = await response.text();
Expand Down
10 changes: 4 additions & 6 deletions src/cloudflare/internal/vectorize.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,11 @@ type VectorizeDistanceMetric = "euclidean" | "cosine" | "dot-product";
*/
type VectorizeMetadataRetrievalLevel = "all" | "indexed" | "none";

interface VectorizeQueryOptions<
MetadataReturn extends boolean | VectorizeMetadataRetrievalLevel = boolean,
> {
interface VectorizeQueryOptions{
topK?: number;
namespace?: string;
returnValues?: boolean;
returnMetadata?: MetadataReturn;
returnMetadata?: boolean | VectorizeMetadataRetrievalLevel;
filter?: VectorizeVectorMetadataFilter;
}

Expand Down Expand Up @@ -196,7 +194,7 @@ declare abstract class VectorizeIndex {
*/
public query(
vector: VectorFloatArray | number[],
options: VectorizeQueryOptions
options?: VectorizeQueryOptions
): Promise<VectorizeMatches>;
/**
* Insert a list of vectors into the index dataset. If a provided id exists, an error will be thrown.
Expand Down Expand Up @@ -243,7 +241,7 @@ declare abstract class Vectorize {
*/
public query(
vector: VectorFloatArray | number[],
options: VectorizeQueryOptions<VectorizeMetadataRetrievalLevel>
options?: VectorizeQueryOptions
): Promise<VectorizeMatches>;
/**
* Insert a list of vectors into the index dataset. If a provided id exists, an error will be thrown.
Expand Down
10 changes: 4 additions & 6 deletions types/defines/vectorize.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,11 @@ type VectorizeDistanceMetric = "euclidean" | "cosine" | "dot-product";
*/
type VectorizeMetadataRetrievalLevel = "all" | "indexed" | "none";

interface VectorizeQueryOptions<
MetadataReturn extends boolean | VectorizeMetadataRetrievalLevel = boolean,
> {
interface VectorizeQueryOptions{
topK?: number;
namespace?: string;
returnValues?: boolean;
returnMetadata?: MetadataReturn;
returnMetadata?: boolean | VectorizeMetadataRetrievalLevel;
filter?: VectorizeVectorMetadataFilter;
}

Expand Down Expand Up @@ -188,7 +186,7 @@ declare abstract class VectorizeIndex {
*/
public query(
vector: VectorFloatArray | number[],
options: VectorizeQueryOptions
options?: VectorizeQueryOptions
): Promise<VectorizeMatches>;
/**
* Insert a list of vectors into the index dataset. If a provided id exists, an error will be thrown.
Expand Down Expand Up @@ -235,7 +233,7 @@ declare abstract class Vectorize {
*/
public query(
vector: VectorFloatArray | number[],
options: VectorizeQueryOptions<VectorizeMetadataRetrievalLevel>
options?: VectorizeQueryOptions
): Promise<VectorizeMatches>;
/**
* Insert a list of vectors into the index dataset. If a provided id exists, an error will be thrown.
Expand Down

0 comments on commit 13044c0

Please sign in to comment.