Skip to content

Commit

Permalink
Merge pull request #3074 from cloudflare/ggu/vectorize-in-nin
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettgu10 authored Nov 8, 2024
2 parents df6d46c + fdd5c8f commit 8434636
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/cloudflare/internal/test/vectorize/vectorize-api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,37 @@ export const test_vector_search_vector_query = {
};
assert.deepStrictEqual(results, expected);
}

{
// 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: {
text: {
$in: [
'Peter Piper picked a peck of pickled peppers',
'She sells seashells by the seashore',
],
},
},
});
assert.equal(true, results.count > 0);
/** @type {VectorizeMatches} */
const expected = {
matches: [
{
id: 'b0daca4a-ffd8-4865-926b-e24800af2a2d',
score: 0.71151,
},
{
id: 'a44706aa-a366-48bc-8cc1-3feffd87d548',
score: 0.68913,
},
],
count: 2,
};
assert.deepStrictEqual(results, expected);
}
},
};

Expand Down
10 changes: 10 additions & 0 deletions src/cloudflare/internal/test/vectorize/vectorize-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ export default {
(m) => m.metadata?.['text'] === criteria
);
}
if (
body?.filter?.['text'] &&
typeof body?.filter?.['text'] === 'object' &&
body?.filter?.['text']?.['$in'] !== undefined
) {
const criteria = body?.filter?.['text']?.['$in'];
returnSet = returnSet.filter((m) =>
criteria.includes(m.metadata?.['text'])
);
}
if (!body?.returnValues)
returnSet.forEach((v) => {
delete v.values;
Expand Down
7 changes: 7 additions & 0 deletions src/cloudflare/internal/vectorize.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface VectorizeError {
* This list is expected to grow as support for more operations are released.
*/
type VectorizeVectorMetadataFilterOp = '$eq' | '$ne';
type VectorizeVectorMetadataFilterCollectionOp = '$in' | '$nin';

/**
* Filter criteria for vector metadata used to limit the retrieved query result set.
Expand All @@ -47,6 +48,12 @@ type VectorizeVectorMetadataFilter = {
VectorizeVectorMetadataValue,
string[]
> | null;
}
| {
[Op in VectorizeVectorMetadataFilterCollectionOp]?: Exclude<
VectorizeVectorMetadataValue,
string[]
>[];
};
};

Expand Down

0 comments on commit 8434636

Please sign in to comment.