From 9945226adcc400dbab3de33c4b79886cc23860be Mon Sep 17 00:00:00 2001 From: Tommy Smith Date: Wed, 10 Jul 2024 17:41:24 +0100 Subject: [PATCH] Send vectors optimistically to handle legacy vectorizers in >1.24.0 --- src/collections/data/index.ts | 1 + src/collections/data/integration.test.ts | 37 +++++++++++++++++++++++- src/collections/serialize/index.ts | 2 ++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/collections/data/index.ts b/src/collections/data/index.ts index 0ad0f0b0..505cd788 100644 --- a/src/collections/data/index.ts +++ b/src/collections/data/index.ts @@ -193,6 +193,7 @@ const data = ( if (Array.isArray(object.vectors)) { const supportsNamedVectors = await dbVersionSupport.supportsNamedVectors(); if (supportsNamedVectors.supports) { + obj.vector = object.vectors; obj.vectors = { default: object.vectors }; } else { obj.vector = object.vectors; diff --git a/src/collections/data/integration.test.ts b/src/collections/data/integration.test.ts index e7722c42..014fe789 100644 --- a/src/collections/data/integration.test.ts +++ b/src/collections/data/integration.test.ts @@ -2,7 +2,7 @@ /* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */ import { v4 } from 'uuid'; import { WeaviateUnsupportedFeatureError } from '../../errors.js'; -import weaviate, { WeaviateClient } from '../../index.js'; +import weaviate, { WeaviateClient, weaviateV2 } from '../../index.js'; import { GeoCoordinate, PhoneNumber } from '../../proto/v1/properties.js'; import { Collection } from '../collection/index.js'; import { CrossReference, CrossReferences, Reference } from '../references/index.js'; @@ -1000,3 +1000,38 @@ describe('Testing of the collection.data methods with a vector index', () => { expect(obj2?.vectors.default).toEqual([5, 6, 7, 8]); }); }); + +describe('Testing of BYOV insertion with legacy vectorizer', () => { + const collectionName = 'TestBYOVEdgeCase'; + const oldClient = weaviateV2.client({ scheme: 'http', host: 'localhost:8080' }); + + beforeAll(() => + oldClient.schema + .classCreator() + .withClass({ + class: collectionName, + vectorizer: 'none', + }) + .do() + ); + + afterAll(() => oldClient.schema.classDeleter().withClassName(collectionName).do()); + + it('should insert and retrieve many vectors using the new client', async () => { + const client = await weaviate.connectToLocal(); + const collection = client.collections.get(collectionName); + await collection.data.insertMany([{ vectors: [1, 2, 3] }, { vectors: [4, 5, 6] }]); + const objects = await collection.query.fetchObjects({ includeVector: true }).then((res) => res.objects); + expect(objects.length).toEqual(2); + expect(objects[0].vectors.default).toEqual([1, 2, 3]); + expect(objects[1].vectors.default).toEqual([4, 5, 6]); + }); + + it('should insert and retrieve single vectors using the new client', async () => { + const client = await weaviate.connectToLocal(); + const collection = client.collections.get(collectionName); + const id = await collection.data.insert({ vectors: [7, 8, 9] }); + const object = await collection.query.fetchObjectById(id, { includeVector: true }); + expect(object?.vectors.default).toEqual([7, 8, 9]); + }); +}); diff --git a/src/collections/serialize/index.ts b/src/collections/serialize/index.ts index 2dd40387..b639e37a 100644 --- a/src/collections/serialize/index.ts +++ b/src/collections/serialize/index.ts @@ -1096,6 +1096,8 @@ export class Serialize { name: 'default', }), ]; + vectorBytes = Serialize.vectorToBytes(obj.vectors); + // required in case collection was made with <1.24.0 and has since been migrated to >=1.24.0 } else if (obj.vectors !== undefined) { vectorBytes = Serialize.vectorToBytes(obj.vectors); }