Spaces:
Sleeping
Sleeping
Update multiagents.py
Browse files- multiagents.py +138 -14
multiagents.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# a multi agent proposal to solve HF agent course final assignment
|
| 2 |
import os
|
| 3 |
import dotenv
|
| 4 |
import openai
|
|
@@ -36,21 +35,148 @@ class OpenAIAgent:
|
|
| 36 |
if hasattr(tool, "name") and hasattr(tool, "run"):
|
| 37 |
name = tool.name
|
| 38 |
description = tool.__doc__ or ""
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
# Handle normal Python functions
|
| 49 |
elif hasattr(tool, "__name__"):
|
| 50 |
name = tool.__name__
|
| 51 |
description = tool.__doc__ or ""
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
return functions
|
| 55 |
|
| 56 |
def _execute_tool(self, tool_name: str, arguments: Dict[str, Any]):
|
|
@@ -145,10 +271,8 @@ class ManagerAgent(OpenAIAgent):
|
|
| 145 |
agent_info = "\n".join([f"- {agent.name}: {agent.description}" for agent in self.managed_agents])
|
| 146 |
|
| 147 |
system_prompt = f"""You are {self.name}. {self.description}
|
| 148 |
-
|
| 149 |
Available agents you can delegate to:
|
| 150 |
{agent_info}
|
| 151 |
-
|
| 152 |
When you need to delegate a task, clearly state which agent should handle it and what specific task they should perform.
|
| 153 |
You should coordinate the work and synthesize the results from different agents to provide a comprehensive answer.
|
| 154 |
"""
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import dotenv
|
| 3 |
import openai
|
|
|
|
| 35 |
if hasattr(tool, "name") and hasattr(tool, "run"):
|
| 36 |
name = tool.name
|
| 37 |
description = tool.__doc__ or ""
|
| 38 |
+
|
| 39 |
+
# Create proper schema based on tool name
|
| 40 |
+
if name == "search_web":
|
| 41 |
+
params = {
|
| 42 |
+
"type": "object",
|
| 43 |
+
"properties": {
|
| 44 |
+
"query": {"type": "string", "description": "Search query"}
|
| 45 |
+
},
|
| 46 |
+
"required": ["query"]
|
| 47 |
+
}
|
| 48 |
+
elif name == "fetch_webpage":
|
| 49 |
+
params = {
|
| 50 |
+
"type": "object",
|
| 51 |
+
"properties": {
|
| 52 |
+
"url": {"type": "string", "description": "URL to fetch"}
|
| 53 |
+
},
|
| 54 |
+
"required": ["url"]
|
| 55 |
+
}
|
| 56 |
+
elif name == "get_youtube_transcript":
|
| 57 |
+
params = {
|
| 58 |
+
"type": "object",
|
| 59 |
+
"properties": {
|
| 60 |
+
"url": {"type": "string", "description": "YouTube URL"}
|
| 61 |
+
},
|
| 62 |
+
"required": ["url"]
|
| 63 |
+
}
|
| 64 |
+
elif name == "get_youtube_title_description":
|
| 65 |
+
params = {
|
| 66 |
+
"type": "object",
|
| 67 |
+
"properties": {
|
| 68 |
+
"url": {"type": "string", "description": "YouTube URL"}
|
| 69 |
+
},
|
| 70 |
+
"required": ["url"]
|
| 71 |
+
}
|
| 72 |
+
elif name == "get_text_transcript_from_audio_file":
|
| 73 |
+
params = {
|
| 74 |
+
"type": "object",
|
| 75 |
+
"properties": {
|
| 76 |
+
"file_path": {"type": "string", "description": "Path to audio file"}
|
| 77 |
+
},
|
| 78 |
+
"required": ["file_path"]
|
| 79 |
+
}
|
| 80 |
+
elif name == "analyze_image":
|
| 81 |
+
params = {
|
| 82 |
+
"type": "object",
|
| 83 |
+
"properties": {
|
| 84 |
+
"image_path": {"type": "string", "description": "Path to image file"}
|
| 85 |
+
},
|
| 86 |
+
"required": ["image_path"]
|
| 87 |
+
}
|
| 88 |
+
else:
|
| 89 |
+
# Default schema for unknown tools
|
| 90 |
+
params = {
|
| 91 |
+
"type": "object",
|
| 92 |
+
"properties": {
|
| 93 |
+
"input": {"type": "string", "description": "Input for the tool"}
|
| 94 |
+
},
|
| 95 |
+
"required": ["input"]
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
functions.append({
|
| 99 |
+
"type": "function",
|
| 100 |
+
"function": {
|
| 101 |
+
"name": name,
|
| 102 |
+
"description": description,
|
| 103 |
+
"parameters": params
|
| 104 |
+
}
|
| 105 |
+
})
|
| 106 |
+
|
| 107 |
# Handle normal Python functions
|
| 108 |
elif hasattr(tool, "__name__"):
|
| 109 |
name = tool.__name__
|
| 110 |
description = tool.__doc__ or ""
|
| 111 |
+
|
| 112 |
+
# Create proper schema based on function name
|
| 113 |
+
if name == "search_web":
|
| 114 |
+
params = {
|
| 115 |
+
"type": "object",
|
| 116 |
+
"properties": {
|
| 117 |
+
"query": {"type": "string", "description": "Search query"}
|
| 118 |
+
},
|
| 119 |
+
"required": ["query"]
|
| 120 |
+
}
|
| 121 |
+
elif name == "fetch_webpage":
|
| 122 |
+
params = {
|
| 123 |
+
"type": "object",
|
| 124 |
+
"properties": {
|
| 125 |
+
"url": {"type": "string", "description": "URL to fetch"}
|
| 126 |
+
},
|
| 127 |
+
"required": ["url"]
|
| 128 |
+
}
|
| 129 |
+
elif name == "get_youtube_transcript":
|
| 130 |
+
params = {
|
| 131 |
+
"type": "object",
|
| 132 |
+
"properties": {
|
| 133 |
+
"url": {"type": "string", "description": "YouTube URL"}
|
| 134 |
+
},
|
| 135 |
+
"required": ["url"]
|
| 136 |
+
}
|
| 137 |
+
elif name == "get_youtube_title_description":
|
| 138 |
+
params = {
|
| 139 |
+
"type": "object",
|
| 140 |
+
"properties": {
|
| 141 |
+
"url": {"type": "string", "description": "YouTube URL"}
|
| 142 |
+
},
|
| 143 |
+
"required": ["url"]
|
| 144 |
+
}
|
| 145 |
+
elif name == "get_text_transcript_from_audio_file":
|
| 146 |
+
params = {
|
| 147 |
+
"type": "object",
|
| 148 |
+
"properties": {
|
| 149 |
+
"file_path": {"type": "string", "description": "Path to audio file"}
|
| 150 |
+
},
|
| 151 |
+
"required": ["file_path"]
|
| 152 |
+
}
|
| 153 |
+
elif name == "analyze_image":
|
| 154 |
+
params = {
|
| 155 |
+
"type": "object",
|
| 156 |
+
"properties": {
|
| 157 |
+
"image_path": {"type": "string", "description": "Path to image file"}
|
| 158 |
+
},
|
| 159 |
+
"required": ["image_path"]
|
| 160 |
+
}
|
| 161 |
+
else:
|
| 162 |
+
# Default schema for unknown functions
|
| 163 |
+
params = {
|
| 164 |
+
"type": "object",
|
| 165 |
+
"properties": {
|
| 166 |
+
"input": {"type": "string", "description": "Input for the function"}
|
| 167 |
+
},
|
| 168 |
+
"required": ["input"]
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
functions.append({
|
| 172 |
+
"type": "function",
|
| 173 |
+
"function": {
|
| 174 |
+
"name": name,
|
| 175 |
+
"description": description,
|
| 176 |
+
"parameters": params
|
| 177 |
+
}
|
| 178 |
+
})
|
| 179 |
+
|
| 180 |
return functions
|
| 181 |
|
| 182 |
def _execute_tool(self, tool_name: str, arguments: Dict[str, Any]):
|
|
|
|
| 271 |
agent_info = "\n".join([f"- {agent.name}: {agent.description}" for agent in self.managed_agents])
|
| 272 |
|
| 273 |
system_prompt = f"""You are {self.name}. {self.description}
|
|
|
|
| 274 |
Available agents you can delegate to:
|
| 275 |
{agent_info}
|
|
|
|
| 276 |
When you need to delegate a task, clearly state which agent should handle it and what specific task they should perform.
|
| 277 |
You should coordinate the work and synthesize the results from different agents to provide a comprehensive answer.
|
| 278 |
"""
|