cure lydias dementia
This commit is contained in:
parent
eca615b4de
commit
d2f0270896
1 changed files with 75 additions and 58 deletions
133
lydia.js
133
lydia.js
|
|
@ -72,6 +72,7 @@ screen.append(inputBox);
|
||||||
inputBox.focus();
|
inputBox.focus();
|
||||||
|
|
||||||
let chatHistory = [];
|
let chatHistory = [];
|
||||||
|
let conversationHistory = [];
|
||||||
let currentStreamMessage = "";
|
let currentStreamMessage = "";
|
||||||
|
|
||||||
function addMessage(role, content) {
|
function addMessage(role, content) {
|
||||||
|
|
@ -152,12 +153,21 @@ function finalizeStreamMessage() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let assistantMessage;
|
||||||
if (letterIndex !== -1) {
|
if (letterIndex !== -1) {
|
||||||
const messageContent = currentStreamMessage.substring(letterIndex).trim();
|
assistantMessage = currentStreamMessage.substring(letterIndex).trim();
|
||||||
addMessage(assistantname, messageContent);
|
addMessage(assistantname, assistantMessage);
|
||||||
} else {
|
} else {
|
||||||
|
assistantMessage = currentStreamMessage;
|
||||||
addMessage(assistantname, currentStreamMessage);
|
addMessage(assistantname, currentStreamMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add assistant message to conversation history
|
||||||
|
conversationHistory.push({
|
||||||
|
role: "assistant",
|
||||||
|
content: assistantMessage,
|
||||||
|
});
|
||||||
|
|
||||||
currentStreamMessage = "";
|
currentStreamMessage = "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -176,67 +186,40 @@ function setFaceBoxContent(content) {
|
||||||
|
|
||||||
async function sendMessage(message) {
|
async function sendMessage(message) {
|
||||||
if (!message.trim()) return;
|
if (!message.trim()) return;
|
||||||
//if (message.startsWith("!")) {
|
|
||||||
if (message.trim().startsWith("l!")) {
|
|
||||||
// command handler (if command, dont tell ollama about it, just handle it right here) -- THIS DOES NOT WORK AS OF NOW
|
|
||||||
const commandParts = message.slice(2).split(" ");
|
|
||||||
const command = commandParts[0];
|
|
||||||
const args = commandParts.slice(1).join(" ");
|
|
||||||
|
|
||||||
switch (command) {
|
addMessage(username, message);
|
||||||
case "help":
|
|
||||||
chatBox.setContent(
|
|
||||||
"available commands:\nl!help - show this help message\nl!face <text> - set face to text",
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
case "clear":
|
|
||||||
chatHistory = [];
|
|
||||||
chatBox.setContent("");
|
|
||||||
break;
|
|
||||||
case "face":
|
|
||||||
if (args.trim()) {
|
|
||||||
setFaceBoxContent(args.trim());
|
|
||||||
} else {
|
|
||||||
setFaceBoxContent("=w=");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
chatBox.setContent(`unknown command: ${command}`);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// if not command, pass to ollama
|
|
||||||
addMessage(username, message);
|
|
||||||
|
|
||||||
try {
|
// Add user message to conversation history
|
||||||
currentStreamMessage = "";
|
conversationHistory.push({
|
||||||
|
role: "user",
|
||||||
|
content: message,
|
||||||
|
});
|
||||||
|
|
||||||
const response = await ollama.chat({
|
try {
|
||||||
model: assistantmodel,
|
currentStreamMessage = "";
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
role: "system",
|
|
||||||
content: systemprompt,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
role: "user",
|
|
||||||
content: message,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
stream: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
for await (const part of response) {
|
const response = await ollama.chat({
|
||||||
if (part.message && part.message.content) {
|
model: assistantmodel,
|
||||||
currentStreamMessage += part.message.content;
|
messages: [
|
||||||
updateStreamMessage(currentStreamMessage);
|
{
|
||||||
}
|
role: "system",
|
||||||
|
content: systemprompt,
|
||||||
|
},
|
||||||
|
...conversationHistory,
|
||||||
|
],
|
||||||
|
stream: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
for await (const part of response) {
|
||||||
|
if (part.message && part.message.content) {
|
||||||
|
currentStreamMessage += part.message.content;
|
||||||
|
updateStreamMessage(currentStreamMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
finalizeStreamMessage();
|
|
||||||
} catch (error) {
|
|
||||||
addMessage(assistantname, `Failed to get response: ${error.message}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
finalizeStreamMessage();
|
||||||
|
} catch (error) {
|
||||||
|
addMessage(assistantname, `Failed to get response: ${error.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -244,7 +227,41 @@ inputBox.on("submit", async (text) => {
|
||||||
if (text.trim()) {
|
if (text.trim()) {
|
||||||
inputBox.clearValue();
|
inputBox.clearValue();
|
||||||
inputBox.focus();
|
inputBox.focus();
|
||||||
await sendMessage(text);
|
|
||||||
|
// Handle commands
|
||||||
|
if (text.trim().startsWith("l!")) {
|
||||||
|
const commandParts = text.trim().slice(2).split(" ");
|
||||||
|
const command = commandParts[0];
|
||||||
|
const args = commandParts.slice(1).join(" ");
|
||||||
|
|
||||||
|
switch (command) {
|
||||||
|
case "help":
|
||||||
|
addMessage(
|
||||||
|
assistantname,
|
||||||
|
"available commands:\nl!help - if you wanna know what i can do, run this\nl!clear - clear chat history, if you want me to forget everything, just run this!\nl!face <text> - if you want to force my expression, here you go! not sure i'll be too happy about it though.",
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "clear":
|
||||||
|
chatHistory = [];
|
||||||
|
conversationHistory = [];
|
||||||
|
chatBox.setContent("");
|
||||||
|
screen.render();
|
||||||
|
break;
|
||||||
|
case "face":
|
||||||
|
if (args.trim()) {
|
||||||
|
setFaceBoxContent(args.trim());
|
||||||
|
} else {
|
||||||
|
setFaceBoxContent("=w=");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
addMessage(assistantname, `unknown command: ${command}`);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Regular message - send to ollama
|
||||||
|
await sendMessage(text);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue