Skip to content

Commit

Permalink
add aliases; add sentiment analysis; add alpaca UC; misc. fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
edfletcher committed Apr 15, 2023
1 parent 07eeb75 commit 4b376ab
Show file tree
Hide file tree
Showing 22 changed files with 2,511 additions and 317 deletions.
23 changes: 21 additions & 2 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,27 @@ const _config = {
openai: {
secretKey: null,
model: 'text-davinci-003',
temperature: 0.7,
maxTokens: 4000
chatModel: 'gpt-3.5-turbo',
temperature: 0.9,
maxTokens: 3700,
viaHTML: 'the <a href="https://beta.openai.com/docs/guides/completion" target="_blank">OpenAI text completion API</a>'
},

alpaca: {
/*
hosts: {
// will result in queries to:
// https://alpaca.example.com/prompt and https://alpaca-ht.example.com/prompt
"example.com": {
models: ["alpaca", "alpaca-ht"],
scheme: "https"
}
}
*/
hosts: {},
waitTimeSeconds: 5,
viaHTML: '<a href="https://github.com/edfletcher/alpaca.http" target="_blank">alpaca.http</a> running the <a href="https://huggingface.co/Sosaka/Alpaca-native-4bit-ggml/blob/main/ggml-alpaca-7b-q4.bin" target="_blank">7 billion 4-bit weights Alpaca model</a>',
camelidaeFrontendAvailable: false
},

_secretKeys: SECRET_KEYS,
Expand Down
30 changes: 23 additions & 7 deletions discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const { plotMpmData } = require('./discord/plotting');
const eventHandlers = require('./discord/events');
const registerContextMenus = require('./discord/contextMenus');
const parsers = require('./lib/parsers');
const { loadAliases, tryResolvingAlias } = require('./discord/lib/ucAliases');
const UCHistory = require('./discord/userCommandHistory');
const {
PREFIX,
Expand Down Expand Up @@ -235,6 +236,9 @@ const formatForAllowedSpeakerResponse = (s, raw = false) =>

let clientOnSigint = () => {};
client.once('ready', async () => {
const commandAliases = await loadAliases();
console.log(`Loaded ${Object.keys(commandAliases).length} user command aliases`);

console.log('Ready!');

client.user.setStatus('idle');
Expand Down Expand Up @@ -364,7 +368,7 @@ client.once('ready', async () => {

sendToBotChan({
embeds: [
new MessageEmbed().setDescription('\n```\n' + (await banner('Hello!')) + '\n```\n\n')
new MessageEmbed().setDescription('\n```\n' + (await banner('Hi!')) + '\n```\n\n')
]
}, true);

Expand Down Expand Up @@ -392,10 +396,17 @@ client.once('ready', async () => {
return pipedHandler();
}

let { command, args } = parsers.parseCommandAndArgs(trimContent, { autoPrefixCurrentCommandChar });
console.log(trimContent, '-> user command parsed ->', { command, args });
args = parsers.parseArgsForQuotes(args);
console.debug('args parsed for quotes', args);
const aliasedTCList = trimContent.slice(autoPrefixCurrentCommandChar ? 0 : 1).split(/\s+/);
const aliased = tryResolvingAlias(aliasedTCList[0]);
console.warn('USE', aliased ? [aliased, ...aliasedTCList.slice(1)].join(' ') : trimContent);
let { command, args } = parsers.parseCommandAndArgs(
aliased ? [aliased, ...aliasedTCList.slice(1)].join(' ') : trimContent,
{
// must auto-prefix if aliased
autoPrefixCurrentCommandChar: !!(aliased ?? autoPrefixCurrentCommandChar)
}
);
console.log(trimContent, '-> user command parsed ->', { command, args, aliased });

const fmtedCmdStr = '`' + `${command} ${args.join(' ')}` + '`';
const redis = new Redis(config.redis.url);
Expand All @@ -406,6 +417,9 @@ client.once('ready', async () => {
const cmdFunc = userCommands(command);
resolvedName = cmdFunc?.__resolvedFullCommandName ?? command;

args = parsers.parseArgsForQuotes(args);
console.debug('args parsed for quotes', args);

// this should be removed ASAP in favor of scopedRedisClient,
// but need to find all uses of it first...
const publish = async (publishObj) => redis.publish(PREFIX, JSON.stringify(publishObj));
Expand Down Expand Up @@ -449,7 +463,7 @@ client.once('ready', async () => {
}

if (!cmdFunc) {
localSender(`\`${command}\` is not a valid DRC user command. Run \`!help\` to see all available commands.`);
localSender(`\`${command}\` is not a valid DRC user command. Run \`${config.app.allowedSpeakersCommandPrefixCharacter}help\` to see all available commands.`);
throw new UserCommandNotFound();
}

Expand Down Expand Up @@ -481,7 +495,9 @@ client.once('ready', async () => {
}

const result = await cmdFunc(context, ...args);
console.log(`Executed user command "${command}" (${resolvedName}) with result -->\n`, result);
console.log(`Executed user command "${command}" (${resolvedName}) ` +
(aliased ? `from alias "${aliasedTCList[0]}"` : '') +
' with result -->\n', result);

let toBotChan;
if (result && result.__drcFormatter) {
Expand Down
11 changes: 10 additions & 1 deletion discord/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { MessageMentions: { CHANNELS_PATTERN } } = require('discord.js');
const senderNickFromMessage = require('./lib/senderNickFromMessage');
const { serveMessages } = require('./lib/serveMessages');
const { isHTTPRunning } = require('../lib/isXRunning');
const { attachSentimentToMessages, averageSentiments, transformAveragesForDigestHTTP, roundSentimentScoreOnMessages } = require('../lib/sentiments');

function createArgObjOnContext (context, data, subaction, noNetworkFirstArg = false) {
const tmplArr = [senderNickFromMessage(data?.message)];
Expand Down Expand Up @@ -37,7 +38,15 @@ async function clearSquelched (context, ...a) {
async function digest (context, ...a) {
const data = (await context.redis.lrange([context.key, 'squelch'].join(':'), 0, -1)).map(JSON.parse).reverse();

await serveMessages(context, data, { ttl: 1440 });
const sentiments = await attachSentimentToMessages(null, data.map(({ data }) => data), null, context.options);
const sentimentsAvg = averageSentiments(sentiments);
roundSentimentScoreOnMessages(data.map(({ data }) => data));
await serveMessages(context, data, {
ttl: 1440,
extra: {
sentiments: transformAveragesForDigestHTTP(sentimentsAvg)
}
});

if (!context.options.keep) {
clearSquelched(context);
Expand Down
8 changes: 8 additions & 0 deletions discord/events/messageReactionAdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ async function isUserHere (context, data) {
return userCommands('isUserHere')(context, ...createArgObjOnContext(context, data, null, true));
}

async function gptQuestion (context, data) {
console.log('CREATED', createArgObjOnContext(context, data, 'gpt'));
console.log('CONTENT?', data?.message?.content);
// return userCommands('gpt')(context, ...createArgObjOnContext(context, data, 'gpt'));
}

const allowedReactions = {
'%F0%9F%87%AC': gptQuestion, // "🇬"

'%F0%9F%87%BC': whois, // "🇼"
'%E2%9D%94': whois, // "❔"
'%E2%9D%93': whois, // "❓"
Expand Down
116 changes: 79 additions & 37 deletions discord/lib/listMgmtUCGenerators.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,52 @@ const {
simpleEscapeForDiscord
} = require('./strings');

function attachHelp (cmdFunc, commandName, additionalCommands, { overrideHelpFields = {} } = {}) {
const addlCommandsHelp = Object.entries(additionalCommands ?? {})
.filter(([k]) => k.indexOf('_') !== 0)
.reduce((a, [k, v]) => ({
[k]: {
text: k
},
...a
}), {});

const defaultHelp = {
title: `Add or remove strings to the \`${commandName}\` list.`,
usage: 'network subcommand [string]',
subcommands: {
add: {
header: 'Notes',
text: `Adds \`string\` to the \`${commandName}\` list.`
},
remove: {
header: 'Notes',
text: `Removes \`string\` from the \`${commandName}\` list.`
},
clear: {
header: 'Notes',
text: `Removes all strings from the \`${commandName}\` list.`
},
...addlCommandsHelp
}
};

cmdFunc.__drcHelp = () => {
return {
...defaultHelp,
...overrideHelpFields
};
};
}

// keySubstitue only applies (if set) to additionalCommands!
function generateListManagementUCExport (commandName, additionalCommands, disallowClear = false, keySubstitute = null) {
function generateListManagementUCExport (commandName,
additionalCommands,
disallowClear = false,
keySubstitute = null,
callOnChange = null,
overrideHelpFields = {}
) {
const f = async function (context, ...a) {
const [netStub, cmd] = a;

Expand All @@ -35,10 +79,21 @@ function generateListManagementUCExport (commandName, additionalCommands, disall
return convertDiscordChannelsToIRCInString(a.slice(2).join(' '), context);
};

const addRmArgs = async () => [key, await argStr()];

let onChangeWrapper = async (a) => a;
if (callOnChange) {
onChangeWrapper = async (changed) => {
if (changed) {
await callOnChange(context, ...await addRmArgs());
}
};
}

return scopedRedisClient(async (redis) => {
switch (cmd) {
case 'add':
await redis.sadd(key, await argStr());
await onChangeWrapper(await redis.sadd(...await addRmArgs()));
break;
case 'clear':
// this really should be a button for confirmation instead of hardcoded!
Expand All @@ -47,7 +102,7 @@ function generateListManagementUCExport (commandName, additionalCommands, disall
}
break;
case 'remove':
await redis.srem(key, await argStr());
await onChangeWrapper(await redis.srem(...await addRmArgs()));
break;
}

Expand All @@ -68,42 +123,22 @@ function generateListManagementUCExport (commandName, additionalCommands, disall
});
};

const addlCommandsHelp = Object.entries(additionalCommands ?? {})
.filter(([k]) => k.indexOf('_') !== 0)
.reduce((a, [k, v]) => ({
[k]: {
text: k
},
...a
}), {});

f.__drcHelp = () => {
return {
title: `Add or remove strings to the \`${commandName}\` list.`,
usage: 'network subcommand [string]',
subcommands: {
add: {
header: 'Notes',
text: `Adds \`string\` to the \`${commandName}\` list.`
},
remove: {
header: 'Notes',
text: `Removes \`string\` from the \`${commandName}\` list.`
},
clear: {
header: 'Notes',
text: `Removes all strings from the \`${commandName}\` list.`
},
...addlCommandsHelp
}
};
};

attachHelp(f, commandName, additionalCommands, { overrideHelpFields });
return f;
}

function generatePerChanListManagementUCExport (commandName, additionalCommands, enforceChannelSpec = true) {
return function (context, ...a) {
function generateListManagementUCExportOpts (commandName, {
additionalCommands,
disallowClear = false,
keySubstitute = null,
callOnChange = null,
overrideHelpFields = {}
} = {}) {
return generateListManagementUCExport(commandName, additionalCommands, disallowClear, keySubstitute, callOnChange, overrideHelpFields);
}

function generatePerChanListManagementUCExport (commandName, additionalCommands, enforceChannelSpec = true, options = {}) {
const cmdFunc = function (context, ...a) {
const [netStub, channelIdSpec] = context.options._;
const { network } = matchNetwork(netStub);
let channel = channelIdSpec;
Expand All @@ -122,17 +157,24 @@ function generatePerChanListManagementUCExport (commandName, additionalCommands,
return addlCmd({ key, network, ...context }, ...a);
}

const cmdFunctor = generateListManagementUCExport(`${commandName}_${key}`, additionalCommands);
const cmdFunctor = generateListManagementUCExportOpts(`${commandName}_${key}`, {
...options,
additionalCommands
});

context.options._[1] = context.options._[0];
a[1] = a[0];
context.options._.shift();
a.shift();
return cmdFunctor(context, ...a);
};

attachHelp(cmdFunc, commandName, additionalCommands, options);
return cmdFunc;
}

module.exports = {
generateListManagementUCExport,
generateListManagementUCExportOpts,
generatePerChanListManagementUCExport
};
8 changes: 2 additions & 6 deletions discord/lib/serveMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,14 @@ async function serveMessages (context, data, opts = {}) {

const name = nanoid();

if (!data.length) {
context.sendToBotChan(`No messages for \`${context.network}\` were found.`);
return;
}

context.registerOneTimeHandler('http:get-req:' + name, name, async () => {
await scopedRedisClient(async (r) => {
await r.publish(PREFIX, JSON.stringify({
type: 'http:get-res:' + name,
data: {
network: context.network,
elements: data
elements: data,
extra: opts.extra ?? {}
}
}));
});
Expand Down
Loading

0 comments on commit 4b376ab

Please sign in to comment.