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

Add sparseVector to QueryByVectorValues type #236

Merged
merged 2 commits into from
Jul 10, 2024
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
5 changes: 0 additions & 5 deletions src/data/__tests__/indexHostSingleton.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,6 @@ describe('IndexHostSingleton', () => {
expect(mockDescribeIndex).toHaveBeenNthCalledWith(1, 'index-1');
expect(mockDescribeIndex).toHaveBeenNthCalledWith(2, 'index-2');

console.log(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

whoops, must have left this in previously 😅

'mockIndexOperationsBuilder',
JSON.stringify(mockIndexOperationsBuilder.mock)
);

expect(mockIndexOperationsBuilder).toHaveBeenCalledTimes(2);
expect(mockIndexOperationsBuilder).toHaveBeenNthCalledWith(
1,
Expand Down
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
4 changes: 4 additions & 0 deletions src/utils/__tests__/user-agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ describe('user-agent', () => {
config.sourceTag = ' MY SOURCE TAG 1234 ##### !!!!!!';
userAgent = buildUserAgent(config);
expect(userAgent).toContain('source_tag=my_source_tag_1234');

config.sourceTag = ' MY SOURCE TAG :1234-ABCD';
userAgent = buildUserAgent(config);
expect(userAgent).toContain('source_tag=my_source_tag_:1234abcd');
});
});
});
4 changes: 2 additions & 2 deletions src/utils/user-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ const normalizeSourceTag = (sourceTag: string) => {
/**
* normalize sourceTag
* 1. Lowercase
* 2. Limit charset to [a-z0-9_ ]
* 2. Limit charset to [a-z0-9_ :]
* 3. Trim left/right spaces
* 4. Condense multiple spaces to one, and replace with underscore
*/
return sourceTag
.toLowerCase()
.replace(/[^a-z0-9_ ]/g, '')
.replace(/[^a-z0-9_ :]/g, '')
.trim()
.replace(/[ ]+/g, '_');
};
Loading