-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
google-genai[minor]: Add tool calling support (#5507)
* google-genai[minor]: Add tool calling support * cr * chore: lint files * fixes * docs and build error fixes * dont set name in response * add ls links * cr * Multi modal docs * :cr * fixup multi modal tool calls * cr
- Loading branch information
1 parent
4482001
commit d574ca4
Showing
11 changed files
with
797 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { StructuredTool } from "@langchain/core/tools"; | ||
import { ChatGoogleGenerativeAI } from "@langchain/google-genai"; | ||
import { z } from "zod"; | ||
|
||
const model = new ChatGoogleGenerativeAI({ | ||
model: "gemini-pro", | ||
}); | ||
|
||
// Define your tool | ||
class FakeBrowserTool extends StructuredTool { | ||
schema = z.object({ | ||
url: z.string(), | ||
query: z.string().optional(), | ||
}); | ||
|
||
name = "fake_browser_tool"; | ||
|
||
description = | ||
"useful for when you need to find something on the web or summarize a webpage."; | ||
|
||
async _call(_: z.infer<this["schema"]>): Promise<string> { | ||
return "fake_browser_tool"; | ||
} | ||
} | ||
|
||
// Bind your tools to the model | ||
const modelWithTools = model.bind({ | ||
tools: [new FakeBrowserTool()], | ||
}); | ||
// Or, you can use `.bindTools` which works the same under the hood | ||
// const modelWithTools = model.bindTools([new FakeBrowserTool()]); | ||
|
||
const res = await modelWithTools.invoke([ | ||
[ | ||
"human", | ||
"Search the web and tell me what the weather will be like tonight in new york. use a popular weather website", | ||
], | ||
]); | ||
|
||
console.log(res.tool_calls); | ||
|
||
/* | ||
[ | ||
{ | ||
name: 'fake_browser_tool', | ||
args: { | ||
query: 'weather in new york', | ||
url: 'https://www.google.com/search?q=weather+in+new+york' | ||
} | ||
} | ||
] | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { StructuredTool } from "@langchain/core/tools"; | ||
import { ChatGoogleGenerativeAI } from "@langchain/google-genai"; | ||
import { z } from "zod"; | ||
|
||
const model = new ChatGoogleGenerativeAI({ | ||
model: "gemini-pro", | ||
}); | ||
|
||
// Define your tool | ||
class FakeBrowserTool extends StructuredTool { | ||
schema = z.object({ | ||
url: z.string(), | ||
query: z.string().optional(), | ||
}); | ||
|
||
name = "fake_browser_tool"; | ||
|
||
description = | ||
"useful for when you need to find something on the web or summarize a webpage."; | ||
|
||
async _call(_: z.infer<this["schema"]>): Promise<string> { | ||
return "fake_browser_tool"; | ||
} | ||
} | ||
const tool = new FakeBrowserTool(); | ||
|
||
// Bind your tools to the model | ||
const modelWithTools = model.withStructuredOutput(tool.schema, { | ||
name: tool.name, // this is optional | ||
}); | ||
// Optionally, you can pass just a Zod schema, or JSONified Zod schema | ||
// const modelWithTools = model.withStructuredOutput( | ||
// zodSchema, | ||
// ); | ||
|
||
const res = await modelWithTools.invoke([ | ||
[ | ||
"human", | ||
"Search the web and tell me what the weather will be like tonight in new york. use a popular weather website", | ||
], | ||
]); | ||
|
||
console.log(res); | ||
/* | ||
{ | ||
url: 'https://www.accuweather.com/en/us/new-york-ny/10007/night-weather-forecast/349014', | ||
query: 'weather tonight' | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.