a27ab9dfba
更换ReAct agent✓ 修复Thread.timer管理逻辑✓ 优化提示词减小返回格式出错概率(格式出错会导致重复执行)✓ 消息窗里加上执行任务标记✓ 更换gpt 3.5模型测试✓
33 lines
833 B
Python
33 lines
833 B
Python
import abc
|
||
import math
|
||
from typing import Any
|
||
|
||
from langchain.tools import BaseTool
|
||
|
||
|
||
class Calculator(BaseTool, abc.ABC):
|
||
name = "Calculator"
|
||
description = "Useful for when you need to answer questions about math(不能用于非数字计算)"
|
||
|
||
def __init__(self):
|
||
super().__init__()
|
||
|
||
async def _arun(self, *args: Any, **kwargs: Any) -> Any:
|
||
# 用例中没有用到 arun 不予具体实现
|
||
pass
|
||
|
||
|
||
def _run(self, para: str) -> str:
|
||
para = para.replace("^", "**")
|
||
if "sqrt" in para:
|
||
para = para.replace("sqrt", "math.sqrt")
|
||
elif "log" in para:
|
||
para = para.replace("log", "math.log")
|
||
return eval(para)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
calculator_tool = Calculator()
|
||
result = calculator_tool.run("sqrt(2) + 3")
|
||
print(result)
|