-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.ts
355 lines (333 loc) · 11.1 KB
/
bot.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// FediChatBot: An LLM-powered chatbot for the fediverse
// Copyright (C) 2025 Hong Minhee <https://hongminhee.org/>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import Template from "@deno-library/template";
import {
type Actor,
type Bot,
createBot,
parseSemVer,
type Session,
} from "@fedify/botkit";
import type { Message, MessageClass } from "@fedify/botkit/message";
import {
link,
markdown,
mention,
mentions,
type Text,
text,
} from "@fedify/botkit/text";
import { getActorHandle } from "@fedify/fedify/vocab";
import { DenoKvMessageQueue, DenoKvStore } from "@fedify/fedify/x/denokv";
import { get, set } from "@kitsonk/kv-toolbox/blob";
import {
AIMessage,
HumanMessage,
type MessageContent,
SystemMessage,
} from "@langchain/core/messages";
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
import { configure, getConsoleSink, getLogger } from "@logtape/logtape";
import "@std/dotenv/load";
import { encodeBase64 } from "@std/encoding/base64";
import { join } from "@std/path/join";
import { AsyncLocalStorage } from "node:async_hooks";
import { detect } from "tinyld";
import { FilterXSS } from "xss";
import { z } from "zod";
import metadata from "./deno.json" with { type: "json" };
await configure({
sinks: {
console: getConsoleSink(),
},
loggers: [
{
category: ["logtape", "meta"],
lowestLevel: "warning",
sinks: ["console"],
},
{ category: "fedichatbot", lowestLevel: "debug", sinks: ["console"] },
{ category: "fedify", lowestLevel: "warning", sinks: ["console"] },
],
contextLocalStorage: new AsyncLocalStorage(),
});
const logger = getLogger("fedichatbot");
const kv: Deno.Kv = await Deno.openKv();
const bot: Bot<void> = createBot<void>({
username: "FediChatBot",
name: "FediChatBot",
summary: text`An LLM-powered chatbot for the fediverse, powered by ${
link("BotKit", "https://botkit.fedify.dev/")
} and ${
link(
"Gemini 2.0 Flash (experimental)",
"https://ai.google.dev/gemini-api/docs/models/gemini#gemini-2.0-flash",
)
}.`,
icon: new URL(
"https://raw.githubusercontent.com/fedify-dev/fedichatbot/refs/heads/main/logo.png",
),
properties: {
"Model": link(
"Gemini 2.0 Flash (experimental)",
"https://ai.google.dev/gemini-api/docs/models/gemini#gemini-2.0-flash",
),
"Source code": link("GitHub", "https://github.com/fedify-dev/fedichatbot"),
"Powered by": link("BotKit", "https://botkit.fedify.dev/"),
"Created by": mention("@[email protected]"),
},
software: {
name: "fedichatbot",
version: parseSemVer(metadata.version),
repository: new URL("https://github.com/fedify-dev/fedichatbot"),
},
pages: { color: "sand" },
kv: new DenoKvStore(kv),
queue: new DenoKvMessageQueue(kv),
behindProxy: Deno.env.get("DENO_DEPLOYMENT_ID") == null,
});
bot.onFollow = async (session, followRequest) => {
const actor = followRequest.follower;
const response = await llm.invoke([
getSystemMessage(session),
await getIntroMessage(session, actor, await getFollowPrompt(actor)),
]);
const message = response.content.toString();
const language = detect(message);
const md: Text<"block", void> = markdown(message);
await session.publish(
await mentions(session, md, actor) ? md : text`${mention(actor)}\n\n${md}`,
{ language },
);
};
const reactionResponseSchema = z.object({
whetherToLike: z.boolean().describe(
"Whether to press the like button on the message.",
),
});
bot.onMention = async (session, msg) => {
if (msg.replyTarget != null) return;
const actor = msg.actor;
const llmWithStruct = llm.withStructuredOutput(reactionResponseSchema);
const whetherToLike = await llmWithStruct.invoke([
getSystemMessage(session),
await getIntroMessage(session, actor, await getMentionPrompt(actor)),
await getHumanMessage(msg, true),
]);
logger.debug("Whether to like: {whetherToLike}", whetherToLike);
if (whetherToLike.whetherToLike) await msg.like();
const response = await llm.invoke([
getSystemMessage(session),
await getIntroMessage(session, actor, await getMentionPrompt(actor)),
await getHumanMessage(msg, false),
]);
const message = response.content.toString();
const language = detect(message);
const md: Text<"block", void> = markdown(message);
await msg.reply(
await mentions(session, md, actor) ? md : text`${mention(actor)}\n\n${md}`,
{ language },
);
};
bot.onReply = async (session, msg) => {
const thread: Message<MessageClass, void>[] = [msg];
for (let m = msg; m.replyTarget != null; m = m.replyTarget) {
thread.unshift(m.replyTarget);
}
const messages: (SystemMessage | HumanMessage | AIMessage)[] = [
getSystemMessage(session),
await getIntroMessage(
session,
thread[0].actor,
thread[0].actor?.id?.href === session.actorId.href
? await getFollowPrompt(thread[0].mentions[0])
: await getMentionPrompt(thread[0].actor),
),
];
for (const msg of thread) {
const message = msg.actor?.id?.href === session.actorId.href
? new AIMessage(msg.text)
: await getHumanMessage(msg, false);
messages.push(message);
}
const llmWithStruct = llm.withStructuredOutput(reactionResponseSchema);
const whetherToLike = await llmWithStruct.invoke([
...messages.slice(0, messages.length - 1),
await getHumanMessage(thread[thread.length - 1], true),
]);
if (whetherToLike.whetherToLike) await msg.like();
logger.debug("Whether to like: {whetherToLike}", whetherToLike);
const response = await llm.invoke(messages);
const message = response.content.toString();
const language = detect(message);
const md: Text<"block", void> = markdown(message);
await msg.reply(
await mentions(session, md, msg.actor)
? md
: text`${mention(msg.actor)}\n\n${md}`,
{ language },
);
};
const llm = new ChatGoogleGenerativeAI({
model: "gemini-2.0-flash-exp",
temperature: 0.25,
maxRetries: 2,
});
const SYSTEM_PROMPT_TEMPLATE = await Deno.readTextFile(
join(import.meta.dirname!, "prompts", "system.txt"),
);
const FOLLOW_PROMPT_TEMPLATE = await Deno.readTextFile(
join(import.meta.dirname!, "prompts", "follow.txt"),
);
const MENTION_PROMPT_TEMPLATE = await Deno.readTextFile(
join(import.meta.dirname!, "prompts", "mention.txt"),
);
const REACTION_PROMPT_TEMPLATE = await Deno.readTextFile(
join(import.meta.dirname!, "prompts", "reaction.txt"),
);
const template = new Template({ isEscape: false });
function getSystemPrompt(session: Session<void>): string {
return template.render(SYSTEM_PROMPT_TEMPLATE, {
fediverseHandle: session.actorHandle,
license: metadata.license,
version: metadata.version,
dateTime: new Date().toUTCString(),
});
}
function getSystemMessage(session: Session<void>): SystemMessage {
const lg = logger.getChild("prompts");
const text = getSystemPrompt(session);
lg.debug("System prompt:\n{text}", { text });
return new SystemMessage(text);
}
const textXss = new FilterXSS({
allowList: {},
stripIgnoreTag: true,
});
async function getFollowPrompt(actor: Actor): Promise<string> {
let fediverseHandle: string;
try {
fediverseHandle = await getActorHandle(actor);
} catch {
fediverseHandle = "not available";
}
const bio = actor.summary?.toString();
return template.render(FOLLOW_PROMPT_TEMPLATE, {
displayName: actor.name?.toString() ?? "not available",
fediverseHandle,
quotedBio: bio == null
? "Not available."
: `> ${textXss.process(bio).replaceAll("\n", "\n> ")}`,
});
}
function getReactionPrompt(message: Message<MessageClass, void>): string {
const msg = message.text;
return template.render(REACTION_PROMPT_TEMPLATE, {
quotedMessage: msg == null
? "Not available."
: `> ${msg.replaceAll("\n", "\n> ")}`,
});
}
async function getIntroMessage(
session: Session<void>,
actor: Actor,
prompt: string,
): Promise<HumanMessage> {
const lg = logger.getChild("prompts");
const options = {
contextLoader: session.context.contextLoader,
documentLoader: await session.context.getDocumentLoader(session.bot),
suppressError: true,
};
lg.debug("Intro prompt:\n{prompt}", { prompt });
const content: MessageContent = [
{ type: "text", text: prompt },
];
const icon = await actor.getIcon(options);
if (icon != null && icon.url != null) {
const url = icon.url instanceof URL ? icon.url : icon.url.href;
if (url != null) {
content.push({
type: "image_url",
image_url: { url: await toDataUrl(url) },
});
}
}
const image = await actor.getImage(options);
if (image != null && image.url != null) {
const url = image.url instanceof URL ? image.url : image.url.href;
if (url != null) {
content.push({
type: "image_url",
image_url: { url: await toDataUrl(url) },
});
}
}
return new HumanMessage({ content });
}
async function getMentionPrompt(actor: Actor): Promise<string> {
let fediverseHandle: string;
try {
fediverseHandle = await getActorHandle(actor);
} catch {
fediverseHandle = "not available";
}
const bio = actor.summary?.toString();
return template.render(MENTION_PROMPT_TEMPLATE, {
displayName: actor.name?.toString() ?? "not available",
fediverseHandle,
quotedBio: bio == null
? "Not available."
: `> ${textXss.process(bio).replaceAll("\n", "\n> ")}`,
});
}
async function getHumanMessage<T extends MessageClass>(
msg: Message<T, void>,
reaction: boolean,
): Promise<HumanMessage> {
const attachments = msg.attachments.map(async (doc) => {
if (!doc.mediaType?.startsWith("image/")) return null;
const url = doc.url instanceof URL ? doc.url : doc.url?.href;
if (url == null) return null;
return {
type: "image_url",
image_url: { url: await toDataUrl(url) },
};
});
return new HumanMessage({
content: [
{ type: "text", text: reaction ? getReactionPrompt(msg) : msg.text },
...(await Promise.all(attachments)).filter((a) => a != null),
],
});
}
async function toDataUrl(imageUrl: string | URL): Promise<string> {
const cached = await get(kv, ["imageCache", imageUrl.toString()]);
let bytes: ArrayBuffer;
if (cached.value == null) {
const response = await fetch(imageUrl);
const blob = await response.blob();
bytes = await blob.arrayBuffer();
await set(kv, ["imageCache", imageUrl.toString()], bytes, {
expireIn: 1000 * 60 * 60 * 3, // 3 hours
});
} else {
bytes = cached.value;
}
return `data:image/jpeg;base64,${encodeBase64(bytes)}`;
}
bot.federation.startQueue();
export default bot;