Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
OurBuilding Ambient Agents with LangGraph course is now available on LangChain Academy!
Open In ColabOpen on GitHub

How to use built-in tools and toolkits

Prerequisites

This guide assumes familiarity with the following concepts:

Tools

LangChain has a large collection of 3rd party tools. Please visitTool Integrations for a list of the available tools.

important

When using 3rd party tools, make sure that you understand how the tool works, what permissionsit has. Read over its documentation and check if anything is required from youfrom a security point of view. Please see oursecurityguidelines for more information.

Let's try out theWikipedia integration.

!pip install-qU langchain-community wikipedia
from langchain_community.toolsimport WikipediaQueryRun
from langchain_community.utilitiesimport WikipediaAPIWrapper

api_wrapper= WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=100)
tool= WikipediaQueryRun(api_wrapper=api_wrapper)

print(tool.invoke({"query":"langchain"}))
Page: LangChain
Summary: LangChain is a framework designed to simplify the creation of applications

The tool has the following defaults associated with it:

print(f"Name:{tool.name}")
print(f"Description:{tool.description}")
print(f"args schema:{tool.args}")
print(f"returns directly?:{tool.return_direct}")
Name: wikipedia
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.
args schema: {'query': {'description': 'query to look up on wikipedia', 'title': 'Query', 'type': 'string'}}
returns directly?: False

Customizing Default Tools

We can also modify the built in name, description, and JSON schema of the arguments.

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.

from langchain_community.toolsimport WikipediaQueryRun
from langchain_community.utilitiesimport WikipediaAPIWrapper
from pydanticimport BaseModel, Field


classWikiInputs(BaseModel):
"""Inputs to the wikipedia tool."""

query:str= Field(
description="query to look up in Wikipedia, should be 3 or less words"
)


tool= WikipediaQueryRun(
name="wiki-tool",
description="look up things in wikipedia",
args_schema=WikiInputs,
api_wrapper=api_wrapper,
return_direct=True,
)

print(tool.run("langchain"))
Page: LangChain
Summary: LangChain is a framework designed to simplify the creation of applications
print(f"Name:{tool.name}")
print(f"Description:{tool.description}")
print(f"args schema:{tool.args}")
print(f"returns directly?:{tool.return_direct}")
Name: wiki-tool
Description: look up things in wikipedia
args schema: {'query': {'description': 'query to look up in Wikipedia, should be 3 or less words', 'title': 'Query', 'type': 'string'}}
returns directly?: True

How to use built-in toolkits

Toolkits are collections of tools that are designed to be used together for specific tasks. They have convenient loading methods.

All Toolkits expose aget_tools method which returns a list of tools.

You're usually meant to use them this way:

# Initialize a toolkit
toolkit= ExampleTookit(...)

# Get list of tools
tools= toolkit.get_tools()

[8]ページ先頭

©2009-2025 Movatter.jp