Skip to content

Commit

Permalink
Add sparseVector to QueryByVectorValues type (#236)
Browse files Browse the repository at this point in the history
## Problem
`sparseVector?: RecordSparseValues` was left out of the
`QueryByVectorValues` type. While the API supports sending
`sparseVector` with query requests, if you attempt to compile the client
with TypeScript you'll get a build error.

Issue: #218

## Solution

- Update the `QueryByVectorValues` type to include `sparseVector?:
RecordSparseValues`. This should allow users to pass a `sparseVector`
along with their `query` operation if needed.
- Update `query` docstring + integration tests.

## Type of Change
- [X] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update
- [ ] Infrastructure change (CI configs, etc)
- [ ] Non-code change (docs, etc)
- [ ] None of the above: (explain here)

## Test Plan
Verify CI integration tests pass. I added `sparseVector` to several of
our happy-path `query` tests.

Testing this via the repl wasn't sufficient as you could pass the value
directly, and you don't get TS build errors.

To properly test you'll need a TypeScript project that uses the Pinecone
SDK, and queries with a `sparseVector` in the request object. `tsc` will
fail if using the previous client.
  • Loading branch information
austin-denoble committed Jul 15, 2024
1 parent 7eff996 commit b37a8a9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/data/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
RecordIdSchema,
RecordSparseValuesSchema,
RecordValues,
RecordSparseValues,
RecordValuesSchema,
} from './types';
import type { PineconeRecord, RecordMetadata } from './types';
Expand Down Expand Up @@ -94,6 +95,11 @@ export type QueryByVectorValues = QueryShared & {
* Vector values output from an embedding model.
*/
vector: RecordValues;

/**
* The sparse values of the query vector, if applicable.
*/
sparseVector?: RecordSparseValues;
};
/**
* The options that may be passed to {@link Index.query }
Expand Down
12 changes: 11 additions & 1 deletion src/integration/data/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('query', () => {
const recordsToUpsert = generateRecords({
dimension: 5,
quantity: numberOfRecords,
withSparseValues: true,
});
expect(recordsToUpsert).toHaveLength(3);
expect(recordsToUpsert[0].id).toEqual('0');
Expand Down Expand Up @@ -96,7 +97,7 @@ describe('query', () => {
);
});

test('query with vector values', async () => {
test('query with vector and sparseVector values', async () => {
const topK = 1;
const assertions = (results) => {
expect(results.matches).toBeDefined();
Expand All @@ -108,6 +109,10 @@ describe('query', () => {
() =>
ns.query({
vector: [0.11, 0.22, 0.33, 0.44, 0.55],
sparseVector: {
indices: [32, 5, 3, 2, 1],
values: [0.11, 0.22, 0.33, 0.44, 0.55],
},
topK,
}),
assertions
Expand All @@ -116,6 +121,10 @@ describe('query', () => {

test('query with includeValues: true', async () => {
const queryVec = Array.from({ length: 5 }, () => Math.random());
const sparseVec = {
indices: [0, 1, 2, 3, 4],
values: Array.from({ length: 5 }, () => Math.random()),
};

const assertions = (results) => {
expect(results.matches).toBeDefined();
Expand All @@ -127,6 +136,7 @@ describe('query', () => {
() =>
ns.query({
vector: queryVec,
sparseVector: sparseVec,
topK: 2,
includeValues: true,
includeMetadata: true,
Expand Down

0 comments on commit b37a8a9

Please sign in to comment.