Skip to content

Commit

Permalink
add vector operation:
Browse files Browse the repository at this point in the history
delete
update
upsert
  • Loading branch information
ciminf committed May 10, 2022
1 parent 39570da commit 8786441
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 27 deletions.
1 change: 1 addition & 0 deletions lib/interface/vectors/delete-result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export interface DeleteResult {}
6 changes: 6 additions & 0 deletions lib/interface/vectors/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Delete {
ids?: string[];
deleteAll?: boolean;
namespace?: string;
filter?: object;
}
6 changes: 6 additions & 0 deletions lib/interface/vectors/index.ts
Original file line number Diff line number Diff line change
@@ -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';
1 change: 1 addition & 0 deletions lib/interface/vectors/update-result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export interface UpdateResult {}
6 changes: 6 additions & 0 deletions lib/interface/vectors/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Update {
id: string;
values?: number[];
setMetadata?: object;
namespace?: string;
}
3 changes: 3 additions & 0 deletions lib/interface/vectors/upsert-result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface UpsertResult {
upsertedCount: number;
}
10 changes: 10 additions & 0 deletions lib/interface/vectors/upsert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface Vector {
id: string;
values: number[];
metadata?: object;
}

export interface Upsert {
vectors: Vector[];
namespace?: string;
}
71 changes: 47 additions & 24 deletions lib/service/pinecone.vector.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ import { Inject, Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { map, Observable } from 'rxjs';
import {
Delete,
DeleteResult,
Fetch,
FetchResult,
IndexStatsResult,
PINECONE_CONFIG,
PineconeConfig,
Query,
QueryResults,
Update,
UpdateResult,
Upsert,
UpsertResult,
} from '../interface';

@Injectable()
Expand Down Expand Up @@ -53,14 +59,20 @@ export class PineconeVectorService {
);
}

// TODO...
// delete(): Observable<IndexStat> {
// return this.httpService.get<IndexStat>(`/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<DeleteResult> {
const url = this.indexUrl('/vectors/delete', index);

return this.httpService.post<DeleteResult>(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.
Expand All @@ -82,20 +94,31 @@ export class PineconeVectorService {
);
}

// TODO...
// update(): Observable<IndexStat> {
// return this.httpService.get<IndexStat>(`/describe_index_stats`).pipe(
// map((axiosResponse) => {
// return axiosResponse.data;
// }),
// );
// }
//
// upsert(): Observable<IndexStat> {
// return this.httpService.get<IndexStat>(`/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<UpdateResult> {
const url = this.indexUrl('/vectors/update', index);
return this.httpService.post<UpdateResult>(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<UpsertResult> {
const url = this.indexUrl('/vectors/upsert', index);
return this.httpService.post<UpsertResult>(url, upsert).pipe(
map((axiosResponse) => {
return axiosResponse.data;
}),
);
}
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nhogs/nestjs-pinecone",
"version": "0.0.2",
"version": "0.0.3",
"engines": {
"node": ">=16.x"
},
Expand Down

0 comments on commit 8786441

Please sign in to comment.