Example AI command that utilizes several prompts
const lib = require('lib')({ token: process.env.STDLIB_SECRET_TOKEN });
const event = context.params.event;
let {
channel_id, content, mentions, id, author: { username }
} = event;
const {
discord: { channels: { typing, messages: { create } } },
keyvalue: { store: { set, get } }
} = lib;
await typing.create({ channel_id });
// Remove bot mention
content = content.replace(/^<@(\d+)>\s/, '');
// Remove prompt invocation (example: 'console:')
const code = content.replace(/(.*):\s|(```\w*\s*)/mgi, ''); // \``
// Replace user mentions with user's display name
const message = content.replace(/^<@(\d+)>\s/, '').replace(/<@(\d+)>/gi, (_, id) => {
return (mention = mentions?.find((m) => m?.id === id)) ? `${mention.member.nick ?? mention.username}` : '';
});
// Create map with different prompts
const prompts = new Map([
[
'console', [
'Please act as a JavaScript console and process the following commands.',
'Your response should be only the terminal output.',
`Command: ${code}`,
'Response:'
].join('\n')
],
[
'code', [
'Provide a refactored version of this JavaScript code to make it more efficient and easier to read, while preserving its functionality:',
`${code}.`,
'Response:'
].join('\n')
],
[
'generate code', [
'Create a JavaScript command based on the following description:',
`${code}`,
'Response:'
].join('\n')
],
[
'eli5', [
`Explain this concept to me as if I'm a 5-year-old: ${code}`,
'Response:'
].join('\n')
],
[
'tldr', [
`Please summarize the following text into a concise and clear summary that captures its main ideas and themes, while being as brief as possible: ${code}`,
'Response:'
].join('\n')
]
]);
// Check message content for specific prompt invocation
const testPrompt = (message?.split(':')?.[0].replace(/\d*/i, '')).toLowerCase();
const key = `::${username}::`;
const length = 5;
const value = await get({ key, defaultValue: [] });
const time = new Intl.DateTimeFormat('en-US', {
dateStyle: 'full',
timeStyle: 'long'
}).format(new Date(event.timestamp));
let conversation = [
`${time} ${username} said: ${message}`, 'Your response:'
].join('\n');
// Checks prompts map for prompt invocation and sets default prompt if necessary
prompt = prompts.get(testPrompt) ?? [
`Have a conversation with ${username} and act as if you have a unique personality and character.`,
'I want you to be lively, curious, and engaging.',
'Feel free to bring up any topics that interest you.',
value.join('\n'),
conversation
].join('\n');
// Placeholder variable to check when setting competions fields
const stuff = !['eli5', 'tldr'].some((word) => testPrompt.indexOf(word) !== -1) && prompts.has(testPrompt);
try {
let response = await lib.openai.playground.completions.create({
model: 'text-davinci-003',
prompt: [prompt],
max_tokens: stuff ? 256 : 512,
temperature: stuff ? 0 : 0.7,
top_p: 0.9,
n: 1,
echo: false,
presence_penalty: stuff ? 0 : 0.6,
frequency_penalty: 0,
best_of: 1
});
stuff ? response = `\`\`\`js\n${response.choices[0].text}\`\`\`` : response = response.choices[0].text;
conversation = conversation + response;
value.push(conversation);
while (value.length >= length) value.shift();
return Promise.all([
create({ channel_id, content: [response].join('\n'),
message_reference: { message_id: id, fail_if_not_exists: false }
}), set({ key, value, ttl: 1000 })
]);
} catch (e) {
console.log(e);
await create({ channel_id, content: `${e}` });
}