Skip to content

Commit

Permalink
docs[patch]: Update tools docs (#5458)
Browse files Browse the repository at this point in the history
* Update tools docs

* Links
  • Loading branch information
jacoblee93 authored May 19, 2024
1 parent d1fb0eb commit 4c94752
Show file tree
Hide file tree
Showing 9 changed files with 399 additions and 318 deletions.
Binary file added docs/core_docs/data/sunny_day.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/core_docs/docs/how_to/custom_tools.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"\n",
"This guide assumes familiarity with the following concepts:\n",
"\n",
"- [How to use LangChain tools](/docs/how_to/tools)\n",
"- [LangChain tools](/docs/concepts#tools)\n",
"- [Agents](/docs/concepts/#agents)\n",
"\n",
":::\n",
Expand Down
9 changes: 4 additions & 5 deletions docs/core_docs/docs/how_to/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,11 @@ Indexing is the process of keeping your vectorstore in-sync with the underlying

LangChain Tools contain a description of the tool (to pass to the language model) as well as the implementation of the function to call).

- [How to: use LangChain tools](/docs/how_to/tools)
- [How to: create custom tools](/docs/how_to/custom_tools)
- [How to: use built-in tools and built-in toolkits](/docs/how_to/tools_builtin)
- [How to: use a chat model to call tools](/docs/how_to/tool_calling/)
- [How to: use LangChain toolkits](/docs/how_to/toolkits)
- [How to: define a custom tool](/docs/how_to/custom_tools)
- [How to: convert LangChain tools to OpenAI functions](/docs/how_to/tools_as_openai_functions)
- [How to: use tools without function calling](/docs/how_to/tools_prompting)
- [How to: add ad-hoc tool calling capability to LLMs and Chat Models](/docs/how_to/tools_prompting)
- [How to: call tools using multi-modal data](/docs/how_to/tool_calls_multi_modal)

### Agents

