feat(tools): 增强工具注册表与内置工具能力
This commit is contained in:
@@ -1,8 +1,47 @@
|
||||
import re
|
||||
|
||||
|
||||
def calculate_rate(user_input: str) -> dict:
|
||||
return {"rate": 1.0, "note": "模拟比例计算结果"}
|
||||
"""
|
||||
从自然语言中提取两个数值并计算比例。
|
||||
|
||||
V1 目标不是构建复杂公式引擎,而是提供一个可演示的“业务工具”示例:
|
||||
只要输入中出现两个数字,就将其解释为“已完成值 / 总数”。
|
||||
"""
|
||||
numbers = [float(item) for item in re.findall(r"\d+(?:\.\d+)?", user_input)]
|
||||
if len(numbers) < 2:
|
||||
return {
|
||||
"success": False,
|
||||
"rate": 0.0,
|
||||
"numerator": 0.0,
|
||||
"denominator": 0.0,
|
||||
"note": "未能从输入中提取两个数字,无法计算比例。",
|
||||
}
|
||||
numerator, denominator = numbers[0], numbers[1]
|
||||
if denominator == 0:
|
||||
return {
|
||||
"success": False,
|
||||
"rate": 0.0,
|
||||
"numerator": numerator,
|
||||
"denominator": denominator,
|
||||
"note": "分母为 0,无法计算比例。",
|
||||
}
|
||||
return {
|
||||
"success": True,
|
||||
"numerator": numerator,
|
||||
"denominator": denominator,
|
||||
"rate": round(numerator / denominator, 4),
|
||||
"note": "已按输入中的前两个数字完成比例计算。",
|
||||
}
|
||||
|
||||
|
||||
def query_demo_records(user_input: str) -> dict:
|
||||
"""
|
||||
查询示例业务记录。
|
||||
|
||||
该工具依赖 Audit 模块中的 DemoBusinessRecord 演示表,用于证明
|
||||
“场景 + 结构化数据 + 工具调用”可以组成更可信的业务 Agent。
|
||||
"""
|
||||
try:
|
||||
from apps.audit.models import DemoBusinessRecord
|
||||
except Exception as exc:
|
||||
@@ -32,11 +71,49 @@ def query_demo_records(user_input: str) -> dict:
|
||||
|
||||
|
||||
def check_required_fields(user_input: str) -> dict:
|
||||
return {"missing_fields": [], "note": "模拟必填项检查结果"}
|
||||
"""
|
||||
检查输入中声明的必填项是否全部出现。
|
||||
|
||||
约定格式示例:
|
||||
“请检查必填项:合同编号、供应商、金额。当前只提供了合同编号和金额。”
|
||||
"""
|
||||
required_match = re.search(r"必填项[::](.+?)(?:。|\.)", user_input)
|
||||
provided_match = re.search(r"(?:当前|已|仅)?提供了(.+?)(?:。|\.)", user_input)
|
||||
required_fields = _split_cn_items(required_match.group(1) if required_match else "")
|
||||
provided_fields = set(_split_cn_items(provided_match.group(1) if provided_match else ""))
|
||||
missing_fields = [field for field in required_fields if field not in provided_fields]
|
||||
return {
|
||||
"required_fields": required_fields,
|
||||
"provided_fields": list(provided_fields),
|
||||
"missing_fields": missing_fields,
|
||||
"note": "已根据输入中的“必填项/提供了”描述完成检查。",
|
||||
}
|
||||
|
||||
|
||||
def generate_action_items(user_input: str) -> dict:
|
||||
return {"items": [f"围绕问题继续核实:{user_input}"]}
|
||||
"""
|
||||
生成最小可执行行动项。
|
||||
|
||||
该工具主要用于演示“模型回答之外,还可以得到结构化待办建议”。
|
||||
"""
|
||||
return {
|
||||
"items": [
|
||||
"先确认问题背景和适用场景。",
|
||||
f"围绕当前问题继续核实:{user_input}",
|
||||
"根据知识库和审计结果安排下一步处理动作。",
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def _split_cn_items(raw_text: str) -> list[str]:
|
||||
"""将中文顿号、逗号和连接词分隔的字段串切分为列表。"""
|
||||
normalized = (
|
||||
raw_text.replace("和", "、")
|
||||
.replace("以及", "、")
|
||||
.replace(",", "、")
|
||||
.replace(",", "、")
|
||||
)
|
||||
return [item.strip(" 。.") for item in normalized.split("、") if item.strip(" 。.")]
|
||||
|
||||
|
||||
BUILTIN_TOOLS = {
|
||||
|
||||
Reference in New Issue
Block a user