Spaces:
Running
Running
Harshil Patel
commited on
Commit
·
fcb1a95
1
Parent(s):
e0d574f
Integrate AgentManager with tools
Browse files- src/agent_manager.py +26 -10
- tools/agent_creater_tool.py +22 -52
- tools/ask_agent_tool.py +17 -31
- tools/fire_agent.py +19 -33
- tools/get_agents_tool.py +6 -5
src/agent_manager.py
CHANGED
|
@@ -7,10 +7,12 @@ from src.singleton import singleton
|
|
| 7 |
|
| 8 |
class Agent(ABC):
|
| 9 |
|
| 10 |
-
def __init__(self, agent_name: str, base_model: str, system_prompt: str):
|
| 11 |
self.agent_name = agent_name
|
| 12 |
self.base_model = base_model
|
| 13 |
self.system_prompt = system_prompt
|
|
|
|
|
|
|
| 14 |
self.create_model()
|
| 15 |
|
| 16 |
@abstractmethod
|
|
@@ -28,6 +30,12 @@ class Agent(ABC):
|
|
| 28 |
"""delete agent"""
|
| 29 |
pass
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
class OllamaAgent(Agent):
|
| 32 |
|
| 33 |
def create_model(self):
|
|
@@ -56,7 +64,7 @@ class AgentManager():
|
|
| 56 |
def __init__(self):
|
| 57 |
self._agents = {}
|
| 58 |
self._agent_types ={
|
| 59 |
-
ollama: OllamaAgent
|
| 60 |
}
|
| 61 |
|
| 62 |
self._load_agents()
|
|
@@ -74,7 +82,7 @@ class AgentManager():
|
|
| 74 |
raise ValueError(f"Unsupported base model {base_model}")
|
| 75 |
|
| 76 |
# create agent
|
| 77 |
-
self._agents[agent_name] = agent_class(agent_name, base_model, system_prompt)
|
| 78 |
|
| 79 |
#save agent to file
|
| 80 |
self._save_agent(
|
|
@@ -91,8 +99,8 @@ class AgentManager():
|
|
| 91 |
|
| 92 |
def get_agent(self, agent_name: str) -> Agent:
|
| 93 |
"""Get existing agent by name"""
|
| 94 |
-
if agent_name in self._agents:
|
| 95 |
-
raise ValueError(f"Agent {agent_name}
|
| 96 |
return self._agents[agent_name]
|
| 97 |
|
| 98 |
def list_agents(self) -> dict:
|
|
@@ -171,7 +179,10 @@ class AgentManager():
|
|
| 171 |
print(f"Error saving agent {agent_name}: {e}")
|
| 172 |
|
| 173 |
def _get_agent_type(self, base_model)->str:
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
| 175 |
return "ollama"
|
| 176 |
else:
|
| 177 |
return "unknown"
|
|
@@ -186,18 +197,23 @@ class AgentManager():
|
|
| 186 |
models = json.loads(f.read())
|
| 187 |
|
| 188 |
for name, data in models.items():
|
|
|
|
|
|
|
| 189 |
base_model = data["base_model"]
|
| 190 |
system_prompt = data["system_prompt"]
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
|
|
|
| 194 |
|
| 195 |
if manager_class:
|
| 196 |
# Create the agent with the appropriate manager class
|
| 197 |
self._agents[name] = manager_class(
|
| 198 |
name,
|
| 199 |
base_model,
|
| 200 |
-
system_prompt
|
|
|
|
|
|
|
| 201 |
)
|
| 202 |
except Exception as e:
|
| 203 |
print(f"Error loading agents: {e}")
|
|
|
|
| 7 |
|
| 8 |
class Agent(ABC):
|
| 9 |
|
| 10 |
+
def __init__(self, agent_name: str, base_model: str, system_prompt: str, creation_cost: str, invoke_cost: str):
|
| 11 |
self.agent_name = agent_name
|
| 12 |
self.base_model = base_model
|
| 13 |
self.system_prompt = system_prompt
|
| 14 |
+
self.creation_cost = creation_cost
|
| 15 |
+
self.invoke_cost = invoke_cost
|
| 16 |
self.create_model()
|
| 17 |
|
| 18 |
@abstractmethod
|
|
|
|
| 30 |
"""delete agent"""
|
| 31 |
pass
|
| 32 |
|
| 33 |
+
def get_costs(self):
|
| 34 |
+
return {
|
| 35 |
+
"create_cost": self.creation_cost,
|
| 36 |
+
"invoke_cost": self.invoke_cost
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
class OllamaAgent(Agent):
|
| 40 |
|
| 41 |
def create_model(self):
|
|
|
|
| 64 |
def __init__(self):
|
| 65 |
self._agents = {}
|
| 66 |
self._agent_types ={
|
| 67 |
+
"ollama": OllamaAgent
|
| 68 |
}
|
| 69 |
|
| 70 |
self._load_agents()
|
|
|
|
| 82 |
raise ValueError(f"Unsupported base model {base_model}")
|
| 83 |
|
| 84 |
# create agent
|
| 85 |
+
self._agents[agent_name] = agent_class(agent_name, base_model, system_prompt, create_cost,invoke_cost )
|
| 86 |
|
| 87 |
#save agent to file
|
| 88 |
self._save_agent(
|
|
|
|
| 99 |
|
| 100 |
def get_agent(self, agent_name: str) -> Agent:
|
| 101 |
"""Get existing agent by name"""
|
| 102 |
+
if agent_name not in self._agents:
|
| 103 |
+
raise ValueError(f"Agent {agent_name} does not exists")
|
| 104 |
return self._agents[agent_name]
|
| 105 |
|
| 106 |
def list_agents(self) -> dict:
|
|
|
|
| 179 |
print(f"Error saving agent {agent_name}: {e}")
|
| 180 |
|
| 181 |
def _get_agent_type(self, base_model)->str:
|
| 182 |
+
|
| 183 |
+
if base_model == "llama3.2":
|
| 184 |
+
return "ollama"
|
| 185 |
+
elif base_model == "mistral":
|
| 186 |
return "ollama"
|
| 187 |
else:
|
| 188 |
return "unknown"
|
|
|
|
| 197 |
models = json.loads(f.read())
|
| 198 |
|
| 199 |
for name, data in models.items():
|
| 200 |
+
if name in self._agents:
|
| 201 |
+
continue
|
| 202 |
base_model = data["base_model"]
|
| 203 |
system_prompt = data["system_prompt"]
|
| 204 |
+
creation_cost = data["create_cost"]
|
| 205 |
+
invoke_cost = data["invoke_cost"]
|
| 206 |
+
model_type = self._get_agent_type(base_model)
|
| 207 |
+
manager_class = self._agent_types.get(model_type)
|
| 208 |
|
| 209 |
if manager_class:
|
| 210 |
# Create the agent with the appropriate manager class
|
| 211 |
self._agents[name] = manager_class(
|
| 212 |
name,
|
| 213 |
base_model,
|
| 214 |
+
system_prompt,
|
| 215 |
+
creation_cost,
|
| 216 |
+
invoke_cost
|
| 217 |
)
|
| 218 |
except Exception as e:
|
| 219 |
print(f"Error loading agents: {e}")
|
tools/agent_creater_tool.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
__all__ = ['AgentCreator']
|
| 4 |
|
| 5 |
class AgentCreator():
|
|
@@ -48,65 +47,36 @@ class AgentCreator():
|
|
| 48 |
}
|
| 49 |
}
|
| 50 |
}
|
| 51 |
-
|
| 52 |
-
def does_agent_exist(self, agent_name):
|
| 53 |
-
ollama = importlib.import_module("ollama")
|
| 54 |
-
all_agents = [a.model for a in ollama.list().models]
|
| 55 |
-
if agent_name in all_agents or f'{agent_name}:latest' in all_agents:
|
| 56 |
-
return True
|
| 57 |
-
|
| 58 |
-
return False
|
| 59 |
|
| 60 |
def run(self, **kwargs):
|
| 61 |
print("Running Agent Creator")
|
| 62 |
agent_name = kwargs.get("agent_name")
|
| 63 |
base_model = kwargs.get("base_model")
|
| 64 |
system_prompt = kwargs.get("system_prompt")
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
return {
|
| 70 |
"status": "error",
|
| 71 |
-
"message": "
|
| 72 |
"output": None
|
| 73 |
}
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
)
|
| 81 |
-
|
| 82 |
-
if "success" in ollama_response["status"]:
|
| 83 |
-
try:
|
| 84 |
-
with open("./models/models.json", "r", encoding="utf8") as f:
|
| 85 |
-
models = f.read()
|
| 86 |
-
models = json.loads(models)
|
| 87 |
-
models[agent_name] = {
|
| 88 |
-
"base_model": base_model,
|
| 89 |
-
"description": kwargs.get("description"),
|
| 90 |
-
"system_prompt": system_prompt,
|
| 91 |
-
"create_cost": self.inputSchema["creates"]["types"][base_model]["create_cost"],
|
| 92 |
-
"invoke_cost": self.inputSchema["creates"]["types"][base_model]["invoke_cost"],
|
| 93 |
-
}
|
| 94 |
-
with open("./models/models.json", "w", encoding="utf8") as f:
|
| 95 |
-
f.write(json.dumps(models, indent=4))
|
| 96 |
-
|
| 97 |
-
return {
|
| 98 |
-
"status": "success",
|
| 99 |
-
"message": "Agent successfully created",
|
| 100 |
-
"cost": self.inputSchema["creates"]["types"][base_model]["create_cost"],
|
| 101 |
-
}
|
| 102 |
-
except Exception as e:
|
| 103 |
-
print("Error while writing to models.json", e)
|
| 104 |
-
return {
|
| 105 |
-
"status": "error",
|
| 106 |
-
"message": f"Agent creation failed: {e}",
|
| 107 |
-
}
|
| 108 |
-
else:
|
| 109 |
-
return {
|
| 110 |
-
"status": "error",
|
| 111 |
-
"message": "Agent creation failed",
|
| 112 |
-
}
|
|
|
|
| 1 |
+
from src.agent_manager import AgentManager
|
|
|
|
| 2 |
__all__ = ['AgentCreator']
|
| 3 |
|
| 4 |
class AgentCreator():
|
|
|
|
| 47 |
}
|
| 48 |
}
|
| 49 |
}
|
| 50 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
def run(self, **kwargs):
|
| 53 |
print("Running Agent Creator")
|
| 54 |
agent_name = kwargs.get("agent_name")
|
| 55 |
base_model = kwargs.get("base_model")
|
| 56 |
system_prompt = kwargs.get("system_prompt")
|
| 57 |
+
description = kwargs.get("description")
|
| 58 |
+
create_cost = self.inputSchema["creates"]["types"][base_model]["create_cost"]
|
| 59 |
+
invoke_cost = self.inputSchema["creates"]["types"][base_model]["invoke_cost"]
|
| 60 |
|
| 61 |
+
agent_manager = AgentManager()
|
| 62 |
+
try:
|
| 63 |
+
agent_manager.create_agent(
|
| 64 |
+
agent_name=agent_name,
|
| 65 |
+
base_model=base_model,
|
| 66 |
+
system_prompt=system_prompt,
|
| 67 |
+
description=description,
|
| 68 |
+
create_cost=create_cost,
|
| 69 |
+
invoke_cost=invoke_cost
|
| 70 |
+
)
|
| 71 |
+
except ValueError as e:
|
| 72 |
return {
|
| 73 |
"status": "error",
|
| 74 |
+
"message": f"Error occurred: {str(e)}",
|
| 75 |
"output": None
|
| 76 |
}
|
| 77 |
|
| 78 |
+
return {
|
| 79 |
+
"status": "success",
|
| 80 |
+
"message": "Agent successfully created",
|
| 81 |
+
"cost": create_cost,
|
| 82 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tools/ask_agent_tool.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
-
import importlib
|
| 2 |
from src.budget_manager import BudgetManager
|
| 3 |
-
from
|
| 4 |
|
| 5 |
__all__ = ['AskAgent']
|
| 6 |
|
|
@@ -29,51 +28,38 @@ class AskAgent():
|
|
| 29 |
}
|
| 30 |
}
|
| 31 |
|
| 32 |
-
def does_agent_exist(self, ollama, agent_name):
|
| 33 |
-
all_agents = [a.model for a in ollama.list().models]
|
| 34 |
-
if agent_name in all_agents or f'{agent_name}:latest' in all_agents:
|
| 35 |
-
return True
|
| 36 |
-
|
| 37 |
-
return False
|
| 38 |
-
|
| 39 |
def run(self, **kwargs):
|
| 40 |
print("Asking agent a question")
|
| 41 |
|
| 42 |
agent_name = kwargs.get("agent_name")
|
| 43 |
prompt = kwargs.get("prompt")
|
| 44 |
-
|
| 45 |
-
ollama = importlib.import_module("ollama")
|
| 46 |
-
|
| 47 |
budget_manager = BudgetManager()
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
if agent == agent_name:
|
| 53 |
-
agent_question_cost = all_agents[agent]["invoke_cost"]
|
| 54 |
-
break
|
| 55 |
-
print("Agent question cost", agent_question_cost)
|
| 56 |
-
if not budget_manager.can_spend(agent_question_cost):
|
| 57 |
return {
|
| 58 |
"status": "error",
|
| 59 |
-
"message": f"
|
| 60 |
"output": None
|
| 61 |
}
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
return {
|
| 65 |
"status": "error",
|
| 66 |
-
"message": "
|
| 67 |
"output": None
|
| 68 |
}
|
| 69 |
|
| 70 |
-
agent_response =
|
| 71 |
-
|
| 72 |
-
messages=[{"role": "user", "content": prompt}],
|
| 73 |
-
)
|
| 74 |
-
print("Agent response", agent_response.message.content)
|
| 75 |
return {
|
| 76 |
"status": "success",
|
| 77 |
"message": "Agent has replied to the given prompt",
|
| 78 |
-
"output": agent_response
|
| 79 |
}
|
|
|
|
|
|
|
| 1 |
from src.budget_manager import BudgetManager
|
| 2 |
+
from src.agent_manager import AgentManager
|
| 3 |
|
| 4 |
__all__ = ['AskAgent']
|
| 5 |
|
|
|
|
| 28 |
}
|
| 29 |
}
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def run(self, **kwargs):
|
| 32 |
print("Asking agent a question")
|
| 33 |
|
| 34 |
agent_name = kwargs.get("agent_name")
|
| 35 |
prompt = kwargs.get("prompt")
|
| 36 |
+
agent_manger = AgentManager()
|
|
|
|
|
|
|
| 37 |
budget_manager = BudgetManager()
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
agent = agent_manger.get_agent(agent_name=agent_name)
|
| 41 |
+
except ValueError as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
return {
|
| 43 |
"status": "error",
|
| 44 |
+
"message": f"Error occurred: {str(e)}",
|
| 45 |
"output": None
|
| 46 |
}
|
| 47 |
+
|
| 48 |
+
agent_costs = agent.get_costs()
|
| 49 |
+
agent_question_cost = agent_costs["invoke_cost"]
|
| 50 |
+
print("Agent question cost", agent_question_cost)
|
| 51 |
+
|
| 52 |
+
if not budget_manager.can_spend(agent_question_cost):
|
| 53 |
return {
|
| 54 |
"status": "error",
|
| 55 |
+
"message": f"Do not have enough budget to ask the agent a question. Asking the agent costs {agent_question_cost} but only {budget_manager.get_current_remaining_budget()} is remaining",
|
| 56 |
"output": None
|
| 57 |
}
|
| 58 |
|
| 59 |
+
agent_response = agent.ask_agent(prompt=prompt)
|
| 60 |
+
print("Agent response", agent_response)
|
|
|
|
|
|
|
|
|
|
| 61 |
return {
|
| 62 |
"status": "success",
|
| 63 |
"message": "Agent has replied to the given prompt",
|
| 64 |
+
"output": agent_response,
|
| 65 |
}
|
tools/fire_agent.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
-
import importlib
|
| 2 |
from src.budget_manager import BudgetManager
|
|
|
|
| 3 |
|
| 4 |
__all__ = ['FireAgent']
|
| 5 |
|
|
@@ -22,46 +22,32 @@ class FireAgent():
|
|
| 22 |
"required": ["agent_name"],
|
| 23 |
}
|
| 24 |
}
|
| 25 |
-
|
| 26 |
-
def does_agent_exist(self, agent_name):
|
| 27 |
-
ollama = importlib.import_module("ollama")
|
| 28 |
-
all_agents = [a.model for a in ollama.list().models]
|
| 29 |
-
if agent_name in all_agents or f'{agent_name}:latest' in all_agents:
|
| 30 |
-
return True
|
| 31 |
-
|
| 32 |
-
return False
|
| 33 |
|
| 34 |
def run(self, **kwargs):
|
| 35 |
-
print("Running Agent
|
| 36 |
agent_name= kwargs.get("agent_name")
|
| 37 |
-
ollama = importlib.import_module("ollama")
|
| 38 |
-
json = importlib.import_module("json")
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
return {
|
| 42 |
"status": "error",
|
| 43 |
-
"message": "
|
| 44 |
"output": None
|
| 45 |
}
|
| 46 |
-
|
| 47 |
budget_manager = BudgetManager()
|
| 48 |
|
| 49 |
-
|
| 50 |
-
models = f.read()
|
| 51 |
-
models = json.loads(models)
|
| 52 |
-
budget_manager.add_to_expense(-1* int(models[agent_name]["create_cost"]))
|
| 53 |
-
del models[agent_name]
|
| 54 |
-
with open("./models/models.json", "w", encoding="utf8") as f:
|
| 55 |
-
f.write(json.dumps(models, indent=4))
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
}
|
| 63 |
-
else:
|
| 64 |
-
return {
|
| 65 |
-
"status": "error",
|
| 66 |
-
"message": "Agent firing failed",
|
| 67 |
-
}
|
|
|
|
|
|
|
| 1 |
from src.budget_manager import BudgetManager
|
| 2 |
+
from src.agent_manager import AgentManager
|
| 3 |
|
| 4 |
__all__ = ['FireAgent']
|
| 5 |
|
|
|
|
| 22 |
"required": ["agent_name"],
|
| 23 |
}
|
| 24 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def run(self, **kwargs):
|
| 27 |
+
print("Running Fire Agent")
|
| 28 |
agent_name= kwargs.get("agent_name")
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
agent_manager = AgentManager()
|
| 31 |
+
|
| 32 |
+
agent = agent_manager.get_agent(agent_name=agent_name)
|
| 33 |
+
|
| 34 |
+
agent_costs = agent.get_costs()
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
agent_manager.delete_agent(agent_name=agent_name)
|
| 38 |
+
except ValueError as e:
|
| 39 |
return {
|
| 40 |
"status": "error",
|
| 41 |
+
"message": f"Error occurred: {str(e)}",
|
| 42 |
"output": None
|
| 43 |
}
|
| 44 |
+
|
| 45 |
budget_manager = BudgetManager()
|
| 46 |
|
| 47 |
+
budget_manager.add_to_expense(-1* int(agent_costs["create_cost"]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
return {
|
| 50 |
+
"status": "success",
|
| 51 |
+
"message": "Agent successfully fired.",
|
| 52 |
+
"current_expense": budget_manager.get_current_expense()
|
| 53 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tools/get_agents_tool.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import
|
| 2 |
|
| 3 |
__all__ = ['GetAgents']
|
| 4 |
|
|
@@ -16,11 +16,12 @@ class GetAgents():
|
|
| 16 |
}
|
| 17 |
|
| 18 |
def run(self, **kwargs):
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
return {
|
| 23 |
"status": "success",
|
| 24 |
"message": "Agents list retrieved successfully",
|
| 25 |
-
"agents":
|
| 26 |
}
|
|
|
|
| 1 |
+
from src.agent_manager import AgentManager
|
| 2 |
|
| 3 |
__all__ = ['GetAgents']
|
| 4 |
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
def run(self, **kwargs):
|
| 19 |
+
|
| 20 |
+
agent_manger = AgentManager()
|
| 21 |
+
agents = agent_manger.list_agents()
|
| 22 |
+
|
| 23 |
return {
|
| 24 |
"status": "success",
|
| 25 |
"message": "Agents list retrieved successfully",
|
| 26 |
+
"agents": agents,
|
| 27 |
}
|