Fix Claude function tools with prefills

This commit is contained in:
Cohee 2024-05-29 02:25:32 +03:00
parent 2aeaf43c28
commit 24b6f99abf
1 changed files with 4 additions and 0 deletions

View File

@ -138,6 +138,10 @@ async function sendClaudeRequest(request, response) {
requestBody.system = converted_prompt.systemPrompt; requestBody.system = converted_prompt.systemPrompt;
} }
if (Array.isArray(request.body.tools) && request.body.tools.length > 0) { if (Array.isArray(request.body.tools) && request.body.tools.length > 0) {
// Claude doesn't do prefills on function calls, and doesn't allow empty messages
if (converted_prompt.messages.length && converted_prompt.messages[converted_prompt.messages.length - 1].role === 'assistant') {
converted_prompt.messages.push({ role: 'user', content: '.' });
}
additionalHeaders['anthropic-beta'] = 'tools-2024-05-16'; additionalHeaders['anthropic-beta'] = 'tools-2024-05-16';
requestBody.tool_choice = { type: request.body.tool_choice === 'required' ? 'any' : 'auto' }; requestBody.tool_choice = { type: request.body.tool_choice === 'required' ? 'any' : 'auto' };
requestBody.tools = request.body.tools requestBody.tools = request.body.tools