3434from aixplain .utils import config
3535from typing import Dict , List , Optional , Text , Union
3636
37- from aixplain .factories .agent_factory .utils import build_agent , validate_llm , validate_name
3837from aixplain .utils .file_utils import _request_with_retry
3938from urllib .parse import urljoin
4039
@@ -65,74 +64,49 @@ def create(
6564 Returns:
6665 Agent: created Agent
6766 """
68- validate_name (name )
69- # validate LLM ID
70- validate_llm (llm_id )
67+ from aixplain .factories .agent_factory .utils import build_agent
7168
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"
7290 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 )} " )
11892 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 )
136110 return agent
137111
138112 @classmethod
@@ -141,6 +115,7 @@ def create_model_tool(
141115 model : Optional [Union [Model , Text ]] = None ,
142116 function : Optional [Union [Function , Text ]] = None ,
143117 supplier : Optional [Union [Supplier , Text ]] = None ,
118+ description : Text = "" ,
144119 ) -> ModelTool :
145120 """Create a new model tool."""
146121 if function is not None and isinstance (function , str ):
@@ -154,7 +129,7 @@ def create_model_tool(
154129 break
155130 if isinstance (supplier , str ):
156131 supplier = None
157- return ModelTool (function = function , supplier = supplier , model = model )
132+ return ModelTool (function = function , supplier = supplier , model = model , description = description )
158133
159134 @classmethod
160135 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]
164139 @classmethod
165140 def list (cls ) -> Dict :
166141 """List all agents available in the platform."""
142+ from aixplain .factories .agent_factory .utils import build_agent
143+
167144 url = urljoin (config .BACKEND_URL , "sdk/agents" )
168145 headers = {"x-api-key" : config .TEAM_API_KEY , "Content-Type" : "application/json" }
169146
147+ resp = {}
170148 payload = {}
171149 logging .info (f"Start service for GET List Agents - { url } - { headers } - { json .dumps (payload )} " )
172150 try :
173151 r = _request_with_retry ("get" , url , headers = headers )
174152 resp = r .json ()
153+ except Exception :
154+ raise Exception ("Agent Listing Error: Please contact the administrators." )
175155
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 )
194172
195173 @classmethod
196174 def get (cls , agent_id : Text , api_key : Optional [Text ] = None ) -> Agent :
197175 """Get agent by id."""
176+ from aixplain .factories .agent_factory .utils import build_agent
177+
198178 url = urljoin (config .BACKEND_URL , f"sdk/agents/{ agent_id } " )
199179 if config .AIXPLAIN_API_KEY != "" :
200180 headers = {"x-aixplain-key" : f"{ config .AIXPLAIN_API_KEY } " , "Content-Type" : "application/json" }
0 commit comments