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[patch]: Replace StructuredTool with tool func in docs #6267

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 11 additions & 17 deletions docs/core_docs/docs/how_to/graph_semantic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof informationInput>): Promise<string> {\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",
"});"
]
},
{
Expand Down Expand Up @@ -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",
Expand Down
25 changes: 10 additions & 15 deletions docs/core_docs/docs/how_to/tool_calls_multimodal.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof this.schema>) {\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",
Expand Down
78 changes: 31 additions & 47 deletions docs/core_docs/docs/how_to/tools_prompting.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof this.schema>) {\n",
" return (input.first_int * input.second_int).toString()\n",
" }\n",
"}\n",
"\n",
"const multiply = new Multiply()\n"
"})\n"
]
},
{
Expand All @@ -113,8 +107,8 @@
}
],
"source": [
"console.log(multiply.name)\n",
"console.log(multiply.description)"
"console.log(multiplyTool.name)\n",
"console.log(multiplyTool.description)"
]
},
{
Expand All @@ -132,7 +126,7 @@
}
],
"source": [
"await multiply.invoke({ first_int: 4, second_int: 5 })"
"await multiplyTool.invoke({ first_int: 4, second_int: 5 })"
]
},
{
Expand Down Expand Up @@ -160,7 +154,7 @@
"source": [
"import { renderTextDescription } from \"langchain/tools/render\";\n",
"\n",
"const renderedTools = renderTextDescription([multiply])"
"const renderedTools = renderTextDescription([multiplyTool])"
]
},
{
Expand Down Expand Up @@ -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",
Expand All @@ -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<typeof this.schema>) {\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<typeof this.schema>) {\n",
" return Math.pow(input.first_int, input.second_int).toString()\n",
" }\n",
"}\n",
"const exponentiate = new Exponentiate()\n",
" }),\n",
"});\n",
"\n"
]
},
Expand Down Expand Up @@ -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<string, StructuredToolInterface> = Object.fromEntries(tools.map(tool => [tool.name, tool]))\n",
Expand Down
Loading