session summarization
This commit is contained in:
@@ -9,30 +9,34 @@ const MAX_SUMMARY_TOKENS = parseInt(getEnv('SUMMARY_MAX_TOKENS', SUMMARIES.MAX_S
|
|||||||
const MIN_EPISODES_SINCE = parseInt(getEnv('SUMMARY_MIN_EPISODES', SUMMARIES.MIN_EPISODES_SINCE));
|
const MIN_EPISODES_SINCE = parseInt(getEnv('SUMMARY_MIN_EPISODES', SUMMARIES.MIN_EPISODES_SINCE));
|
||||||
|
|
||||||
function buildSummaryPrompt(episodes, existingSummary = null) {
|
function buildSummaryPrompt(episodes, existingSummary = null) {
|
||||||
const context = episodes
|
const MAX_CHARS = 3000; // truncate input to keep Phi3 focused
|
||||||
|
let context = episodes
|
||||||
.map(ep => `User: ${ep.user_message}\nAssistant: ${ep.ai_response}`)
|
.map(ep => `User: ${ep.user_message}\nAssistant: ${ep.ai_response}`)
|
||||||
.join('\n\n');
|
.join('\n\n');
|
||||||
|
|
||||||
|
// Truncate from the start if too long — keep the most recent exchanges
|
||||||
|
if (context.length > MAX_CHARS) {
|
||||||
|
context = context.slice(-MAX_CHARS);
|
||||||
|
}
|
||||||
|
|
||||||
const instruction = existingSummary
|
const instruction = existingSummary
|
||||||
? `Update this summary to include the new conversation exchanges below. Output only the updated summary — do not continue the conversation.
|
? `Update the summary below to include the new exchanges. Write 3-5 sentences in third person. Output only the updated summary text, nothing else.
|
||||||
|
|
||||||
Previous summary:
|
Previous summary:
|
||||||
${existingSummary}
|
${existingSummary}
|
||||||
|
|
||||||
New exchanges to incorporate:`
|
New exchanges:
|
||||||
: `Summarise this conversation. Output only the summary — do not continue the conversation or give recommendations.
|
${context}`
|
||||||
|
: `Summarize the conversation below in 3-5 sentences. Write in third person. Output only the summary text, nothing else.
|
||||||
|
|
||||||
Conversation:`;
|
Conversation:
|
||||||
|
${context}`;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'<|im_start|>system',
|
'<|user|>',
|
||||||
'You are a conversation summarisation assistant. You write concise, factual summaries for long-term memory storage. Output only the summary text — no preamble, no labels.<|im_end|>',
|
|
||||||
'<|im_start|>user',
|
|
||||||
instruction,
|
instruction,
|
||||||
'',
|
'<|end|>',
|
||||||
context,
|
'<|assistant|>',
|
||||||
'<|im_end|>',
|
|
||||||
'<|im_start|>assistant',
|
|
||||||
].join('\n');
|
].join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,8 +51,8 @@ async function generateSummary(episodes, existingSummary = null) {
|
|||||||
prompt,
|
prompt,
|
||||||
stream: false,
|
stream: false,
|
||||||
options: {
|
options: {
|
||||||
temperature: 0.3, // slightly higher than entities — summaries benefit from some fluency
|
temperature: 0.2, // slightly higher than entities — summaries benefit from some fluency
|
||||||
num_predict: 300, // generous but bounded — keeps summaries from running long
|
num_predict: 200, // generous but bounded — keeps summaries from running long
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user