Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Dec 14, 2024
1 parent 66dd2a0 commit 3467859
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions libs/langchain-google-common/src/utils/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1020,22 +1020,24 @@ export function getGeminiAPI(config?: GeminiAPIConfig): GoogleAIAPI {
if (!tools || tools.length === 0) {
return [];
}
return tools.reduce((acc: GeminiTool[], tool) => {
if (isLangChainTool(tool)) {
if (!acc[0]) {
acc[0] = { functionDeclarations: [] };
}
if (!acc[0].functionDeclarations) {
acc[0].functionDeclarations = [];
}
acc[0].functionDeclarations.push(
structuredToolToFunctionDeclaration(tool)
);
} else {
acc.push(tool as GeminiTool);
}
return acc;
}, []) as GeminiTool[];

// Group all LangChain tools into a single functionDeclarations array
const langChainTools = tools.filter(isLangChainTool);
const otherTools = tools.filter(
(tool) => !isLangChainTool(tool)
) as GeminiTool[];

const result: GeminiTool[] = [...otherTools];

if (langChainTools.length > 0) {
result.push({
functionDeclarations: langChainTools.map(
structuredToolToFunctionDeclaration
),
});
}

return result;
}

function formatToolConfig(
Expand Down

0 comments on commit 3467859

Please sign in to comment.