wt002 commited on
Commit
7b1f7dd
·
verified ·
1 Parent(s): 230cd5f

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +97 -36
agent.py CHANGED
@@ -81,7 +81,9 @@ def calculator(inputs: dict):
81
  return a % b
82
  else:
83
  return "Unknown operation"
84
-
 
 
85
 
86
  @tool
87
  def wiki_search(query: str) -> str:
@@ -282,12 +284,16 @@ enabled_tool_names = [
282
 
283
  tools = [tool_map[name] for name in enabled_tool_names]
284
 
 
 
285
  tools = []
286
  for name in enabled_tool_names:
287
- if name in tool_map:
288
- tools.append(tool_map[name])
289
- else:
290
- print(f"⚠️ Warning: Tool '{name}' not found in tool_map. Skipping.")
 
 
291
 
292
  # -------------------------------
293
  # Step 2: Load the JSON file or tasks (Replace this part if you're loading tasks dynamically)
@@ -503,57 +509,61 @@ def get_llm(provider: str, config: dict):
503
 
504
 
505
 
506
-
507
  # ----------------------------------------------------------------
508
  # Planning & Execution Logic
509
  # ----------------------------------------------------------------
510
- def planner(question: str, tools: list) -> list:
 
 
 
 
511
  question = question.lower().strip()
512
 
513
- # Define intent-based keywords (broad categories)
514
  intent_keywords = {
515
  "math": ["calculate", "evaluate", "add", "subtract", "multiply", "divide", "modulus", "plus", "minus", "times"],
516
  "wiki_search": ["who is", "what is", "define", "explain", "tell me about", "overview of"],
517
  "web_search": ["search", "find", "look up", "google", "latest news", "current info"],
518
- "arxiv": ["arxiv", "research paper", "scientific paper", "preprint"],
519
- "youtube": ["youtube", "watch", "play video", "show me a video"],
520
  "video_analysis": ["analyze video", "summarize video", "video content"],
521
  "data_analysis": ["analyze", "plot", "graph", "data", "visualize"],
522
  "wikidata_query": ["wikidata", "sparql", "run sparql", "query wikidata"],
523
- "general_qa": ["why", "how", "difference between", "compare", "what happens", "reason for", "cause of", "effect of"]
524
  }
525
 
526
- matched_tools = []
527
-
528
- # Try to find tools whose description matches intent
529
  for intent, keywords in intent_keywords.items():
530
  if any(keyword in question for keyword in keywords):
531
- for tool in tools:
532
- description = getattr(tool, "description", "").lower()
533
- name = getattr(tool, "name", "").lower()
534
-
535
- # Match based on intent keywords appearing in tool description or name
536
- if intent in description or intent in name:
537
- matched_tools.append(tool)
538
-
539
- # Break after first matching intent — you can remove this if you want to allow multi-intent matching
540
- if matched_tools:
541
- break
542
 
543
- # Fallback 1: try using general-purpose tools if available
 
 
 
 
 
 
 
 
 
544
  if not matched_tools:
545
  matched_tools = [
546
- tool for tool in tools
547
- if "default" in getattr(tool, "name", "").lower()
548
  or "qa" in getattr(tool, "description", "").lower()
549
  ]
550
 
551
- # Fallback 2: return first tool to prevent failure
552
- return matched_tools if matched_tools else [tools[0]]
553
 
554
 
555
 
556
  def task_classifier(question: str) -> str:
 
 
 
557
  question = question.lower().strip()
558
 
559
  # Context-aware intent patterns
@@ -599,6 +609,44 @@ def task_classifier(question: str) -> str:
599
 
600
  return "default"
601
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
602
 
603
  # Function to extract math operation from the question
604
 
@@ -640,18 +688,31 @@ def extract_math_from_question(question: str):
640
 
641
  # Example tool set (adjust these to match your actual tool names)
642
  tools = {
643
- "math": calc_tool, # Example tool for math tasks
644
- "wiki_search": wiki_tool, # Example tool for wiki search tasks
645
- "retriever": retriever_tool, # Example tool for retriever tasks
646
- "default": default_tool # Fallback tool
 
 
 
 
 
 
647
  }
648
 
 
649
  # The task order can also include the tools for each task
650
  priority_order = [
651
- {"task": "math", "tool": "math"}, # Priority task and tool
652
  {"task": "wiki_search", "tool": "wiki_search"},
 
 
 
653
  {"task": "retriever", "tool": "retriever"},
654
- {"task": "default", "tool": "default"} # Fallback tool
 
 
 
655
  ]
656
 
657
  def decide_task(state: dict) -> str:
 
81
  return a % b
82
  else:
83
  return "Unknown operation"
84
+
85
+
86
+
87
 
88
  @tool
89
  def wiki_search(query: str) -> str:
 
284
 
285
  tools = [tool_map[name] for name in enabled_tool_names]
286
 
287
+
288
+ # Safe version
289
  tools = []
290
  for name in enabled_tool_names:
291
+ if name not in tool_map:
292
+ print(f"❌ Tool not found: {name}")
293
+ continue
294
+ tools.append(tool_map[name])
295
+
296
+
297
 
298
  # -------------------------------
299
  # Step 2: Load the JSON file or tasks (Replace this part if you're loading tasks dynamically)
 
509
 
510
 
511
 
 
512
  # ----------------------------------------------------------------
513
  # Planning & Execution Logic
514
  # ----------------------------------------------------------------
515
+ def planner(question: str, tools: list) -> tuple:
516
+ """
517
+ Select the best-matching tool(s) for a question based on keyword-based intent detection and tool metadata.
518
+ Returns the detected intent and matched tools.
519
+ """
520
  question = question.lower().strip()
521
 
522
+ # Define intent-based keywords
523
  intent_keywords = {
524
  "math": ["calculate", "evaluate", "add", "subtract", "multiply", "divide", "modulus", "plus", "minus", "times"],
525
  "wiki_search": ["who is", "what is", "define", "explain", "tell me about", "overview of"],
526
  "web_search": ["search", "find", "look up", "google", "latest news", "current info"],
527
+ "arxiv_search": ["arxiv", "research paper", "scientific paper", "preprint"],
528
+ "get_youtube_transcript": ["youtube", "watch", "play video", "show me a video"],
529
  "video_analysis": ["analyze video", "summarize video", "video content"],
530
  "data_analysis": ["analyze", "plot", "graph", "data", "visualize"],
531
  "wikidata_query": ["wikidata", "sparql", "run sparql", "query wikidata"],
532
+ "default": ["why", "how", "difference between", "compare", "what happens", "reason for", "cause of", "effect of"]
533
  }
534
 
535
+ # Step 1: Identify intent
536
+ detected_intent = None
 
537
  for intent, keywords in intent_keywords.items():
538
  if any(keyword in question for keyword in keywords):
539
+ detected_intent = intent
540
+ break
 
 
 
 
 
 
 
 
 
541
 
542
+ # Step 2: Match tools by intent
543
+ matched_tools = []
544
+ if detected_intent:
545
+ for tool in tools:
546
+ name = getattr(tool, "name", "").lower()
547
+ description = getattr(tool, "description", "").lower()
548
+ if detected_intent in name or detected_intent in description:
549
+ matched_tools.append(tool)
550
+
551
+ # Step 3: Fallback to general-purpose/default tools if no match found
552
  if not matched_tools:
553
  matched_tools = [
554
+ tool for tool in tools
555
+ if "default" in getattr(tool, "name", "").lower()
556
  or "qa" in getattr(tool, "description", "").lower()
557
  ]
558
 
559
+ return detected_intent, matched_tools if matched_tools else [tools[0]]
 
560
 
561
 
562
 
563
  def task_classifier(question: str) -> str:
564
+ """
565
+ Classifies the question into one of the predefined task categories.
566
+ """
567
  question = question.lower().strip()
568
 
569
  # Context-aware intent patterns
 
609
 
610
  return "default"
611
 
612
+ def select_tool_and_run(question: str, tools: list) -> any:
613
+ """
614
+ Select the best tool based on question intent and run it.
615
+ """
616
+ # Step 1: Use task_classifier to identify the intent of the question
617
+ intent = task_classifier(question)
618
+
619
+ # Step 2: Use the intent_tool_map to map the intent to the tool
620
+ intent_tool_map = {
621
+ "math": "calculator",
622
+ "wiki_search": "wiki_tool",
623
+ "web_search": "web_search_tool",
624
+ "arxiv": "arxiv_tool",
625
+ "get_youtube_transcript": "youtube_tool",
626
+ "video_analysis": "video_tool",
627
+ "analyze_tool": "analyze_tool",
628
+ "wikidata_query": "wikidata_query",
629
+ "default": "default_tool"
630
+ }
631
+
632
+ tool_name = intent_tool_map.get(intent, "default_tool")
633
+
634
+ # Step 3: Get the selected tool
635
+ tool_func = tools.get(tool_name)
636
+
637
+ if not tool_func:
638
+ print(f"No matching tool found for {intent}")
639
+ return None
640
+
641
+ # Step 4: Call the tool function and return the result
642
+ return tool_func.run(question) # or tool_func(question), depending on how your tools are structured
643
+
644
+ # Run the function to select and execute the tool
645
+ result = select_tool_and_run(question, tools)
646
+
647
+ # Output the result
648
+ print(result)
649
+
650
 
651
  # Function to extract math operation from the question
652
 
 
688
 
689
  # Example tool set (adjust these to match your actual tool names)
690
  tools = {
691
+ "math": calculator,
692
+ "wiki_search": wiki_tool,
693
+ "web_search": web_search_tool,
694
+ "arvix_search": arvix_tool,
695
+ "retriever": retriever_tool,
696
+ "get_youtube_transcript": youtube_tool,
697
+ "extract_video_id": video_tool,
698
+ "analyze_attachment": analyze_tool,
699
+ "wikidata_query": wikiq_tool,
700
+ "default": default_tool
701
  }
702
 
703
+
704
  # The task order can also include the tools for each task
705
  priority_order = [
706
+ {"task": "math", "tool": "math"},
707
  {"task": "wiki_search", "tool": "wiki_search"},
708
+ {"task": "web_search", "tool": "web_search"},
709
+ {"task": "arvix_search", "tool": "arvix_search"},
710
+ {"task": "wikidata_query", "tool": "wikidata_query"},
711
  {"task": "retriever", "tool": "retriever"},
712
+ {"task": "get_youtube_transcript", "tool": "get_youtube_transcript"},
713
+ {"task": "extract_video_id", "tool": "extract_video_id"},
714
+ {"task": "analyze_attachment", "tool": "analyze_attachment"},
715
+ {"task": "default", "tool": "default"} # Fallback
716
  ]
717
 
718
  def decide_task(state: dict) -> str: