langchain/docs/docs/how_to/tools_builtin.ipynb
Eugene Yurtsev a766815a99
experimental[patch]/docs[patch]: Update links to security docs (#22864)
Minor update to newest version of security docs (content should be
identical).
2024-06-13 20:29:34 +00:00

237 lines
6.6 KiB
Plaintext

{
"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",
"## Tools\n",
"\n",
"LangChain has a large collection of 3rd party tools. 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.2/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": null,
"id": "84f70856-b865-4658-9930-7577fb4712ce",
"metadata": {},
"outputs": [],
"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 the following defaults associated with it:"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "7f094f01-2e98-4947-acc4-0846963a96e0",
"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}\")"
]
},
{
"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": 56,
"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": 57,
"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}\")"
]
},
{
"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",
"```"
]
}
],
"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
}