From 8786441d1a823e4bccbecf8e962a940a14be1e76 Mon Sep 17 00:00:00 2001 From: Popotojs Date: Tue, 10 May 2022 16:11:41 +0200 Subject: [PATCH] add vector operation: delete update upsert --- lib/interface/vectors/delete-result.ts | 1 + lib/interface/vectors/delete.ts | 6 +++ lib/interface/vectors/index.ts | 6 +++ lib/interface/vectors/update-result.ts | 1 + lib/interface/vectors/update.ts | 6 +++ lib/interface/vectors/upsert-result.ts | 3 ++ lib/interface/vectors/upsert.ts | 10 ++++ lib/service/pinecone.vector.service.ts | 71 +++++++++++++++++--------- package-lock.json | 4 +- package.json | 2 +- 10 files changed, 83 insertions(+), 27 deletions(-) create mode 100644 lib/interface/vectors/delete-result.ts create mode 100644 lib/interface/vectors/delete.ts create mode 100644 lib/interface/vectors/update-result.ts create mode 100644 lib/interface/vectors/update.ts create mode 100644 lib/interface/vectors/upsert-result.ts create mode 100644 lib/interface/vectors/upsert.ts diff --git a/lib/interface/vectors/delete-result.ts b/lib/interface/vectors/delete-result.ts new file mode 100644 index 0000000..82f1d4a --- /dev/null +++ b/lib/interface/vectors/delete-result.ts @@ -0,0 +1 @@ +export interface DeleteResult {} diff --git a/lib/interface/vectors/delete.ts b/lib/interface/vectors/delete.ts new file mode 100644 index 0000000..b73354c --- /dev/null +++ b/lib/interface/vectors/delete.ts @@ -0,0 +1,6 @@ +export interface Delete { + ids?: string[]; + deleteAll?: boolean; + namespace?: string; + filter?: object; +} diff --git a/lib/interface/vectors/index.ts b/lib/interface/vectors/index.ts index 6a97745..a44efec 100644 --- a/lib/interface/vectors/index.ts +++ b/lib/interface/vectors/index.ts @@ -1,5 +1,11 @@ +export * from './delete'; +export * from './delete-result'; export * from './fetch'; export * from './fetch-results'; export * from './index-stats-results'; export * from './query'; export * from './query-results'; +export * from './update'; +export * from './update-result'; +export * from './upsert'; +export * from './upsert-result'; diff --git a/lib/interface/vectors/update-result.ts b/lib/interface/vectors/update-result.ts new file mode 100644 index 0000000..c1cf7fd --- /dev/null +++ b/lib/interface/vectors/update-result.ts @@ -0,0 +1 @@ +export interface UpdateResult {} diff --git a/lib/interface/vectors/update.ts b/lib/interface/vectors/update.ts new file mode 100644 index 0000000..b3b2614 --- /dev/null +++ b/lib/interface/vectors/update.ts @@ -0,0 +1,6 @@ +export interface Update { + id: string; + values?: number[]; + setMetadata?: object; + namespace?: string; +} diff --git a/lib/interface/vectors/upsert-result.ts b/lib/interface/vectors/upsert-result.ts new file mode 100644 index 0000000..1596d3e --- /dev/null +++ b/lib/interface/vectors/upsert-result.ts @@ -0,0 +1,3 @@ +export interface UpsertResult { + upsertedCount: number; +} diff --git a/lib/interface/vectors/upsert.ts b/lib/interface/vectors/upsert.ts new file mode 100644 index 0000000..ae98eba --- /dev/null +++ b/lib/interface/vectors/upsert.ts @@ -0,0 +1,10 @@ +export interface Vector { + id: string; + values: number[]; + metadata?: object; +} + +export interface Upsert { + vectors: Vector[]; + namespace?: string; +} diff --git a/lib/service/pinecone.vector.service.ts b/lib/service/pinecone.vector.service.ts index 016f321..9ccd013 100644 --- a/lib/service/pinecone.vector.service.ts +++ b/lib/service/pinecone.vector.service.ts @@ -2,6 +2,8 @@ import { Inject, Injectable } from '@nestjs/common'; import { HttpService } from '@nestjs/axios'; import { map, Observable } from 'rxjs'; import { + Delete, + DeleteResult, Fetch, FetchResult, IndexStatsResult, @@ -9,6 +11,10 @@ import { PineconeConfig, Query, QueryResults, + Update, + UpdateResult, + Upsert, + UpsertResult, } from '../interface'; @Injectable() @@ -53,14 +59,20 @@ export class PineconeVectorService { ); } - // TODO... - // delete(): Observable { - // return this.httpService.get(`/vectors/delete`).pipe( - // map((axiosResponse) => { - // return axiosResponse.data; - // }), - // ); - // } + /** + * The Delete operation deletes vectors, by id, from a single namespace. You can delete items by their id, from a single namespace. + * @param del + * @param index + */ + delete(del: Delete, index?: string): Observable { + const url = this.indexUrl('/vectors/delete', index); + + return this.httpService.post(url, del).pipe( + map((axiosResponse) => { + return axiosResponse.data; + }), + ); + } /** * The Fetch operation looks up and returns vectors, by id, from a single namespace. The returned vectors include the vector data and/or metadata. @@ -82,20 +94,31 @@ export class PineconeVectorService { ); } - // TODO... - // update(): Observable { - // return this.httpService.get(`/describe_index_stats`).pipe( - // map((axiosResponse) => { - // return axiosResponse.data; - // }), - // ); - // } - // - // upsert(): Observable { - // return this.httpService.get(`/describe_index_stats`).pipe( - // map((axiosResponse) => { - // return axiosResponse.data; - // }), - // ); - // } + /** + * The Update operation updates vector in a namespace. If a value is included, it will overwrite the previous value. If a set_metadata is included, the values of the fields specified in it will be added or overwrite the previous value. + * @param update + * @param index + */ + update(update: Update, index?: string): Observable { + const url = this.indexUrl('/vectors/update', index); + return this.httpService.post(url, update).pipe( + map((axiosResponse) => { + return axiosResponse.data; + }), + ); + } + + /** + * The Upsert operation writes vectors into a namespace. If a new value is upserted for an existing vector id, it will overwrite the previous value. + * @param upsert + * @param index + */ + upsert(upsert: Upsert, index?: string): Observable { + const url = this.indexUrl('/vectors/upsert', index); + return this.httpService.post(url, upsert).pipe( + map((axiosResponse) => { + return axiosResponse.data; + }), + ); + } } diff --git a/package-lock.json b/package-lock.json index 236974a..0336555 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@nhogs/nestjs-pinecone", - "version": "0.0.2", + "version": "0.0.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@nhogs/nestjs-pinecone", - "version": "0.0.2", + "version": "0.0.3", "devDependencies": { "@nestjs/axios": "^0.0.7", "@nestjs/common": "^8.4.4", diff --git a/package.json b/package.json index bf2f022..699beda 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nhogs/nestjs-pinecone", - "version": "0.0.2", + "version": "0.0.3", "engines": { "node": ">=16.x" },