fix(basic.gblib): FUNCTION GPT.

This commit is contained in:
Rodrigo Rodriguez 2024-03-06 14:38:37 -03:00
parent 49af4cfab7
commit c6cd7cd5b8
3 changed files with 36 additions and 30 deletions

View file

@ -266,7 +266,6 @@ export class GBVMService extends GBService {
let connections = null; let connections = null;
if (Fs.existsSync(filePath)) { if (Fs.existsSync(filePath)) {
connections = JSON.parse(Fs.readFileSync(filePath, 'utf8')); connections = JSON.parse(Fs.readFileSync(filePath, 'utf8'));
} }
tableDef.forEach(async t => { tableDef.forEach(async t => {
@ -725,19 +724,22 @@ export class GBVMService extends GBService {
} }
public static getMetadata(mainName: string, propertiesText, description) { public static getMetadata(mainName: string, propertiesText, description) {
let properties; let properties = {};
if (!propertiesText) { if (!propertiesText || !description) {
return {} return {}
} }
const getType = asClause => { const getType = asClause => {
if (asClause.indexOf('AS STRING') !== -1) {
asClause = asClause.trim().toUpperCase();
if (asClause.indexOf('STRING') !== -1) {
return 'string'; return 'string';
} }
else if (asClause.indexOf('AS OBJECT') !== -1) { else if (asClause.indexOf('OBJECT') !== -1) {
return 'object'; return 'object';
} }
else if (asClause.indexOf('AS INTEGER') !== -1 || asClause.indexOf('AS NUMBER') !== -1) { else if (asClause.indexOf('INTEGER') !== -1 || asClause.indexOf('NUMBER') !== -1) {
return 'number'; return 'number';
} else { } else {
return 'enum'; return 'enum';
@ -755,13 +757,16 @@ export class GBVMService extends GBService {
} else if (t === 'string') { } else if (t === 'string') {
element = z.string(); element = z.string();
} else if (t === 'object') { } else if (t === 'object') {
element = z.quotelessJson({}); element = z.string();
} else if (t === 'number') { } else if (t === 'number') {
element = z.number(); element = z.number();
} else {
GBLog.warn(`Element type invalid specified on .docx: ${propertiesExp[0]}`);
} }
element.describe(propertiesExp[3]); element.describe(propertiesExp[3]);
element['type'] = t; element['type'] = t;
properties[propertiesExp[1]] = element; properties[propertiesExp[1].trim()] = element;
} }
@ -862,7 +867,7 @@ export class GBVMService extends GBService {
emmit = false; emmit = false;
} }
const descriptionKeyword = /^\s*DESCRIPTION\s*\"(.*)\"/gim; const descriptionKeyword = /^\s*DESCRIPTION\s(.*)/gim;
let descriptionReg = descriptionKeyword.exec(line); let descriptionReg = descriptionKeyword.exec(line);
if (descriptionReg) { if (descriptionReg) {
description = descriptionReg[1]; description = descriptionReg[1];

View file

@ -81,7 +81,7 @@ export class ChatServices {
subjects: GuaribasSubject[] subjects: GuaribasSubject[]
) { ) {
if (!process.env.OPENAI_KEY) { if (!process.env.OPENAI_API_KEY) {
return { answer: undefined, questionId: 0 }; return { answer: undefined, questionId: 0 };
} }
@ -99,12 +99,13 @@ export class ChatServices {
// Adds .gbdialog as functions if any to GPT Functions. // Adds .gbdialog as functions if any to GPT Functions.
await CollectionUtil.asyncForEach(Object.values(min.scriptMap), async script => { await CollectionUtil.asyncForEach(Object.keys(min.scriptMap), async script => {
const path = DialogKeywords.getGBAIPath(min.botId, "gbdialog", null); const path = DialogKeywords.getGBAIPath(min.botId, "gbdialog", null);
const localFolder = Path.join('work', path, `${script}.json`); const functionJSON = Path.join('work', path, `${script}.json`);
if (Fs.existsSync(localFolder)) { if (Fs.existsSync(functionJSON)) {
const func = Fs.readFileSync(localFolder).toJSON(); const func = JSON.parse(Fs.readFileSync(functionJSON, 'utf8'));
functions.push(func); functions.push(func);
} }
@ -117,7 +118,7 @@ export class ChatServices {
// in plain text to be used in system prompt. // in plain text to be used in system prompt.
let functionDef = Object.keys(functions) let functionDef = Object.keys(functions)
.map((toolname) => `${toolname}: ${functions[toolname].description}`) .map((toolname) => `${functions[toolname].function.name}: ${functions[toolname].function.description}`)
.join("\n"); .join("\n");
let promptTemplate = `Answer in ${contentLocale}. let promptTemplate = `Answer in ${contentLocale}.
@ -152,6 +153,7 @@ export class ChatServices {
}); });
const llm = new ChatOpenAI({ const llm = new ChatOpenAI({
openAIApiKey: process.env.OPENAI_API_KEY,
modelName: "gpt-3.5-turbo-0125", modelName: "gpt-3.5-turbo-0125",
temperature: 0, temperature: 0,
}); });

View file

@ -314,7 +314,7 @@ export class KBService implements IGBKBService {
query = `${query} ${text}`; query = `${query} ${text}`;
} }
} }
let returnedScore = 0;
const key = instance.searchKey ? instance.searchKey : const key = instance.searchKey ? instance.searchKey :
GBServer.globals.minBoot.instance.searchKey; GBServer.globals.minBoot.instance.searchKey;
const host = instance.searchHost ? instance.searchHost : const host = instance.searchHost ? instance.searchHost :
@ -346,7 +346,7 @@ export class KBService implements IGBKBService {
top: 1 top: 1
}); });
let returnedScore = 0;
// Searches via Search (Azure Search). // Searches via Search (Azure Search).
@ -370,19 +370,18 @@ export class KBService implements IGBKBService {
} }
} }
} }
GBLog.info(
`SEARCH returned LOW level score, calling GPT
returnedScore: ${returnedScore} < required (searchScore): ${searchScore}`
);
return await ChatServices.answerByGPT(min,pid,
query,
searchScore,
subjects
);
} }
GBLog.info(
`SEARCH returned LOW level score, calling GPT
returnedScore: ${returnedScore} < required (searchScore): ${searchScore}`
);
return await ChatServices.answerByGPT(min,pid,
query,
searchScore,
subjects
);
} }