| 495 | | 495 | |
| 496 | if (tools.length) { | 496 | if (tools.length) { |
| 497 | body.tools = tools; | 497 | body.tools = tools; |
| | 498 | |
| | 499 | const toolChoice = request.body.tool_choice; |
| | 500 | let functionCallingConfig; |
| | 501 | |
| | 502 | // Translate OpenAI's `tool_choice` to Gemini's `functionCallingConfig` |
| | 503 | if (typeof toolChoice === 'string') { |
| | 504 | switch (toolChoice) { |
| | 505 | case 'none': |
| | 506 | functionCallingConfig = { mode: 'NONE' }; |
| | 507 | break; |
| | 508 | case 'required': |
| | 509 | functionCallingConfig = { mode: 'ANY' }; |
| | 510 | break; |
| | 511 | case 'auto': |
| | 512 | functionCallingConfig = { mode: 'AUTO' }; |
| | 513 | break; |
| | 514 | } |
| | 515 | } else if (typeof toolChoice === 'object' && toolChoice?.function?.name) { |
| | 516 | // Force a specific function call |
| | 517 | functionCallingConfig = { |
| | 518 | mode: 'ANY', |
| | 519 | allowedFunctionNames: [toolChoice.function.name], |
| | 520 | }; |
| | 521 | } |
| | 522 | |
| | 523 | if (functionCallingConfig) { |
| | 524 | body.toolConfig = { functionCallingConfig }; |
| | 525 | } |
| 498 | } | 526 | } |
| 499 | | 527 | |
| 500 | return body; | 528 | return body; |