new(gpt.gblib): PDF opener.

This commit is contained in:
Rodrigo Rodriguez 2024-03-22 22:51:36 -03:00
parent 81237bd787
commit 4fa02a625b
6 changed files with 38 additions and 17 deletions

View file

@ -339,7 +339,8 @@ export class GBConversationalService {
} }
public async sendEvent(min: GBMinInstance, step: GBDialogStep, name: string, value: Object): Promise<any> { public async sendEvent(min: GBMinInstance, step: GBDialogStep, name: string, value: Object): Promise<any> {
if (!this.userMobile(step) && step.context.activity.channelId !== 'msteams') { if ( step.context.activity.channelId !== 'msteams' &&
step.context.activity.channelId !== 'omnichannel') {
GBLog.info( GBLog.info(
`Sending event ${name}:${typeof value === 'object' ? JSON.stringify(value) : value ? value : ''} to client...` `Sending event ${name}:${typeof value === 'object' ? JSON.stringify(value) : value ? value : ''} to client...`
); );

View file

@ -891,6 +891,10 @@ export class GBDeployer implements IGBDeployer {
`/kb/${gbaiName}/${packageName}/images`, `/kb/${gbaiName}/${packageName}/images`,
express.static(urlJoin('work', gbaiName, filename, 'images')) express.static(urlJoin('work', gbaiName, filename, 'images'))
); );
GBServer.globals.server.use(
`/kb/${gbaiName}/${packageName}/docs`,
express.static(urlJoin('work', gbaiName, filename, 'docs'))
);
GBServer.globals.server.use( GBServer.globals.server.use(
`/kb/${gbaiName}/${packageName}/audios`, `/kb/${gbaiName}/${packageName}/audios`,
express.static(urlJoin('work', gbaiName, filename, 'audios')) express.static(urlJoin('work', gbaiName, filename, 'audios'))

View file

@ -717,9 +717,9 @@ export class GBMinService {
'Ocp-Apim-Subscription-Key': instance.speechKey 'Ocp-Apim-Subscription-Key': instance.speechKey
} }
}; };
const url = urlJoin(instance.speechEndpoint, '/sts/v1.0/issueToken');
try { try {
const res = await fetch(instance.speechEndpoint, options); const res = await fetch(url, options);
return res.text(); return res.text();
} catch (error) { } catch (error) {
const msg = `Error calling Speech to Text client. Error is: ${error}.`; const msg = `Error calling Speech to Text client. Error is: ${error}.`;

View file

@ -64,7 +64,7 @@ import { GBLogEx } from '../../core.gbapp/services/GBLogEx.js';
export interface CustomOutputParserFields { } export interface CustomOutputParserFields { }
export type ExpectedOutput = string; export type ExpectedOutput = any;
function isChatGeneration( function isChatGeneration(
llmOutput: ChatGeneration | Generation llmOutput: ChatGeneration | Generation
@ -94,7 +94,8 @@ class CustomHandler extends BaseCallbackHandler {
const logHandler = new CustomHandler(); const logHandler = new CustomHandler();
export class GBLLMOutputParser extends BaseLLMOutputParser<ExpectedOutput> { export class GBLLMOutputParser extends
BaseLLMOutputParser<ExpectedOutput> {
lc_namespace = ["langchain", "output_parsers"]; lc_namespace = ["langchain", "output_parsers"];
private toolChain: RunnableSequence private toolChain: RunnableSequence
@ -148,7 +149,9 @@ export class GBLLMOutputParser extends BaseLLMOutputParser<ExpectedOutput> {
const {url} = await ChatServices.pdfPageAsImage(this.min, metadata.file, const {url} = await ChatServices.pdfPageAsImage(this.min, metadata.file,
metadata.page); metadata.page);
result = `![alt text](${url}) result = `![alt text](${url})
${result}`; ${text}`;
return [ result, metadata.file, metadata.page];
} }
} }
@ -403,6 +406,7 @@ export class ChatServices {
]); ]);
let result; let result;
let text, file, page;
// Choose the operation mode of answer generation, based on // Choose the operation mode of answer generation, based on
@ -418,7 +422,8 @@ export class ChatServices {
} }
else if (LLMMode === "document") { else if (LLMMode === "document") {
result = await combineDocumentsChain.invoke(question); [text, file, page] = await combineDocumentsChain.invoke(question);
result = text;
} else if (LLMMode === "function") { } else if (LLMMode === "function") {
@ -435,18 +440,18 @@ export class ChatServices {
GBLog.info(`Invalid Answer Mode in Config.xlsx: ${LLMMode}.`); GBLog.info(`Invalid Answer Mode in Config.xlsx: ${LLMMode}.`);
} }
const resultToPersist = result.replace(/\!\[.*\)/gi, ''); // Removes .MD url.
await memory.saveContext( await memory.saveContext(
{ {
input: question, input: question,
}, },
{ {
output: resultToPersist output: result.replace(/\!\[.*\)/gi, '') // Removes .MD url beforing adding to history.
} }
); );
GBLog.info(`GPT Result: ${result.toString()}`); GBLog.info(`GPT Result: ${result.toString()}`);
return { answer: result.toString(), questionId: 0 }; return { answer: result.toString(), file, questionId: 0, page };
} }

View file

@ -37,20 +37,18 @@
import { GBServer } from '../../../src/app.js'; import { GBServer } from '../../../src/app.js';
import { BotAdapter } from 'botbuilder'; import { BotAdapter } from 'botbuilder';
import { WaterfallDialog } from 'botbuilder-dialogs'; import { WaterfallDialog } from 'botbuilder-dialogs';
import { ChatGPTAPIBrowser } from 'chatgpt';
import { GBLog, GBMinInstance, IGBDialog, IGBPackage } from 'botlib'; import { GBLog, GBMinInstance, IGBDialog, IGBPackage } from 'botlib';
import { Messages } from '../strings.js'; import { Messages } from '../strings.js';
import { KBService } from './../services/KBService.js'; import { KBService } from './../services/KBService.js';
import { GuaribasAnswer } from '../models/index.js'; import { GuaribasAnswer } from '../models/index.js';
import { SecService } from '../../security.gbapp/services/SecService.js'; import { SecService } from '../../security.gbapp/services/SecService.js';
import { CollectionUtil, AzureText } from 'pragmatismo-io-framework'; import { CollectionUtil } from 'pragmatismo-io-framework';
import { GBVMService } from '../../basic.gblib/services/GBVMService.js'; import { GBVMService } from '../../basic.gblib/services/GBVMService.js';
import { GBImporter } from '../../core.gbapp/services/GBImporterService.js'; import { GBImporter } from '../../core.gbapp/services/GBImporterService.js';
import { GBDeployer } from '../../core.gbapp/services/GBDeployer.js'; import { GBDeployer } from '../../core.gbapp/services/GBDeployer.js';
import { GBConfigService } from '../../core.gbapp/services/GBConfigService.js';
import Fs from 'fs';
import urlJoin from 'url-join'; import urlJoin from 'url-join';
import { SystemKeywords } from '../../basic.gblib/services/SystemKeywords.js'; import { SystemKeywords } from '../../basic.gblib/services/SystemKeywords.js';
import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords.js';
/** /**
* Dialog arguments. * Dialog arguments.
@ -235,19 +233,32 @@ export class AskDialog extends IGBDialog {
return; return;
} }
const results = await service.ask(min, user, step, step.context.activity['pid'], text, searchScore, null /* user.subjects */); const results:any = await service.ask(min, user, step, step.context.activity['pid'], text, searchScore, null /* user.subjects */);
// If there is some result, answer immediately. // If there is some result, answer immediately.
if (results !== undefined && results.answer !== undefined) { if (results !== undefined && results.answer !== undefined) {
if (results.file){
const path = DialogKeywords.getGBAIPath(min.botId, `gbkb`);
const url = urlJoin('kb', path, 'docs', results.file);
await min.conversationalService.sendEvent(
min, step, 'play', {
playerType: 'url',
data: `${url}#page=${results.page}&toolbar=0&messages=0&statusbar=0&navpanes=0`
});
}
// Sends the answer to all outputs, including projector. // Sends the answer to all outputs, including projector.
answer = results.answer; answer = results.answer.trim();
return await AskDialog.handleAnswer(service, min, step, user, answer); return await AskDialog.handleAnswer(service, min, step, user, answer);
} }
GBLog.info(`SEARCH called but NO answer could be found (zero results).`); GBLog.info(`SEARCH called but NO answer could be found (zero results).`);
// Not found. // Not found.

View file

@ -653,7 +653,7 @@ export class KBService implements IGBKBService {
} else { } else {
await min.conversationalService.sendText(min, step, answer); await min.conversationalService.sendText(min, step, answer);
await min.conversationalService.sendEvent(min, step, 'stop', undefined);
} }
} }