34
34
from aixplain .utils import config
35
35
from typing import Dict , List , Optional , Text , Union
36
36
37
- from aixplain .factories .agent_factory .utils import build_agent , validate_llm , validate_name
38
37
from aixplain .utils .file_utils import _request_with_retry
39
38
from urllib .parse import urljoin
40
39
@@ -65,74 +64,49 @@ def create(
65
64
Returns:
66
65
Agent: created Agent
67
66
"""
68
- validate_name (name )
69
- # validate LLM ID
70
- validate_llm (llm_id )
67
+ from aixplain .factories .agent_factory .utils import build_agent
71
68
69
+ agent = None
70
+ url = urljoin (config .BACKEND_URL , "sdk/agents" )
71
+ headers = {"x-api-key" : api_key }
72
+
73
+ if isinstance (supplier , dict ):
74
+ supplier = supplier ["code" ]
75
+ elif isinstance (supplier , Supplier ):
76
+ supplier = supplier .value ["code" ]
77
+
78
+ payload = {
79
+ "name" : name ,
80
+ "assets" : [tool .to_dict () for tool in tools ],
81
+ "description" : description ,
82
+ "supplier" : supplier ,
83
+ "version" : version ,
84
+ "llmId" : llm_id ,
85
+ "status" : "draft" ,
86
+ }
87
+ agent = build_agent (payload = payload , api_key = api_key )
88
+ agent .validate ()
89
+ response = "Unspecified error"
72
90
try :
73
- agent = None
74
- url = urljoin (config .BACKEND_URL , "sdk/agents" )
75
- headers = {"x-api-key" : api_key }
76
-
77
- if isinstance (supplier , dict ):
78
- supplier = supplier ["code" ]
79
- elif isinstance (supplier , Supplier ):
80
- supplier = supplier .value ["code" ]
81
-
82
- tool_payload = []
83
- for tool in tools :
84
- if isinstance (tool , ModelTool ):
85
- tool .validate ()
86
- tool_payload .append (
87
- {
88
- "function" : tool .function .value if tool .function is not None else None ,
89
- "type" : "model" ,
90
- "description" : tool .description ,
91
- "supplier" : tool .supplier .value ["code" ] if tool .supplier else None ,
92
- "version" : tool .version if tool .version else None ,
93
- "assetId" : tool .model ,
94
- }
95
- )
96
- elif isinstance (tool , PipelineTool ):
97
- tool .validate ()
98
- tool_payload .append (
99
- {
100
- "assetId" : tool .pipeline ,
101
- "description" : tool .description ,
102
- "type" : "pipeline" ,
103
- }
104
- )
105
- else :
106
- raise Exception ("Agent Creation Error: Tool type not supported." )
107
-
108
- payload = {
109
- "name" : name ,
110
- "assets" : tool_payload ,
111
- "description" : description ,
112
- "supplier" : supplier ,
113
- "version" : version ,
114
- "llmId" : llm_id ,
115
- }
116
-
117
- logging .info (f"Start service for POST Create Agent - { url } - { headers } - { json .dumps (payload )} " )
91
+ logging .debug (f"Start service for POST Create Agent - { url } - { headers } - { json .dumps (payload )} " )
118
92
r = _request_with_retry ("post" , url , headers = headers , json = payload )
119
- if 200 < = r .status_code < 300 :
120
- response = r . json ()
121
- agent = build_agent ( payload = response , api_key = api_key )
122
- else :
123
- error = r .json ()
124
- error_msg = "Agent Onboarding Error: Please contact the administrators."
125
- if "message" in error :
126
- msg = error [ "message" ]
127
- if error [ "message" ] == "err.name_already_exists" :
128
- msg = "Agent name already exists."
129
- elif error ["message" ] == "err.asset_is_not_available " :
130
- msg = "Some tools are not available ."
131
- error_msg = f"Agent Onboarding Error (HTTP { r . status_code } ): { msg } "
132
- logging . exception ( error_msg )
133
- raise Exception ( error_msg )
134
- except Exception as e :
135
- raise Exception (e )
93
+ response = r .json ()
94
+ except Exception :
95
+ raise Exception ( "Agent Onboarding Error: Please contact the administrators." )
96
+
97
+ if 200 < = r .status_code < 300 :
98
+ agent = build_agent ( payload = response , api_key = api_key )
99
+ else :
100
+ error_msg = f"Agent Onboarding Error: { response } "
101
+ if "message" in response :
102
+ msg = response [ "message" ]
103
+ if response ["message" ] == "err.name_already_exists " :
104
+ msg = "Agent name already exists ."
105
+ elif response [ "message" ] == "err.asset_is_not_available" :
106
+ msg = "Some tools are not available."
107
+ error_msg = f"Agent Onboarding Error (HTTP { r . status_code } ): { msg } "
108
+ logging . exception ( error_msg )
109
+ raise Exception (error_msg )
136
110
return agent
137
111
138
112
@classmethod
@@ -141,6 +115,7 @@ def create_model_tool(
141
115
model : Optional [Union [Model , Text ]] = None ,
142
116
function : Optional [Union [Function , Text ]] = None ,
143
117
supplier : Optional [Union [Supplier , Text ]] = None ,
118
+ description : Text = "" ,
144
119
) -> ModelTool :
145
120
"""Create a new model tool."""
146
121
if function is not None and isinstance (function , str ):
@@ -154,7 +129,7 @@ def create_model_tool(
154
129
break
155
130
if isinstance (supplier , str ):
156
131
supplier = None
157
- return ModelTool (function = function , supplier = supplier , model = model )
132
+ return ModelTool (function = function , supplier = supplier , model = model , description = description )
158
133
159
134
@classmethod
160
135
def create_pipeline_tool (cls , description : Text , pipeline : Union [Pipeline , Text ]) -> PipelineTool :
@@ -164,37 +139,42 @@ def create_pipeline_tool(cls, description: Text, pipeline: Union[Pipeline, Text]
164
139
@classmethod
165
140
def list (cls ) -> Dict :
166
141
"""List all agents available in the platform."""
142
+ from aixplain .factories .agent_factory .utils import build_agent
143
+
167
144
url = urljoin (config .BACKEND_URL , "sdk/agents" )
168
145
headers = {"x-api-key" : config .TEAM_API_KEY , "Content-Type" : "application/json" }
169
146
147
+ resp = {}
170
148
payload = {}
171
149
logging .info (f"Start service for GET List Agents - { url } - { headers } - { json .dumps (payload )} " )
172
150
try :
173
151
r = _request_with_retry ("get" , url , headers = headers )
174
152
resp = r .json ()
153
+ except Exception :
154
+ raise Exception ("Agent Listing Error: Please contact the administrators." )
175
155
176
- if 200 <= r .status_code < 300 :
177
- agents , page_total , total = [], 0 , 0
178
- results = resp
179
- page_total = len (results )
180
- total = len (results )
181
- logging .info (f"Response for GET List Agents - Page Total: { page_total } / Total: { total } " )
182
- for agent in results :
183
- agents .append (build_agent (agent ))
184
- return {"results" : agents , "page_total" : page_total , "page_number" : 0 , "total" : total }
185
- else :
186
- error_msg = "Agent Listing Error: Please contact the administrators."
187
- if "message" in resp :
188
- msg = resp ["message" ]
189
- error_msg = f"Agent Listing Error (HTTP { r .status_code } ): { msg } "
190
- logging .exception (error_msg )
191
- raise Exception (error_msg )
192
- except Exception as e :
193
- raise Exception (e )
156
+ if 200 <= r .status_code < 300 :
157
+ agents , page_total , total = [], 0 , 0
158
+ results = resp
159
+ page_total = len (results )
160
+ total = len (results )
161
+ logging .info (f"Response for GET List Agents - Page Total: { page_total } / Total: { total } " )
162
+ for agent in results :
163
+ agents .append (build_agent (agent ))
164
+ return {"results" : agents , "page_total" : page_total , "page_number" : 0 , "total" : total }
165
+ else :
166
+ error_msg = "Agent Listing Error: Please contact the administrators."
167
+ if isinstance (resp , dict ) and "message" in resp :
168
+ msg = resp ["message" ]
169
+ error_msg = f"Agent Listing Error (HTTP { r .status_code } ): { msg } "
170
+ logging .exception (error_msg )
171
+ raise Exception (error_msg )
194
172
195
173
@classmethod
196
174
def get (cls , agent_id : Text , api_key : Optional [Text ] = None ) -> Agent :
197
175
"""Get agent by id."""
176
+ from aixplain .factories .agent_factory .utils import build_agent
177
+
198
178
url = urljoin (config .BACKEND_URL , f"sdk/agents/{ agent_id } " )
199
179
if config .AIXPLAIN_API_KEY != "" :
200
180
headers = {"x-aixplain-key" : f"{ config .AIXPLAIN_API_KEY } " , "Content-Type" : "application/json" }
0 commit comments