LambdaAgent added
Browse files- src/manager/agent_manager.py +64 -2
src/manager/agent_manager.py
CHANGED
@@ -3,6 +3,7 @@ from typing import Dict, Type, Any, Optional, Tuple
|
|
3 |
import os
|
4 |
import json
|
5 |
import ollama
|
|
|
6 |
from src.manager.utils.singleton import singleton
|
7 |
from src.manager.utils.streamlit_interface import output_assistant_response
|
8 |
from google import genai
|
@@ -220,6 +221,63 @@ class GroqAgent(Agent):
|
|
220 |
"""Get agent type"""
|
221 |
return self.type
|
222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
223 |
@singleton
|
224 |
class AgentManager():
|
225 |
budget_manager: BudgetManager = BudgetManager()
|
@@ -233,6 +291,7 @@ class AgentManager():
|
|
233 |
"ollama": OllamaAgent,
|
234 |
"gemini": GeminiAgent,
|
235 |
"groq": GroqAgent,
|
|
|
236 |
}
|
237 |
|
238 |
self._load_agents()
|
@@ -336,7 +395,8 @@ class AgentManager():
|
|
336 |
invoke_resource_cost,
|
337 |
create_expense_cost,
|
338 |
invoke_expense_cost,
|
339 |
-
output_expense_cost,
|
|
|
340 |
|
341 |
self.validate_budget(create_resource_cost,
|
342 |
create_expense_cost)
|
@@ -473,7 +533,6 @@ class AgentManager():
|
|
473 |
output_assistant_response(f"Error saving agent {agent_name}: {e}")
|
474 |
|
475 |
def _get_agent_type(self, base_model) -> str:
|
476 |
-
|
477 |
if base_model == "llama3.2":
|
478 |
return "ollama"
|
479 |
elif base_model == "mistral":
|
@@ -484,9 +543,12 @@ class AgentManager():
|
|
484 |
return "gemini"
|
485 |
elif "groq" in base_model:
|
486 |
return "groq"
|
|
|
|
|
487 |
else:
|
488 |
return "unknown"
|
489 |
|
|
|
490 |
def _load_agents(self) -> None:
|
491 |
"""Load agent configurations from disk"""
|
492 |
try:
|
|
|
3 |
import os
|
4 |
import json
|
5 |
import ollama
|
6 |
+
import requests
|
7 |
from src.manager.utils.singleton import singleton
|
8 |
from src.manager.utils.streamlit_interface import output_assistant_response
|
9 |
from google import genai
|
|
|
221 |
"""Get agent type"""
|
222 |
return self.type
|
223 |
|
224 |
+
class LambdaAgent(Agent):
|
225 |
+
type = "cloud"
|
226 |
+
|
227 |
+
def __init__(self,
|
228 |
+
agent_name: str,
|
229 |
+
base_model: str,
|
230 |
+
system_prompt: str,
|
231 |
+
create_resource_cost: int,
|
232 |
+
invoke_resource_cost: int,
|
233 |
+
create_expense_cost: int = 0,
|
234 |
+
invoke_expense_cost: int = 0,
|
235 |
+
output_expense_cost: int = 0,
|
236 |
+
lambda_url: str = "",
|
237 |
+
api_key: str = ""):
|
238 |
+
if not lambda_url:
|
239 |
+
raise ValueError("Lambda URL must be provided for LambdaAgent.")
|
240 |
+
|
241 |
+
self.lambda_url = lambda_url
|
242 |
+
self.api_key = api_key or os.getenv("LAMBDA_API_KEY")
|
243 |
+
if not self.api_key:
|
244 |
+
raise ValueError("Lambda API key must be provided or set in LAMBDA_API_KEY environment variable.")
|
245 |
+
|
246 |
+
super().__init__(agent_name,
|
247 |
+
base_model,
|
248 |
+
system_prompt,
|
249 |
+
create_resource_cost,
|
250 |
+
invoke_resource_cost,
|
251 |
+
create_expense_cost,
|
252 |
+
invoke_expense_cost,
|
253 |
+
output_expense_cost)
|
254 |
+
|
255 |
+
def create_model(self) -> None:
|
256 |
+
pass # Lambda already deployed
|
257 |
+
|
258 |
+
def ask_agent(self, prompt: str) -> str:
|
259 |
+
try:
|
260 |
+
headers = {
|
261 |
+
"Content-Type": "application/json",
|
262 |
+
"x-api-key": self.api_key # Required by API Gateway if enabled
|
263 |
+
}
|
264 |
+
payload = {
|
265 |
+
"prompt": prompt,
|
266 |
+
"system_prompt": self.system_prompt
|
267 |
+
}
|
268 |
+
response = requests.post(self.lambda_url, headers=headers, json=payload)
|
269 |
+
response.raise_for_status()
|
270 |
+
return response.json().get("response", "")
|
271 |
+
except Exception as e:
|
272 |
+
print(f"Error calling Lambda agent: {e}")
|
273 |
+
raise
|
274 |
+
|
275 |
+
def delete_agent(self) -> None:
|
276 |
+
pass
|
277 |
+
|
278 |
+
def get_type(self) -> str:
|
279 |
+
return self.type
|
280 |
+
|
281 |
@singleton
|
282 |
class AgentManager():
|
283 |
budget_manager: BudgetManager = BudgetManager()
|
|
|
291 |
"ollama": OllamaAgent,
|
292 |
"gemini": GeminiAgent,
|
293 |
"groq": GroqAgent,
|
294 |
+
"lambda": LambdaAgent,
|
295 |
}
|
296 |
|
297 |
self._load_agents()
|
|
|
395 |
invoke_resource_cost,
|
396 |
create_expense_cost,
|
397 |
invoke_expense_cost,
|
398 |
+
output_expense_cost,
|
399 |
+
**additional_params)
|
400 |
|
401 |
self.validate_budget(create_resource_cost,
|
402 |
create_expense_cost)
|
|
|
533 |
output_assistant_response(f"Error saving agent {agent_name}: {e}")
|
534 |
|
535 |
def _get_agent_type(self, base_model) -> str:
|
|
|
536 |
if base_model == "llama3.2":
|
537 |
return "ollama"
|
538 |
elif base_model == "mistral":
|
|
|
543 |
return "gemini"
|
544 |
elif "groq" in base_model:
|
545 |
return "groq"
|
546 |
+
elif base_model.startswith("lambda-"):
|
547 |
+
return "lambda"
|
548 |
else:
|
549 |
return "unknown"
|
550 |
|
551 |
+
|
552 |
def _load_agents(self) -> None:
|
553 |
"""Load agent configurations from disk"""
|
554 |
try:
|