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
)