-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_engine_manager.py
More file actions
104 lines (87 loc) · 3.48 KB
/
agent_engine_manager.py
File metadata and controls
104 lines (87 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import vertexai
from vertexai import agent_engines
import json
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
_vertex_ai_initialized = False
def initialize_vertex_ai(project_id: str, location: str = None):
"""
Initializes the Vertex AI SDK with the given project ID and optional location.
This function should be called before any other functions in this module.
"""
global _vertex_ai_initialized
if not project_id:
raise ValueError("Project ID cannot be None or empty for Vertex AI initialization.")
try:
vertexai.init(project=project_id, location=location)
_vertex_ai_initialized = True
logger.info(f"Vertex AI initialized successfully for project: {project_id}" + (f" and location: {location}" if location else ""))
except Exception as e:
logger.error(f"Error initializing Vertex AI for project {project_id}: {e}")
_vertex_ai_initialized = False
raise # Re-raise the exception to make the client aware of the failure
def _ensure_vertex_ai_initialized():
"""Checks if Vertex AI has been initialized."""
if not _vertex_ai_initialized:
raise RuntimeError(
"Vertex AI SDK has not been initialized. "
"Please call agent_engine_manager.initialize_vertex_ai(project_id, [location]) first."
)
def list_agents():
"""
Lists all agents in the Agent Engine.
Returns:
str: A JSON string representing a list of agents, each with name, display_name, resource_name, and create_time.
"""
_ensure_vertex_ai_initialized()
agents_list = []
for agent in agent_engines.list():
agents_list.append({
"name": agent.name,
"display_name": agent.display_name,
"resource_name": agent.resource_name,
"create_time": str(agent.create_time)
})
return json.dumps(agents_list, indent=2)
def delete_agent(resource_name):
"""
Deletes an agent from the Agent Engine by its resource name.
Args:
resource_name (str): The resource name of the agent to delete.
Returns:
str: A success message indicating the agent was deleted.
"""
_ensure_vertex_ai_initialized()
agent_engines.delete(resource_name)
return f"Agent with resource name {resource_name} deleted successfully."
def get_agent(resource_id):
"""Retrieves an agent from the Agent Engine by its resource ID."""
_ensure_vertex_ai_initialized()
# Assuming this is the correct method to retrieve by ID
agent = agent_engines.get(resource_id)
return json.dumps({
"name": agent.name,
"display_name": agent.display_name,
"resource_name": agent.resource_name,
"create_time": str(agent.create_time)
}, indent=2)
def list_agents_by_display_name(display_name):
"""
Lists agents in the Agent Engine that match a given display name.
Args:
display_name (str): The display name to filter agents by.
Returns:
str: A JSON string representing a list of agents matching the display name.
"""
_ensure_vertex_ai_initialized()
agents_list = []
for agent in agent_engines.list(filter=f'display_name="{display_name}"'):
agents_list.append({
"name": agent.name,
"display_name": agent.display_name,
"resource_name": agent.resource_name,
"create_time": str(agent.create_time)
})
return json.dumps(agents_list, indent=2)