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,8 +639,10 @@ 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]:
"""Returns a list of default tools that can be used with OctoToolsSwarm."""
image_captioner = Tool(
name="Image_Captioner_Tool", name="Image_Captioner_Tool",
description="Generates a caption for an image.", description="Generates a caption for an image.",
metadata={ metadata={
@ -650,9 +652,9 @@ image_captioner = Tool(
"best_practices": "Use with clear, well-lit images. Provide specific prompts for better results.", "best_practices": "Use with clear, well-lit images. Provide specific prompts for better results.",
}, },
execute_func=image_captioner_execute, execute_func=image_captioner_execute,
) )
object_detector = Tool( object_detector = Tool(
name="Object_Detector_Tool", name="Object_Detector_Tool",
description="Detects objects in an image.", description="Detects objects in an image.",
metadata={ metadata={
@ -662,9 +664,9 @@ object_detector = Tool(
"best_practices": "Provide a list of specific object labels to detect. Use high-resolution images.", "best_practices": "Provide a list of specific object labels to detect. Use high-resolution images.",
}, },
execute_func=object_detector_execute, execute_func=object_detector_execute,
) )
web_search = Tool( web_search = Tool(
name="Web_Search_Tool", name="Web_Search_Tool",
description="Performs a web search.", description="Performs a web search.",
metadata={ metadata={
@ -674,9 +676,9 @@ web_search = Tool(
"best_practices": "Use specific and descriptive keywords for better results.", "best_practices": "Use specific and descriptive keywords for better results.",
}, },
execute_func=web_search_execute, execute_func=web_search_execute,
) )
calculator = Tool( calculator = Tool(
name="Python_Calculator_Tool", name="Python_Calculator_Tool",
description="Evaluates a Python expression.", description="Evaluates a Python expression.",
metadata={ metadata={
@ -686,20 +688,38 @@ calculator = Tool(
"best_practices": "Use for basic arithmetic and simple calculations.", "best_practices": "Use for basic arithmetic and simple calculations.",
}, },
execute_func=python_calculator_execute, execute_func=python_calculator_execute,
) )
# Create an OctoToolsSwarm agent return [image_captioner, object_detector, web_search, calculator]
agent = OctoToolsSwarm(tools=[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
# with open("example.png", "w") as f:
# f.write("Dummy image content")
# Only execute the example when this script is run directly
# if __name__ == "__main__":
# print("Running OctoToolsSwarm example...")
# # Create an OctoToolsSwarm agent with default tools
# tools = get_default_tools()
# agent = OctoToolsSwarm(tools=tools)
# # Example query
# 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" # 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) # result = agent.run(query, image=image_path)
# # Display results
# print("\n=== FINAL ANSWER ===")
# print(result["final_answer"]) # print(result["final_answer"])
# print(result["trajectory"]) # Uncomment to see the full trajectory
# print("\n".join(result["conversation"])) # Uncomment to see agent conversation # 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