Skip to content

Commit 02bd0e5

Browse files
author
Ryan
committed
handle context sequence exhaustion
1 parent 483c96d commit 02bd0e5

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ const model = await llama.loadModel({
3333
});
3434
const context = await model.createContext();
3535

36+
// Add this function to free sequences
37+
async function recreateContext() {
38+
await context.free();
39+
return await model.createContext();
40+
}
41+
3642
// Helper function to format chat messages
3743
function formatChatMessage(nick, msg) {
3844
return `<${nick.replace(/[\p\c]/g, '').replace(/ +/g, '_').toLowerCase()}> ${msg}`;
@@ -70,8 +76,21 @@ app.post('/chat', async (req, res) => {
7076
const randomPrompt = PROMPTS[Math.floor(Math.random() * PROMPTS.length)];
7177
const systemPrompt = `${INTRODUCTION} ${randomPrompt}`;
7278

79+
let contextSequence;
80+
try {
81+
contextSequence = context.getSequence();
82+
} catch (error) {
83+
if (error.message === 'No sequences left') {
84+
// Recreate context if we run out of sequences
85+
await recreateContext();
86+
contextSequence = context.getSequence();
87+
} else {
88+
throw error;
89+
}
90+
}
91+
7392
const session = new LlamaChatSession({
74-
contextSequence: context.getSequence(),
93+
contextSequence: contextSequence,
7594
systemPrompt: systemPrompt
7695
});
7796

0 commit comments

Comments
 (0)