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

c3b416b4cfb04883cc288d79737f6b1d8680deef

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +6 -4Showing whitespace changes
public/css/toggle-dependent.css+2 -0
@@ -474,6 +474,8 @@ label[for="trim_spaces"]:has(input:checked) i.warning {
474474
475475#claude_function_prefill_warning {
476476 display: none;
477+ color: red;
478+ font-weight: bold;
477479}
478480
479481#openai_settings:has(#openai_function_calling:checked):has(#claude_assistant_prefill:not(:placeholder-shown), #claude_assistant_impersonation:not(:placeholder-shown)) #claude_function_prefill_warning {
src/endpoints/backends/chat-completions.js+4 -4
@@ -130,10 +130,6 @@ async function sendClaudeRequest(request, response) {
130130 delete requestBody.system;
131131 }
132132 if (useTools) {
133- // Claude doesn't do prefills on function calls, and doesn't allow empty messages
134- if (convertedPrompt.messages.length && convertedPrompt.messages[convertedPrompt.messages.length - 1].role === 'assistant') {
135- convertedPrompt.messages.push({ role: 'user', content: '.' });
136- }
137133 additionalHeaders['anthropic-beta'] = 'tools-2024-05-16';
138134 requestBody.tool_choice = { type: request.body.tool_choice };
139135 requestBody.tools = request.body.tools
@@ -141,6 +137,10 @@ async function sendClaudeRequest(request, response) {
141137 .map(tool => tool.function)
142138 .map(fn => ({ name: fn.name, description: fn.description, input_schema: fn.parameters }));
143139
140+ // Claude doesn't do prefills on function calls, and doesn't allow empty messages
141+ if (requestBody.tools.length && convertedPrompt.messages.length && convertedPrompt.messages[convertedPrompt.messages.length - 1].role === 'assistant') {
142+ convertedPrompt.messages.push({ role: 'user', content: [{ type: 'text', text: '\u200b' }] });
143+ }
144144 if (enableSystemPromptCache && requestBody.tools.length) {
145145 requestBody.tools[requestBody.tools.length - 1]['cache_control'] = { type: 'ephemeral' };
146146 }