ChienChung commited on
Commit
afb9ca0
·
verified ·
1 Parent(s): 2f37efa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -19
app.py CHANGED
@@ -373,27 +373,35 @@ def summarise_tool(query: str) -> str:
373
  return f"摘要錯誤: {e}"
374
 
375
  def _calc_tool(query: str) -> str:
 
376
  try:
377
- # 嘗試判斷是不是「單的數學算式
378
- cleaned = query.lower().replace(" ", "")
379
- math_symbols = set("0123456789+-*/().%")
380
- formula_keywords = ["sin", "cos", "tan", "log", "sqrt", "exp", "^"]
381
-
382
- is_formula = all(c in math_symbols or any(fn in cleaned for fn in formula_keywords) for c in cleaned)
383
-
384
- if is_formula:
385
- # ✅ 嘗試用 numexpr 處理
386
- expression = query.replace("^", "**") # 支援 3^2 寫法
387
- result = ne.evaluate(expression)
388
  return f"The result is: {result}"
389
- else:
390
- # ✅ 自然語言形式 GPT 回答
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
391
  response = llm_gpt4.invoke(query)
392
- if isinstance(response, AIMessage):
393
- response = response.content
394
- return response
395
- except Exception as e:
396
- return f"Calculation error: {e}"
397
 
398
  @tool("python_calc")
399
  def python_calc_tool(query: str) -> str:
@@ -491,7 +499,7 @@ router_task = Task(
491
  Based on the user's query, decide which agent or tool is best suited to handle it:
492
  - 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**.
493
  - If the query contains words like 'summarize', 'summary', or 'main points', use the **Summarizer Agent**.
494
- - If the query involves numbers, calculations, or logic (e.g., '50 * 23 - 5', 'what is 10% of 800'), send it to the **Math Agent**.
495
  - 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**.
496
  - 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**.
497
  - 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.
 
373
  return f"摘要錯誤: {e}"
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
+ # ✅ 對有 sin/cos/log/sqrt 的輸入,動加 radians 並用 eval 處理
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)
388
+ expr = re.sub(r'tan\(([^)]+)\)', r'tan(math.radians(\1))', expr)
389
+ expr = expr.replace("^", "**")
390
+
391
+ result = eval(expr, {"__builtins__": None}, {
392
+ "math": math, "sin": math.sin, "cos": math.cos, "tan": math.tan,
393
+ "log": math.log10, "sqrt": math.sqrt, "exp": math.exp,
394
+ "pi": math.pi, "e": math.e
395
+ })
396
+ return f"The result is: {result}"
397
+
398
+ except Exception:
399
+ # ✅ fallback:若 eval 也無法處理,交給 GPT 幫忙自然語言理解與回覆
400
+ try:
401
  response = llm_gpt4.invoke(query)
402
+ return response.content if isinstance(response, AIMessage) else response
403
+ except Exception as e:
404
+ return f"Natural language math error: {e}"
 
 
405
 
406
  @tool("python_calc")
407
  def python_calc_tool(query: str) -> str:
 
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.