Skip to content

Commit

Permalink
fix up answers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan committed Dec 7, 2024
1 parent 2b108c7 commit b1243e4
Showing 1 changed file with 9 additions and 21 deletions.
30 changes: 9 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,20 @@ const model = await llama.loadModel({
});

// Add this function to free sequences
let context = await model.createContext();
let context = await model.createContext({
sequencesCount: 2048 // Increase this number based on your needs
});
async function recreateContext() {
await context.dispose();
context = await model.createContext();
context = await model.createContext({
sequencesCount: 2048 // Use the same value here
});
return context;
}

// Helper function to format chat messages
function formatChatMessage(nick, msg) {
return `<${nick.replace(/[\p\c]/g, '').replace(/ +/g, '_').toLowerCase()}> ${msg}`;
return `<${nick.replace(/[\p\c]/g, '').replace(/ +/g, '_').toLowerCase()}>: ${msg}`;
}

app.post('/chat', async (req, res) => {
Expand Down Expand Up @@ -92,19 +96,18 @@ app.post('/chat', async (req, res) => {

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

// Generate response
const response = await session.prompt(promptContext);

// Process response
let cleanResponse = response
.toLowerCase()
.replace(/"/g, '')
.trim()
.replace(/metaai/g, 'MetaAI')
.split('\n')[0];
.replace(/<metaai>/g, '');

// Update context
const newContext = userContexts[userId];
Expand All @@ -115,21 +118,6 @@ app.post('/chat', async (req, res) => {
newContext.push(formattedInput);
newContext.push(formatChatMessage('metaai', cleanResponse));

// Check for repetition
let repeated = 0;
for (let i = Math.floor(newContext.length / 2); i >= 1; i--) {
if (newContext[newContext.length - 1] === newContext[i * 2]) {
repeated++;
}
}

if (repeated >= STUCK_THRESHOLD) {
cleanResponse = "...i'm repeating myself, ain't i? forgetting everything...";
userContexts[userId] = [];
} else {
userContexts[userId] = newContext;
}

// Set cooldown
userCooldowns[userId] = now + COOLDOWN_TIME;

Expand Down

0 comments on commit b1243e4

Please sign in to comment.