Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VS-257: Fix Vectorize query options #2473

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!body?.returnMetadata || body?.returnMetadata === "none")
if ((body?.returnMetadata || 'none') === "none")

nit: Might be a bit cleaner

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) ) {
garvit-gupta marked this conversation as resolved.
Show resolved Hide resolved
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') {
garvit-gupta marked this conversation as resolved.
Show resolved Hide resolved
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,
> {
RamIdeas marked this conversation as resolved.
Show resolved Hide resolved
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
Loading