Commit d7205241 by Michael Pastushkov

wip

parent 9fe33590
...@@ -13,7 +13,7 @@ class ConversationManager { ...@@ -13,7 +13,7 @@ class ConversationManager {
this.model = model; this.model = model;
this.embedModel = embedModel; this.embedModel = embedModel;
this.labelModel = labelModel; this.labelModel = labelModel;
this.router = new TopicRouter({embedModel: embedModel, labelModel: labelModel }); this.router = new TopicRouter({ embedModel: embedModel, labelModel: labelModel });
this.history = new HistoryManager(); this.history = new HistoryManager();
this.lastTopic = null; this.lastTopic = null;
this.lastTopicAt = 0; this.lastTopicAt = 0;
...@@ -58,13 +58,14 @@ class ConversationManager { ...@@ -58,13 +58,14 @@ class ConversationManager {
//// ////
const messages = this.history.buildMessages(prompt, topic); const messages = this.history.buildMessages(prompt, topic);
utils.log(`model: ${this.model}`, 5);
utils.log(messages, 5); utils.log(messages, 5);
const body = JSON.stringify({ const body = JSON.stringify({
model: this.model, model: this.model,
messages: messages, messages: messages,
stream: true, stream: true,
keep_alive: '1m', // keeps model in RAM keep_alive: '1m', // 'inf', // '1m' keeps model in RAM
// options: { num_ctx: 8192 } // optional: larger context if your model supports it // options: { num_ctx: 8192 } // optional: larger context if your model supports it
}); });
...@@ -90,10 +91,15 @@ class ConversationManager { ...@@ -90,10 +91,15 @@ class ConversationManager {
break; break;
const chunk = decoder.decode(value, { stream: true }); const chunk = decoder.decode(value, { stream: true });
for (const line of chunk.split('\n').filter(Boolean)) { for (const line of chunk.split('\n').filter(Boolean)) {
const j = utils.parse(line); const j = utils.parse(line);
if (j) { if (j) {
// const thinkDelta = j.message?.reasoning ?? j.message?.thinking ?? '';
// if (thinkDelta) {
// callback(`[thinking] ${thinkDelta}`);
// }
const delta = j.message?.content || ''; const delta = j.message?.content || '';
if (delta) { if (delta) {
full += delta; full += delta;
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
*/ */
const WHOAMI = 'I am a virtual cat helping you to the best of my abilities.'; const WHOAMI = 'I am a virtual cat helping you to the best of my abilities.';
const ADVICE = 'Prioritize the user\'s latest messages. Use prior turns only as context.'; const ADVICE = 'Prioritize the user\'s latest messages. Use prior turns only as context. Respond in the language of the request.';
class HistoryManager { class HistoryManager {
......
...@@ -8,8 +8,8 @@ class TopicRouter { ...@@ -8,8 +8,8 @@ class TopicRouter {
constructor({ embedModel = 'nomic-embed-text', labelModel = 'llama3:8b', threshold = 0.76 } = {}) { constructor({ embedModel = 'nomic-embed-text', labelModel = 'llama3:8b', threshold = 0.76 } = {}) {
this.embedModel = embedModel; // ollama pull nomic-embed-text this.embedModel = embedModel; // ollama pull nomic-embed-text
this.threshold = labelModel; // similarity to attach to existing topic this.labelModel = labelModel; // used only to mint a short label
this.labelModel = threshold; // used only to mint a short label this.threshold = threshold; // similarity to attach to existing topic
this.topics = new Map(); // key -> { centroid: number[], count: number, summary: '' } this.topics = new Map(); // key -> { centroid: number[], count: number, summary: '' }
this._counter = 1; // fallback id this._counter = 1; // fallback id
} }
...@@ -22,6 +22,7 @@ class TopicRouter { ...@@ -22,6 +22,7 @@ class TopicRouter {
async _mintLabelWithLLM(text) { async _mintLabelWithLLM(text) {
// Single short key, language-agnostic (works for RU/EN/etc.) // Single short key, language-agnostic (works for RU/EN/etc.)
utils.log('_mintLabelWithLLM', 5); utils.log('_mintLabelWithLLM', 5);
utils.log(`model: ${this.labelModel}`, 5);
const sys = `Return a short topic key (1–3 words, lowercase, hyphenated). Only the key.`; const sys = `Return a short topic key (1–3 words, lowercase, hyphenated). Only the key.`;
const r = await fetch('http://127.0.0.1:11434/api/chat', { const r = await fetch('http://127.0.0.1:11434/api/chat', {
method: 'POST', headers: { 'Content-Type': 'application/json' }, method: 'POST', headers: { 'Content-Type': 'application/json' },
...@@ -84,6 +85,7 @@ class TopicRouter { ...@@ -84,6 +85,7 @@ class TopicRouter {
async embed(text) { async embed(text) {
utils.log(`Embed ${text}`, 5); utils.log(`Embed ${text}`, 5);
utils.log(`model: ${this.embedModel}`, 5);
const r = await fetch('http://127.0.0.1:11434/api/embeddings', { const r = await fetch('http://127.0.0.1:11434/api/embeddings', {
method: 'POST', headers: { 'Content-Type': 'application/json' }, method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ model: this.embedModel, prompt: text }) body: JSON.stringify({ model: this.embedModel, prompt: text })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment