From 1c6489712fc36f51c1578e206243c784c810bbcb Mon Sep 17 00:00:00 2001 From: Md Abid Hussain <101964499+md-abid-hussain@users.noreply.github.com> Date: Wed, 27 Nov 2024 20:40:54 +0000 Subject: [PATCH] updated types for kb --- src/knowledge_bases/knowledge_base.ts | 47 ++++++++++++++----- .../knowledge_baseApiClient.ts | 4 +- .../knowledge_baseRestApiClient.ts | 15 ++++-- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/knowledge_bases/knowledge_base.ts b/src/knowledge_bases/knowledge_base.ts index d289a26..95dbd3b 100644 --- a/src/knowledge_bases/knowledge_base.ts +++ b/src/knowledge_bases/knowledge_base.ts @@ -1,6 +1,19 @@ import SqlQueryResult from '../sql/sqlQueryResult'; import KnowledgeBaseApiClient from './knowledge_baseApiClient'; +/** + * Parameters for configuring a Knowledge Base. + * + * @property {Array} [metadata_columns] - Optional array of metadata column names. + * @property {Array} [content_columns] - Optional array of content column names. + * @property {string | null} [id_column] - Optional ID column name, can be null. + */ +export type KnowledgeBaseParams = { + metadata_columns?: Array; + content_columns?: Array; + id_column?: string | null; +} + /** * Represent MindsDB Knowledge Base and all supported operations. */ @@ -18,13 +31,13 @@ export default class KnowledgeBase { /** Name of Embedding model used */ model: string | null; /** Params for knowledge base in JSON Object */ - params: any; + params: KnowledgeBaseParams; /** Metadata columns name */ metadataColumns: Array; /** Content column names (default content) */ contentColumns: Array; /** ID column name */ - idColumn: string; + idColumn: string | null; /** Query string for knowledge base */ query: string | null; /** Value to limit the knowledge base output result */ @@ -37,12 +50,17 @@ export default class KnowledgeBase { * * @param {KnowledgeBaseApiClient} knowledgeBaseApiClient - API client for executing Knowledge Base operations * @param {string} project - Project name in which knowledge base belongs - * @param {any} data - Knowledge Base data in JSON Object + * @param {unknown} data - Knowledge Base data in JSON Object */ constructor( knowledgeBaseApiClient: KnowledgeBaseApiClient, project: string, - data: any + data: { + name: string; + storage: string | null; + model: string | null; + params: KnowledgeBaseParams | string; + } ) { this.knowledgeBaseApiClient = knowledgeBaseApiClient; this.project = project; @@ -50,20 +68,21 @@ export default class KnowledgeBase { this.tableName = `${project}.${this.name}`; this.storage = data.storage || null; this.model = data.model || null; - let params: any = data.params || {}; + let params = data.params || {}; + let kbParams: KnowledgeBaseParams = {} if (typeof params === 'string') { try { - params = JSON.parse(params); + kbParams = JSON.parse(params); } catch (error) { - params = {}; + kbParams = {}; } } - this.metadataColumns = params['metadata_columns'] || []; - this.contentColumns = params['content_columns'] || []; - this.idColumn = params['id_column'] || null; + this.metadataColumns = kbParams['metadata_columns'] || []; + this.contentColumns = kbParams['content_columns'] || []; + this.idColumn = kbParams['id_column'] || null; - this.params = params; + this.params = kbParams; this.query = null; this.limit = null; @@ -137,9 +156,11 @@ export default class KnowledgeBase { * Insert data into Knowledge Base * kb.insert([{column1: value1, column2: value2}]); * - * @param {Array} records - Array of JSON object + * @param {Array} records - Array of JSON object */ - async insert(records: Array) { + async insert(records: Array<{ + [key: string]: unknown; + }>) { let valueString = ''; records.forEach((row) => { valueString += '('; diff --git a/src/knowledge_bases/knowledge_baseApiClient.ts b/src/knowledge_bases/knowledge_baseApiClient.ts index 47f5ec8..4310073 100644 --- a/src/knowledge_bases/knowledge_baseApiClient.ts +++ b/src/knowledge_bases/knowledge_baseApiClient.ts @@ -31,7 +31,7 @@ export default abstract class KnowledgeBaseApiClient { * @param {Array} metadataColumns - Metadata columns name (optional) * @param {Array} contentColumns - Content column names (default content) (optional) * @param {string} idColumn - ID column name (optional) - * @param {any} params - Params for knowledge base in JSON Object (optional) + * @param {unknown} params - Params for knowledge base in JSON Object (optional) */ abstract createKnowledgeBase( name: string, @@ -41,7 +41,7 @@ export default abstract class KnowledgeBaseApiClient { metadataColumns?: Array, contentColumns?: Array, idColumn?: string, - params?: any + params?: unknown ): Promise; /** diff --git a/src/knowledge_bases/knowledge_baseRestApiClient.ts b/src/knowledge_bases/knowledge_baseRestApiClient.ts index 1463fa3..4c71559 100644 --- a/src/knowledge_bases/knowledge_baseRestApiClient.ts +++ b/src/knowledge_bases/knowledge_baseRestApiClient.ts @@ -2,7 +2,7 @@ import { Axios } from 'axios'; import Constants from '../constants'; import SqlApiClient from '../sql/sqlApiClient'; import SqlQueryResult from '../sql/sqlQueryResult'; -import KnowledgeBase from './knowledge_base'; +import KnowledgeBase, { KnowledgeBaseParams } from './knowledge_base'; import KnowledgeBaseApiClient from './knowledge_baseApiClient'; /** Implementation of KnowledgeBaseApiClient that goes through the REST API */ @@ -34,7 +34,12 @@ export default class KnowledgeBaseRestApiClient extends KnowledgeBaseApiClient { const response = await this.sqlClient.runQuery(astQuery); return response.rows.map( - (row) => new KnowledgeBase(this, row.project, row) + (row) => new KnowledgeBase(this, row.project, row as { + name: string; + storage: string | null; + model: string | null; + params: KnowledgeBaseParams | string; + }) ); } @@ -71,7 +76,7 @@ export default class KnowledgeBaseRestApiClient extends KnowledgeBaseApiClient { * @param {Array} metadataColumns - Metadata columns name (optional) * @param {Array} contentColumns - Content column names (default content) (optional) * @param {string} idColumn - ID column name (optional) - * @param {any} params - Params for knowledge base in JSON Object (optional) + * @param {unknown} params - Params for knowledge base in JSON Object (optional) */ override async createKnowledgeBase( name: string, @@ -81,9 +86,9 @@ export default class KnowledgeBaseRestApiClient extends KnowledgeBaseApiClient { metadataColumns?: string[], contentColumns?: string[], idColumn?: string, - params?: any + params?: unknown ): Promise { - let paramsOut: { [key: string]: any } = {}; + let paramsOut: { [key: string]: unknown } = {}; if (metadataColumns) { paramsOut['metadata_columns'] = metadataColumns; }