Skip to content

Commit

Permalink
feat: milvus api v2 (#295)
Browse files Browse the repository at this point in the history
Signed-off-by: Shuyou <[email protected]>
  • Loading branch information
zhanshuyou committed Apr 18, 2024
1 parent 6ed6eaa commit 272681a
Show file tree
Hide file tree
Showing 16 changed files with 1,119 additions and 78 deletions.
34 changes: 28 additions & 6 deletions milvus/HttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { HttpClientConfig, FetchOptions } from './types';
import { Collection, Vector } from './http';
import {
Collection,
Vector,
User,
Role,
Partition,
MilvusIndex,
Alias,
Import,
} from './http';
import {
DEFAULT_DB,
DEFAULT_HTTP_TIMEOUT,
Expand Down Expand Up @@ -48,9 +57,7 @@ export class HttpBaseClient {
get baseURL() {
return (
this.config.baseURL ||
`${this.config.endpoint}/${
this.config.version || DEFAULT_HTTP_ENDPOINT_VERSION
}`
`${this.config.endpoint}/${DEFAULT_HTTP_ENDPOINT_VERSION}`
);
}

Expand Down Expand Up @@ -167,5 +174,20 @@ export class HttpBaseClient {
}
}

// The HttpClient class extends the functionality of the HttpBaseClient class by mixing in the Collection and Vector APIs.
export class HttpClient extends Collection(Vector(HttpBaseClient)) {}
/**
* The HttpClient class extends the functionality
* of the HttpBaseClient class by mixing in the
* - Collection
* - Vector
* - Alias
* - Partition
* - MilvusIndex
* - Import
* - Role
* - User APIs.
*/
export class HttpClient extends User(
Role(
MilvusIndex(Import(Alias(Partition(Collection(Vector(HttpBaseClient))))))
)
) {}
2 changes: 1 addition & 1 deletion milvus/const/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const DEFAULT_DB = 'default'; // default database name
export const DEFAULT_DYNAMIC_FIELD = '$meta'; // default dynamic field name
export const DEFAULT_COUNT_QUERY_STRING = 'count(*)'; // default count query string
export const DEFAULT_HTTP_TIMEOUT = 60000; // default http timeout, 60s
export const DEFAULT_HTTP_ENDPOINT_VERSION = 'v1'; // api version, default v1
export const DEFAULT_HTTP_ENDPOINT_VERSION = 'v2'; // api version, default v1

export const DEFAULT_POOL_MAX = 10; // default max pool client number
export const DEFAULT_POOL_MIN = 2; // default min pool client number
56 changes: 56 additions & 0 deletions milvus/http/Alias.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { HttpBaseClient } from '../HttpClient';
import {
Constructor,
FetchOptions,
HttpAliasBaseReq,
HttpBaseResponse,
HttpAliasCreateReq,
HttpAliasAlterReq,
HttpAliasDescribeReq,
HttpAliasDropReq,
HttpAliasDescribeResponse,
} from '../types';

/**
*
* @param {Constructor<HttpBaseClient>} Base - The base class to be extended.
* @returns {class} - The extended class with additional methods for collection management.
*
* @method listAliases - Lists all aliases in a collection.
* @method createAlias - Creates a new alias in a collection.
* @method describeAlias - Describes an alias.
* @method dropAlias - Deletes an alias.
* @method alterAlias - Modifies an alias to another collection.
*/
export function Alias<T extends Constructor<HttpBaseClient>>(Base: T) {
return class extends Base {
get aliasPrefix() {
return '/vectordb/aliases';
}

async listAliases(params: HttpAliasBaseReq, options?: FetchOptions) {
const url = `${this.aliasPrefix}/list`;
return await this.POST<HttpBaseResponse<string[]>>(url, params, options);
}

async createAlias(params: HttpAliasCreateReq, options?: FetchOptions) {
const url = `${this.aliasPrefix}/create`;
return await this.POST<HttpBaseResponse>(url, params, options);
}

async describeAlias(params: HttpAliasDescribeReq, options?: FetchOptions) {
const url = `${this.aliasPrefix}/describe`;
return await this.POST<HttpAliasDescribeResponse>(url, params, options);
}

async dropAlias(params: HttpAliasDropReq, options?: FetchOptions) {
const url = `${this.aliasPrefix}/drop`;
return await this.POST<HttpBaseResponse>(url, params, options);
}

async alterAlias(params: HttpAliasAlterReq, options?: FetchOptions) {
const url = `${this.aliasPrefix}/alter`;
return await this.POST<HttpBaseResponse>(url, params, options);
}
};
}
79 changes: 70 additions & 9 deletions milvus/http/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ import {
HttpBaseResponse,
HttpBaseReq,
FetchOptions,
HttpCollectionRenameReq,
HttpCollectionHasResponse,
HttpCollectionStatisticsResponse,
HttpCollectionLoadStateReq,
HttpCollectionLoadStateResponse,
} from '../types';
import {
DEFAULT_PRIMARY_KEY_FIELD,
DEFAULT_METRIC_TYPE,
DEFAULT_VECTOR_FIELD,
DEFAULT_DB,
} from '../const';

/**
Expand All @@ -26,20 +32,31 @@ import {
* @method describeCollection - Retrieves the description of a specific collection.
* @method dropCollection - Deletes a specific collection from Milvus.
* @method listCollections - Lists all collections in the Milvus cluster.
* @method hasCollection - Checks if a collection exists in the Milvus cluster.
* @method renameCollection - Renames a collection in the Milvus cluster.
* @method getCollectionStatistics - Retrieves statistics about a collection.
* @method loadCollection - Loads a collection into memory.
* @method releaseCollection - Releases a collection from memory.
* @method getCollectionLoadState - Retrieves the load state of a collection.
*/
export function Collection<T extends Constructor<HttpBaseClient>>(Base: T) {
return class extends Base {
get collectionPrefix() {
return '/vectordb/collections';
}

// POST create collection
async createCollection(
data: HttpCollectionCreateReq,
options?: FetchOptions
): Promise<HttpBaseResponse> {
const url = `/vector/collections/create`;
const url = `${this.collectionPrefix}/create`;

// if some keys not provided, using default value
data.metricType = data.metricType || DEFAULT_METRIC_TYPE;
data.primaryField = data.primaryField || DEFAULT_PRIMARY_KEY_FIELD;
data.vectorField = data.vectorField || DEFAULT_VECTOR_FIELD;
data.primaryFieldName =
data.primaryFieldName || DEFAULT_PRIMARY_KEY_FIELD;
data.vectorFieldName = data.vectorFieldName || DEFAULT_VECTOR_FIELD;

return await this.POST<HttpBaseResponse>(url, data, options);
}
Expand All @@ -49,8 +66,8 @@ export function Collection<T extends Constructor<HttpBaseClient>>(Base: T) {
params: HttpBaseReq,
options?: FetchOptions
): Promise<HttpCollectionDescribeResponse> {
const url = `/vector/collections/describe`;
return await this.GET<HttpCollectionDescribeResponse>(
const url = `${this.collectionPrefix}/describe`;
return await this.POST<HttpCollectionDescribeResponse>(
url,
params,
options
Expand All @@ -62,19 +79,63 @@ export function Collection<T extends Constructor<HttpBaseClient>>(Base: T) {
data: HttpBaseReq,
options?: FetchOptions
): Promise<HttpBaseResponse> {
const url = `/vector/collections/drop`;
const url = `${this.collectionPrefix}/drop`;

return await this.POST<HttpBaseResponse>(url, data, options);
}

// GET list collections
async listCollections(
params: HttpCollectionListReq = {},
params: HttpCollectionListReq = { dbName: DEFAULT_DB },
options?: FetchOptions
): Promise<HttpCollectionListResponse> {
const url = `/vector/collections`;
const url = `${this.collectionPrefix}/list`;

return await this.POST<HttpCollectionListResponse>(url, params, options);
}

async hasCollection(params: Required<HttpBaseReq>, options?: FetchOptions) {
const url = `${this.collectionPrefix}/has`;
return await this.POST<HttpCollectionHasResponse>(url, params, options);
}

async renameCollection(
params: HttpCollectionRenameReq,
options?: FetchOptions
) {
const url = `${this.collectionPrefix}/rename`;
return await this.POST<HttpBaseResponse>(url, params, options);
}

async getCollectionStatistics(params: HttpBaseReq, options?: FetchOptions) {
const url = `${this.collectionPrefix}/get_stats`;
return await this.POST<HttpCollectionStatisticsResponse>(
url,
params,
options
);
}

async loadCollection(params: HttpBaseReq, options?: FetchOptions) {
const url = `${this.collectionPrefix}/load`;
return await this.POST<HttpBaseResponse>(url, params, options);
}

async releaseCollection(params: HttpBaseReq, options?: FetchOptions) {
const url = `${this.collectionPrefix}/release`;
return await this.POST<HttpBaseResponse>(url, params, options);
}

return await this.GET<HttpCollectionListResponse>(url, params, options);
async getCollectionLoadState(
params: HttpCollectionLoadStateReq,
options?: FetchOptions
) {
const url = `${this.collectionPrefix}/get_load_state`;
return await this.POST<HttpCollectionLoadStateResponse>(
url,
params,
options
);
}
};
}
48 changes: 48 additions & 0 deletions milvus/http/Import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { HttpBaseClient } from '../HttpClient';
import {
Constructor,
FetchOptions,
HttpBaseReq,
HttpImportListResponse,
HttpImportCreateReq,
HttpImportCreateResponse,
HttpImportProgressReq,
} from '../types';

/**
*
* @param {Constructor<HttpBaseClient>} Base - The base class to be extended.
* @returns {class} - The extended class with additional methods for collection management.
*
* @method listImportJobs - Lists all import jobs.
* @method createImportJobs - Creates new import jobs.
* @method getImportJobProgress - Retrieves the progress of an import job.
*/
export function Import<T extends Constructor<HttpBaseClient>>(Base: T) {
return class extends Base {
get importPrefix() {
return '/vectordb/jobs/import';
}

async listImportJobs(params: HttpBaseReq, options?: FetchOptions) {
const url = `${this.importPrefix}/list`;
return await this.POST<HttpImportListResponse>(url, params, options);
}

async createImportJobs(
params: HttpImportCreateReq,
options?: FetchOptions
) {
const url = `${this.importPrefix}/create`;
return await this.POST<HttpImportCreateResponse>(url, params, options);
}

async getImportJobProgress(
params: HttpImportProgressReq,
options?: FetchOptions
) {
const url = `${this.importPrefix}/get_progress`;
return await this.POST<HttpImportCreateResponse>(url, params, options);
}
};
}
48 changes: 48 additions & 0 deletions milvus/http/MilvusIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { HttpBaseClient } from '../HttpClient';
import {
Constructor,
FetchOptions,
HttpBaseReq,
HttpBaseResponse,
HttpIndexCreateReq,
HttpIndexBaseReq,
HttpIndexDescribeResponse,
} from '../types';

/**
*
* @param {Constructor<HttpBaseClient>} Base - The base class to be extended.
* @returns {class} - The extended class with additional methods for collection management.
*
*@method createIndex - Creates an index.
*@method dropIndex - Deletes an index.
*@method describeIndex - Describes an index.
*@method listIndexes - Lists all indexes.
*/
export function MilvusIndex<T extends Constructor<HttpBaseClient>>(Base: T) {
return class extends Base {
get indexPrefix() {
return '/vectordb/indexes';
}

async createIndex(params: HttpIndexCreateReq, options?: FetchOptions) {
const url = `${this.indexPrefix}/create`;
return this.POST<HttpBaseResponse>(url, params, options);
}

async dropIndex(params: HttpIndexBaseReq, options?: FetchOptions) {
const url = `${this.indexPrefix}/drop`;
return this.POST<HttpBaseResponse>(url, params, options);
}

async describeIndex(params: HttpIndexBaseReq, options?: FetchOptions) {
const url = `${this.indexPrefix}/describe`;
return this.POST<HttpIndexDescribeResponse>(url, params, options);
}

async listIndexes(params: HttpBaseReq, options?: FetchOptions) {
const url = `${this.indexPrefix}/list`;
return this.POST<HttpBaseResponse<string[]>>(url, params, options);
}
};
}

0 comments on commit 272681a

Please sign in to comment.