Expand Down
155 changes: 144 additions & 11 deletions docs/core_docs/docs/how_to/tool_calling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
"source": [
"# How to use a chat model to call tools\n",
"\n",
":::info Prerequisites\n",
"\n",
"This guide assumes familiarity with the following concepts:\n",
"\n",
"- [Chat models](/docs/concepts/#chat-models)\n",
"- [LangChain Tools](/docs/concepts/#tools)\n",
"\n",
":::\n",
"\n",
"```{=mdx}\n",
":::info\n",
"We use the term tool calling interchangeably with function calling. Although\n",
Expand Down Expand Up @@ -40,17 +49,6 @@
"LangChain implements standard interfaces for defining tools, passing them to LLMs, \n",
"and representing tool calls. This guide will show you how to use them.\n",
"\n",
":::info Prerequisites\n",
"\n",
"This guide assumes familiarity with the following concepts:\n",
"\n",
"- [Chat models](/docs/concepts/#chat-models)\n",
"- [LangChain Tools](/docs/concepts/#tools)\n",
"\n",
":::\n",
"\n",
"\n",
"\n",
"## Passing tools to LLMs\n",
"\n",
"Chat models that support tool calling features implement a [`.bindTools()`](https://v02.api.js.langchain.com/classes/langchain_core_language_models_chat_models.BaseChatModel.html#bindTools) method, which \n",
Expand Down Expand Up @@ -383,6 +381,141 @@
"]);\n",
"console.log(res.tool_calls);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Binding model-specific formats (advanced)\n",
"\n",
"Providers adopt different conventions for formatting tool schemas. For instance, OpenAI uses a format like this:\n",
"\n",
"- `type`: The type of the tool. At the time of writing, this is always \"function\".\n",
"- `function`: An object containing tool parameters.\n",
"- `function.name`: The name of the schema to output.\n",
"- `function.description`: A high level description of the schema to output.\n",
"- `function.parameters`: The nested details of the schema you want to extract, formatted as a [JSON schema](https://json-schema.org/) object.\n",
"\n",
"We can bind this model-specific format directly to the model if needed. Here's an example:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage {\n",
" lc_serializable: \u001b[33mtrue\u001b[39m,\n",
" lc_kwargs: {\n",
" content: \u001b[32m\"\"\u001b[39m,\n",
" tool_calls: [\n",
" {\n",
" name: \u001b[32m\"calculator\"\u001b[39m,\n",
" args: { operation: \u001b[32m\"multiply\"\u001b[39m, number1: \u001b[33m119\u001b[39m, number2: \u001b[33m8\u001b[39m },\n",
" id: \u001b[32m\"call_pBlKOPNMRN4AAMkPaOKLLcyj\"\u001b[39m\n",
" }\n",
" ],\n",
" invalid_tool_calls: [],\n",
" additional_kwargs: {\n",
" function_call: \u001b[90mundefined\u001b[39m,\n",
" tool_calls: [\n",
" {\n",
" id: \u001b[32m\"call_pBlKOPNMRN4AAMkPaOKLLcyj\"\u001b[39m,\n",
" type: \u001b[32m\"function\"\u001b[39m,\n",
" function: \u001b[36m[Object]\u001b[39m\n",
" }\n",
" ]\n",
" },\n",
" response_metadata: {}\n",
" },\n",
" lc_namespace: [ \u001b[32m\"langchain_core\"\u001b[39m, \u001b[32m\"messages\"\u001b[39m ],\n",
" content: \u001b[32m\"\"\u001b[39m,\n",
" name: \u001b[90mundefined\u001b[39m,\n",
" additional_kwargs: {\n",
" function_call: \u001b[90mundefined\u001b[39m,\n",
" tool_calls: [\n",
" {\n",
" id: \u001b[32m\"call_pBlKOPNMRN4AAMkPaOKLLcyj\"\u001b[39m,\n",
" type: \u001b[32m\"function\"\u001b[39m,\n",
" function: {\n",
" name: \u001b[32m\"calculator\"\u001b[39m,\n",
" arguments: \u001b[32m'{\"operation\":\"multiply\",\"number1\":119,\"number2\":8}'\u001b[39m\n",
" }\n",
" }\n",
" ]\n",
" },\n",
" response_metadata: {\n",
" tokenUsage: { completionTokens: \u001b[33m24\u001b[39m, promptTokens: \u001b[33m85\u001b[39m, totalTokens: \u001b[33m109\u001b[39m },\n",
" finish_reason: \u001b[32m\"tool_calls\"\u001b[39m\n",
" },\n",
" tool_calls: [\n",
" {\n",
" name: \u001b[32m\"calculator\"\u001b[39m,\n",
" args: { operation: \u001b[32m\"multiply\"\u001b[39m, number1: \u001b[33m119\u001b[39m, number2: \u001b[33m8\u001b[39m },\n",
" id: \u001b[32m\"call_pBlKOPNMRN4AAMkPaOKLLcyj\"\u001b[39m\n",
" }\n",
" ],\n",
" invalid_tool_calls: []\n",
"}"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import { ChatOpenAI } from \"@langchain/openai\";\n",
"\n",
"const model = new ChatOpenAI({ model: \"gpt-4o\" });\n",
"\n",
"const modelWithTools = model.bind({\n",
" tools: [{\n",
" \"type\": \"function\",\n",
" \"function\": {\n",
" \"name\": \"calculator\",\n",
" \"description\": \"Can perform mathematical operations.\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"operation\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The type of operation to execute.\",\n",
" \"enum\": [\"add\", \"subtract\", \"multiply\", \"divide\"]\n",
" },\n",
" \"number1\": {\"type\": \"number\", \"description\": \"First integer\"},\n",
" \"number2\": {\"type\": \"number\", \"description\": \"Second integer\"},\n",
" },\n",
" \"required\": [\"number1\", \"number2\"],\n",
" },\n",
" },\n",
" }],\n",
"});\n",
"\n",
"await modelWithTools.invoke(`Whats 119 times 8?`);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is functionally equivalent to the `bind_tools()` calls above."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Next steps\n",
"\n",
"Now you've learned how to bind tool schemas to a chat model and to call those tools. Next, check out some more specific uses of tool calling:\n",
"\n",
"- [Building tool-using chains and agents](/docs/how_to/#tools)\n",
"- [Getting structured outputs from models](/docs/how_to/structured_output/)"
]
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 4c94752

Please sign in to comment.