Tools怎么使用?可以定义自己的tool么?
要使用Tools,首先需要定义所需的工具。工具是代理程序与外部世界交互的方式。您可以定义自己的工具,也可以使用现有的工具。
定义自定义工具的方法有两种:
- 使用Tool数据类:您可以使用Tool数据类来创建一个新的工具。工具由名称、描述、函数和其他组件组成。
from langchain.agents import Tool
tools = [
Tool(
name="CustomTool",
func=my_custom_function,
description="This is a custom tool for my specific use case"
)
]
- 子类化BaseTool类:您还可以通过子类化BaseTool类来从头开始创建自定义工具。通过重写基类中的方法来实现功能。
from langchain.agents import BaseTool
class CustomTool(BaseTool):
name = "CustomTool"
description = "This is a custom tool for my specific use case"
def _run(self, input: str) -> str:
# Your logic here
return result
tools = [CustomTool()]
在定义完工具之后,您可以使用这些工具来初始化代理程序。您可以将自定义工具与其他现有工具一起传递给代理的初始化函数。
from langchain.agents import initialize_agent
from langchain.agents import AgentType
agent = initialize_agent(tools, agent=AgentType.DEFAULT)
现在,您就可以使用这些工具来与代理进行交互了.