1、Tools 可以被Agent、Chain、LLM所使用。
2、tool 的必备属性有:name、description、JSON schema (tool输入)、调用的函数、工具的结果是否应直接返回给用户。其中name、description和 JSON schema 可用于提示 LLM、写入在LLM的system prompt当中,以便它知道如何指定要执行的操作,然后调用的函数相当于执行该操作。
3、tool建议:输入越简单、LLM越容易使用,比如单个字符串。
4、自定义tool实现:Defining Custom Tools
推荐使用 StructuredTool 类构建 tool
# 输入为单个字符串
def search_function(query: str):return "LangChain"search = StructuredTool.from_function(func=search_function,name="Search",description="useful for when you need to answer questions about current events",# coroutine= ... <- you can specify an async method if desired as well
)
# 输入是dict
class CalculatorInput(BaseModel):a: int = Field(description="first number")b: int = Field(description="second number")def multiply(a: int, b: int) -> int:"""Multiply two numbers."""return a * bcalculator = StructuredTool.from_function(func=multiply,name="Calculator",description="multiply numbers",args_schema=CalculatorInput,return_direct=True,# coroutine= ... <- you can specify an async method if desired as well
)