From 82cecaee82f0ef42650e6201410b7b4cceb16115 Mon Sep 17 00:00:00 2001 From: Kashif Minhaj Date: Fri, 20 Dec 2024 03:57:38 +0530 Subject: [PATCH] fix(google-common): add tool_use param when using structured outputs (#7400) --- libs/langchain-google-common/src/chat_models.ts | 1 + libs/langchain-google-common/src/utils/gemini.ts | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/libs/langchain-google-common/src/chat_models.ts b/libs/langchain-google-common/src/chat_models.ts index 1de5280fbe72..7d476b94cf2c 100644 --- a/libs/langchain-google-common/src/chat_models.ts +++ b/libs/langchain-google-common/src/chat_models.ts @@ -469,6 +469,7 @@ export abstract class ChatGoogleBase } const llm = this.bind({ tools, + tool_choice: functionName, }); if (!includeRaw) { diff --git a/libs/langchain-google-common/src/utils/gemini.ts b/libs/langchain-google-common/src/utils/gemini.ts index 48bd41fb5c2f..23c46f3783db 100644 --- a/libs/langchain-google-common/src/utils/gemini.ts +++ b/libs/langchain-google-common/src/utils/gemini.ts @@ -1047,10 +1047,20 @@ export function getGeminiAPI(config?: GeminiAPIConfig): GoogleAIAPI { return undefined; } + if (["auto", "any", "none"].includes(parameters.tool_choice)) { + return { + functionCallingConfig: { + mode: parameters.tool_choice as "auto" | "any" | "none", + allowedFunctionNames: parameters.allowed_function_names, + }, + }; + } + + // force tool choice to be a single function name in case of structured output return { functionCallingConfig: { - mode: parameters.tool_choice as "auto" | "any" | "none", - allowedFunctionNames: parameters.allowed_function_names, + mode: "any", + allowedFunctionNames: [parameters.tool_choice], }, }; }