Spaces:
Running
Running
agent creation for testing cost
Browse files
default_tools/test_cost/agent_creator_tool.py
ADDED
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from src.agent_manager import AgentManager
|
2 |
+
from src.config.model_selector import choose_best_model
|
3 |
+
from src.utils.runtime_selector import detect_runtime_environment
|
4 |
+
from cost_benefit import get_best_model
|
5 |
+
__all__ = ['AgentCreator']
|
6 |
+
|
7 |
+
class AgentCreator():
|
8 |
+
dependencies = ["ollama==0.4.7",
|
9 |
+
"pydantic==2.11.1",
|
10 |
+
"pydantic_core==2.33.0"]
|
11 |
+
|
12 |
+
inputSchema = {
|
13 |
+
"name": "AgentCreator",
|
14 |
+
"description": "Creates an AI agent for you. Please make sure to invoke the created agent using the AskAgent tool.",
|
15 |
+
"parameters": {
|
16 |
+
"type": "object",
|
17 |
+
"properties":{
|
18 |
+
"agent_name": {
|
19 |
+
"type": "string",
|
20 |
+
"description": "Name of the AI agent that is to be created. This name cannot have spaces or special characters. It should be a single word.",
|
21 |
+
},
|
22 |
+
"base_model": {
|
23 |
+
"type": "string",
|
24 |
+
"description": "A base model from which the new agent mode is to be created. Available models are: llama3.2, mistral, gemini-2.5-flash-preview-04-17, gemini-2.5-pro-preview-03-25, gemini-2.0-flash, gemini-2.0-flash-lite, gemini-1.5-flash, gemini-1.5-flash-8b, gemini-1.5-pro, and gemini-2.0-flash-live-001"
|
25 |
+
},
|
26 |
+
"system_prompt": {
|
27 |
+
"type": "string",
|
28 |
+
"description": "This is the system prompt that will be used to create the agent. It should be a string that describes the role of the agent and its capabilities."
|
29 |
+
},
|
30 |
+
"description": {
|
31 |
+
"type": "string",
|
32 |
+
"description": "Description of the agent. This is a string that describes the agent and its capabilities. It should be a single line description.",
|
33 |
+
},
|
34 |
+
},
|
35 |
+
"required": ["agent_name", "system_prompt", "description"],
|
36 |
+
#"required": ["agent_name", "base_model", "system_prompt", "description"],
|
37 |
+
},
|
38 |
+
"creates": {
|
39 |
+
"selector": "base_model",
|
40 |
+
"types": {
|
41 |
+
"llama3.2":{
|
42 |
+
"description": "3 Billion parameter model",
|
43 |
+
"create_cost": 10,
|
44 |
+
"invoke_cost": 20,
|
45 |
+
},
|
46 |
+
"mistral":{
|
47 |
+
"description": "7 Billion parameter model",
|
48 |
+
"create_cost": 20,
|
49 |
+
"invoke_cost": 50,
|
50 |
+
},
|
51 |
+
"gemini-2.5-flash-preview-04-17": {
|
52 |
+
"description": "Adaptive thinking, cost efficiency",
|
53 |
+
"create_cost": 20,
|
54 |
+
"invoke_cost": 50
|
55 |
+
},
|
56 |
+
"gemini-2.5-pro-preview-03-25": {
|
57 |
+
"description": "Enhanced thinking and reasoning, multimodal understanding, advanced coding, and more",
|
58 |
+
"create_cost": 20,
|
59 |
+
"invoke_cost": 50
|
60 |
+
},
|
61 |
+
"gemini-2.0-flash": {
|
62 |
+
"description": "Next generation features, speed, thinking, realtime streaming, and multimodal generation",
|
63 |
+
"create_cost": 20,
|
64 |
+
"invoke_cost": 50
|
65 |
+
},
|
66 |
+
"gemini-2.0-flash-lite": {
|
67 |
+
"description": "Cost efficiency and low latency",
|
68 |
+
"create_cost": 20,
|
69 |
+
"invoke_cost": 50
|
70 |
+
},
|
71 |
+
"gemini-1.5-flash": {
|
72 |
+
"description": "Fast and versatile performance across a diverse variety of tasks",
|
73 |
+
"create_cost": 20,
|
74 |
+
"invoke_cost": 50
|
75 |
+
},
|
76 |
+
"gemini-1.5-flash-8b": {
|
77 |
+
"description": "High volume and lower intelligence tasks",
|
78 |
+
"create_cost": 20,
|
79 |
+
"invoke_cost": 50
|
80 |
+
},
|
81 |
+
"gemini-1.5-pro": {
|
82 |
+
"description": "Complex reasoning tasks requiring more intelligence",
|
83 |
+
"create_cost": 20,
|
84 |
+
"invoke_cost": 50
|
85 |
+
},
|
86 |
+
# "gemini-embedding-exp": {
|
87 |
+
# "description": "Measuring the relatedness of text strings",
|
88 |
+
# "create_cost": 20,
|
89 |
+
# "invoke_cost": 50
|
90 |
+
# },
|
91 |
+
# "imagen-3.0-generate-002": {
|
92 |
+
# "description": "Our most advanced image generation model",
|
93 |
+
# "create_cost": 20,
|
94 |
+
# "invoke_cost": 50
|
95 |
+
# },
|
96 |
+
# "veo-2.0-generate-001": {
|
97 |
+
# "description": "High quality video generation",
|
98 |
+
# "create_cost": 20,
|
99 |
+
# "invoke_cost": 50
|
100 |
+
# },
|
101 |
+
"gemini-2.0-flash-live-001": {
|
102 |
+
"description": "Low-latency bidirectional voice and video interactions",
|
103 |
+
"create_cost": 20,
|
104 |
+
"invoke_cost": 50
|
105 |
+
}
|
106 |
+
}
|
107 |
+
}
|
108 |
+
}
|
109 |
+
|
110 |
+
def run(self, **kwargs):
|
111 |
+
print("Running Agent Creator")
|
112 |
+
agent_name = kwargs.get("agent_name")
|
113 |
+
|
114 |
+
# Get full model info (not just name)
|
115 |
+
model_info = choose_best_model(return_full=True)
|
116 |
+
base_model = kwargs.get("base_model") or choose_best_model()
|
117 |
+
base_model = model_info["model"]
|
118 |
+
token_cost = model_info.get("token_cost", 0.0001)
|
119 |
+
speed = model_info.get("tokens_sec", 30)
|
120 |
+
score = model_info.get("score", 1)
|
121 |
+
|
122 |
+
env = detect_runtime_environment()
|
123 |
+
print(f"\n[DEBUG] Detected Runtime Environment: {env}")
|
124 |
+
print(f"[DEBUG] Selected Model: {base_model}")
|
125 |
+
print(f"[DEBUG] Token Cost: {token_cost}, Speed: {speed}, Score: {score}")
|
126 |
+
|
127 |
+
system_prompt = kwargs.get("system_prompt")
|
128 |
+
description = kwargs.get("description")
|
129 |
+
#create_cost = self.inputSchema["creates"]["types"][base_model]["create_cost"]
|
130 |
+
#if base_model not in self.inputSchema["creates"]["types"]:
|
131 |
+
# print(f"[WARN] Auto-selected model '{base_model}' not in schema. Falling back to gemini-2.0-flash")
|
132 |
+
# base_model = "gemini-2.0-flash"
|
133 |
+
#invoke_cost = self.inputSchema["creates"]["types"][base_model]["invoke_cost"]
|
134 |
+
|
135 |
+
# Dynamically calculated costs
|
136 |
+
create_cost = round(10 + (token_cost * 10000) + (50 / (speed + 1)), 2)
|
137 |
+
invoke_cost = round(create_cost * 2, 2)
|
138 |
+
|
139 |
+
print(f"[INFO] Assigned Create Cost: {create_cost}, Invoke Cost: {invoke_cost}")
|
140 |
+
|
141 |
+
agent_manager = AgentManager()
|
142 |
+
try:
|
143 |
+
_, remaining_budget = agent_manager.create_agent(
|
144 |
+
agent_name=agent_name,
|
145 |
+
base_model=base_model,
|
146 |
+
system_prompt=system_prompt,
|
147 |
+
description=description,
|
148 |
+
create_cost=create_cost,
|
149 |
+
invoke_cost=invoke_cost
|
150 |
+
)
|
151 |
+
except ValueError as e:
|
152 |
+
return {
|
153 |
+
"status": "error",
|
154 |
+
"message": f"Error occurred: {str(e)}",
|
155 |
+
"output": None
|
156 |
+
}
|
157 |
+
|
158 |
+
return {
|
159 |
+
"status": "success",
|
160 |
+
"message": "Agent successfully created",
|
161 |
+
"remaining_budget": remaining_budget,
|
162 |
+
}
|
163 |
+
|
164 |
+
|