Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -75,6 +75,19 @@ def duckduckgo_search(query: str) -> str:
|
|
| 75 |
return f"An error occurred during DuckDuckGo search: {e}"
|
| 76 |
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
# load the system prompt from the file
|
| 79 |
# Ensure this file exists and has the content from Step 2
|
| 80 |
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
|
@@ -84,7 +97,8 @@ sys_msg = SystemMessage(content=system_prompt)
|
|
| 84 |
tools = [
|
| 85 |
wiki_search,
|
| 86 |
duckduckgo_search,
|
| 87 |
-
web_search,
|
|
|
|
| 88 |
]
|
| 89 |
|
| 90 |
|
|
|
|
| 75 |
return f"An error occurred during DuckDuckGo search: {e}"
|
| 76 |
|
| 77 |
|
| 78 |
+
@tool
|
| 79 |
+
def arithmetic(expression: str) -> str:
|
| 80 |
+
"""执行数学计算并返回结果。支持基本的算术运算如加减乘除、幂运算等。"""
|
| 81 |
+
try:
|
| 82 |
+
# 使用Python的eval函数安全地计算表达式
|
| 83 |
+
# 限制只能使用基本算术运算,不允许导入模块或执行其他危险操作
|
| 84 |
+
allowed_names = {"__builtins__": {}}
|
| 85 |
+
allowed_symbols = {}
|
| 86 |
+
result = eval(expression, allowed_names, allowed_symbols)
|
| 87 |
+
return str(result)
|
| 88 |
+
except Exception as e:
|
| 89 |
+
return f"计算表达式时出错: {e}"
|
| 90 |
+
|
| 91 |
# load the system prompt from the file
|
| 92 |
# Ensure this file exists and has the content from Step 2
|
| 93 |
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
|
|
|
| 97 |
tools = [
|
| 98 |
wiki_search,
|
| 99 |
duckduckgo_search,
|
| 100 |
+
web_search,
|
| 101 |
+
arithmetic,
|
| 102 |
]
|
| 103 |
|
| 104 |
|