Add get_default_tools utility function to OctoToolsSwarm

pull/787/head
harshalmore31 2 months ago
parent 23666c345a
commit cd65990ade

@ -81,7 +81,7 @@ from swarms.structs.swarms_api import (
AgentInput, AgentInput,
) )
from swarms.structs.talk_hier import TalkHier, AgentRole, CommunicationEvent from swarms.structs.talk_hier import TalkHier, AgentRole, CommunicationEvent
from swarms.structs.octotools import OctoToolsSwarm, Tool, ToolType from swarms.structs.octotools import OctoToolsSwarm, Tool, ToolType, get_default_tools
__all__ = [ __all__ = [
"Agent", "Agent",
@ -151,6 +151,7 @@ __all__ = [
"OctoToolsSwarm", "OctoToolsSwarm",
"Tool", "Tool",
"ToolType", "ToolType",
"get_default_tools",
"TalkHier", "TalkHier",
"AgentRole", "AgentRole",
"CommunicationEvent", "CommunicationEvent",

@ -639,67 +639,87 @@ def python_calculator_execute(expression: str, **kwargs) -> str:
return f"Error: {e}" return f"Error: {e}"
# Create Tool instances # Create utility function to get default tools
image_captioner = Tool( def get_default_tools() -> List[Tool]:
name="Image_Captioner_Tool", """Returns a list of default tools that can be used with OctoToolsSwarm."""
description="Generates a caption for an image.", image_captioner = Tool(
metadata={ name="Image_Captioner_Tool",
"input_types": {"image": "str", "prompt": "str"}, description="Generates a caption for an image.",
"output_type": "str", metadata={
"limitations": "May struggle with complex scenes or ambiguous objects.", "input_types": {"image": "str", "prompt": "str"},
"best_practices": "Use with clear, well-lit images. Provide specific prompts for better results.", "output_type": "str",
}, "limitations": "May struggle with complex scenes or ambiguous objects.",
execute_func=image_captioner_execute, "best_practices": "Use with clear, well-lit images. Provide specific prompts for better results.",
) },
execute_func=image_captioner_execute,
object_detector = Tool( )
name="Object_Detector_Tool",
description="Detects objects in an image.", object_detector = Tool(
metadata={ name="Object_Detector_Tool",
"input_types": {"image": "str", "labels": "list"}, description="Detects objects in an image.",
"output_type": "list", metadata={
"limitations": "Accuracy depends on the quality of the image and the clarity of the objects.", "input_types": {"image": "str", "labels": "list"},
"best_practices": "Provide a list of specific object labels to detect. Use high-resolution images.", "output_type": "list",
}, "limitations": "Accuracy depends on the quality of the image and the clarity of the objects.",
execute_func=object_detector_execute, "best_practices": "Provide a list of specific object labels to detect. Use high-resolution images.",
) },
execute_func=object_detector_execute,
web_search = Tool( )
name="Web_Search_Tool",
description="Performs a web search.", web_search = Tool(
metadata={ name="Web_Search_Tool",
"input_types": {"query": "str"}, description="Performs a web search.",
"output_type": "str", metadata={
"limitations": "May not find specific or niche information.", "input_types": {"query": "str"},
"best_practices": "Use specific and descriptive keywords for better results.", "output_type": "str",
}, "limitations": "May not find specific or niche information.",
execute_func=web_search_execute, "best_practices": "Use specific and descriptive keywords for better results.",
) },
execute_func=web_search_execute,
calculator = Tool( )
name="Python_Calculator_Tool",
description="Evaluates a Python expression.", calculator = Tool(
metadata={ name="Python_Calculator_Tool",
"input_types": {"expression": "str"}, description="Evaluates a Python expression.",
"output_type": "str", metadata={
"limitations": "Cannot handle complex mathematical functions or libraries.", "input_types": {"expression": "str"},
"best_practices": "Use for basic arithmetic and simple calculations.", "output_type": "str",
}, "limitations": "Cannot handle complex mathematical functions or libraries.",
execute_func=python_calculator_execute, "best_practices": "Use for basic arithmetic and simple calculations.",
) },
execute_func=python_calculator_execute,
# Create an OctoToolsSwarm agent )
agent = OctoToolsSwarm(tools=[image_captioner, object_detector, web_search, calculator])
return [image_captioner, object_detector, web_search, calculator]
# Run the agent with a query
# query = "Who is the president of US, final all the PM of humans?"
# # Create a dummy image file for testing # Only execute the example when this script is run directly
# with open("example.png", "w") as f: # if __name__ == "__main__":
# f.write("Dummy image content") # print("Running OctoToolsSwarm example...")
# image_path = "example.png" # # Create an OctoToolsSwarm agent with default tools
# result = agent.run(query, image=image_path) # tools = get_default_tools()
# agent = OctoToolsSwarm(tools=tools)
# print(result["final_answer"])
# print(result["trajectory"]) # Uncomment to see the full trajectory # # Example query
# print("\n".join(result["conversation"])) # Uncomment to see agent conversation # query = "What is the square root of the number of objects in this image?"
# # Create a dummy image file for testing if it doesn't exist
# image_path = "example.png"
# if not os.path.exists(image_path):
# with open(image_path, "w") as f:
# f.write("Dummy image content")
# print(f"Created dummy image file: {image_path}")
# # Run the agent
# result = agent.run(query, image=image_path)
# # Display results
# print("\n=== FINAL ANSWER ===")
# print(result["final_answer"])
# print("\n=== TRAJECTORY SUMMARY ===")
# for step in result["trajectory"]:
# print(f"Step {step.get('step', 'N/A')}: {step.get('component', 'Unknown')}")
# print("\nOctoToolsSwarm example completed.")
Loading…
Cancel
Save