50 lines
1.6 KiB
Python
50 lines
1.6 KiB
Python
|
|
from datetime import datetime
|
||
|
|
from typing import Dict, Any
|
||
|
|
|
||
|
|
from app.tools.base import ToolResult, BaseTool
|
||
|
|
from app.tools.registry import ToolRegistry
|
||
|
|
|
||
|
|
|
||
|
|
@ToolRegistry.register
|
||
|
|
class GetCurrentTimeTool(BaseTool):
|
||
|
|
"""获取当前时间工具"""
|
||
|
|
|
||
|
|
@property
|
||
|
|
def parameters_schema(self) -> Dict[str, Any]:
|
||
|
|
"""
|
||
|
|
获取当前时间工具的 OpenAI function calling schema
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
工具的 JSON Schema 定义
|
||
|
|
"""
|
||
|
|
return {
|
||
|
|
"type": "object",
|
||
|
|
"properties": {},
|
||
|
|
"required": []
|
||
|
|
}
|
||
|
|
|
||
|
|
def execute(self, **kwargs) -> ToolResult:
|
||
|
|
"""
|
||
|
|
获取当前时间工具
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
包含当前时间信息的字典
|
||
|
|
"""
|
||
|
|
now = datetime.now()
|
||
|
|
|
||
|
|
return ToolResult(success=True,
|
||
|
|
data={
|
||
|
|
"current_time": now.strftime("%Y-%m-%d %H:%M:%S"),
|
||
|
|
"date": now.strftime("%Y-%m-%d"),
|
||
|
|
"time": now.strftime("%H:%M:%S"),
|
||
|
|
"year": now.year,
|
||
|
|
"month": now.month,
|
||
|
|
"day": now.day,
|
||
|
|
"hour": now.hour,
|
||
|
|
"minute": now.minute,
|
||
|
|
"second": now.second,
|
||
|
|
"weekday": now.strftime("%A"),
|
||
|
|
"weekday_cn": ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"][
|
||
|
|
now.weekday()]
|
||
|
|
})
|