new(all): ChatGPT integration.

This commit is contained in:
rodrigorodriguez 2022-12-15 23:03:20 -03:00
parent 179b20a248
commit b75d9cf793
7 changed files with 1532 additions and 39 deletions

1495
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -87,6 +87,7 @@
"botframework-connector": "4.18.0",
"botlib": "3.0.0",
"c3-chart-maker": "0.2.8",
"chatgpt": "^2.4.2",
"chrome-remote-interface": "0.31.3",
"cli-progress": "3.11.2",
"cli-spinner": "0.2.10",
@ -110,7 +111,7 @@
"koa-router": "12.0.0",
"lodash": "4.17.21",
"luxon": "3.1.0",
"mammoth": "1.5.1",
"mammoth": "1.5.1",
"moment": "1.3.0",
"ms-rest-azure": "3.0.0",
"nexmo": "2.9.1",

View file

@ -335,7 +335,7 @@ export class GBMinService {
});
client.clientAuthorizations.add(
'AuthorizationBotConnector',
new Swagger.ApiKeyAuthorization('Authorization', `Bearer ${min.instance.webchatKey}`, 'header')
new Swagger.ApiKeyAfuthorization('Authorization', `Bearer ${min.instance.webchatKey}`, 'header')
);
const response = await client.Conversations.Conversations_StartConversation();
const conversationId = response.obj.conversationId;

View file

@ -39,6 +39,7 @@
import { GBServer } from '../../../src/app.js';
import { BotAdapter } from 'botbuilder';
import { WaterfallDialog } from 'botbuilder-dialogs';
import { ChatGPTAPIBrowser } from 'chatgpt';
import { GBLog, GBMinInstance, IGBDialog, IGBPackage } from 'botlib';
import { Messages } from '../strings.js';
import { KBService } from './../services/KBService.js';
@ -69,7 +70,7 @@ export class AskDialog extends IGBDialog {
* @param bot The bot adapter.
* @param min The minimal bot instance data.
*/
public static setup (bot: BotAdapter, min: GBMinInstance) {
public static setup(bot: BotAdapter, min: GBMinInstance) {
const service = new KBService(min.core.sequelize);
const importer = new GBImporter(min.core);
this.deployer = new GBDeployer(min.core, importer);
@ -79,7 +80,7 @@ export class AskDialog extends IGBDialog {
min.dialogs.add(new WaterfallDialog('/ask', AskDialog.getAskDialog(min)));
}
private static getAskDialog (min: GBMinInstance) {
private static getAskDialog(min: GBMinInstance) {
return [
async step => {
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
@ -157,7 +158,7 @@ export class AskDialog extends IGBDialog {
];
}
private static getAnswerDialog (min: GBMinInstance, service: KBService) {
private static getAnswerDialog(min: GBMinInstance, service: KBService) {
return [
async step => {
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
@ -291,6 +292,25 @@ export class AskDialog extends IGBDialog {
});
return await step.replaceDialog('/ask', { isReturning: true });
}
if (process.env.OPENAI_EMAIL) {
if (!GBServer.globals.chatGPT) {
GBServer.globals.chatGPT = new ChatGPTAPIBrowser({
email: process.env.OPENAI_EMAIL,
password: process.env.OPENAI_PASSWORD
});
await GBServer.globals.chatGPT.init();
}
GBLog.info(`ChatGPT being used...`);
const response = await GBServer.globals.chatGPT.sendMessage(text);
if (!response) {
GBLog.info(`SEARCH called but NO answer could be found (zero results).`);
} else {
await min.conversationalService.sendText(min, step, response);
}
return await step.replaceDialog('/ask', { isReturning: true });
}
// Not found.
@ -302,7 +322,7 @@ export class AskDialog extends IGBDialog {
];
}
private static async handleAnswer (service: KBService, min: GBMinInstance, step: any, answer: GuaribasAnswer) {
private static async handleAnswer(service: KBService, min: GBMinInstance, step: any, answer: GuaribasAnswer) {
const text = answer.content;
if (text.endsWith('.docx')) {
const mainName = GBVMService.getMethodNameFromVBSFilename(text);
@ -313,11 +333,11 @@ export class AskDialog extends IGBDialog {
}
}
private static getChannel (step): string {
private static getChannel(step): string {
return !isNaN(step.context.activity['mobile']) ? 'whatsapp' : step.context.activity.channelId;
}
private static getAnswerEventDialog (service: KBService, min: GBMinInstance) {
private static getAnswerEventDialog(service: KBService, min: GBMinInstance) {
return [
async step => {
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {

View file

@ -122,34 +122,34 @@ export class GuaribasQuestion extends Model<GuaribasQuestion> {
@PrimaryKey
@AutoIncrement
@Column(DataType.INTEGER)
questionId: number;
declare questionId: number;
@Column(DataType.STRING(64))
subject1: string;
declare subject1: string;
@Column(DataType.STRING(64))
subject2: string;
declare subject2: string;
@Column(DataType.STRING(64))
subject3: string;
declare subject3: string;
@Column(DataType.STRING(64))
subject4: string;
declare subject4: string;
@Column(DataType.STRING(1024))
keywords: string;
declare keywords: string;
@Column(DataType.BOOLEAN)
skipIndex: boolean;
declare skipIndex: boolean;
@Column(DataType.STRING(512))
from: string;
declare from: string;
@Column(DataType.STRING(512))
to: string;
declare to: string;
@Column(DataType.TEXT)
content: string;
declare content: string;
@Column(DataType.DATE)
@CreatedAt
@ -162,18 +162,18 @@ export class GuaribasQuestion extends Model<GuaribasQuestion> {
//tslint:disable-next-line:no-use-before-declare
@ForeignKey(() => GuaribasAnswer)
@Column(DataType.INTEGER)
answerId: number;
declare answerId: number;
@BelongsTo(() => GuaribasInstance)
instance: GuaribasInstance;
declare instance: GuaribasInstance;
@ForeignKey(() => GuaribasInstance)
@Column(DataType.INTEGER)
instanceId: number;
declare instanceId: number;
@ForeignKey(() => GuaribasPackage)
@Column(DataType.INTEGER)
packageId: number;
declare packageId: number;
@BelongsTo(() => GuaribasPackage)
package: GuaribasPackage;

View file

@ -291,17 +291,17 @@ export class KBService implements IGBKBService {
content: string;
subject1: string;
subject2: string;
subject: string;
subject3: string;
subject4: string;
}
const client = new SearchClient<SearchResults>(instance.searchHost.split('.')[0], 'azuresql-index', {
const client = new SearchClient<SearchResults>('https://' + instance.searchHost, 'azuresql-index', {
key: instance.searchKey
} as any);
const results = await client.search(query, {
filter: `instanceId eq ${instance.instanceId} and skipIndex eq false`,
searchFields: ['content', 'subject1', 'subject2', 'subject', 'subject4'],
searchFields: ['content', 'subject1', 'subject2', 'subject3', 'subject4'],
select: ['instanceId', 'questionId', 'answerId'],
skip: 0,
top: 1
@ -341,10 +341,6 @@ export class KBService implements IGBKBService {
}
}
if (!found) {
GBLog.info(`SEARCH called but NO answer could be found (zero results).`);
}
return { answer: undefined, questionId: 0 };
}
}

View file

@ -53,4 +53,5 @@ export class RootData {
public entryPointDialog: string; // To replace default welcome dialog.
public debugConversationId: any; // Used to self-message during debug.
public debuggers: any[]; // Client of attached Debugger instances by botId.
public chatGPT: any; // ChatGPT API handle (shared Browser).
}