首先,我观察到项目自带的所有的工具都是放在libs\chatchat-server\chatchat\server\agent\tools_factory目录下。
其次,我先在该目录下创建了一个新的python文件:***.py。代码如下:
@regist_tool(title="柴油机参数获取")
def diesel_engine_status() -> BaseToolOutput:
"""
Using a simulator to obtain real-time status parameters of diesel engines.
获取船舶柴油发动机的实时运行参数和状态。
"""
try:
# 创建新的模拟器实例(避免状态问题)
engine = MarineDieselEngine()
current_data = engine.generate_data()
current_alert = engine.get_current_alert()
# 构建输出文本
txt = "当前柴油机参数:\n"
for k, v in current_data.items():
txt += f" {k}: {v}\n"
if current_alert:
txt += f"\n⚠️ 当前报警: {current_alert['parameter']} - {current_alert['message']}"
else:
txt += "\n✅ 状态正常"
# 重要:返回BaseToolOutput对象,与calculate.py示例保持一致
return BaseToolOutput(txt)
except Exception as e:
error_msg = f"模拟器错误,无法查到当前柴油机状态参数!错误信息:{str(e)}"
return BaseToolOutput(error_msg)
之后,我在该目录下的__init__.py中对我自定义工具进行了导入,并且在这里将几个我不需要的工具注释掉。
# from .url_reader import url_reader
from .diesel_engine_status import diesel_engine_status # 自定义的
# from .arxiv import arxiv
# from .calculate import calculate
# from .search_internet import search_internet
# from .search_local_knowledgebase import search_local_knowledgebase
# from .search_youtube import search_youtube
# from .shell import shell
# from .text2image import text2images
# from .text2sql import text2sql
# from .weather_check import weather_check
# from .wolfram import wolfram
# from .amap_poi_search import amap_poi_search
# from .amap_weather import amap_weather
# from .wikipedia_search import wikipedia_search
# from .text2promql import text2promql
最后,在settings.py和tool_settings.yaml文件中对相关配置进行了修改,所有修改都是按照网上的教程做的。
但是,当我重启项目后,我自定义的工具无法看到和调用;而我注释掉的工具依然能看到,且能正常调用。命令行也没有报错信息。
能麻烦帮我解答一下困惑吗?谢谢!