使用@tool 装饰器修饰,可以自动将普通 Python 函数转化为智能体可调用的工具。
此方式最直接,代码量极少,非常适合快速验证想法或创建参数简单的工具。
3.1 自定义工具描述:description
情况1:仅提供docstring信息
在bind_tools()调用时,先将函数封装为BaseTool 类型的对象,再传递给convert_to_openai_tool 函数,生成工具的描述。
from langchain_core.utils.function_calling import convert_to_openai_tool
from langchain.tools import tool
@tool
def get_weather(city: str):
return f"{city}天气晴朗"
print(convert_to_openai_tool(get_weather))
@tool 会从docstring 生成描述信息,同样要求遵循Google docstring 规范。如果没有docstring则报错,如下。
Traceback...
ValueError: Function must have a docstring if description not provided.
补充docstring
from langchain_core.utils.function_calling import convert_to_openai_tool
from langchain.tools import tool
@tool
def get_weather(city: str):
"""
天气查询工具
"""
return f"{city}天气晴朗"
print(convert_to_openai_tool(get_weather))
情况2:添加工具描述:description
@tool 的参数description 可以更改工具描述,优先级高于docstring 的函数说明
from langchain_core.utils.function_calling import convert_to_openai_tool
from langchain.tools import tool
from rich import print as rprint
@tool(description="根据城市名称查询当日天气的工具")
def get_weather(city: str):
"""
天气查询工具
"""
return f"{city}天气晴朗"
rprint(convert_to_openai_tool(get_weather))
情况3:解析docstring:parse_docstring
当我们没有向@tool 传递description 参数时,默认情况下, tool 会将docstring 整体视为description ,如下
from langchain_core.utils.function_calling import convert_to_openai_tool
from rich import print as rprint
@tool
def get_weather(city: str, units: str = "celsius", include_forecast: bool =
False) -> str:
"""
获取当日天气,可选择是否同时查询未来五日天气预报
Args:
city: 城市
units: 气温单位,可选:celsius-摄氏度,fahrenheit-华氏度
include_forecast: 是否包含未来五日的天气预报
"""
temp = 22 if units == "celsius" else 72
result = f'{city}当天气温: {temp} {"摄氏度" if units == "celsius" else "华
氏度"}'
if include_forecast:
result += "\n未来五天都是晴天"
return result
rprint(convert_to_openai_tool(get_weather))
通过将parse_docstring 设置为True,docstring会被解析,填充到相应的字段描述中。
from langchain_core.utils.function_calling import convert_to_openai_tool
from rich import print as rprint
@tool(parse_docstring=True)
def get_weather(city: str, units: str = "celsius", include_forecast: bool =
False) -> str:
"""
获取当日天气,可选择是否同时查询未来五日天气预报
Args:
city: 城市
units: 气温单位,可选:celsius-摄氏度,fahrenheit-华氏度
include_forecast: 是否包含未来五日的天气预报
"""
temp = 22 if units == "celsius" else 72
result = f'{city}当天气温: {temp} {"摄氏度" if units == "celsius" else "华
氏度"}'
if include_forecast:
result += "\n未来五天都是晴天"
return result
rprint(convert_to_openai_tool(get_weather))
要注意:不使用@tool 装饰器时,docstring不合法会被视为普通文本,作为description ,但如果使用了@tool 时docstring 不合法,将会抛出异常
from langchain_core.utils.function_calling import convert_to_openai_tool
@tool(parse_docstring=True)
def get_weather(city: str, units: str = "celsius", include_forecast: bool =
False) -> str:
"""
获取当日天气,可选择是否同时查询未来五日天气预报
Args:
city: 城市
units: 气温单位,可选:celsius-摄氏度,fahrenheit-华氏度
include_forecast: 是否包含未来五日的天气预报
"""
temp = 22 if units == "celsius" else 72
result = f'{city}当天气温: {temp} {"摄氏度" if units == "celsius" else "华
氏度"}'
if include_forecast:
result += "\n未来五天都是晴天"
return result
convert_to_openai_tool(get_weather)
3.2 更改工具名称:name_or_callable
默认情况,使用函数名作为工具名称,但可以向@tool 传参name_or_callable ,以更改工具名称。
from langchain_core.utils.function_calling import convert_to_openai_tool
from langchain.tools import tool
@tool(name_or_callable="getWeather")
def get_weather(city: str):
"""
天气查询工具
"""
return f"{city}天气晴朗"
print(convert_to_openai_tool(get_weather))
说明:@tool中参数name_or_callable名称可以省略
from langchain_core.utils.function_calling import convert_to_openai_tool
from langchain.tools import tool
@tool("getWeather")
def get_weather(city: str):
"""
天气查询工具
"""
print("天气晴朗")
print(convert_to_openai_tool(get_weather))
说明:不要使用config或runtime作为参数名,这些是LangChain内部保留的。
开发中,习惯使用函数名作为工具名称,不推荐自定义工具名称。
















请登录后查看评论内容