Open WebUI工具,帮助AI获取当前亚洲/上海时间

之前用AI安排进度和行程,无法知道当前时间,官网的工具没有自己需要的。就之前在别人的代码上更改了{AI帮忙修改}。
先上测试结果



open webui官网获取

我的博客

代码如下

from datetime import datetime, timezone, timedelta


class Tools:
    """
    CW_NTP Tool to retrieve and display the current date and time
    in Shanghai, Asia (UTC+8).

    Note: This implementation does not use an NTP client due to limitations
    within the Open WebUI environment. Time accuracy relies on the system clock.
    """

    def get_shanghai_time(self) -> str:
        """
        Calculates the current time for Shanghai, Asia (UTC+8) based on the system clock.
        """
        try:
            current_utc_time = datetime.now(
                timezone.utc
            )  # More efficient than replacing timezone info
            shanghai_time = current_utc_time + timedelta(hours=8)
            return shanghai_time.strftime("%A, %B %d, %Y %H:%M:%S (Shanghai Time)")
        except Exception as e:
            # More specific error handling would be ideal, but limited by Open WebUI
            return f"Error calculating Shanghai time: {e}"

    def preface_with_time(self, text: str) -> str:
        """
        Prepends the calculated Shanghai timestamp to the given text.
        """
        current_time = self.get_shanghai_time()
        return f"{current_time}\n\n{text}"


如有错误请给我留言

10 Likes

感谢分享,最近刚刚部署,学习一下

1 Like

感谢大佬分享

1 Like

上海时间是个什么时间 :face_with_hand_over_mouth:

感谢楼主分享

东八区时间,IANA 时区标识符是Asia/Shanghai

感谢分享 :tieba_013:

感谢大佬分享,话说这个导入了后怎么使用呢?请教一下各位佬
发了对话,给我输出一堆用代码获取时间的回复

调用这个工具就可以,找找选项

好的,我研究下,谢谢

帖子更新了,对话框+勾选下这个工具就可以。

1 Like

好用,方便,感谢大佬

欢迎大帅(水)哥。(太水了)