Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -490,7 +490,7 @@ def get_llm(provider: str, config: dict):
|
|
| 490 |
def planner(question: str, tools: list) -> list:
|
| 491 |
question = question.lower().strip()
|
| 492 |
|
| 493 |
-
# Define
|
| 494 |
intent_keywords = {
|
| 495 |
"math": ["calculate", "evaluate", "add", "subtract", "multiply", "divide", "modulus", "plus", "minus", "times"],
|
| 496 |
"wiki_search": ["who is", "what is", "define", "explain", "tell me about", "overview of"],
|
|
@@ -505,22 +505,31 @@ def planner(question: str, tools: list) -> list:
|
|
| 505 |
|
| 506 |
matched_tools = []
|
| 507 |
|
| 508 |
-
#
|
| 509 |
-
for
|
| 510 |
-
|
| 511 |
-
|
|
|
|
|
|
|
| 512 |
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
matched_tools.append(tool)
|
| 517 |
-
break # No need to check other keywords for this tool once matched
|
| 518 |
|
| 519 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 520 |
if not matched_tools:
|
| 521 |
-
matched_tools = [
|
|
|
|
|
|
|
|
|
|
|
|
|
| 522 |
|
| 523 |
-
|
|
|
|
| 524 |
|
| 525 |
|
| 526 |
|
|
@@ -572,9 +581,11 @@ def task_classifier(question: str) -> str:
|
|
| 572 |
|
| 573 |
|
| 574 |
# Function to extract math operation from the question
|
|
|
|
| 575 |
def extract_math_from_question(question: str):
|
| 576 |
question = question.lower()
|
| 577 |
-
|
|
|
|
| 578 |
ops = {
|
| 579 |
"add": "+", "plus": "+",
|
| 580 |
"subtract": "-", "minus": "-",
|
|
@@ -584,18 +595,29 @@ def extract_math_from_question(question: str):
|
|
| 584 |
}
|
| 585 |
|
| 586 |
for word, symbol in ops.items():
|
| 587 |
-
question =
|
| 588 |
|
| 589 |
-
#
|
| 590 |
match = re.search(r'(\d+)\s*([\+\-\*/%])\s*(\d+)', question)
|
| 591 |
if match:
|
| 592 |
num1 = int(match.group(1))
|
| 593 |
operator = match.group(2)
|
| 594 |
num2 = int(match.group(3))
|
| 595 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 596 |
return None
|
| 597 |
|
| 598 |
|
|
|
|
| 599 |
# Example tool set (adjust these to match your actual tool names)
|
| 600 |
tools = {
|
| 601 |
"math": calc_tool, # Example tool for math tasks
|
|
|
|
| 490 |
def planner(question: str, tools: list) -> list:
|
| 491 |
question = question.lower().strip()
|
| 492 |
|
| 493 |
+
# Define intent-based keywords (broad categories)
|
| 494 |
intent_keywords = {
|
| 495 |
"math": ["calculate", "evaluate", "add", "subtract", "multiply", "divide", "modulus", "plus", "minus", "times"],
|
| 496 |
"wiki_search": ["who is", "what is", "define", "explain", "tell me about", "overview of"],
|
|
|
|
| 505 |
|
| 506 |
matched_tools = []
|
| 507 |
|
| 508 |
+
# Try to find tools whose description matches intent
|
| 509 |
+
for intent, keywords in intent_keywords.items():
|
| 510 |
+
if any(keyword in question for keyword in keywords):
|
| 511 |
+
for tool in tools:
|
| 512 |
+
description = getattr(tool, "description", "").lower()
|
| 513 |
+
name = getattr(tool, "name", "").lower()
|
| 514 |
|
| 515 |
+
# Match based on intent keywords appearing in tool description or name
|
| 516 |
+
if intent in description or intent in name:
|
| 517 |
+
matched_tools.append(tool)
|
|
|
|
|
|
|
| 518 |
|
| 519 |
+
# Break after first matching intent — you can remove this if you want to allow multi-intent matching
|
| 520 |
+
if matched_tools:
|
| 521 |
+
break
|
| 522 |
+
|
| 523 |
+
# Fallback 1: try using general-purpose tools if available
|
| 524 |
if not matched_tools:
|
| 525 |
+
matched_tools = [
|
| 526 |
+
tool for tool in tools
|
| 527 |
+
if "default" in getattr(tool, "name", "").lower()
|
| 528 |
+
or "qa" in getattr(tool, "description", "").lower()
|
| 529 |
+
]
|
| 530 |
|
| 531 |
+
# Fallback 2: return first tool to prevent failure
|
| 532 |
+
return matched_tools if matched_tools else [tools[0]]
|
| 533 |
|
| 534 |
|
| 535 |
|
|
|
|
| 581 |
|
| 582 |
|
| 583 |
# Function to extract math operation from the question
|
| 584 |
+
|
| 585 |
def extract_math_from_question(question: str):
|
| 586 |
question = question.lower()
|
| 587 |
+
|
| 588 |
+
# Map natural language to symbols
|
| 589 |
ops = {
|
| 590 |
"add": "+", "plus": "+",
|
| 591 |
"subtract": "-", "minus": "-",
|
|
|
|
| 595 |
}
|
| 596 |
|
| 597 |
for word, symbol in ops.items():
|
| 598 |
+
question = re.sub(rf"\b{word}\b", symbol, question)
|
| 599 |
|
| 600 |
+
# Extract math expression like "12 + 5"
|
| 601 |
match = re.search(r'(\d+)\s*([\+\-\*/%])\s*(\d+)', question)
|
| 602 |
if match:
|
| 603 |
num1 = int(match.group(1))
|
| 604 |
operator = match.group(2)
|
| 605 |
num2 = int(match.group(3))
|
| 606 |
+
return {
|
| 607 |
+
"a": num1,
|
| 608 |
+
"b": num2,
|
| 609 |
+
"operation": {
|
| 610 |
+
"+": "add",
|
| 611 |
+
"-": "subtract",
|
| 612 |
+
"*": "multiply",
|
| 613 |
+
"/": "divide",
|
| 614 |
+
"%": "modulus"
|
| 615 |
+
}[operator]
|
| 616 |
+
}
|
| 617 |
return None
|
| 618 |
|
| 619 |
|
| 620 |
+
|
| 621 |
# Example tool set (adjust these to match your actual tool names)
|
| 622 |
tools = {
|
| 623 |
"math": calc_tool, # Example tool for math tasks
|