olivebot/llm/agent/tools/MyTimer.py
xszyou 87ed1c4425 Fay年翻更新
- 升级Agent(chat_module=agent切换):升级到langgraph react agent逻辑、集成到主分支fay中、基于自动决策工具调用机制、基于日程跟踪的主动沟通、支持外部观测数据传入;
- 修复因线程同步问题导致的配置文件读写不稳定
- 聊天采纳功能的bug修复
2024-11-20 23:44:47 +08:00

57 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import abc
import sqlite3
import re
from typing import Any
from langchain.tools import BaseTool
class MyTimer(BaseTool, abc.ABC):
name: str = "MyTimer"
description: str = ("用于设置日程。接受3个参数格式为: HH:MM|YYYYYYY|事项内容,所用标点符号必须为标准的英文字符。"
"其中,'HH:MM' 表示时间24小时制'YYYYYYY' 表示循环规则每位代表一天从星期一至星期日1为循环0为不循环"
"'1000100'代表每周一和周五循环),'事项内容' 是提醒的具体内容。返回例子15:15|0000000|提醒主人叫咖啡")
uid: int = 0
async def _arun(self, *args: Any, **kwargs: Any) -> Any:
# 用例中没有用到 arun 不予具体实现
pass
def _run(self, para: str) -> str:
# 拆分输入字符串
parts = para.split("|")
if len(parts) != 3:
return f"输入格式错误,当前字符串{para} len:{len(parts)}。请按照 HH:MM|YYYYYYY|事项内容 格式提供参数15:15|0000001|提醒主人叫咖啡。"
time = parts[0].strip("'")
repeat_rule = parts[1].strip("'")
content = parts[2].strip("'")
# 验证时间格式
if not re.match(r'^[0-2][0-9]:[0-5][0-9]$', time):
return "时间格式错误。请按照'HH:MM'格式提供时间。"
# 验证循环规则格式
if not re.match(r'^[01]{7}$', repeat_rule):
return "循环规则格式错误。请提供长度为7的0和1组成的字符串。"
# 验证事项内容
if not isinstance(content, str) or not content:
return "事项内容必须为非空字符串。"
# 数据库操作
conn = sqlite3.connect('timer.db')
cursor = conn.cursor()
try:
cursor.execute("INSERT INTO timer (time, repeat_rule, content, uid) VALUES (?, ?, ?, ?)", (time, repeat_rule, content, self.uid))
conn.commit()
except sqlite3.Error as e:
return f"数据库错误: {e}"
finally:
conn.close()
return "日程设置成功"
if __name__ == "__main__":
my_timer = MyTimer()
result = my_timer._run("15:15|0000001|提醒主人叫咖啡")
print(result)