Claude: fix prefill tool calling hack with at-depth caching

This commit is contained in:
Cohee 2024-11-19 20:48:02 +02:00
parent 67869364a5
commit c3b416b4cf
2 changed files with 6 additions and 4 deletions

View File

@ -474,6 +474,8 @@ label[for="trim_spaces"]:has(input:checked) i.warning {
#claude_function_prefill_warning {
display: none;
color: red;
font-weight: bold;
}
#openai_settings:has(#openai_function_calling:checked):has(#claude_assistant_prefill:not(:placeholder-shown), #claude_assistant_impersonation:not(:placeholder-shown)) #claude_function_prefill_warning {

View File

@ -130,10 +130,6 @@ async function sendClaudeRequest(request, response) {
delete requestBody.system;
}
if (useTools) {
// Claude doesn't do prefills on function calls, and doesn't allow empty messages
if (convertedPrompt.messages.length && convertedPrompt.messages[convertedPrompt.messages.length - 1].role === 'assistant') {
convertedPrompt.messages.push({ role: 'user', content: '.' });
}
additionalHeaders['anthropic-beta'] = 'tools-2024-05-16';
requestBody.tool_choice = { type: request.body.tool_choice };
requestBody.tools = request.body.tools
@ -141,6 +137,10 @@ async function sendClaudeRequest(request, response) {
.map(tool => tool.function)
.map(fn => ({ name: fn.name, description: fn.description, input_schema: fn.parameters }));
// Claude doesn't do prefills on function calls, and doesn't allow empty messages
if (requestBody.tools.length && convertedPrompt.messages.length && convertedPrompt.messages[convertedPrompt.messages.length - 1].role === 'assistant') {
convertedPrompt.messages.push({ role: 'user', content: [{ type: 'text', text: '\u200b' }] });
}
if (enableSystemPromptCache && requestBody.tools.length) {
requestBody.tools[requestBody.tools.length - 1]['cache_control'] = { type: 'ephemeral' };
}