Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -374,14 +374,15 @@ def summarise_tool(query: str) -> str:
|
|
| 374 |
|
| 375 |
def _calc_tool(query: str) -> str:
|
| 376 |
import math
|
|
|
|
| 377 |
try:
|
| 378 |
-
#
|
| 379 |
if re.fullmatch(r"[0-9\.\+\-\*/%\^\(\)\s]+", query.strip()):
|
| 380 |
cleaned = query.strip().replace("^", "**")
|
| 381 |
result = ne.evaluate(cleaned)
|
| 382 |
return f"The result is: {result}"
|
| 383 |
|
| 384 |
-
#
|
| 385 |
expr = query.lower()
|
| 386 |
expr = re.sub(r'sin\(([^)]+)\)', r'sin(math.radians(\1))', expr)
|
| 387 |
expr = re.sub(r'cos\(([^)]+)\)', r'cos(math.radians(\1))', expr)
|
|
@@ -396,12 +397,15 @@ def _calc_tool(query: str) -> str:
|
|
| 396 |
return f"The result is: {result}"
|
| 397 |
|
| 398 |
except Exception:
|
| 399 |
-
# ✅ fallback:若 eval 也無法處理,交給 GPT 幫忙自然語言理解與回覆
|
| 400 |
try:
|
| 401 |
-
|
| 402 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 403 |
except Exception as e:
|
| 404 |
-
return f"Natural language
|
| 405 |
|
| 406 |
@tool("python_calc")
|
| 407 |
def python_calc_tool(query: str) -> str:
|
|
@@ -499,7 +503,7 @@ router_task = Task(
|
|
| 499 |
Based on the user's query, decide which agent or tool is best suited to handle it:
|
| 500 |
- If the query is related to the content of an uploaded file (e.g., 'what is this document about?'), send it to the **Document QA Agent**.
|
| 501 |
- If the query contains words like 'summarize', 'summary', or 'main points', use the **Summarizer Agent**.
|
| 502 |
-
- If the query involves any kind of math, calculation, percentage, angle, log, sqrt, cos, sin, arithmetic expressions, or includes phrases like "how much", "calculate", "what is X + Y", send it to the **Math Agent**.
|
| 503 |
- If the user uploaded a CSV file and asks about table content, data trends, or uses words like 'data', 'table', 'csv', 'column', or 'row', send it to the **CSV Agent**.
|
| 504 |
- If the user asks about current events, trending topics, or online information (e.g., 'What is LangChain?', 'latest news'), send it to the **Search Agent**.
|
| 505 |
- If the query is about current date, time, or day of week (e.g., 'what is today\\'s date?', 'what time is it?', 'what day is it?'), send it to the **Search Agent** instead of the General Agent.
|
|
|
|
| 374 |
|
| 375 |
def _calc_tool(query: str) -> str:
|
| 376 |
import math
|
| 377 |
+
import re
|
| 378 |
try:
|
| 379 |
+
# 處理純算式(只有數字與符號)
|
| 380 |
if re.fullmatch(r"[0-9\.\+\-\*/%\^\(\)\s]+", query.strip()):
|
| 381 |
cleaned = query.strip().replace("^", "**")
|
| 382 |
result = ne.evaluate(cleaned)
|
| 383 |
return f"The result is: {result}"
|
| 384 |
|
| 385 |
+
# 有 sin/cos/log 等字詞,自動套 math + radians
|
| 386 |
expr = query.lower()
|
| 387 |
expr = re.sub(r'sin\(([^)]+)\)', r'sin(math.radians(\1))', expr)
|
| 388 |
expr = re.sub(r'cos\(([^)]+)\)', r'cos(math.radians(\1))', expr)
|
|
|
|
| 397 |
return f"The result is: {result}"
|
| 398 |
|
| 399 |
except Exception:
|
|
|
|
| 400 |
try:
|
| 401 |
+
# ✅ fallback 給 GPT,自然語言清楚回答(避免亂符號)
|
| 402 |
+
response = llm_gpt4.invoke(f"Please calculate this and explain briefly in plain English: {query}. Avoid math symbols like $ or \\n or \\(.")
|
| 403 |
+
result = response.content if isinstance(response, AIMessage) else response
|
| 404 |
+
result = re.sub(r"\\\[.*?\\\]", "", result) # 清除 \[...\]
|
| 405 |
+
result = re.sub(r"\\\(.*?\\\)", "", result) # 清除 \(...\)
|
| 406 |
+
return result.strip()
|
| 407 |
except Exception as e:
|
| 408 |
+
return f"Natural language fallback error: {e}"
|
| 409 |
|
| 410 |
@tool("python_calc")
|
| 411 |
def python_calc_tool(query: str) -> str:
|
|
|
|
| 503 |
Based on the user's query, decide which agent or tool is best suited to handle it:
|
| 504 |
- If the query is related to the content of an uploaded file (e.g., 'what is this document about?'), send it to the **Document QA Agent**.
|
| 505 |
- If the query contains words like 'summarize', 'summary', or 'main points', use the **Summarizer Agent**.
|
| 506 |
+
- If the query involves any kind of math, calculation, percentage, angle, log, sqrt, cos, sin, arithmetic expressions, or includes phrases like "how much", "calculate", "what is X + Y", "x^2 + y^2", send it to the **Math Agent**.
|
| 507 |
- If the user uploaded a CSV file and asks about table content, data trends, or uses words like 'data', 'table', 'csv', 'column', or 'row', send it to the **CSV Agent**.
|
| 508 |
- If the user asks about current events, trending topics, or online information (e.g., 'What is LangChain?', 'latest news'), send it to the **Search Agent**.
|
| 509 |
- If the query is about current date, time, or day of week (e.g., 'what is today\\'s date?', 'what time is it?', 'what day is it?'), send it to the **Search Agent** instead of the General Agent.
|