Skip to content

Commit

Permalink
add cache control to claude
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaki-1052 committed Jan 3, 2025
1 parent 37f67fb commit d0cb73c
Showing 1 changed file with 63 additions and 34 deletions.
97 changes: 63 additions & 34 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -771,40 +771,18 @@ const savedHistory = chatHistory.map(entry => {

// Function to get a unique file name
function getUniqueFilePath(basePath, baseTitle) {
// Function to sanitize the title and remove invalid characters
function sanitizeTitle(title) {
// Replace invalid characters with underscores
// This regex removes characters that are typically problematic in filenames
const sanitized = title
.replace(/[<>:"/\\|?*\x00-\x1F]/g, '_') // Remove invalid Windows/Unix chars
.replace(/[\s]+/g, '_') // Replace spaces with underscore
.replace(/[^a-zA-Z0-9._-#@]/g, '_') // Replace any other non-alphanumeric chars
.trim();

// If sanitization results in empty string or only special chars, return default
return sanitized && sanitized !== '_' ? sanitized : 'Conversation_History';
}

try {
// Sanitize the base title
const safeTitle = sanitizeTitle(baseTitle);
let counter = 1;
let fileName = `${safeTitle}.txt`;
let filePath = path.join(basePath, fileName);

// Keep checking until we find a filename that doesn't exist
while (fs.existsSync(filePath)) {
counter++;
fileName = `${safeTitle}-${counter}.txt`;
filePath = path.join(basePath, fileName);
}
let counter = 1;
let fileName = `${baseTitle}.txt`;
let filePath = path.join(basePath, fileName);

return filePath;
} catch (error) {
// If any error occurs, return a safe default path
console.error('Error generating file path:', error);
return path.join(basePath, 'Conversation_History.txt');
// Keep checking until we find a filename that doesn't exist
while (fs.existsSync(filePath)) {
counter++;
fileName = `${baseTitle}-${counter}.txt`;
filePath = path.join(basePath, fileName);
}

return filePath;
}

let summary = '';
Expand Down Expand Up @@ -1893,6 +1871,48 @@ console.log(googleModel);

// See closed issue on this repo for more details.

// formatting claude array

function parseInstructionsIntoSections(instructionsText) {
// Find the major section boundaries
const roleAssignmentEnd = instructionsText.indexOf('</role_assignment>') + '</role_assignment>'.length;
const claudeInfoStart = instructionsText.indexOf('<claude_info>');
const claudeInfoEnd = instructionsText.indexOf('</claude_info>') + '</claude_info>'.length;
const taskInstructionsStart = instructionsText.indexOf('<task_instructions>');
const methodsEnd = instructionsText.indexOf('</methods>') + '</methods>'.length;
const finalStart = instructionsText.indexOf('<final>');

// Create the four main sections
const sections = [
{
// Section 1: From <instructions> through </role_assignment>
content: instructionsText.substring(0, roleAssignmentEnd)
},
{
// Section 2: <claude_info> section
content: instructionsText.substring(claudeInfoStart, claudeInfoEnd)
},
{
// Section 3: From <task_instructions> through </methods>
content: instructionsText.substring(taskInstructionsStart, methodsEnd)
},
{
// Section 4: From <final> through </instructions>
content: instructionsText.substring(finalStart)
}
];

return sections;
}

function formatSectionsIntoSystemMessage(sections) {
return sections.map(section => ({
type: "text",
text: section.content,
cache_control: { type: "ephemeral" }
}));
}


// Handle POST request to '/message'

Expand Down Expand Up @@ -1981,10 +2001,19 @@ if (modelID.startsWith('gpt') || modelID.startsWith('claude')) {
systemMessage = await initializeConversationHistory();
epochs = epochs + 1;
} else if (modelID.startsWith('claude')) {
systemMessage = await initializeClaudeInstructions();
// Get the instructions text first
const instructionsText = await initializeClaudeInstructions();

// Parse the instructions into sections
const sections = parseInstructionsIntoSections(instructionsText);

// Format the sections into the system message array
systemMessage = formatSectionsIntoSystemMessage(sections);

epochs = epochs + 1;
}
}



// Add text content if present
Expand Down Expand Up @@ -2211,7 +2240,7 @@ if (modelID.startsWith('llama-3.1')) {
model: modelID,
max_tokens: 4000,
temperature: temperature,
system: claudeInstructions,
system: systemMessage,
messages: claudeHistory,
};
headers = {
Expand Down

0 comments on commit d0cb73c

Please sign in to comment.