diff --git a/docs/core_docs/docs/how_to/graph_semantic.ipynb b/docs/core_docs/docs/how_to/graph_semantic.ipynb index 36f335e304b4..a8c619c944d2 100644 --- a/docs/core_docs/docs/how_to/graph_semantic.ipynb +++ b/docs/core_docs/docs/how_to/graph_semantic.ipynb @@ -180,24 +180,18 @@ "metadata": {}, "outputs": [], "source": [ - "import { StructuredTool } from \"@langchain/core/tools\";\n", + "import { tool } from \"@langchain/core/tools\";\n", "import { z } from \"zod\";\n", "\n", - "const informationInput = z.object({\n", - " entity: z.string().describe(\"movie or a person mentioned in the question\"),\n", - "});\n", - "\n", - "class InformationTool extends StructuredTool {\n", - " schema = informationInput;\n", - "\n", - " name = \"Information\";\n", - "\n", - " description = \"useful for when you need to answer questions about various actors or movies\";\n", - "\n", - " async _call(input: z.infer): Promise {\n", - " return getInformation(input.entity);\n", - " }\n", - "}" + "const informationTool = tool((input) => {\n", + " return getInformation(input.entity);\n", + "}, {\n", + " name: \"Information\",\n", + " description: \"useful for when you need to answer questions about various actors or movies\",\n", + " schema: z.object({\n", + " entity: z.string().describe(\"movie or a person mentioned in the question\"),\n", + " }),\n", + "});" ] }, { @@ -227,7 +221,7 @@ "import { RunnableSequence } from \"@langchain/core/runnables\";\n", "\n", "const llm = new ChatOpenAI({ model: \"gpt-3.5-turbo\", temperature: 0 })\n", - "const tools = [new InformationTool()]\n", + "const tools = [informationTool]\n", "\n", "const llmWithTools = llm.bind({\n", " functions: tools.map(convertToOpenAIFunction),\n", diff --git a/docs/core_docs/docs/how_to/tool_calls_multimodal.ipynb b/docs/core_docs/docs/how_to/tool_calls_multimodal.ipynb index a948c68811a1..1f8119ecd13d 100644 --- a/docs/core_docs/docs/how_to/tool_calls_multimodal.ipynb +++ b/docs/core_docs/docs/how_to/tool_calls_multimodal.ipynb @@ -271,22 +271,17 @@ ], "source": [ "import { SystemMessage } from \"@langchain/core/messages\";\n", - "import { StructuredTool } from \"@langchain/core/tools\";\n", - "\n", - "class SummaryTool extends StructuredTool {\n", - " schema = z.object({\n", - " summary: z.string().describe(\"The summary of the content to log\")\n", - " })\n", - "\n", - " description = \"Log the summary of the content\"\n", - "\n", - " name = \"summary_tool\"\n", + "import { tool } from \"@langchain/core/tools\";\n", "\n", - " async _call(input: z.infer) {\n", - " return input.summary\n", - " }\n", - "}\n", - "const summaryTool = new SummaryTool()\n", + "const summaryTool = tool((input) => {\n", + " return input.summary;\n", + "}, {\n", + " name: \"summary_tool\",\n", + " description: \"Log the summary of the content\",\n", + " schema: z.object({\n", + " summary: z.string().describe(\"The summary of the content to log\")\n", + " }),\n", + "});\n", "\n", "const audioUrl = \"https://www.pacdv.com/sounds/people_sound_effects/applause-1.wav\";\n", "\n", diff --git a/docs/core_docs/docs/how_to/tools_prompting.ipynb b/docs/core_docs/docs/how_to/tools_prompting.ipynb index be665e49604c..0d4c92defd78 100644 --- a/docs/core_docs/docs/how_to/tools_prompting.ipynb +++ b/docs/core_docs/docs/how_to/tools_prompting.ipynb @@ -76,25 +76,19 @@ "metadata": {}, "outputs": [], "source": [ - "import { StructuredTool } from \"@langchain/core/tools\";\n", + "import { tool } from \"@langchain/core/tools\";\n", "import { z } from \"zod\";\n", "\n", - "class Multiply extends StructuredTool {\n", - " schema = z.object({\n", + "const multiplyTool = tool((input) => {\n", + " return (input.first_int * input.second_int).toString()\n", + "}, {\n", + " name: \"multiply\",\n", + " description: \"Multiply two integers together.\",\n", + " schema: z.object({\n", " first_int: z.number(),\n", " second_int: z.number(),\n", " })\n", - "\n", - " name = \"multiply\"\n", - "\n", - " description = \"Multiply two integers together.\"\n", - "\n", - " async _call(input: z.infer) {\n", - " return (input.first_int * input.second_int).toString()\n", - " }\n", - "}\n", - "\n", - "const multiply = new Multiply()\n" + "})\n" ] }, { @@ -113,8 +107,8 @@ } ], "source": [ - "console.log(multiply.name)\n", - "console.log(multiply.description)" + "console.log(multiplyTool.name)\n", + "console.log(multiplyTool.description)" ] }, { @@ -132,7 +126,7 @@ } ], "source": [ - "await multiply.invoke({ first_int: 4, second_int: 5 })" + "await multiplyTool.invoke({ first_int: 4, second_int: 5 })" ] }, { @@ -160,7 +154,7 @@ "source": [ "import { renderTextDescription } from \"langchain/tools/render\";\n", "\n", - "const renderedTools = renderTextDescription([multiply])" + "const renderedTools = renderTextDescription([multiplyTool])" ] }, { @@ -246,7 +240,7 @@ "source": [ "import { RunnableLambda, RunnablePick } from \"@langchain/core/runnables\"\n", "\n", - "const chain = prompt.pipe(model).pipe(new JsonOutputParser()).pipe(new RunnablePick(\"arguments\")).pipe(new RunnableLambda({ func: (input) => multiply.invoke({\n", + "const chain = prompt.pipe(model).pipe(new JsonOutputParser()).pipe(new RunnablePick(\"arguments\")).pipe(new RunnableLambda({ func: (input) => multiplyTool.invoke({\n", " first_int: input[0],\n", " second_int: input[1]\n", "}) }))\n", @@ -270,37 +264,27 @@ "metadata": {}, "outputs": [], "source": [ - "class Add extends StructuredTool {\n", - " schema = z.object({\n", + "const addTool = tool((input) => {\n", + " return (input.first_int + input.second_int).toString()\n", + "}, {\n", + " name: \"add\",\n", + " description: \"Add two integers together.\",\n", + " schema: z.object({\n", " first_int: z.number(),\n", " second_int: z.number(),\n", - " })\n", - "\n", - " name = \"add\"\n", - "\n", - " description = \"Add two integers together.\"\n", - "\n", - " async _call(input: z.infer) {\n", - " return (input.first_int + input.second_int).toString()\n", - " }\n", - "}\n", - "const add = new Add()\n", - "\n", - "class Exponentiate extends StructuredTool {\n", - " schema = z.object({\n", + " }),\n", + "});\n", + "\n", + "const exponentiateTool = tool((input) => {\n", + " return Math.pow(input.first_int, input.second_int).toString()\n", + "}, {\n", + " name: \"exponentiate\",\n", + " description: \"Exponentiate the base to the exponent power.\",\n", + " schema: z.object({\n", " first_int: z.number(),\n", " second_int: z.number(),\n", - " })\n", - "\n", - " name = \"exponentiate\"\n", - "\n", - " description = \"Exponentiate the base to the exponent power.\"\n", - "\n", - " async _call(input: z.infer) {\n", - " return Math.pow(input.first_int, input.second_int).toString()\n", - " }\n", - "}\n", - "const exponentiate = new Exponentiate()\n", + " }),\n", + "});\n", "\n" ] }, @@ -337,7 +321,7 @@ "source": [ "import { StructuredToolInterface } from \"@langchain/core/tools\"\n", "\n", - "const tools = [add, exponentiate, multiply]\n", + "const tools = [addTool, exponentiateTool, multiplyTool]\n", "\n", "const toolChain = (modelOutput) => {\n", " const toolMap: Record = Object.fromEntries(tools.map(tool => [tool.name, tool]))\n",