revert to more "chat" based api calls
This commit is contained in:
68
src/main.ts
68
src/main.ts
@ -2,10 +2,8 @@ import {
|
|||||||
NewStatusBody,
|
NewStatusBody,
|
||||||
Notification,
|
Notification,
|
||||||
OllamaConfigOptions,
|
OllamaConfigOptions,
|
||||||
// OllamaChatRequest,
|
OllamaChatRequest,
|
||||||
// OllamaChatResponse,
|
OllamaChatResponse,
|
||||||
OllamaRequest,
|
|
||||||
OllamaResponse,
|
|
||||||
PostAncestorsForModel,
|
PostAncestorsForModel,
|
||||||
} from "../types.js";
|
} from "../types.js";
|
||||||
// import striptags from "striptags";
|
// import striptags from "striptags";
|
||||||
@ -53,7 +51,7 @@ const ollamaConfig: OllamaConfigOptions = {
|
|||||||
temperature: 0.6,
|
temperature: 0.6,
|
||||||
top_p: 0.85,
|
top_p: 0.85,
|
||||||
top_k: 40,
|
top_k: 40,
|
||||||
num_ctx: 2048,
|
num_ctx: 8192,
|
||||||
repeat_penalty: 1.1,
|
repeat_penalty: 1.1,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -62,7 +60,7 @@ const ollamaConfig: OllamaConfigOptions = {
|
|||||||
|
|
||||||
const generateOllamaRequest = async (
|
const generateOllamaRequest = async (
|
||||||
notification: Notification
|
notification: Notification
|
||||||
): Promise<OllamaResponse | undefined> => {
|
): Promise<OllamaChatResponse | undefined> => {
|
||||||
const {
|
const {
|
||||||
whitelistOnly,
|
whitelistOnly,
|
||||||
ollamaModel,
|
ollamaModel,
|
||||||
@ -97,22 +95,42 @@ const generateOllamaRequest = async (
|
|||||||
});
|
});
|
||||||
// console.log(conversationHistory);
|
// console.log(conversationHistory);
|
||||||
}
|
}
|
||||||
const oneOffPrompt = `${notification.status.account.fqn} says: ${notification.status.pleroma.content["text/plain"]}\n[/INST]`;
|
|
||||||
const contextPrompt = `<<SYS>>[INST]\n${ollamaSystemPrompt}\nHere is the previous conversation context in JSON format:\n${JSON.stringify(
|
// Simplified user message (remove [/INST] as it's not needed for Llama 3)
|
||||||
conversationHistory
|
const userMessage = `${notification.status.account.fqn} says: ${notification.status.pleroma.content["text/plain"]}`;
|
||||||
)}\nAssume the {account_fqn} key is the user who posted the {plaintext_content} to the users in {mentions}\nReply as if you are a party to the conversation. If you see '@nice-ai' or 'nice-ai' in the {mentions}, you are an addressee of the conversation.\nAppend the '@' sign to each username at the beginning when addressing users.<</SYS>>`;
|
|
||||||
const ollamaRequestBody: OllamaRequest = {
|
let systemContent = ollamaSystemPrompt;
|
||||||
|
if (replyWithContext) {
|
||||||
|
// Simplified context instructions (avoid heavy JSON; summarize for clarity)
|
||||||
|
systemContent = `${ollamaSystemPrompt}\n\nPrevious conversation context:\n${conversationHistory
|
||||||
|
.map(
|
||||||
|
(post) =>
|
||||||
|
`${post.account_fqn} (to ${post.mentions.join(", ")}): ${
|
||||||
|
post.plaintext_content
|
||||||
|
}`
|
||||||
|
)
|
||||||
|
.join(
|
||||||
|
"\n"
|
||||||
|
)}\nReply as if you are a party to the conversation. If '@nice-ai' is mentioned, respond directly. Prefix usernames with '@' when addressing them.`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Switch to chat request format (messages array auto-handles Llama 3 template)
|
||||||
|
const ollamaRequestBody: OllamaChatRequest = {
|
||||||
model: ollamaModel,
|
model: ollamaModel,
|
||||||
prompt: oneOffPrompt,
|
messages: [
|
||||||
system: replyWithContext ? contextPrompt : ollamaSystemPrompt,
|
{ role: "system", content: systemContent as string },
|
||||||
|
{ role: "user", content: userMessage },
|
||||||
|
],
|
||||||
stream: false,
|
stream: false,
|
||||||
options: ollamaConfig,
|
options: ollamaConfig,
|
||||||
};
|
};
|
||||||
const response = await fetch(`${ollamaUrl}/api/generate`, {
|
|
||||||
|
// Change endpoint to /api/chat
|
||||||
|
const response = await fetch(`${ollamaUrl}/api/chat`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify(ollamaRequestBody),
|
body: JSON.stringify(ollamaRequestBody),
|
||||||
});
|
});
|
||||||
const ollamaResponse: OllamaResponse = await response.json();
|
const ollamaResponse: OllamaChatResponse = await response.json();
|
||||||
|
|
||||||
await storePromptData(notification, ollamaResponse);
|
await storePromptData(notification, ollamaResponse);
|
||||||
return ollamaResponse;
|
return ollamaResponse;
|
||||||
@ -124,7 +142,7 @@ const generateOllamaRequest = async (
|
|||||||
|
|
||||||
const postReplyToStatus = async (
|
const postReplyToStatus = async (
|
||||||
notification: Notification,
|
notification: Notification,
|
||||||
ollamaResponseBody: OllamaResponse
|
ollamaResponseBody: OllamaChatResponse
|
||||||
) => {
|
) => {
|
||||||
const { pleromaInstanceUrl, bearerToken } = envConfig;
|
const { pleromaInstanceUrl, bearerToken } = envConfig;
|
||||||
const emojiList = await getInstanceEmojis();
|
const emojiList = await getInstanceEmojis();
|
||||||
@ -136,7 +154,7 @@ const postReplyToStatus = async (
|
|||||||
let mentions: string[];
|
let mentions: string[];
|
||||||
const statusBody: NewStatusBody = {
|
const statusBody: NewStatusBody = {
|
||||||
content_type: "text/markdown",
|
content_type: "text/markdown",
|
||||||
status: `${ollamaResponseBody.response} :${randomEmoji}:`,
|
status: `${ollamaResponseBody.message.content} :${randomEmoji}:`,
|
||||||
in_reply_to_id: notification.status.id,
|
in_reply_to_id: notification.status.id,
|
||||||
};
|
};
|
||||||
if (
|
if (
|
||||||
@ -176,26 +194,28 @@ const createTimelinePost = async () => {
|
|||||||
ollamaUrl,
|
ollamaUrl,
|
||||||
pleromaInstanceUrl,
|
pleromaInstanceUrl,
|
||||||
} = envConfig;
|
} = envConfig;
|
||||||
const ollamaRequestBody: OllamaRequest = {
|
const ollamaRequestBody: OllamaChatRequest = {
|
||||||
model: ollamaModel,
|
model: ollamaModel,
|
||||||
prompt: "Say something random.",
|
messages: [
|
||||||
system: ollamaSystemPrompt,
|
{ role: "system", content: ollamaSystemPrompt as string },
|
||||||
|
{ role: "user", content: "Say something random." },
|
||||||
|
],
|
||||||
stream: false,
|
stream: false,
|
||||||
// options: ollamaConfig,
|
options: ollamaConfig,
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${ollamaUrl}/api/generate`, {
|
const response = await fetch(`${ollamaUrl}/api/chat`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify(ollamaRequestBody),
|
body: JSON.stringify(ollamaRequestBody),
|
||||||
});
|
});
|
||||||
if (!response.ok)
|
if (!response.ok)
|
||||||
throw new Error("Error generating ad-hoc Ollama response");
|
throw new Error("Error generating ad-hoc Ollama response");
|
||||||
|
|
||||||
const ollamaResponse: OllamaResponse = await response.json();
|
const ollamaResponse: OllamaChatResponse = await response.json();
|
||||||
|
|
||||||
const newStatusBody: NewStatusBody = {
|
const newStatusBody: NewStatusBody = {
|
||||||
content_type: "text/markdown",
|
content_type: "text/markdown",
|
||||||
status: ollamaResponse.response,
|
status: ollamaResponse.message.content,
|
||||||
};
|
};
|
||||||
|
|
||||||
const pleromaResponse = await fetch(
|
const pleromaResponse = await fetch(
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
import { Notification, OllamaResponse } from "../types.js";
|
import { Notification, OllamaChatResponse } from "../types.js";
|
||||||
import { trimInputData } from "./util.js";
|
import { trimInputData } from "./util.js";
|
||||||
import { prisma } from "./main.js";
|
import { prisma } from "./main.js";
|
||||||
|
|
||||||
const storePromptData = async (
|
const storePromptData = async (
|
||||||
notification: Notification,
|
notification: Notification,
|
||||||
ollamaResponseBody: OllamaResponse
|
ollamaResponseBody: OllamaChatResponse
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
await prisma.response.updateMany({
|
await prisma.response.updateMany({
|
||||||
where: { pleromaNotificationId: notification.id },
|
where: { pleromaNotificationId: notification.id },
|
||||||
data: {
|
data: {
|
||||||
response: ollamaResponseBody.response,
|
response: ollamaResponseBody.message.content,
|
||||||
request: trimInputData(notification.status.content),
|
request: trimInputData(notification.status.content),
|
||||||
to: notification.account.fqn,
|
to: notification.account.fqn,
|
||||||
isProcessing: false,
|
isProcessing: false,
|
||||||
|
Reference in New Issue
Block a user