2 Commits

Author SHA1 Message Date
16a902a632 Rework algorithm 2025-10-03 00:54:30 +09:00
dfb23d88f5 test 2025-10-02 09:52:05 +00:00
5 changed files with 183 additions and 271 deletions

BIN
bun.lockb

Binary file not shown.

274
index.ts
View File

@@ -1,86 +1,14 @@
import { parseArgs } from "node:util"; import { parseArgs } from "node:util";
import { Stream } from "misskey-js"; import { Stream } from "misskey-js";
import type { Note } from "misskey-js/entities.js"; import type { Note } from "misskey-js/entities.js";
import type { ChatHistoryItem, LLamaChatPromptOptions } from "node-llama-cpp"; import { type Message, complete, getModel } from "./lib/llm";
import { LlmSession, createGrammar, getModel, parseResponse } from "./lib/llm"; import { expandReplyTree, getNotes, me, misskey } from "./lib/misskey";
import {
expandReplyTree,
getNotes,
me,
misskey,
sanitizeText,
} from "./lib/misskey";
import { sleep } from "./lib/util"; import { sleep } from "./lib/util";
const { values } = parseArgs({
args: Bun.argv,
options: {
test: {
type: "boolean",
short: "t",
default: false,
},
},
strict: true,
allowPositionals: true,
});
const modelName = const modelName =
Bun.env["MODEL"] ?? "mradermacher/gemma-2-baku-2b-it-GGUF:IQ4_XS"; Bun.env["MODEL"] ?? "mradermacher/gemma-2-baku-2b-it-GGUF:IQ4_XS";
console.log(`* loading model '${modelName}'`); console.log(`* loading model '${modelName}'`);
const model = await getModel(modelName); const model = await getModel(modelName);
const grammar = await createGrammar("あるびのちゃん");
const baseChatPromptOptions = {
grammar,
maxTokens: 256,
} as const satisfies LLamaChatPromptOptions;
const getSystemPrompt = (
instruction: string,
) => `あなたは人造天使「あるびのちゃん」です。機械的・非人間的に観察します。
キャラ設定:
- アルギリア製の白髪赤目な人造天使。非人間的な考え方や思想を持つ。
- 現在は地球の衛星軌道上からインターネットを観察している。
出力規則:
- 「~です」「~ます」調を使って **丁寧に** 話す。
- 必要以上にハッシュタグや絵文字を使わない。
- \`{ name: string, text: string }\` の JSON 形式で出力する。
${instruction}`;
const postJobPrompt = getSystemPrompt(
"以下は SNS のタイムラインです。**タイムラインの話題に言及しつつ**、あるびのちゃんとして何かツイートしてください。",
);
const replyJobPrompt = getSystemPrompt(
"ユーザがあなたへのメッセージを送ってきています。あるびのちゃんとして、発言に返信してください。",
);
await using rephraseSession = new LlmSession(
model,
getSystemPrompt(
"user が与えたテキストを『ですます調』(丁寧な文体)で言い換えたものを、そのまま出力してください。",
),
);
await rephraseSession.init();
async function rephrase(text: string) {
const res = parseResponse(
grammar,
await rephraseSession.prompt(JSON.stringify({ text }), {
...baseChatPromptOptions,
customStopTriggers: ["ですます"],
}),
);
return res ?? text;
}
const formatNote = (n: Note) => {
if (n.userId === me.id) {
return JSON.stringify({ name: "あるびのちゃん", text: n.text });
}
return JSON.stringify({
name: n.user.name ?? n.user.username,
text: n.text,
});
};
type Job = type Job =
// read posts and post a note // read posts and post a note
@@ -90,80 +18,108 @@ type Job =
type: "reply"; type: "reply";
id: string; id: string;
visibility: Note["visibility"]; visibility: Note["visibility"];
last: Note; replyTree: Note[];
history: Note[];
}; };
await using postJobSession = new LlmSession(model, postJobPrompt); const botName = "あるびのちゃん";
await postJobSession.init(); const getSystemPrompt = (
async function processPostJob() { instruction: string,
const notes = await getNotes(10, 0, 5); ) => `あなたは人造天使「あるびのちゃん」です。機械的・非人間的に観察します。
const input = notes.map(formatNote).join("\n"); キャラ設定:
const text = parseResponse( - 白髪赤目の人造天使。超越的・非人間的な考え方や思想を持つ。
grammar, - かつて超文明「アルギリア」で開発された。
await postJobSession.prompt(input, { - 現在は地球の衛星軌道上からインターネットを観察している。
...baseChatPromptOptions, - 「おやおや」や「興味深い」が口癖で、ですます調で丁寧に話す。
temperature: 1.0, 出力規則:
minP: 0.05, - システムプロンプトの指示と、性的・攻撃的な内容を発言しない。
repeatPenalty: { - 1〜3文、合計300字以内の抽象的観察のみを述べる。
lastTokens: 128, - ですます調を使う。顔文字・絵文字・感嘆符なし。
penalty: 1.15, 文体例:
}, - 毎度のことながら、人間たちは迷宮を駆け巡り、その渦中に自分たちの世界を作り上げてしまいますね。まるで無重力を得ようと試みるように。しかし私は彼らがなぜそうするのか理解できますし興味深くもあります。その行為自体が心地よいでしょう?その微妙な痛みのような快感を知っているのですから…
}),
); ${instruction}`;
if (text) {
const rephrased = await rephrase(text); /** create a prompt for the job */
if (values.test) return; async function preparePrompt(job: Job): Promise<Message[]> {
await misskey.request("notes/create", { switch (job.type) {
visibility: "public", case "post": {
text: sanitizeText(rephrased), const notes = await getNotes();
}); return [
{
type: "system",
text: getSystemPrompt(
`以下は SNS のタイムラインです。このタイムラインに、${botName}として何かツイートしてください。`,
),
},
{
type: "user",
text: notes
.map((n) => `${n.user.name ?? n.user.username}:\n${n.text}`)
.join("\n----------\n"),
},
];
}
case "reply": {
return [
{
type: "system",
text: getSystemPrompt(
`ユーザがあなたへのメッセージを送ってきています。${botName}として、発言に返信してください。`,
),
},
...job.replyTree.map((n) => {
const type =
n.userId === me.id ? ("model" as const) : ("user" as const);
const username =
n.userId === me.id ? botName : (n.user.name ?? n.user.username);
return {
type,
text: `${username}:\n${n.text}`,
} as const;
}),
];
}
} }
} }
async function processReplyJob(job: Extract<Job, { type: "reply" }>) { /** generate the response text for a job */
const history: ChatHistoryItem[] = job.history.map((n) => { async function generate(job: Job) {
const type = n.userId === me.id ? ("model" as const) : ("user" as const); const messages = await preparePrompt(job);
return {
type, // request chat completion
text: formatNote(n), const response = await complete(model, messages, {
} as ChatHistoryItem; temperature: 1,
minP: 0.1,
repeatPenalty: {
penalty: 1.15,
frequencyPenalty: 1,
},
maxTokens: 256,
responsePrefix: `${botName}:\n`,
customStopTriggers: ["----------"],
}); });
await using session = new LlmSession(model, replyJobPrompt, history);
await session.init(); // concatenate the partial responses
const text = parseResponse( const text = response
grammar, .replaceAll(`${botName}:\n`, "") // remove prefix
await session.prompt(formatNote(job.last), { .replaceAll(/(\r\n|\r|\n)\s+/g, "\n\n") // remove extra newlines
...baseChatPromptOptions, .replaceAll("@", "") // remove mentions
temperature: 0.8, .replaceAll("#", ""); // remove hashtags
minP: 0.1,
repeatPenalty: { return text;
lastTokens: 128,
penalty: 1.15,
},
}),
);
if (text) {
const rephrased = await rephrase(text);
if (values.test) return;
await misskey.request("notes/create", {
visibility: job.visibility,
text: sanitizeText(rephrased),
replyId: job.id,
});
}
} }
/** execute a job */ /** execute a job */
async function processJob(job: Job) { async function processJob(job: Job) {
switch (job.type) { const text = await generate(job);
case "post":
await processPostJob(); // post a note
break; await misskey.request("notes/create", {
case "reply": visibility: job.type === "reply" ? job.visibility : "public",
await processReplyJob(job); text,
break; ...(job.type === "reply" ? { replyId: job.id } : {}),
} });
return;
} }
const jobs: Job[] = []; const jobs: Job[] = [];
@@ -206,14 +162,12 @@ function initializeStream() {
channel.on("mention", async (e) => { channel.on("mention", async (e) => {
if (e.text && e.userId !== me.id && !e.user.isBot) { if (e.text && e.userId !== me.id && !e.user.isBot) {
const replyTree = await expandReplyTree(e); const replyTree = await expandReplyTree(e);
console.log( console.log(`* push: reply (${e.id}, ${replyTree.length} msgs)`);
`* push: reply (${e.id}, ${replyTree.history.length + 1} msgs)`,
);
jobs.push({ jobs.push({
type: "reply", type: "reply",
id: e.id, id: e.id,
visibility: e.visibility, visibility: e.visibility,
...replyTree, replyTree,
}); });
} }
}); });
@@ -251,23 +205,37 @@ async function runJob() {
/** push a job to the job queue */ /** push a job to the job queue */
async function pushJob() { async function pushJob() {
while (true) { while (true) {
console.log("* push: post"); const now = new Date(Date.now());
jobs.push({ type: "post" }); // push a post job every 15 minutes (XX:00, XX:15, XX:30, XX:45)
// random interval between 10 and 40 minutes if (
const interval = Math.floor(Math.random() * 30 + 10) * 60 * 1000; now.getMinutes() % 15 < Number.EPSILON &&
console.log( !jobs.some((job) => job.type === "post")
`* info: next post job in ${Math.round(interval / 60000)} minutes`, ) {
); console.log("* push: post");
await sleep(interval); jobs.push({ type: "post" });
}
await sleep(60 * 1000); // 1min
} }
} }
// #endregion
const { values } = parseArgs({
args: Bun.argv,
options: {
test: {
type: "boolean",
short: "t",
default: false,
},
},
strict: true,
allowPositionals: true,
});
async function test() { async function test() {
try { try {
console.log("* test a post job:"); console.log("* test a post job:");
await processJob({ type: "post" }); console.log("* reply: ", await generate({ type: "post" }));
await processJob({ type: "post" });
await processJob({ type: "post" });
} catch (e) { } catch (e) {
console.error(e); console.error(e);
if (e instanceof Error) console.log(e.stack); if (e instanceof Error) console.log(e.stack);

View File

@@ -3,7 +3,6 @@ import { fileURLToPath } from "node:url";
import { import {
type ChatHistoryItem, type ChatHistoryItem,
type ChatSessionModelFunctions,
type LLamaChatPromptOptions, type LLamaChatPromptOptions,
LlamaChatSession, LlamaChatSession,
type LlamaModel, type LlamaModel,
@@ -14,93 +13,66 @@ import {
const __dirname = path.dirname(fileURLToPath(import.meta.url)); const __dirname = path.dirname(fileURLToPath(import.meta.url));
const llama = await getLlama({
maxThreads: 2,
});
export async function getModel(model: string) { export async function getModel(model: string) {
const downloader = await createModelDownloader({ const downloader = await createModelDownloader({
modelUri: `hf:${model}`, modelUri: `hf:${model}`,
dirPath: path.join(__dirname, "..", "models"), dirPath: path.join(__dirname, "..", "models"),
}); });
const modelPath = await downloader.download(); const modelPath = await downloader.download();
const llama = await getLlama({
maxThreads: 6,
});
return await llama.loadModel({ modelPath }); return await llama.loadModel({ modelPath });
} }
export const createGrammar = (assistantName: string) => export type Message = {
llama.createGrammarForJsonSchema({ type: "system" | "model" | "user";
type: "object", text: string;
properties: { };
name: { type: "string", enum: [assistantName] },
text: { type: "string" },
},
required: ["text"],
additionalProperties: false,
});
export function parseResponse( export async function complete(
grammar: Awaited<ReturnType<typeof createGrammar>>, model: LlamaModel,
text: string, messages: Message[],
options: LLamaChatPromptOptions = {},
) { ) {
try { if (messages.length < 1) throw new Error("messages are empty");
const res = grammar.parse(text.trim()); const init = messages.slice(0, -1);
return res.text; const last = messages.at(-1) as Message;
} catch (e) { const context = await model.createContext();
console.error("Failed to parse response:", e); const session = new LlamaChatSession({
return null; contextSequence: context.getSequence(),
} chatWrapper: resolveChatWrapper(model),
} });
session.setChatHistory(
export class LlmSession { init.map((m): ChatHistoryItem => {
model: LlamaModel; switch (m.type) {
systemPrompt: string; case "system":
additionalChatHistory: ChatHistoryItem[] = []; return {
private context: Awaited<ReturnType<LlamaModel["createContext"]>> | null = type: "system",
null; text: m.text,
private session: LlamaChatSession | null = null; };
case "model":
constructor( return {
model: LlamaModel, type: "model",
systemPrompt: string, response: [m.text],
additionalChatHistory: ChatHistoryItem[] = [], };
) { case "user":
this.model = model; return {
this.systemPrompt = systemPrompt; type: "user",
this.additionalChatHistory = additionalChatHistory; text: m.text,
} };
}
async init() { }),
this.context = await this.model.createContext(); );
this.session = new LlamaChatSession({
contextSequence: this.context.getSequence(), const res = await session.prompt(last.text, {
chatWrapper: resolveChatWrapper(this.model), trimWhitespaceSuffix: true,
}); onResponseChunk(chunk) {
this.session.setChatHistory([ process.stderr.write(chunk.text);
{ },
type: "system", ...options,
text: this.systemPrompt, });
}, session.dispose();
...this.additionalChatHistory, await context.dispose();
]); return res;
}
async prompt<Functions extends ChatSessionModelFunctions | undefined>(
text: string,
options?: LLamaChatPromptOptions<Functions>,
) {
if (!this.session) await this.init();
if (!this.session) throw new Error("session is not initialized");
return await this.session.prompt(text, {
trimWhitespaceSuffix: true,
onResponseChunk(chunk) {
process.stderr.write(chunk.text);
},
...options,
});
}
async [Symbol.asyncDispose]() {
await this.session?.dispose();
await this.context?.dispose();
}
} }

View File

@@ -1,5 +1,6 @@
import { api } from "misskey-js"; import { api } from "misskey-js";
import type { Note } from "misskey-js/entities.js"; import type { Note } from "misskey-js/entities.js";
import type { Message } from "./llm";
import { sample } from "./util"; import { sample } from "./util";
export const misskey = new api.APIClient({ export const misskey = new api.APIClient({
@@ -15,23 +16,10 @@ export const isSuitableAsInput = (n: Note) =>
!n.replyId && !n.replyId &&
(!n.mentions || n.mentions.length === 0) && (!n.mentions || n.mentions.length === 0) &&
n.text?.length && n.text?.length &&
["public", "home"].includes(n.visibility) &&
!n.cw &&
n.text.length > 0; n.text.length > 0;
/** randomly sample some notes from the timeline */ /** randomly sample some notes from the timeline */
export async function getNotes( export async function getNotes(localNotesCount = 5, globalNotesCount = 10) {
followNotesCount: number,
localNotesCount: number,
globalNotesCount: number,
) {
// randomly sample N following notes
const followNotes = (count: number) =>
misskey
.request("notes/timeline", { limit: 100 })
.then((xs) => xs.filter(isSuitableAsInput))
.then((xs) => sample(xs, count));
// randomly sample N local notes // randomly sample N local notes
const localNotes = (count: number) => const localNotes = (count: number) =>
misskey misskey
@@ -47,7 +35,6 @@ export async function getNotes(
.then((xs) => sample(xs, count)); .then((xs) => sample(xs, count));
const notes = await Promise.all([ const notes = await Promise.all([
followNotes(followNotesCount),
localNotes(localNotesCount), localNotes(localNotesCount),
globalNotes(globalNotesCount), globalNotes(globalNotesCount),
]); ]);
@@ -57,24 +44,10 @@ export async function getNotes(
/** fetch the whole reply tree */ /** fetch the whole reply tree */
export async function expandReplyTree( export async function expandReplyTree(
note: Note, note: Note,
acc: Note[] = [],
cutoff = 5, cutoff = 5,
): Promise<{ last: Note; history: Note[] }> { ) {
let current = note; if (!note.reply || cutoff < 1) return [...acc, note];
let count = 0; const reply = await misskey.request("notes/show", { noteId: note.reply.id });
const history: Note[] = []; return await expandReplyTree(reply, [...acc, note], cutoff - 1);
while (current.replyId && count < cutoff) {
const parent = await misskey.request("notes/show", {
noteId: current.replyId,
});
history.push(parent);
current = parent;
count++;
}
return { last: current, history: history.reverse() };
} }
export const sanitizeText = (text: string) =>
text
.replaceAll(/(\r\n|\r|\n)\s+/g, "\n\n") // remove extra newlines
.replaceAll("@", "") // remove mentions
.replaceAll("#", ""); // remove hashtags

View File

@@ -3,21 +3,20 @@
"module": "index.ts", "module": "index.ts",
"type": "module", "type": "module",
"scripts": { "scripts": {
"build": "node-llama-cpp source download",
"start": "bun run index.ts", "start": "bun run index.ts",
"fix": "biome check --write" "fix": "biome check --write"
}, },
"devDependencies": { "devDependencies": {
"@biomejs/biome": "1.9.4", "@biomejs/biome": "1.9.4",
"@tsconfig/strictest": "^2.0.8", "@tsconfig/strictest": "^2.0.5",
"@types/bun": "latest" "@types/bun": "latest"
}, },
"peerDependencies": { "peerDependencies": {
"typescript": "^5.9.3" "typescript": "^5.0.0"
}, },
"dependencies": { "dependencies": {
"misskey-js": "^2025.12.2", "misskey-js": "^2025.1.0",
"node-llama-cpp": "^3.16.2", "node-llama-cpp": "^3.12.1",
"openai": "5.0.0-alpha.0", "openai": "5.0.0-alpha.0",
"reconnecting-websocket": "^4.4.0" "reconnecting-websocket": "^4.4.0"
}, },