Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
*/
'use strict';

/** @typedef {import('./src/Processor').ProcessorOptions<Router|BuildRouter>} ProcessorOptions */

const Processor = require('./src/Processor');
const Router = require('./src/Router');
const Request = require('./src/Request');
Expand Down
4 changes: 2 additions & 2 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es2019", "es2020.promise", "es2020.bigint", "es2020.string"],
"lib": ["es2019", "es2020.promise", "es2020.bigint", "es2020.string", "dom"],
"target": "ESNext",
"allowSyntheticDefaultImports": true,
"checkJs": true,
Expand All @@ -14,4 +14,4 @@
"doc",
"docs"
]
}
}
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wingbot",
"version": "3.69.8",
"version": "3.70.0-alpha.2",
"description": "Enterprise Messaging Bot Conversation Engine",
"main": "index.js",
"type": "commonjs",
Expand Down Expand Up @@ -37,6 +37,7 @@
},
"homepage": "https://github.com/wingbot.ai/wingbot#readme",
"devDependencies": {
"@types/mocha": "^10.0.10",
"cpy-cli": "^5.0.0",
"eslint": "^8.56.0",
"eslint-config-airbnb": "^19.0.4",
Expand Down Expand Up @@ -69,4 +70,4 @@
"axios": "^1.6.4",
"handlebars": "^4.0.0"
}
}
}
2 changes: 1 addition & 1 deletion src/LLM.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { PHONE_REGEX, EMAIL_REGEX } = require('./systemEntities/regexps');
/** @typedef {import('./Responder')} Responder */
/** @typedef {import('./Responder').Persona} Persona */
/** @typedef {import('./Router').BaseConfiguration} BaseConfiguration */
/** @typedef {import('./LLMSession').LLMMessage} LLMMessage */
/** @typedef {import('./LLMSession').LLMMessage<any>} LLMMessage */
/** @typedef {import('./LLMSession').LLMRole} LLMRole */
/** @typedef {import('./LLMSession')} LLMSession */
/** @typedef {import('./transcript/transcriptFromHistory').Transcript} Transcript */
Expand Down
27 changes: 17 additions & 10 deletions src/LLMMockProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,27 @@ class LLMMockProvider {
if (prompt.length === 0) {
throw new Error('Empty prompt');
}
const stats = prompt.reduce((o, m) => Object.assign(o, {
[m.role]: (o[m.role] || 0) + 1
}), { system: 0, assistant: 0, user: 0 });

const statsText = JSON.stringify(stats)
.replace(/"/g, '');

const message = this._sequence[this._index];
this._index = (this._index + 1) % this._sequence.length;
// const stats = prompt.reduce((o, m) => Object.assign(o, {
// [m.role]: (o[m.role] || 0) + 1
// }), { system: 0, assistant: 0, user: 0 });
//
// const statsText = JSON.stringify(stats)
// .replace(/"/g, '');
//
/// / const message = this._sequence[this._index];
/// / this._index = (this._index + 1) % this._sequence.length;
//
// return {
// role: LLM.ROLE_ASSISTANT,
// finishReason: 'length',
// eslint-disable-next-line max-len
// content: `${statsText} > ${LLMMockProvider.DEFAULT_MODEL}: ${prompt.map((m) => m.content).join(' ')}`
// };

return {
role: LLM.ROLE_ASSISTANT,
finishReason: 'length',
content: `${statsText} > ${LLMMockProvider.DEFAULT_MODEL}: ${message}`
content: `${options.model || LLMMockProvider.DEFAULT_MODEL}:${prompt.map((m) => m.content).join(' ')}`
};
}

Expand Down
9 changes: 5 additions & 4 deletions src/LLMSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ class LLMSession {
/**
*
* @param {LLM} llm
* @param {LLMMessage[]} [chat]
* @param {LLMMessage<any>[]} [chat]
* @param {SendCallback} [onSend]
*/
constructor (llm, chat = [], onSend = () => {}) {
this._llm = llm;

this._onSend = onSend;

/** @type {LLMMessage[]} */
/** @type {LLMMessage<any>[]} */
this._chat = chat;

this._generatedIndex = null;
Expand All @@ -63,6 +63,7 @@ class LLMSession {
}

_mergeSystem () {
/** @type {LLMMessage<any>[]} */
const sysMessages = [];

const otherMessages = this._chat.filter((message) => {
Expand All @@ -81,7 +82,7 @@ class LLMSession {

const content = sysMessages.reduce((reduced, current, i) => {
if (i === 0) {
return current.content;
return current.content || '';
}
if (!reduced.match(promptRegex)) {
return `${reduced}\n\n${current.content}`;
Expand Down Expand Up @@ -189,7 +190,7 @@ class LLMSession {
/**
*
* @param {LLMProviderOptions} [options={}]
* @returns {Promise<LLMMessage>}
* @returns {Promise<LLMMessage<any>>}
*/
async generate (options = {}) {
const result = await this._llm.generate(this, options);
Expand Down
1 change: 1 addition & 0 deletions src/Responder.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ class Responder {
this._llmContext.set(contextType, []);
}
this._llmContext.get(contextType).push(systemPrompt.trim());

return this;
}

Expand Down
38 changes: 38 additions & 0 deletions src/resolvers/contextMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* @author Vojtech Jedlicka
*/
'use strict';

const Router = require('../Router');
// eslint-disable-next-line no-unused-vars
const Responder = require('../Responder');
const { getLanguageText } = require('./utils');
const compileWithState = require('../utils/compileWithState');

/** @typedef {import('../BuildRouter').BotContext<any>} BotContext */
/** @typedef {import('../Router').Resolver<any>} Resolver */
/** @typedef {import('./utils').Translations} Translations */

/**
*
* @param {object} params
* @param {Translations} params.context
* @param {string} [params.type]
* @param {BotContext} context
* @returns {Resolver}
*/
// eslint-disable-next-line no-unused-vars
function contextMessage (params, context) {

return async (req, res) => {
const translated = getLanguageText(params.context, req.state.lang);
const translatedText = Array.isArray(translated) ? translated[0] : translated;
const statefulPrompt = compileWithState(req, res, translatedText);

res.llmAddSystemPrompt(statefulPrompt, params.type);

return Router.CONTINUE;
};
}

module.exports = contextMessage;
2 changes: 2 additions & 0 deletions src/resolvers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ const subscribtions = require('./subscribtions');
const setState = require('./setState');
const expectedInput = require('./expectedInput');
const skip = require('./skip');
const contextMessage = require('./contextMessage');

module.exports = {
path,
message,
contextMessage,
include,
postback,
expected,
Expand Down
45 changes: 43 additions & 2 deletions src/resolvers/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,23 @@ function selectTranslation (resolverId, params, texts, data) {
];
}

/** @typedef {import('../BuildRouter').BotContext} BotContext */
/** @typedef {import('../Router').Resolver} Resolver */
/** @typedef {import('../BuildRouter').BotContext<any>} BotContext */
/** @typedef {import('../Router').Resolver<any>} Resolver */

/**
*
* @param {object} params
* @param {any} params.text
* @param {boolean} [params.hasCondition]
* @param {string} [params.conditionFn]
* @param {string} [params.conditionDesc]
* @param {boolean} [params.hasEditableCondition]
* @param {any} [params.editableCondition]
* @param {string} [params.mode]
* @param {string} [params.persist]
* @param {any[]} [params.replies]
* @param {"message" | "prompt"} [params.type]
* @param {string} [params.llmContextType]
* @param {BotContext} context
* @returns {Resolver}
*/
Expand All @@ -284,10 +295,40 @@ function message (params, context = {}) {
// @ts-ignore
isLastIndex, isLastMessage, linksMap, configuration, resolverId
} = context;

if (typeof params.text !== 'string' && !Array.isArray(params.text)) {
throw new Error('Message should be a text!');
}

if (params.type === 'prompt') {
return async (req, res) => {
const session = await res.llmSessionWithHistory(params.llmContextType);

const translatedObject = getLanguageText(params.text, req.lang);
const translatedText = Array.isArray(translatedObject)
? translatedObject[0]
: translatedObject;

const response = await session.systemPrompt(translatedText)
.debug()
.generate();

// if (response.finishReason && response.finishReason !== 'length') {
// // error maybe?
// return Router.END;
// }

if (!response.content) {
// no response?
return Router.CONTINUE;
}

res.text(response.content);

return Router.CONTINUE;
};
}

// parse quick replies
let quickReplies;
if (params.replies && !Array.isArray(params.replies)) {
Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function isTextObjectEmpty (text) {
* @param {Translations} translations
* @param {string} [lang]
* @param {boolean} [disableDefaulting] - it will try to find translation for other language
* @returns {null|string}
* @returns {null|string|string[]}
*/
function getLanguageText (translations, lang = null, disableDefaulting = false) {
let foundText;
Expand Down
44 changes: 44 additions & 0 deletions test/llm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
const { strict: assert } = require('assert');
const Router = require('../src/Router');
const Tester = require('../src/Tester');
const message = require('../src/resolvers/message');
const contextMessage = require('../src/resolvers/contextMessage');

describe('<LLM>', () => {

Expand Down Expand Up @@ -57,4 +59,46 @@ describe('<LLM>', () => {
t.debug();
});

it('should work with resolvers', async () => {
const bot = new Router();

bot.use(contextMessage({
context: 'The user is 5 years old.'
}, {}));

bot.use(message({
text: 'Based on the users age explain what nuclear fusion is in 3 sentences.',
type: 'prompt'
}, {}));

const t = new Tester(bot);

await t.text('hello this is user');

t.any()
.contains('The user is 5 years old.')
.contains('Based on the users age explain what nuclear fusion is in 3 sentences.');
});

it('should work with resolvers', async () => {
const bot = new Router();

bot.use(contextMessage({
context: 'The user is 5 years old.'
}, {}));

bot.use(message({
text: 'Based on the users age explain what nuclear fusion is in 3 sentences.',
type: 'prompt'
}, {}));

const t = new Tester(bot);

await t.text('hello this is user');

t.any()
.contains('The user is 5 years old.')
.contains('Based on the users age explain what nuclear fusion is in 3 sentences.');
});

});
1 change: 1 addition & 0 deletions test/resolvers/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('Message voice control', () => {
it('shows translated quick replies', async () => {
const bot = new Router();

// @ts-ignore
bot.use('go', message({
text: [
{ l: 'cs', t: ['Czech text'] },
Expand Down
2 changes: 0 additions & 2 deletions test/transcript/transcriptFromHistory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ describe('transcriptFromHistory', () => {

const transcript = await transcriptFromHistory(t.senderLogger, t.senderId, t.pageId);

// console.log(transcript);

assert.deepStrictEqual(transcript.map((tr) => ({ ...tr, timestamp: null })), [
{
fromBot: false,
Expand Down