new(whatsapp.gblib): Let GPT help call centers...

This commit is contained in:
Rodrigo Rodriguez 2023-08-02 13:58:11 -03:00
parent ef42aed4a3
commit 4c26cdf9db
3 changed files with 29 additions and 8 deletions

View file

@ -100,6 +100,7 @@ import { DebuggerService } from '../../basic.gblib/services/DebuggerService.js';
import { ImageProcessingServices } from '../../basic.gblib/services/ImageProcessingServices.js'; import { ImageProcessingServices } from '../../basic.gblib/services/ImageProcessingServices.js';
import { ScheduleServices } from '../../basic.gblib/services/ScheduleServices.js'; import { ScheduleServices } from '../../basic.gblib/services/ScheduleServices.js';
import mime from 'mime'; import mime from 'mime';
import { ChatServices } from '../../gpt.gblib/services/ChatServices.js';
/** /**
* Minimal service layer for a bot and encapsulation of BOT Framework calls. * Minimal service layer for a bot and encapsulation of BOT Framework calls.
*/ */

View file

@ -32,10 +32,10 @@
'use strict'; 'use strict';
import { GBSSR }from '../../core.gbapp/services/GBSSR.js'; import { GBMinInstance } from 'botlib';
import { Configuration, OpenAIApi } from "openai";
export class ChatServices { export class ChatServices {
/** /**
* Generate text * Generate text
* *
@ -44,8 +44,23 @@ export class ChatServices {
* result = CONTINUE text * result = CONTINUE text
* *
*/ */
public static async continue (text, chatId) { public static async continue(min: GBMinInstance, text: string, chatId) {
let key = min.core.getParam(min.instance, 'Open AI Key', null);
if (!key) {
throw new Error('Open AI Key not configured in .gbot.');
} }
const configuration = new Configuration({
apiKey: key,
});
const openai = new OpenAIApi(configuration);
const chatCompletion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [{role: "user", content: text}],
});
return chatCompletion.data.choices[0].message;
}
} }

View file

@ -48,6 +48,7 @@ import express from 'express';
import { GBSSR } from '../../core.gbapp/services/GBSSR.js'; import { GBSSR } from '../../core.gbapp/services/GBSSR.js';
import pkg from 'whatsapp-web.js'; import pkg from 'whatsapp-web.js';
import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords.js'; import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords.js';
import { ChatServices } from '../../gpt.gblib/services/ChatServices.js';
const { List, Buttons, Client, MessageMedia } = pkg; const { List, Buttons, Client, MessageMedia } = pkg;
/** /**
@ -553,6 +554,10 @@ export class WhatsappDirectLine extends GBService {
} else { } else {
GBLog.info(`USER (${from}) TO AGENT ${agent.userSystemId}: ${text}`); GBLog.info(`USER (${from}) TO AGENT ${agent.userSystemId}: ${text}`);
const prompt = `the person said: ${text}. what can I tell her?`;
const answer = ChatServices.continue(this.min, prompt, 0);
text = `${text} \n\nGeneral Bots: ${answer}`
if (user.agentSystemId.indexOf('@') !== -1) { if (user.agentSystemId.indexOf('@') !== -1) {
// Agent is from Teams or Google Chat. // Agent is from Teams or Google Chat.
await this.min.conversationalService['sendOnConversation'](this.min, agent, text); await this.min.conversationalService['sendOnConversation'](this.min, agent, text);