|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { |
| 19 | + Content, |
| 20 | + FunctionCall, |
| 21 | + FunctionDeclaration, |
| 22 | + FunctionResponse, |
| 23 | + GenerateContentRequest, |
| 24 | + GenerateContentResult, |
| 25 | + SingleRequestOptions, |
| 26 | + Tool, |
| 27 | +} from '../types'; |
| 28 | +import { ApiSettings } from '../types/internal'; |
| 29 | +import { generateContent } from './generate-content'; |
| 30 | + |
| 31 | +const DEFAULT_MAX_SEQUENTIAL_FUNCTION_CALLS = 10; |
| 32 | + |
| 33 | +export interface AutomaticFunctionCallingResult { |
| 34 | + result: GenerateContentResult; |
| 35 | + addedContents: Content[]; |
| 36 | +} |
| 37 | + |
| 38 | +export async function generateContentWithAutomaticFunctionCalling( |
| 39 | + apiSettings: ApiSettings, |
| 40 | + model: string, |
| 41 | + params: GenerateContentRequest, |
| 42 | + result: GenerateContentResult, |
| 43 | + requestOptions?: SingleRequestOptions, |
| 44 | +): Promise<AutomaticFunctionCallingResult> { |
| 45 | + let remainingFunctionCalls = |
| 46 | + requestOptions?.maxSequentialFunctionCalls ?? DEFAULT_MAX_SEQUENTIAL_FUNCTION_CALLS; |
| 47 | + let currentParams = params; |
| 48 | + let currentResult = result; |
| 49 | + const addedContents: Content[] = []; |
| 50 | + |
| 51 | + while (remainingFunctionCalls > 0) { |
| 52 | + const functionCalls = currentResult.response.functionCalls?.(); |
| 53 | + if (!functionCalls?.length) { |
| 54 | + return { result: currentResult, addedContents }; |
| 55 | + } |
| 56 | + |
| 57 | + const functionResponses = await callFunctionReferences(currentParams.tools, functionCalls); |
| 58 | + if (!functionResponses) { |
| 59 | + return { result: currentResult, addedContents }; |
| 60 | + } |
| 61 | + |
| 62 | + const responseContent = getModelResponseContent(currentResult); |
| 63 | + if (!responseContent) { |
| 64 | + return { result: currentResult, addedContents }; |
| 65 | + } |
| 66 | + |
| 67 | + remainingFunctionCalls -= 1; |
| 68 | + const functionResponseContent: Content = { |
| 69 | + role: 'function', |
| 70 | + parts: functionResponses.map(functionResponse => ({ functionResponse })), |
| 71 | + }; |
| 72 | + addedContents.push(responseContent, functionResponseContent); |
| 73 | + currentParams = { |
| 74 | + ...currentParams, |
| 75 | + contents: [...currentParams.contents, responseContent, functionResponseContent], |
| 76 | + }; |
| 77 | + currentResult = await generateContent(apiSettings, model, currentParams, requestOptions); |
| 78 | + } |
| 79 | + |
| 80 | + return { result: currentResult, addedContents }; |
| 81 | +} |
| 82 | + |
| 83 | +function getModelResponseContent(result: GenerateContentResult): Content | undefined { |
| 84 | + const responseContent = result.response.candidates?.[0]?.content; |
| 85 | + if (!responseContent) { |
| 86 | + return undefined; |
| 87 | + } |
| 88 | + return { |
| 89 | + parts: responseContent.parts || [], |
| 90 | + role: responseContent.role || 'model', |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +async function callFunctionReferences( |
| 95 | + tools: Tool[] | undefined, |
| 96 | + functionCalls: FunctionCall[], |
| 97 | +): Promise<FunctionResponse[] | undefined> { |
| 98 | + const declarations = getFunctionDeclarationsWithReferences(tools); |
| 99 | + if (!declarations.length) { |
| 100 | + return undefined; |
| 101 | + } |
| 102 | + |
| 103 | + const functionResponses: FunctionResponse[] = []; |
| 104 | + for (const functionCall of functionCalls) { |
| 105 | + const declaration = declarations.find(candidate => candidate.name === functionCall.name); |
| 106 | + if (!declaration?.functionReference) { |
| 107 | + return undefined; |
| 108 | + } |
| 109 | + |
| 110 | + const response = (await declaration.functionReference(functionCall.args)) as object; |
| 111 | + functionResponses.push({ |
| 112 | + id: functionCall.id, |
| 113 | + name: functionCall.name, |
| 114 | + response, |
| 115 | + }); |
| 116 | + } |
| 117 | + return functionResponses; |
| 118 | +} |
| 119 | + |
| 120 | +function getFunctionDeclarationsWithReferences(tools: Tool[] | undefined): FunctionDeclaration[] { |
| 121 | + return ( |
| 122 | + tools?.flatMap(tool => |
| 123 | + 'functionDeclarations' in tool |
| 124 | + ? (tool.functionDeclarations?.filter(declaration => declaration.functionReference) ?? []) |
| 125 | + : [], |
| 126 | + ) ?? [] |
| 127 | + ); |
| 128 | +} |
0 commit comments