Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs[minor]: Add doc section on structured tool params #6571

Merged
merged 3 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 48 additions & 5 deletions docs/core_docs/docs/how_to/custom_tools.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,51 @@
"\n",
"When constructing your own agent, you will need to provide it with a list of Tools that it can use. While LangChain includes some prebuilt tools, it can often be more useful to use tools that use custom logic. This guide will walk you through some ways you can create custom tools.\n",
"\n",
"The biggest difference here is that the first function requires an object with multiple input fields, while the second one only accepts an object with a single field. Some older agents only work with functions that require single inputs, so it's important to understand the distinction."
"The biggest difference here is that the first function requires an object with multiple input fields, while the second one only accepts an object with a single field. Some older agents only work with functions that require single inputs, so it's important to understand the distinction.\n",
"\n",
"LangChain has a handful of ways to construct tools for different applications. Below I'll show the two most common ways to create tools, and where you might use each."
]
},
{
"cell_type": "markdown",
"id": "82bb159d",
"metadata": {},
"source": [
"## Tool schema\n",
"\n",
"```{=mdx}\n",
":::caution Compatibility\n",
"Only available in `@langchain/core` version 0.2.19 and above.\n",
":::\n",
"```\n",
"\n",
"The simplest way to create a tool is through the [`StructuredToolParams`](https://api.js.langchain.com/interfaces/_langchain_core.tools.StructuredToolParams.html) schema. Every chat model which supports tool calling in LangChain accepts binding tools to the model through this schema. This schema has only three fields\n",
"\n",
"- `name` - The name of the tool.\n",
"- `schema` - The schema of the tool, defined with a Zod object.\n",
"- `description` (optional) - A description of the tool.\n",
"\n",
"This schema does not include a function to pair with the tool, and for this reason it should only be used in situations where you want to generate structured output using an LLM."
bracesproul marked this conversation as resolved.
Show resolved Hide resolved
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4d129789",
"metadata": {},
"outputs": [],
"source": [
"import { z } from \"zod\";\n",
"import { StructuredToolParams } from \"@langchain/core/tools\";\n",
"\n",
"const simpleToolSchema: StructuredToolParams = {\n",
" name: \"get_current_weather\",\n",
" description: \"Get the current weather for a location\",\n",
" schema: z.object({\n",
" city: z.string().describe(\"The city to get the weather for\"),\n",
" state: z.string().optional().describe(\"The state to get the weather for\"),\n",
" })\n",
"}"
]
},
{
Expand All @@ -48,8 +92,7 @@
":::\n",
"```\n",
"\n",
"\n",
"The [`tool`](https://api.js.langchain.com/classes/langchain_core.tools.Tool.html) wrapper function is a convenience method for turning a JavaScript function into a tool. It requires the function itself along with some additional arguments that define your tool. The most important are:\n",
"The [`tool`](https://api.js.langchain.com/classes/langchain_core.tools.Tool.html) wrapper function is a convenience method for turning a JavaScript function into a tool. It requires the function itself along with some additional arguments that define your tool. You should use this over `StructuredToolParams` tools when the resulting tool call executes a function. The most important are:\n",
"\n",
"- The tool's `name`, which the LLM will use as context as well as to reference the tool\n",
"- An optional, but recommended `description`, which the LLM will use as context to know when to use the tool\n",
Expand Down Expand Up @@ -345,9 +388,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Deno",
"display_name": "TypeScript",
"language": "typescript",
"name": "deno"
"name": "tslab"
},
"language_info": {
"file_extension": ".ts",
Expand Down
5 changes: 5 additions & 0 deletions langchain-core/src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export interface ToolParams extends BaseLangChainParams {
responseFormat?: ResponseFormat;
}

/**
* Schema for defining tools.
*
* @version 0.2.19
*/
export interface StructuredToolParams
extends Pick<StructuredToolInterface, "name" | "schema"> {
/**
Expand Down
Loading