Compare commits
4 Commits
cbf6b1d3eb
...
main
Author | SHA1 | Date | |
---|---|---|---|
c3d4f1b1ff | |||
57ab59d342 | |||
71ae54930c | |||
3466a984ac |
@ -0,0 +1,19 @@
|
||||
-- RedefineTables
|
||||
PRAGMA defer_foreign_keys=ON;
|
||||
PRAGMA foreign_keys=OFF;
|
||||
CREATE TABLE "new_Response" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"pleromaNotificationId" TEXT NOT NULL DEFAULT 'null',
|
||||
"to" TEXT NOT NULL DEFAULT 'null',
|
||||
"request" TEXT NOT NULL DEFAULT 'null',
|
||||
"response" TEXT NOT NULL DEFAULT 'null',
|
||||
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"processedAt" DATETIME,
|
||||
"isProcessing" BOOLEAN NOT NULL DEFAULT true,
|
||||
"isComplete" BOOLEAN NOT NULL DEFAULT true
|
||||
);
|
||||
INSERT INTO "new_Response" ("createdAt", "id", "isProcessing", "pleromaNotificationId", "processedAt", "request", "response", "to") SELECT "createdAt", "id", "isProcessing", "pleromaNotificationId", "processedAt", "request", "response", "to" FROM "Response";
|
||||
DROP TABLE "Response";
|
||||
ALTER TABLE "new_Response" RENAME TO "Response";
|
||||
PRAGMA foreign_keys=ON;
|
||||
PRAGMA defer_foreign_keys=OFF;
|
@ -20,6 +20,7 @@ model Response {
|
||||
createdAt DateTime @default(now())
|
||||
processedAt DateTime?
|
||||
isProcessing Boolean @default(true)
|
||||
isComplete Boolean @default(true)
|
||||
}
|
||||
|
||||
model User {
|
||||
|
23
src/main.ts
23
src/main.ts
@ -26,7 +26,7 @@ export const prisma = new PrismaClient();
|
||||
export const envConfig = {
|
||||
pleromaInstanceUrl: process.env.PLEROMA_INSTANCE_URL || "",
|
||||
pleromaInstanceDomain: process.env.PLEROMA_INSTANCE_DOMAIN || "",
|
||||
whitelistOnly: process.env.ONLY_WHITELIST === "true" ? true : false || "true",
|
||||
whitelistOnly: process.env.ONLY_WHITELIST === "true" ? true : false,
|
||||
whitelistedDomains: process.env.WHITELISTED_DOMAINS
|
||||
? process.env.WHITELISTED_DOMAINS.split(",")
|
||||
: [process.env.PLEROMA_INSTANCE_DOMAIN],
|
||||
@ -42,9 +42,14 @@ export const envConfig = {
|
||||
};
|
||||
|
||||
const ollamaConfig: OllamaConfigOptions = {
|
||||
temperature: 1.2,
|
||||
temperature: 1.4,
|
||||
top_k: 100,
|
||||
top_p: 0.8,
|
||||
};
|
||||
|
||||
// this could be helpful
|
||||
// https://replicate.com/blog/how-to-prompt-llama
|
||||
|
||||
const generateOllamaRequest = async (
|
||||
notification: Notification
|
||||
): Promise<OllamaResponse | undefined> => {
|
||||
@ -55,9 +60,10 @@ const generateOllamaRequest = async (
|
||||
striptags(notification.status.content).includes("!prompt") &&
|
||||
!notification.status.account.bot && // sanity check, sort of
|
||||
notification.type === "mention" &&
|
||||
notification.status.visibility === "public" // for safety, let's only respond to public messages
|
||||
notification.status.visibility !== "private" // for safety, let's only respond to public messages
|
||||
) {
|
||||
if (whitelistOnly && !isFromWhitelistedDomain(notification)) {
|
||||
await deleteNotification(notification);
|
||||
return;
|
||||
}
|
||||
if (await alreadyRespondedTo(notification)) {
|
||||
@ -68,9 +74,9 @@ const generateOllamaRequest = async (
|
||||
const ollamaRequestBody: OllamaRequest = {
|
||||
model: ollamaModel,
|
||||
system: ollamaSystemPrompt,
|
||||
prompt: `@${notification.status.account.fqn} says: ${trimInputData(
|
||||
notification.status.content
|
||||
)}`,
|
||||
prompt: `[INST] @${
|
||||
notification.status.account.fqn
|
||||
} says: ${trimInputData(notification.status.content)} [/INST]`,
|
||||
stream: false,
|
||||
options: ollamaConfig,
|
||||
};
|
||||
@ -162,4 +168,9 @@ console.log(
|
||||
console.log(
|
||||
`Accepting prompts from: ${envConfig.whitelistedDomains.join(", ")}`
|
||||
);
|
||||
console.log(
|
||||
`Using model: ${envConfig.ollamaModel}\nConfig: ${JSON.stringify(
|
||||
ollamaConfig
|
||||
)}`
|
||||
);
|
||||
await beginFetchCycle();
|
||||
|
@ -14,6 +14,7 @@ const storePromptData = async (
|
||||
request: trimInputData(notification.status.content),
|
||||
to: notification.account.fqn,
|
||||
isProcessing: false,
|
||||
isComplete: true,
|
||||
},
|
||||
});
|
||||
} catch (error: any) {
|
||||
|
13
src/util.ts
13
src/util.ts
@ -2,7 +2,6 @@ import striptags from "striptags";
|
||||
import { prisma } from "./main.js";
|
||||
import { envConfig } from "./main.js";
|
||||
import { Notification } from "../types.js";
|
||||
import { deleteNotification } from "./api.js";
|
||||
|
||||
const trimInputData = (input: string): string => {
|
||||
const strippedInput = striptags(input);
|
||||
@ -17,6 +16,8 @@ const recordPendingResponse = async (notification: Notification) => {
|
||||
await prisma.response.create({
|
||||
data: {
|
||||
pleromaNotificationId: notification.id,
|
||||
isProcessing: true,
|
||||
isComplete: false,
|
||||
},
|
||||
});
|
||||
} catch (error: any) {
|
||||
@ -24,9 +25,7 @@ const recordPendingResponse = async (notification: Notification) => {
|
||||
}
|
||||
};
|
||||
|
||||
const isFromWhitelistedDomain = async (
|
||||
notification: Notification
|
||||
): Promise<boolean> => {
|
||||
const isFromWhitelistedDomain = (notification: Notification): boolean => {
|
||||
try {
|
||||
const domain = notification.status.account.fqn.split("@")[1];
|
||||
if (envConfig.whitelistedDomains.includes(domain)) {
|
||||
@ -35,8 +34,6 @@ const isFromWhitelistedDomain = async (
|
||||
console.log(
|
||||
`Rejecting prompt request from non-whitelisted domain: ${domain}`
|
||||
);
|
||||
// delete the notification so we don't keep trying to fetch it
|
||||
await deleteNotification(notification);
|
||||
return false;
|
||||
} catch (error: any) {
|
||||
console.error(`Error with domain check: ${error.message}`);
|
||||
@ -49,9 +46,9 @@ const alreadyRespondedTo = async (
|
||||
): Promise<boolean> => {
|
||||
try {
|
||||
const duplicate = await prisma.response.findFirst({
|
||||
where: { pleromaNotificationId: notification.id, isProcessing: true },
|
||||
where: { pleromaNotificationId: notification.id },
|
||||
});
|
||||
if (duplicate) {
|
||||
if (duplicate?.isProcessing || duplicate?.isComplete) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
Reference in New Issue
Block a user