From 5b5491ca876e543d6b89c1ae9c91ca681953c0af Mon Sep 17 00:00:00 2001 From: bracesproul Date: Mon, 19 Aug 2024 14:49:18 -0700 Subject: [PATCH 1/3] docs[minor]: Add doc section on structured tool params --- docs/core_docs/docs/how_to/custom_tools.ipynb | 53 +++++++++++++++++-- langchain-core/src/tools/index.ts | 6 +++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/docs/core_docs/docs/how_to/custom_tools.ipynb b/docs/core_docs/docs/how_to/custom_tools.ipynb index 0d38c5d92d6a..45bd62db8001 100644 --- a/docs/core_docs/docs/how_to/custom_tools.ipynb +++ b/docs/core_docs/docs/how_to/custom_tools.ipynb @@ -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." + ] + }, + { + "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", + "}" ] }, { @@ -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", @@ -345,9 +388,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Deno", + "display_name": "TypeScript", "language": "typescript", - "name": "deno" + "name": "tslab" }, "language_info": { "file_extension": ".ts", diff --git a/langchain-core/src/tools/index.ts b/langchain-core/src/tools/index.ts index a2000a24b21d..fd6c6e8861e2 100644 --- a/langchain-core/src/tools/index.ts +++ b/langchain-core/src/tools/index.ts @@ -48,6 +48,12 @@ export interface ToolParams extends BaseLangChainParams { responseFormat?: ResponseFormat; } + +/** + * Schema for defining tools. + * + * @version 0.2.19 + */ export interface StructuredToolParams extends Pick { /** From c87877995ca4bdf9bf096ecba01f550e303ef626 Mon Sep 17 00:00:00 2001 From: bracesproul Date: Mon, 19 Aug 2024 14:49:37 -0700 Subject: [PATCH 2/3] chore: lint files --- langchain-core/src/tools/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/langchain-core/src/tools/index.ts b/langchain-core/src/tools/index.ts index fd6c6e8861e2..7146e2f36df1 100644 --- a/langchain-core/src/tools/index.ts +++ b/langchain-core/src/tools/index.ts @@ -48,10 +48,9 @@ export interface ToolParams extends BaseLangChainParams { responseFormat?: ResponseFormat; } - /** * Schema for defining tools. - * + * * @version 0.2.19 */ export interface StructuredToolParams From e8f172d9f959741eb25d48144598d4a50d3bed8d Mon Sep 17 00:00:00 2001 From: Brace Sproul Date: Mon, 19 Aug 2024 15:27:43 -0700 Subject: [PATCH 3/3] Update docs/core_docs/docs/how_to/custom_tools.ipynb --- docs/core_docs/docs/how_to/custom_tools.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core_docs/docs/how_to/custom_tools.ipynb b/docs/core_docs/docs/how_to/custom_tools.ipynb index 45bd62db8001..95f68259ac03 100644 --- a/docs/core_docs/docs/how_to/custom_tools.ipynb +++ b/docs/core_docs/docs/how_to/custom_tools.ipynb @@ -56,7 +56,7 @@ "- `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." + "This schema does not include a function to pair with the tool, and for this reason it should only be used in situations where the generated output does not need to be passed as the input argument to a function." ] }, {