import { TENCENT_CLOUD_VECTOR_DB_API_INFO } from "../../../consts/tencentCloudVector"; import { ICollection } from "../../../interfaces/vectorDB/ICollection"; import { TencentCloudVecorDBCollectionCreateAIParams, TencentCloudVecorDBCollectionParams } from "../../../types/tencent"; import { VectorResponseBase, VectorResponseCommon } from "../../../types/vector"; import { CollectionCreateParams, CollectionCreateParamsIndex, CollectionDescribeParams, CollectionDescribeResponse, CollectionDropParams, CollectionIndexRebuildParams, CollectionListParams, CollectionListResponse, CollectionTruncateParams } from "../../../types/vector/collection"; import { Logger } from "../../../utils/Logger"; import { TencentCloudVectorApiClient } from "../../../utils/tencentCloudVectorClient"; export class TencentCloudVectorCollection implements ICollection { client: TencentCloudVectorApiClient constructor(client: TencentCloudVectorApiClient) { this.client = client } create(params: CollectionCreateParams, isAIPackage?: boolean): Promise { const url = isAIPackage ? TENCENT_CLOUD_VECTOR_DB_API_INFO.CollectionView.AiCollectionViewCreate.uri : TENCENT_CLOUD_VECTOR_DB_API_INFO.Collection.CollectionCreate.uri if (!isAIPackage) { params.replicaNum = params.replicaNum || 0 params.shardNum = params.shardNum || 1 params.embedding = { field: params.embedding?.field || "text", vectorField: params.embedding?.vectorField || "vector", model: params.embedding?.model || "text2vec-large-chinese" } params.indexes = params.indexes || [] as CollectionCreateParamsIndex[] if (!this.checkCreateIndexParams(params.indexes, "id")) { params.indexes.push({ fieldName: "id", fieldType: "string", indexType: "primaryKey" }) } if (!this.checkCreateIndexParams(params.indexes, "vector")) { params.indexes.push({ fieldName: "vector", fieldType: "vector", indexType: "HNSW", metricType: "COSINE", params: { M: 16, efConstruction: 200 } }) } return this.client.request(url, "POST", params) } else { const paramsAI: TencentCloudVecorDBCollectionCreateAIParams = { database: params.database, collectionView: params.collection, embedding: { language: params.embedding?.language || "multi", enableWordsEmbedding: params.embedding?.enableWordsEmbedding || false }, indexes: [], splitterPreprocess: params.splitterPreprocess } params.indexes?.forEach(item => { if (item.indexType === "filter") { paramsAI.indexes.push({ fieldName: item.fieldName, fieldType: item.fieldType, indexType: item.indexType }) } }) return this.client.request(url, "POST", paramsAI) } } private checkCreateIndexParams(indexes: CollectionCreateParamsIndex[], type: "id" | "vector"): boolean { if (indexes.length === 0) { return false } if (type === "id") { return indexes.filter(item => item.fieldName === "id").length > 0 } else { return indexes.filter(item => item.fieldName === "vector").length > 0 } } drop(params: CollectionDropParams, isAIPackage?: boolean): Promise { const url = isAIPackage ? TENCENT_CLOUD_VECTOR_DB_API_INFO.CollectionView.AiCollectionViewDrop.uri : TENCENT_CLOUD_VECTOR_DB_API_INFO.Collection.CollectionDrop.uri if (!isAIPackage) { return this.client.request(url, "POST", params) } else { return this.client.request(url, "POST", { database: params.database, collectionView: params.collection } as TencentCloudVecorDBCollectionParams) } } async list(params: CollectionListParams, isAIPackage?: boolean): Promise { const url = isAIPackage ? TENCENT_CLOUD_VECTOR_DB_API_INFO.CollectionView.AiCollectionViewList.uri : TENCENT_CLOUD_VECTOR_DB_API_INFO.Collection.CollectionList.uri if (!isAIPackage) { return await this.client.request(url, "POST", params) } else { const ret = await this.client.request(url, "POST", params) return { code: ret.code, msg: ret.msg, collections: (ret as any).collectionViews || [] } } } async describe(params: CollectionDescribeParams, isAIPackage?: boolean): Promise { const url = isAIPackage ? TENCENT_CLOUD_VECTOR_DB_API_INFO.CollectionView.AiCollectionViewDescribe.uri : TENCENT_CLOUD_VECTOR_DB_API_INFO.Collection.CollectionDescribe.uri if (!isAIPackage) { return this.client.request(url, "POST", params) } else { const ret = await this.client.request(url, "POST", { database: params.database, collectionView: params.collection }) return { code: ret.code, msg: ret.msg, collection: (ret as any).collectionView } } } truncate(params: CollectionTruncateParams, isAIPackage?: boolean): Promise { const url = isAIPackage ? TENCENT_CLOUD_VECTOR_DB_API_INFO.CollectionView.AiCollectionViewTruncate.uri : TENCENT_CLOUD_VECTOR_DB_API_INFO.Collection.CollectionTruncate.uri if (!isAIPackage) { return this.client.request(url, "POST", params) } else { return this.client.request(url, "POST", { database: params.database, collectionView: params.collection } as TencentCloudVecorDBCollectionParams) } } indexRebuild(params: CollectionIndexRebuildParams, isAIPackage?: boolean): Promise { if (isAIPackage) return Promise.reject("Not support indexRebuild for AI package") const url = TENCENT_CLOUD_VECTOR_DB_API_INFO.Index.IndexRebuild.uri return this.client.request(url, "POST", params) } }