docs: how to tools, merge built in tools and toolkits (#21824)

* Rename tools to built in tools
* Merge built in tools and toolkits
* Update links from providers
This commit is contained in:
Eugene Yurtsev 2024-05-17 14:35:57 -04:00 committed by GitHub
parent c4508ca7ef
commit 22d9aed508
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 268 additions and 463 deletions

View File

@ -166,10 +166,9 @@ 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: create tools](/docs/how_to/custom_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 built-in LangChain tools](/docs/how_to/tools)
- [How to: use LangChain toolkits](/docs/how_to/toolkits)
- [How to: use tools with LLMs that do not support tool calling natively](/docs/how_to/tools_prompting)
- [How to: convert LangChain tools to OpenAI functions](/docs/how_to/tools_as_openai_functions)
- [How to: add a human in the loop to tool usage](/docs/how_to/tools_human)

View File

@ -705,7 +705,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.11.4"
}
},
"nbformat": 4,

View File

@ -1,450 +0,0 @@
{
"cells": [
{
"cell_type": "raw",
"id": "7f219241",
"metadata": {},
"source": [
"---\n",
"sidebar_position: 4\n",
"sidebar_class_name: hidden\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "15780a65",
"metadata": {},
"source": [
"# How to use LangChain tools\n",
"\n",
"Tools are interfaces that an agent, chain, or LLM can use to interact with the world.\n",
"They combine a few things:\n",
"\n",
"1. The name of the tool\n",
"2. A description of what the tool is\n",
"3. JSON schema of what the inputs to the tool are\n",
"4. The function to call \n",
"5. Whether the result of a tool should be returned directly to the user\n",
"\n",
"It is useful to have all this information because this information can be used to build action-taking systems! The name, description, and JSON schema can be used to prompt the LLM so it knows how to specify what action to take, and then the function to call is equivalent to taking that action.\n",
"\n",
"The simpler the input to a tool is, the easier it is for an LLM to be able to use it.\n",
"Many agents will only work with tools that have a single string input.\n",
"For a list of agent types and which ones work with more complicated inputs, please see [this documentation](https://python.langchain.com/v0.1/docs/modules/agents/agent_types/)\n",
"\n",
"Importantly, the name, description, and JSON schema (if used) are all used in the prompt. Therefore, it is really important that they are clear and describe exactly how the tool should be used. You may need to change the default name, description, or JSON schema if the LLM is not understanding how to use the tool.\n",
"\n",
"## Default Tools\n",
"\n",
"Let's take a look at how to work with tools. To do this, we'll work with a built in tool."
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "19297004",
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.tools import WikipediaQueryRun\n",
"from langchain_community.utilities import WikipediaAPIWrapper"
]
},
{
"cell_type": "markdown",
"id": "1098e51a",
"metadata": {},
"source": [
"Now we initialize the tool. This is where we can configure it as we please"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "27a48655",
"metadata": {},
"outputs": [],
"source": [
"api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=100)\n",
"tool = WikipediaQueryRun(api_wrapper=api_wrapper)"
]
},
{
"cell_type": "markdown",
"id": "7db48439",
"metadata": {},
"source": [
"This is the default name"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "50f1ece1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Wikipedia'"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tool.name"
]
},
{
"cell_type": "markdown",
"id": "075499b1",
"metadata": {},
"source": [
"This is the default description"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "e9be09e2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, facts, historical events, or other subjects. Input should be a search query.'"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tool.description"
]
},
{
"cell_type": "markdown",
"id": "89c86b00",
"metadata": {},
"source": [
"This is the default JSON schema of the inputs"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "963a2e8c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'query': {'title': 'Query', 'type': 'string'}}"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tool.args"
]
},
{
"cell_type": "markdown",
"id": "5c467a35",
"metadata": {},
"source": [
"We can see if the tool should return directly to the user"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "039334b3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tool.return_direct"
]
},
{
"cell_type": "markdown",
"id": "fc421b02",
"metadata": {},
"source": [
"We can call this tool with a dictionary input"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "6669a13c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Page: LangChain\\nSummary: LangChain is a framework designed to simplify the creation of applications '"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tool.run({\"query\": \"langchain\"})"
]
},
{
"cell_type": "markdown",
"id": "587d6a58",
"metadata": {},
"source": [
"We can also call this tool with a single string input. \n",
"We can do this because this tool expects only a single input.\n",
"If it required multiple inputs, we would not be able to do that."
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "8cb23935",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Page: LangChain\\nSummary: LangChain is a framework designed to simplify the creation of applications '"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tool.run(\"langchain\")"
]
},
{
"cell_type": "markdown",
"id": "19eee1d5",
"metadata": {},
"source": [
"## Customizing Default Tools\n",
"We can also modify the built in name, description, and JSON schema of the arguments.\n",
"\n",
"When defining the JSON schema of the arguments, it is important that the inputs remain the same as the function, so you shouldn't change that. But you can define custom descriptions for each input easily."
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "599c4da7",
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.pydantic_v1 import BaseModel, Field\n",
"\n",
"\n",
"class WikiInputs(BaseModel):\n",
" \"\"\"Inputs to the wikipedia tool.\"\"\"\n",
"\n",
" query: str = Field(\n",
" description=\"query to look up in Wikipedia, should be 3 or less words\"\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "6bde63e1",
"metadata": {},
"outputs": [],
"source": [
"tool = WikipediaQueryRun(\n",
" name=\"wiki-tool\",\n",
" description=\"look up things in wikipedia\",\n",
" args_schema=WikiInputs,\n",
" api_wrapper=api_wrapper,\n",
" return_direct=True,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "eeaa1d9a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'wiki-tool'"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tool.name"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "7599d88c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'look up things in wikipedia'"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tool.description"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "80042cb1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'query': {'title': 'Query',\n",
" 'description': 'query to look up in Wikipedia, should be 3 or less words',\n",
" 'type': 'string'}}"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tool.args"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "8455fb9e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tool.return_direct"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "86f731a8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Page: LangChain\\nSummary: LangChain is a framework designed to simplify the creation of applications '"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tool.run(\"langchain\")"
]
},
{
"cell_type": "markdown",
"id": "c5b8b6bc",
"metadata": {},
"source": [
"## More Topics\n",
"\n",
"This was a quick introduction to tools in LangChain, but there is a lot more to learn\n",
"\n",
"**[Built-In Tools](/docs/integrations/tools/)**: For a list of all built-in tools, see [this page](/docs/integrations/tools/)\n",
" \n",
"**[Custom Tools](/docs/how_to/custom_tools)**: Although built-in tools are useful, it's highly likely that you'll have to define your own tools. See [this guide](/docs/how_to/custom_tools) for instructions on how to do so.\n",
" \n",
"**[Toolkits](/docs/how_to/toolkits)**: Toolkits are collections of tools that work well together. For a more in depth description as well as a list of all built-in toolkits, see [this page](/docs/how_to/toolkits)\n",
"\n",
"**[Tools as OpenAI Functions](/docs/how_to/tools_as_openai_functions/)**: Tools are very similar to OpenAI Functions, and can easily be converted to that format. See [this notebook](/docs/how_to/tools_as_openai_functions) for instructions on how to do that.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "78e2d0b3",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@ -0,0 +1,256 @@
{
"cells": [
{
"cell_type": "raw",
"id": "7f219241",
"metadata": {},
"source": [
"---\n",
"sidebar_position: 4\n",
"sidebar_class_name: hidden\n",
"---"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "e8f68de0-7df7-4bfd-9207-3258431426ef",
"metadata": {},
"source": [
"# How to use built-in tools and toolkits\n",
"\n",
":::info Prerequisites\n",
"\n",
"This guide assumes familiarity with the following concepts:\n",
"\n",
"- [LangChain Tools](/docs/concepts/#tools)\n",
"- [LangChain Toolkits](/docs/concepts/#tools)\n",
"- \n",
":::\n",
"\n",
"LangChain has a large collection of 3rd party tools. \n",
"\n",
"## Tools\n",
"\n",
"Please visit [Tool Integrations](/docs/integrations/tools/) for a list of the available tools.\n",
"\n",
":::{.callout-important}\n",
"\n",
"When using 3rd party tools, make sure that you understand how the tool works, what permissions\n",
"it has. Read over its documentation and check if anything is required from you\n",
"from a security point of view. Please see our [security](https://python.langchain.com/v0.1/docs/security/) \n",
"guidelines for more information.\n",
"\n",
":::\n",
"\n",
"Let's try out the [Wikipedia integration](/docs/integrations/tools/wikipedia/)."
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "84f70856-b865-4658-9930-7577fb4712ce",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.2.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.0\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython -m pip install --upgrade pip\u001b[0m\n"
]
}
],
"source": [
"!pip install -qU wikipedia"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "b4eaed85-c5a6-4ba9-b401-40258b0131c2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Page: LangChain\n",
"Summary: LangChain is a framework designed to simplify the creation of applications \n"
]
}
],
"source": [
"from langchain_community.tools import WikipediaQueryRun\n",
"from langchain_community.utilities import WikipediaAPIWrapper\n",
"\n",
"api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=100)\n",
"tool = WikipediaQueryRun(api_wrapper=api_wrapper)\n",
"\n",
"print(tool.invoke({\"query\": \"langchain\"}))"
]
},
{
"cell_type": "markdown",
"id": "cb870984-52d5-4453-be35-7072a08c6c14",
"metadata": {},
"source": [
"The tool has defaults associated with it:"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "7f094f01-2e98-4947-acc4-0846963a96e0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Name: wikipedia\n",
"Description: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, facts, historical events, or other subjects. Input should be a search query.\n",
"args schema: {'query': {'title': 'Query', 'type': 'string'}}\n",
"returns directly?: False\n"
]
}
],
"source": [
"print(f\"Name: {tool.name}\")\n",
"print(f\"Description: {tool.description}\")\n",
"print(f\"args schema: {tool.args}\")\n",
"print(f\"returns directly?: {tool.return_direct}\") # Only relevant for agents"
]
},
{
"cell_type": "markdown",
"id": "19eee1d5",
"metadata": {},
"source": [
"## Customizing Default Tools\n",
"We can also modify the built in name, description, and JSON schema of the arguments.\n",
"\n",
"When defining the JSON schema of the arguments, it is important that the inputs remain the same as the function, so you shouldn't change that. But you can define custom descriptions for each input easily."
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "1365784c-e666-41c8-a1bb-e50f822b5936",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Page: LangChain\n",
"Summary: LangChain is a framework designed to simplify the creation of applications \n"
]
}
],
"source": [
"from langchain_community.tools import WikipediaQueryRun\n",
"from langchain_community.utilities import WikipediaAPIWrapper\n",
"from langchain_core.pydantic_v1 import BaseModel, Field\n",
"\n",
"\n",
"class WikiInputs(BaseModel):\n",
" \"\"\"Inputs to the wikipedia tool.\"\"\"\n",
"\n",
" query: str = Field(\n",
" description=\"query to look up in Wikipedia, should be 3 or less words\"\n",
" )\n",
"\n",
"\n",
"tool = WikipediaQueryRun(\n",
" name=\"wiki-tool\",\n",
" description=\"look up things in wikipedia\",\n",
" args_schema=WikiInputs,\n",
" api_wrapper=api_wrapper,\n",
" return_direct=True,\n",
")\n",
"\n",
"print(tool.run(\"langchain\"))"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "6e8850d6-6840-443e-a2be-adf64b30975c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Name: wiki-tool\n",
"Description: look up things in wikipedia\n",
"args schema: {'query': {'title': 'Query', 'description': 'query to look up in Wikipedia, should be 3 or less words', 'type': 'string'}}\n",
"returns directly?: True\n"
]
}
],
"source": [
"print(f\"Name: {tool.name}\")\n",
"print(f\"Description: {tool.description}\")\n",
"print(f\"args schema: {tool.args}\")\n",
"print(f\"returns directly?: {tool.return_direct}\") # Only relevant for agents"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "acf0c2f7-ddc6-4633-8cef-59f234321e5c",
"metadata": {},
"source": [
"## How to use built-in toolkits\n",
"\n",
"Toolkits are collections of tools that are designed to be used together for specific tasks. They have convenient loading methods.\n",
"\n",
"For a complete list of available ready-made toolkits, visit [Integrations](/docs/integrations/toolkits/).\n",
"\n",
"All Toolkits expose a `get_tools` method which returns a list of tools.\n",
"\n",
"You're usually meant to use them this way:\n",
"\n",
"```python\n",
"# Initialize a toolkit\n",
"toolkit = ExampleTookit(...)\n",
"\n",
"# Get list of tools\n",
"tools = toolkit.get_tools()\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b1ec2a6d-02f7-4c91-bf94-189694d00a91",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@ -61,4 +61,4 @@ FROM
{'input': 'Return the sql for this question: How many employees are in the company?', 'output': "SELECT \n COUNT(*)\nFROM \n employees"}
```
For more information on tools, see [this page](/docs/how_to/tools).
For more information on tools, see [this page](/docs/how_to/tools_builtin).

View File

@ -31,4 +31,4 @@ from langchain.agents import load_tools
tools = load_tools(["golden-query"])
```
For more information on tools, see [this page](/docs/how_to/tools).
For more information on tools, see [this page](/docs/how_to/tools_builtin).

View File

@ -71,4 +71,4 @@ from langchain.agents import load_tools
tools = load_tools(["google-serper"])
```
For more information on tools, see [this page](/docs/how_to/tools).
For more information on tools, see [this page](/docs/how_to/tools_builtin).

View File

@ -41,4 +41,4 @@ from langchain.agents import load_tools
tools = load_tools(["openweathermap-api"])
```
For more information on tools, see [this page](/docs/how_to/tools).
For more information on tools, see [this page](/docs/how_to/tools_builtin).

View File

@ -77,4 +77,4 @@ from langchain.agents import load_tools
tools = load_tools(["searchapi"])
```
For more information on tools, see [this page](/docs/how_to/tools).
For more information on tools, see [this page](/docs/how_to/tools_builtin).

View File

@ -87,4 +87,4 @@ arxiv_tool = SearxSearchResults(name="Arxiv", wrapper=wrapper,
})
```
For more information on tools, see [this page](/docs/how_to/tools).
For more information on tools, see [this page](/docs/how_to/tools_builtin).

View File

@ -28,4 +28,4 @@ from langchain.agents import load_tools
tools = load_tools(["serpapi"])
```
For more information on this, see [this page](/docs/how_to/tools)
For more information on this, see [this page](/docs/how_to/tools_builtin)

View File

@ -33,4 +33,4 @@ from langchain.agents import load_tools
tools = load_tools(["stackexchange"])
```
For more information on tools, see [this page](/docs/how_to/tools).
For more information on tools, see [this page](/docs/how_to/tools_builtin).

View File

@ -36,4 +36,4 @@ from langchain.agents import load_tools
tools = load_tools(["wolfram-alpha"])
```
For more information on tools, see [this page](/docs/how_to/tools).
For more information on tools, see [this page](/docs/how_to/tools_builtin).