Skip to content

Commit

Permalink
updated types for kb
Browse files Browse the repository at this point in the history
  • Loading branch information
md-abid-hussain committed Nov 27, 2024
1 parent d550414 commit 1c64897
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
47 changes: 34 additions & 13 deletions src/knowledge_bases/knowledge_base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import SqlQueryResult from '../sql/sqlQueryResult';
import KnowledgeBaseApiClient from './knowledge_baseApiClient';

/**
* Parameters for configuring a Knowledge Base.
*
* @property {Array<string>} [metadata_columns] - Optional array of metadata column names.
* @property {Array<string>} [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<string>;
content_columns?: Array<string>;
id_column?: string | null;
}

/**
* Represent MindsDB Knowledge Base and all supported operations.
*/
Expand All @@ -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<string>;
/** Content column names (default content) */
contentColumns: Array<string>;
/** ID column name */
idColumn: string;
idColumn: string | null;
/** Query string for knowledge base */
query: string | null;
/** Value to limit the knowledge base output result */
Expand All @@ -37,33 +50,39 @@ 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;
this.name = data.name;
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;
Expand Down Expand Up @@ -137,9 +156,11 @@ export default class KnowledgeBase {
* Insert data into Knowledge Base
* kb.insert([{column1: value1, column2: value2}]);
*
* @param {Array<any>} records - Array of JSON object
* @param {Array<unknown>} records - Array of JSON object
*/
async insert(records: Array<any>) {
async insert(records: Array<{
[key: string]: unknown;
}>) {
let valueString = '';
records.forEach((row) => {
valueString += '(';
Expand Down
4 changes: 2 additions & 2 deletions src/knowledge_bases/knowledge_baseApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default abstract class KnowledgeBaseApiClient {
* @param {Array<string>} metadataColumns - Metadata columns name (optional)
* @param {Array<string>} 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,
Expand All @@ -41,7 +41,7 @@ export default abstract class KnowledgeBaseApiClient {
metadataColumns?: Array<string>,
contentColumns?: Array<string>,
idColumn?: string,
params?: any
params?: unknown
): Promise<KnowledgeBase>;

/**
Expand Down
15 changes: 10 additions & 5 deletions src/knowledge_bases/knowledge_baseRestApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
})
);
}

Expand Down Expand Up @@ -71,7 +76,7 @@ export default class KnowledgeBaseRestApiClient extends KnowledgeBaseApiClient {
* @param {Array<string>} metadataColumns - Metadata columns name (optional)
* @param {Array<string>} 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,
Expand All @@ -81,9 +86,9 @@ export default class KnowledgeBaseRestApiClient extends KnowledgeBaseApiClient {
metadataColumns?: string[],
contentColumns?: string[],
idColumn?: string,
params?: any
params?: unknown
): Promise<KnowledgeBase> {
let paramsOut: { [key: string]: any } = {};
let paramsOut: { [key: string]: unknown } = {};
if (metadataColumns) {
paramsOut['metadata_columns'] = metadataColumns;
}
Expand Down

0 comments on commit 1c64897

Please sign in to comment.