工具
This commit is contained in:
79
backend/scripts/init_builtin_tools.py
Normal file
79
backend/scripts/init_builtin_tools.py
Normal file
@@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
初始化内置工具脚本
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from app.core.database import SessionLocal
|
||||
from app.models.tool import Tool
|
||||
from app.services.tool_registry import tool_registry
|
||||
from app.services.builtin_tools import (
|
||||
http_request_tool,
|
||||
file_read_tool,
|
||||
HTTP_REQUEST_SCHEMA,
|
||||
FILE_READ_SCHEMA
|
||||
)
|
||||
|
||||
|
||||
def init_builtin_tools():
|
||||
"""初始化内置工具"""
|
||||
db = SessionLocal()
|
||||
|
||||
try:
|
||||
# 注册内置工具到注册表
|
||||
tool_registry.register_builtin_tool(
|
||||
"http_request",
|
||||
http_request_tool,
|
||||
HTTP_REQUEST_SCHEMA
|
||||
)
|
||||
|
||||
tool_registry.register_builtin_tool(
|
||||
"file_read",
|
||||
file_read_tool,
|
||||
FILE_READ_SCHEMA
|
||||
)
|
||||
|
||||
print("✅ 内置工具已注册到工具注册表")
|
||||
|
||||
# 保存到数据库
|
||||
tools_to_create = [
|
||||
("http_request", HTTP_REQUEST_SCHEMA, "发送HTTP请求,支持GET、POST、PUT、DELETE方法"),
|
||||
("file_read", FILE_READ_SCHEMA, "读取文件内容,只能读取项目目录下的文件")
|
||||
]
|
||||
|
||||
created_count = 0
|
||||
for tool_name, tool_schema, description in tools_to_create:
|
||||
existing = db.query(Tool).filter(Tool.name == tool_name).first()
|
||||
if not existing:
|
||||
tool = Tool(
|
||||
name=tool_name,
|
||||
description=description,
|
||||
category="builtin",
|
||||
function_schema=tool_schema,
|
||||
implementation_type="builtin",
|
||||
is_public=True
|
||||
)
|
||||
db.add(tool)
|
||||
created_count += 1
|
||||
print(f"✅ 创建工具: {tool_name}")
|
||||
else:
|
||||
# 更新工具定义
|
||||
existing.function_schema = tool_schema
|
||||
existing.description = description
|
||||
print(f"ℹ️ 更新工具: {tool_name}")
|
||||
|
||||
db.commit()
|
||||
print(f"\n✅ 内置工具初始化完成!创建了 {created_count} 个工具")
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
print(f"❌ 初始化失败: {str(e)}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
init_builtin_tools()
|
||||
Reference in New Issue
Block a user