Skip to content

Commit

Permalink
handle context sequence exhaustion
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan committed Dec 7, 2024
1 parent 483c96d commit 02bd0e5
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ const model = await llama.loadModel({
});
const context = await model.createContext();

// Add this function to free sequences
async function recreateContext() {
await context.free();
return await model.createContext();
}

// Helper function to format chat messages
function formatChatMessage(nick, msg) {
return `<${nick.replace(/[\p\c]/g, '').replace(/ +/g, '_').toLowerCase()}> ${msg}`;
Expand Down Expand Up @@ -70,8 +76,21 @@ app.post('/chat', async (req, res) => {
const randomPrompt = PROMPTS[Math.floor(Math.random() * PROMPTS.length)];
const systemPrompt = `${INTRODUCTION} ${randomPrompt}`;

let contextSequence;
try {
contextSequence = context.getSequence();
} catch (error) {
if (error.message === 'No sequences left') {
// Recreate context if we run out of sequences
await recreateContext();
contextSequence = context.getSequence();
} else {
throw error;
}
}

const session = new LlamaChatSession({
contextSequence: context.getSequence(),
contextSequence: contextSequence,
systemPrompt: systemPrompt
});

Expand Down

0 comments on commit 02bd0e5

Please sign in to comment.