fix (templates): llm-server almost OK.

This commit is contained in:
Rodrigo Rodriguez 2024-09-14 21:26:54 -03:00
parent bf9886e763
commit 93ff7a418e
5 changed files with 77 additions and 92 deletions

View file

@ -1311,15 +1311,16 @@ export class DialogKeywords {
public async messageBot({ pid, text }) { public async messageBot({ pid, text }) {
const { min, user } = await DialogKeywords.getProcessInfo(pid); const { min, user } = await DialogKeywords.getProcessInfo(pid);
GBLogEx.info(min, `MESSAGE BOT: ${text}.`);
const { conversation, client } = min['apiConversations'][pid]; const { conversation, client } = min['apiConversations'][pid];
GBLogEx.info(min, `API messaged bot (Conversation Id: ${conversation.conversationId}): ${text} .`);
await client.apis.Conversations.Conversations_PostActivity({ await client.apis.Conversations.Conversations_PostActivity({
conversationId: conversation.conversationId, conversationId: conversation.conversationId,
activity: { activity: {
textFormat: 'plain', textFormat: 'plain',
text: text, text: text,
type: 'message', type: 'message',
pid: pid,
from: { from: {
id: user.userSystemId, id: user.userSystemId,
name: user.userName name: user.userName
@ -1327,8 +1328,9 @@ export class DialogKeywords {
} }
}); });
min['conversationWelcomed'][conversation.conversationId] = true;
let messages = []; let messages = [];
GBLogEx.info(min, `MessageBot: Starting message polling ${conversation.conversationId}).`); GBLogEx.info(min, `Start API message pooling: ${conversation.conversationId})...`);
let count = POOLING_COUNT; let count = POOLING_COUNT;
while (count--) { while (count--) {
@ -1353,6 +1355,7 @@ export class DialogKeywords {
} }
} }
} catch (err) { } catch (err) {
count = 0;
GBLog.error(`API Message Pooling error: ${GBUtil.toYAML(err)}`); GBLog.error(`API Message Pooling error: ${GBUtil.toYAML(err)}`);
} }
} }

View file

@ -210,13 +210,17 @@ export class GBMinService {
GBServer.globals.debugConversationId = conversationId; GBServer.globals.debugConversationId = conversationId;
const steps = process.env.TEST_MESSAGE.split(';'); const steps = process.env.TEST_MESSAGE.split(';');
const sec = new SecService();
const user = await sec.ensureUser(min, 'testuser', 'testuser', '', 'test', 'testuser', null);
const pid = GBVMService.createProcessInfo(user, min, 'api', null);
await CollectionUtil.asyncForEach(steps, async step => { await CollectionUtil.asyncForEach(steps, async step => {
client.apis.Conversations.Conversations_PostActivity({ client.apis.Conversations.Conversations_PostActivity({
conversationId: conversationId, conversationId: conversationId,
activity: { activity: {
textFormat: 'plain', textFormat: 'plain',
text: step, text: step,
pid: pid,
type: 'message', type: 'message',
from: { from: {
id: 'test', id: 'test',
@ -1197,10 +1201,7 @@ export class GBMinService {
await this.processEventActivity(min, user, context, step); await this.processEventActivity(min, user, context, step);
} }
} catch (error) { } catch (error) {
const msg = `ERROR: ${error.message} ${error.stack} ${error.error ? error.error.body : ''} ${ GBLog.error(`Receiver: ${GBUtil.toYAML(error)}`);
error.error ? (error.error.stack ? error.error.stack : '') : ''
}`;
GBLog.error(msg);
await min.conversationalService.sendText( await min.conversationalService.sendText(
min, min,
@ -1682,7 +1683,7 @@ export class GBMinService {
const user = await sec.ensureUser( const user = await sec.ensureUser(
min, min,
data.userSystemId, data.userSystemId,
data.userName ? data.userName : 'apiuser', data.userName ? data.userName : 'apiuser',
'', '',
'api', 'api',
data.userSystemId, data.userSystemId,
@ -1696,9 +1697,10 @@ export class GBMinService {
const client = await GBUtil.getDirectLineClient(min); const client = await GBUtil.getDirectLineClient(min);
const response = await client.apis.Conversations.Conversations_StartConversation({ const response = await client.apis.Conversations.Conversations_StartConversation({
userSystemId: user.userSystemId, userSystemId: user.userSystemId,
userName: user.userName userName: user.userName,
pid: pid
}); });
min['apiConversations'][pid] = { conversation: response.obj, client: client }; min['apiConversations'][pid] = { conversation: response.obj, client: client };
min['conversationWelcomed'][response.obj.id] = true; min['conversationWelcomed'][response.obj.id] = true;
} }

View file

@ -49,7 +49,7 @@ export const getRouter = (
const activity = createConversationUpdateActivity const activity = createConversationUpdateActivity
(serviceUrl, conversationId, req.query?.userSystemId, (serviceUrl, conversationId, req.query?.userSystemId,
req.query?.userName req.query?.userName, req.query.pid
); );
fetch(botUrl, { fetch(botUrl, {
method: 'POST', method: 'POST',
@ -110,10 +110,12 @@ export const getRouter = (
}); });
// Sends message to bot. Assumes message activities // Sends message to bot. Assumes message activities
router.post(`/directline/${botId}/conversations/:conversationId/activities`, (req, res) => {
router.post(`/api/messages/${botId}/v3/directline/conversations/:conversationId/activities`, (req, res) => {
const incomingActivity = req.body; const incomingActivity = req.body;
// Make copy of activity. Add required fields // Make copy of activity. Add required fields
const activity = createMessageActivity(incomingActivity, serviceUrl, req.params.conversationId); const activity = createMessageActivity(incomingActivity, serviceUrl, req.params.conversationId,
req.params['pid']);
const conversation = getConversation(req.params.conversationId, conversationInitRequired); const conversation = getConversation(req.params.conversationId, conversationInitRequired);
@ -147,20 +149,20 @@ export const getRouter = (
console.warn('/v3/conversations not implemented'); console.warn('/v3/conversations not implemented');
}); });
router.post(`/api/messages/${botId}/v3/directline/conversations/:conversationId/activities`, (req, res) => { // TODO: Check duplicate. router.post(`/api/messages/${botId}/v3/directline/conversations/:conversationId/activities`, (req, res) => {
let activity: IActivity; // let activity: IActivity;
activity = req.body; // activity = req.body;
const conversation = getConversation(req.params.conversationId, conversationInitRequired); // const conversation = getConversation(req.params.conversationId, conversationInitRequired);
if (conversation) { // if (conversation) {
conversation.history.push(activity); // conversation.history.push(activity);
res.status(200).send(); // res.status(200).send();
} else { // } else {
// Conversation was never initialized // // Conversation was never initialized
res.status(400).send(); // res.status(400).send();
} // }
}); // });
router.post(`/api/messages/${botId}/v3/conversations/:conversationId/activities/:activityId`, (req, res) => { router.post(`/api/messages/${botId}/v3/conversations/:conversationId/activities/:activityId`, (req, res) => {
let activity: IActivity; let activity: IActivity;
@ -320,21 +322,22 @@ const deleteStateForUser = (req: express.Request, res: express.Response) => {
const createMessageActivity = ( const createMessageActivity = (
incomingActivity: IMessageActivity, incomingActivity: IMessageActivity,
serviceUrl: string, serviceUrl: string,
conversationId: string conversationId: string, pid
): IMessageActivity => { ): IMessageActivity => {
return { const obj = {
...incomingActivity, ...incomingActivity,
channelId: 'emulator', channelId: 'api',
serviceUrl, serviceUrl,
conversation: { id: conversationId }, conversation: { id: conversationId },
id: uuidv4.v4() id: uuidv4.v4()
}; };
return obj;
}; };
const createConversationUpdateActivity = (serviceUrl: string, conversationId: string, userSystemId, userName): IConversationUpdateActivity => { const createConversationUpdateActivity = (serviceUrl: string, conversationId: string, userSystemId, userName, pid): IConversationUpdateActivity => {
const activity: IConversationUpdateActivity = { const activity: IConversationUpdateActivity = {
type: 'conversationUpdate', type: 'conversationUpdate',
channelId: 'emulator', channelId: 'api',
serviceUrl, serviceUrl,
conversation: { id: conversationId }, conversation: { id: conversationId },
id: uuidv4.v4(), id: uuidv4.v4(),
@ -342,6 +345,7 @@ const createConversationUpdateActivity = (serviceUrl: string, conversationId: st
membersRemoved: [], membersRemoved: [],
from: { id: userSystemId, name: userName } from: { id: userSystemId, name: userName }
}; };
activity['pid'] = pid;
return activity; return activity;
}; };

View file

@ -109,9 +109,7 @@ export class AskDialog extends IGBDialog {
const askForMore = min.core.getParam(min.instance, 'Ask For More', null); const askForMore = min.core.getParam(min.instance, 'Ask For More', null);
if (askForMore) { if (askForMore) {
text = askForMore; text = askForMore;
} } else {
else {
return await step.endDialog(null); return await step.endDialog(null);
} }
} else if (step.context.activity.group || (step.options && step.options.emptyPrompt)) { } else if (step.context.activity.group || (step.options && step.options.emptyPrompt)) {
@ -131,15 +129,7 @@ export class AskDialog extends IGBDialog {
let sec = new SecService(); let sec = new SecService();
const member = step.context.activity.from; const member = step.context.activity.from;
const user = await sec.ensureUser( const user = await sec.ensureUser(min, member.id, member.name, '', 'web', member.name, null);
min,
member.id,
member.name,
'',
'web',
member.name,
null
);
let handled = false; let handled = false;
let nextDialog = null; let nextDialog = null;
@ -210,7 +200,7 @@ export class AskDialog extends IGBDialog {
const locale = step.context.activity.locale; const locale = step.context.activity.locale;
// Stops any content on projector. // Stops any content on projector.
if (step.context.activity.channelId !== 'msteams') { if (step.context.activity.channelId === 'web') {
await min.conversationalService.sendEvent(min, step, 'stop', undefined); await min.conversationalService.sendEvent(min, step, 'stop', undefined);
} }
// Handle extra text from FAQ. // Handle extra text from FAQ.
@ -236,14 +226,21 @@ export class AskDialog extends IGBDialog {
return; return;
} }
const results: any = 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) {
let urls = []; let urls = [];
if (results.sources) { if (results.sources) {
for (const key in results.sources) { for (const key in results.sources) {
const source = results.sources[key]; const source = results.sources[key];
const packagePath = GBUtil.getGBAIPath(min.botId, `gbkb`); const packagePath = GBUtil.getGBAIPath(min.botId, `gbkb`);
@ -253,8 +250,7 @@ export class AskDialog extends IGBDialog {
} }
if (urls.length > 0) { if (urls.length > 0) {
await min.conversationalService.sendEvent( await min.conversationalService.sendEvent(min, step, 'play', {
min, step, 'play', {
playerType: 'multiurl', playerType: 'multiurl',
data: urls data: urls
}); });
@ -268,8 +264,6 @@ export class AskDialog extends IGBDialog {
return await AskDialog.handleAnswer(service, min, step, user, answer); return await AskDialog.handleAnswer(service, min, step, user, answer);
} }
GBLogEx.info(min, `SEARCH called but NO answer could be found (zero results).`); GBLogEx.info(min, `SEARCH called but NO answer could be found (zero results).`);
// Not found. // Not found.
@ -284,7 +278,7 @@ export class AskDialog extends IGBDialog {
} }
private static async handleAnswer(service: KBService, min: GBMinInstance, step: any, user, answer: GuaribasAnswer) { private static async handleAnswer(service: KBService, min: GBMinInstance, step: any, user, answer: GuaribasAnswer) {
let text = typeof (answer) === 'string' ? answer : answer.content; let text = typeof answer === 'string' ? answer : answer.content;
text = text.trim(); text = text.trim();
if (text.endsWith('.docx')) { if (text.endsWith('.docx')) {
const mainName = GBVMService.getMethodNameFromVBSFilename(text); const mainName = GBVMService.getMethodNameFromVBSFilename(text);
@ -381,15 +375,7 @@ export class AskDialog extends IGBDialog {
let sec = new SecService(); let sec = new SecService();
const member = step.context.activity.from; const member = step.context.activity.from;
const user = await sec.ensureUser( const user = await sec.ensureUser(min, member.id, member.name, '', 'web', member.name, null);
min,
member.id,
member.name,
'',
'web',
member.name,
null
);
await step.endDialog(); await step.endDialog();
const pid = step.context.activity['pid']; const pid = step.context.activity['pid'];

View file

@ -14,38 +14,28 @@ For each order placed, return a JSON containing the product name, the table, and
Keep orderedItems with only one item and keep sideItems only with the sides that were specified. Keep orderedItems with only one item and keep sideItems only with the sides that were specified.
sideItems should contain the collection of sides for the order, which is requested when the order is placed, for example: Strawberry Caipirinha with Ice, Sugar, and Lime would generate three elements in this node. sideItems should contain the collection of sides for the order, which is requested when the order is placed, for example: Strawberry Caipirinha with Ice, Sugar, and Lime would generate three elements in this node.
Here is an example of the Order JSON, clear the items and send one with the order made by the person, this is just an example: Here is an example of the Order YAML, clear the items and send one with the order made by the person, this is just an example:
{
orderedItems: [ orderedItems:
{ - item:
item: { id: 102
id: 102, price: 0.30
price: 0.30, name: Banana
name: Banana sideItems:
}, - id: 0
sideItems: [ price: 0
{ quantity: 1
id: 0, quantity: 1
price: 0, notes: a
quantity: 1 - item:
} id: 103
], price: 0.30
quantity: 1, name: Carrot
notes: a sideItems: []
}, quantity: 1
{ notes: none
item: { userId: ${operator}
id: 103, accountIdentifier: Areia
price: 0.30,
name: Carrot
},
sideItems: [],
quantity: 1,
notes: none
}
],
userId: ${operator},
accountIdentifier: Areia,
deliveryTypeId: 2 deliveryTypeId: 2
}
END SYSTEM PROMPT END SYSTEM PROMPT