new(core.gbapp): LLM alerts for data.

This commit is contained in:
me@rodrigorodriguez.com 2024-10-16 02:50:24 -03:00
parent 52f416076e
commit 789fd79777
4 changed files with 102 additions and 85 deletions

View file

@ -481,6 +481,14 @@ export class KeywordsExpressions {
} }
]; ];
keywords[i++] = [
/^\s*(.*)\=\s*(ANSWER)(\s*)(.*)/gim,
($0, $1, $2, $3, $4) => {
const params = this.getParams($4, ['text']);
return `${$1} = await sys.answer ({pid: pid, ${params}})`;
}
];
keywords[i++] = [ keywords[i++] = [
/^\s*(.*)\=\s*(GET IMAGE)(\s*)(.*)/gim, /^\s*(.*)\=\s*(GET IMAGE)(\s*)(.*)/gim,
($0, $1, $2, $3, $4) => { ($0, $1, $2, $3, $4) => {

View file

@ -2460,6 +2460,16 @@ export class SystemKeywords {
GBLogEx.info(min, `BlueSky Automation: ${text}.`); GBLogEx.info(min, `BlueSky Automation: ${text}.`);
} }
/**
*/
public async answer({ pid, text }) {
const { min, user } = await DialogKeywords.getProcessInfo(pid);
const answer = await ChatServices.answerByLLM(pid, min, user, text)
GBLogEx.info(min, `ANSWER ${text} TO ${answer}`);
return answer.answer;
}
/** /**
* HEAR description * HEAR description
* text = REWRITE description * text = REWRITE description
@ -2881,87 +2891,86 @@ export class SystemKeywords {
public async refreshDataSourceCache({ pid, connectionName }) { public async refreshDataSourceCache({ pid, connectionName }) {
const { min, user, params, step } = await DialogKeywords.getProcessInfo(pid); const { min, user, params, step } = await DialogKeywords.getProcessInfo(pid);
let sqliteFilePath =path.join('work', GBUtil.getGBAIPath(min.botId), `${connectionName}.sqlite`); let sqliteFilePath = path.join('work', GBUtil.getGBAIPath(min.botId), `${connectionName}.sqlite`);
// Step 1: Clean the SQLite file if it already exists // Step 1: Clean the SQLite file if it already exists
if (await GBUtil.exists(sqliteFilePath)) { if (await GBUtil.exists(sqliteFilePath)) {
await fs.unlink(sqliteFilePath); // Remove the file await fs.unlink(sqliteFilePath); // Remove the file
GBLogEx.info(min, `${sqliteFilePath} has been cleaned.`); GBLogEx.info(min, `${sqliteFilePath} has been cleaned.`);
} }
// Step 2: Connect to SQLite (Local) // Step 2: Connect to SQLite (Local)
const sqlite = new Sequelize({ const sqlite = new Sequelize({
dialect: 'sqlite', dialect: 'sqlite',
storage: sqliteFilePath storage: sqliteFilePath
}); });
// Get the connection details from the min object // Get the connection details from the min object
let con = min[connectionName]; let con = min[connectionName];
const dialect = con.dialect.name; const dialect = con.dialect.name;
// Step 3: Get the list of all tables from the source database // Step 3: Get the list of all tables from the source database
const tables = await GBUtil.listTables(dialect, min.core.sequelize); const tables = await GBUtil.listTables(dialect, min.core.sequelize);
const tableNames = tables.map(table => Object.values(table)[0]);
// Function to map source database datatypes to SQLite-compatible datatypes // Function to map source database datatypes to SQLite-compatible datatypes
const mapToSQLiteType = (columnType) => { const mapToSQLiteType = (columnType) => {
const typeMapping = { const typeMapping = {
'VARCHAR': DataTypes.STRING, 'VARCHAR': DataTypes.STRING,
'CHAR': DataTypes.STRING, 'CHAR': DataTypes.STRING,
'TEXT': DataTypes.TEXT, 'TEXT': DataTypes.TEXT,
'TINYINT': DataTypes.INTEGER, 'TINYINT': DataTypes.INTEGER,
'SMALLINT': DataTypes.INTEGER, 'SMALLINT': DataTypes.INTEGER,
'MEDIUMINT': DataTypes.INTEGER, 'MEDIUMINT': DataTypes.INTEGER,
'INT': DataTypes.INTEGER, 'INT': DataTypes.INTEGER,
'INTEGER': DataTypes.INTEGER, 'INTEGER': DataTypes.INTEGER,
'BIGINT': DataTypes.BIGINT, 'BIGINT': DataTypes.BIGINT,
'FLOAT': DataTypes.FLOAT, 'FLOAT': DataTypes.FLOAT,
'DOUBLE': DataTypes.DOUBLE, 'DOUBLE': DataTypes.DOUBLE,
'DECIMAL': DataTypes.DECIMAL, 'DECIMAL': DataTypes.DECIMAL,
'DATE': DataTypes.DATE, 'DATE': DataTypes.DATE,
'DATETIME': DataTypes.DATE, 'DATETIME': DataTypes.DATE,
'TIMESTAMP': DataTypes.DATE, 'TIMESTAMP': DataTypes.DATE,
'BLOB': DataTypes.BLOB, 'BLOB': DataTypes.BLOB,
'BOOLEAN': DataTypes.BOOLEAN, 'BOOLEAN': DataTypes.BOOLEAN,
// Add more mappings as needed // Add more mappings as needed
};
// Return mapped type or fallback to STRING if not mapped
return typeMapping[columnType.toUpperCase()] || DataTypes.STRING;
}; };
// Step 4: Retrieve and export data for each table // Return mapped type or fallback to STRING if not mapped
for (const table of tableNames) { return typeMapping[columnType.toUpperCase()] || DataTypes.STRING;
// Retrieve rows from the source table };
const [rows] = await min.core.sequelize.query(`SELECT * FROM ${table}`);
if (rows.length === 0) continue; // Skip if the table has no data // Step 4: Retrieve and export data for each table
for (const table of tables) {
// Retrieve rows from the source table
const [rows] = await min.core.sequelize.query(`SELECT * FROM ${table}`);
// Get the schema for the current table from the source database if (rows.length === 0) continue; // Skip if the table has no data
const columns = await min.core.sequelize.queryInterface.describeTable(table);
// Create a schema object for SQLite // Get the schema for the current table from the source database
const schema = {}; const columns = await min.core.sequelize.queryInterface.describeTable(table);
Object.keys(columns).forEach(col => {
const columnType = columns[col].type;
schema[col] = mapToSQLiteType(columnType); // Map source type to SQLite type
});
// Define the model dynamically for each table in SQLite // Create a schema object for SQLite
const Model = sqlite.define(table, schema, { timestamps: false }); const schema = {};
Object.keys(columns).forEach(col => {
const columnType = columns[col].type;
schema[col] = mapToSQLiteType(columnType); // Map source type to SQLite type
});
// Sync the model (create table) // Define the model dynamically for each table in SQLite
await Model.sync({ force: true }); const Model = sqlite.define(table, schema, { timestamps: false });
// Bulk insert rows into the SQLite table // Sync the model (create table)
await Model.bulkCreate(rows); await Model.sync({ force: true });
}
GBLogEx.info(min, `All tables have been successfully exported to ${sqliteFilePath}`); // Bulk insert rows into the SQLite table
await Model.bulkCreate(rows);
}
// Close SQLite connection GBLogEx.info(min, `All tables have been successfully exported to ${sqliteFilePath}`);
await sqlite.close();
// Close SQLite connection
await sqlite.close();
} }
} }

View file

@ -221,7 +221,7 @@ export class GBUtil {
type: QueryTypes.RAW type: QueryTypes.RAW
} }
)[0]; )[0];
} else if (dialect === 'mariadb') { } else {
tables = await seq.getQueryInterface().showAllTables(); tables = await seq.getQueryInterface().showAllTables();
} }
return tables; return tables;

View file

@ -2,6 +2,6 @@ REM SET SCHEDULE
REFRESH "llm" REFRESH "llm"
list = REWRITE "A list of latest 10 orders made." list = ANSWER "A list of latest 10 orders made."
TALK TO admin TALK "The Report is: \n" + list