Skip to content

Commit

Permalink
Fix for multiple function tools
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Dec 14, 2024
1 parent 8b6ddc7 commit 9756085
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
63 changes: 34 additions & 29 deletions libs/langchain-google-common/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,37 +63,42 @@ function processToolChoice(

export function convertToGeminiTools(tools: GoogleAIToolType[]): GeminiTool[] {
const geminiTools: GeminiTool[] = [];
let functionDeclarationsIndex = -1;
tools.forEach((tool) => {
if (
"functionDeclarations" in tool &&
Array.isArray(tool.functionDeclarations)
) {
const funcs: GeminiFunctionDeclaration[] = tool.functionDeclarations;
geminiTools.push({ functionDeclarations: [...funcs] });
} else if (isLangChainTool(tool)) {
const jsonSchema = zodToGeminiParameters(tool.schema);
geminiTools.push({
functionDeclarations: [
{
name: tool.name,
description: tool.description ?? `A function available to call.`,
parameters: jsonSchema as GeminiFunctionSchema,
},
],
});
} else if (isOpenAITool(tool)) {
geminiTools.push({
functionDeclarations: [
{
name: tool.function.name,
description:
tool.function.description ?? `A function available to call.`,
parameters: jsonSchemaToGeminiParameters(tool.function.parameters),
},
],
});
} else if ("googleSearchRetrieval" in tool || "retrieval" in tool) {
if ("googleSearchRetrieval" in tool || "retrieval" in tool) {
geminiTools.push(tool);
} else {
if (functionDeclarationsIndex === -1) {
geminiTools.push({
functionDeclarations: [],
});
functionDeclarationsIndex = geminiTools.length - 1;
}
if (
"functionDeclarations" in tool &&
Array.isArray(tool.functionDeclarations)
) {
const funcs: GeminiFunctionDeclaration[] = tool.functionDeclarations;
geminiTools[functionDeclarationsIndex].functionDeclarations!.push(
...funcs
);
} else if (isLangChainTool(tool)) {
const jsonSchema = zodToGeminiParameters(tool.schema);
geminiTools[functionDeclarationsIndex].functionDeclarations!.push({
name: tool.name,
description: tool.description ?? `A function available to call.`,
parameters: jsonSchema as GeminiFunctionSchema,
});
} else if (isOpenAITool(tool)) {
geminiTools[functionDeclarationsIndex].functionDeclarations!.push({
name: tool.function.name,
description:
tool.function.description ?? `A function available to call.`,
parameters: jsonSchemaToGeminiParameters(tool.function.parameters),
});
} else {
throw new Error(`Received invalid tool: ${JSON.stringify(tool)}`);
}
}
});
return geminiTools;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ describe("GAuth Anthropic Chat", () => {
console.log(aiMessage.lc_kwargs);
});

test("stream", async () => {
test.only("stream", async () => {
const model = new ChatVertexAI({
modelName,
callbacks,
Expand Down

0 comments on commit 9756085

Please sign in to comment.