summarize

This commit is contained in:
derpystuff 2023-12-09 00:31:08 +01:00
parent de395190aa
commit 24fbe2c566
4 changed files with 126 additions and 0 deletions

View file

@ -0,0 +1,59 @@
const { createEmbed } = require('../../../labscore/utils/embed')
const { editOrReply } = require('../../../labscore/utils/message')
const superagent = require('superagent')
const { codeblock, iconPill, smallIconPill } = require('../../../labscore/utils/markdown')
const { Permissions } = require("detritus-client/lib/constants");
const { canUseLimitedTestCommands } = require('../../../labscore/utils/testing')
const { STATICS, STATIC_ICONS } = require('../../../labscore/utils/statics');
const { summarizeWebpage } = require('../../../labscore/api/obelisk');
const URL_REGEX = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/
module.exports = {
name: 'summarize',
label: 'text',
metadata: {
description: `${iconPill("generative_ai", "LIMITED TESTING")}\n${smallIconPill("reply", "Supports Replies")}\n\nSummarize web pages and articles.`,
description_short: 'Website summaries.',
examples: ['summarize'],
category: 'limited',
usage: 'summarize'
},
permissionsClient: [Permissions.EMBED_LINKS, Permissions.SEND_MESSAGES, Permissions.USE_EXTERNAL_EMOJIS, Permissions.READ_MESSAGE_HISTORY],
run: async (context, args) => {
if(!canUseLimitedTestCommands(context)) return;
context.triggerTyping();
let content = args.text;
if(context.message.messageReference) {
let msg = await context.message.channel.fetchMessage(context.message.messageReference.messageId);
if(msg.content && msg.content.length) content = msg.content
else if(msg.embeds?.length) for(const e of msg.embeds) if(e[1].description?.length) { content = e[1].description; break; }
}
let webUrl = content.match(URL_REGEX)
if(!webUrl) return editOrReply(context, createEmbed("warning", context, `No URLs found.`))
try{
await editOrReply(context, createEmbed("ai_custom", "Generating page summary...", STATIC_ICONS.ai_summary))
let res = await summarizeWebpage(context, webUrl[0])
return editOrReply(context, createEmbed("defaultNoFooter", context, {
author: {
iconUrl: STATIC_ICONS.ai_summary,
name: res.response.body.title || ''
},
description: '- ' + res.response.body.summaries.join('\n- '),
footer: {
text: "Generative AI is experimental. Response may be factually incorrect or biased."
}
}))
}catch(e){
console.log(e)
return editOrReply(context, createEmbed("error", context, e.response.body.message))
}
}
};

View file

@ -0,0 +1,15 @@
const ObeliskHosts = Object.freeze({
prod: process.env.OBELISK_HOST,
local: "http://localhost"
})
const ObeliskApi = Object.freeze({
HOST: ObeliskHosts.prod,
SUMMARIZE_WEBPAGES: "/flamingo/v1/web:summarize"
})
module.exports = {
ObeliskApi,
ObeliskHosts
}

View file

@ -0,0 +1,47 @@
const superagent = require('superagent')
const { ObeliskApi, ObeliskHosts } = require('./endpoints')
async function request(path, type, headers, args, host) {
let timing = Date.now();
url = ObeliskApi.HOST + path
if(process.env.USE_LOCAL_API) url = ObeliskHosts.local + ":" + process.env.USE_LOCAL_API + path
if(host) url = host + path
// apply default headers
if(!headers["Authorization"]) headers["Authorization"] = process.env.OBELISK_API_KEY
if(!headers["x-obelisk-client"]) headers["x-obelisk-client"] = process.env.OBELISK_CLIENT_ID || "labscore-production-001"
if (type === "GET") {
if(!args){
const response = await superagent.get(url);
return {
timings: ((Date.now() - timing) / 1000).toFixed(2),
response: response
};
}
const response = await superagent.get(url)
.query(args)
.set(headers);
return {
timings: ((Date.now() - timing) / 1000).toFixed(2),
response: response
};
}
if (type === "POST") {
const response = await superagent
.post(url)
.set(headers)
.send(args);
return {
timings: ((Date.now() - timing) / 1000).toFixed(2),
response: response
};
}
throw new Error("unsupported, must either use GET or POST");
}
module.exports.summarizeWebpage = async function(context, url){
return await request(ObeliskApi.SUMMARIZE_WEBPAGES, "POST", {}, {
url
})
}

View file

@ -156,6 +156,10 @@ const Statics = Object.freeze({
file: "icons/core/ico_notice_palm_idle.png",
revision: 0
},
ai_summary: {
file: "icons/core/ico_notice_summary.png",
revision: 0
},
warning: {
file: "icons/core/ico_notice_warning.png",
revision: 3
@ -206,6 +210,7 @@ module.exports.STATIC_ICONS = Object.freeze({
ai_clyde: staticAsset(Statics.icons.ai_clyde),
ai_clyde_idle: staticAsset(Statics.icons.ai_clyde_idle),
ai_palm_idle: staticAsset(Statics.icons.ai_palm_idle),
ai_summary: staticAsset(Statics.icons.ai_summary),
warning: staticAsset(Statics.icons.warning)
})