-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcreate_agent_programmatically.py
More file actions
421 lines (377 loc) · 13 KB
/
create_agent_programmatically.py
File metadata and controls
421 lines (377 loc) · 13 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/usr/bin/env python3
"""
Example: Create an agent programmatically
"""
import argparse
import os
import sys
# Add parent directory to Python path so we can import agent_sdk directly
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from agent_sdk import Agentforce
from agent_sdk.core.auth import BasicAuth
from agent_sdk.models.action import Action, Input, Output
from agent_sdk.models.agent import Agent
from agent_sdk.models.attribute_mapping import AttributeMapping
from agent_sdk.models.system_message import SystemMessage
from agent_sdk.models.topic import Topic
from agent_sdk.models.variable import Variable
# Define all agent variables upfront
def create_agent_variables():
"""Create all variables needed for the agent."""
customer_id_var = Variable(
name="customer_id",
description="Customer's unique identifier",
data_type="Text",
include_in_prompt=True,
visibility="Internal",
var_type="conversation",
developer_name="customer_id",
label="Customer ID"
)
last_order_id_var = Variable(
name="last_order_id",
description="ID of the customer's most recent order",
data_type="Text",
include_in_prompt=True,
visibility="Internal",
var_type="conversation",
developer_name="last_order_id",
label="Last Order ID"
)
current_reservation_id_var = Variable(
name="current_reservation_id",
description="ID of the customer's current reservation",
data_type="Text",
include_in_prompt=True,
visibility="Internal",
var_type="conversation",
developer_name="current_reservation_id",
label="Current Reservation ID"
)
customer_name_var = Variable(
name="customer_name",
description="Customer's full name",
data_type="Text",
include_in_prompt=True,
visibility="Internal",
var_type="conversation",
developer_name="customer_name",
label="Customer Name"
)
preferred_contact_method_var = Variable(
name="preferred_contact_method",
description="Customer's preferred contact method",
data_type="Text",
include_in_prompt=False,
visibility="Internal",
var_type="conversation",
developer_name="preferred_contact_method",
label="Preferred Contact Method"
)
return {
"customer_id": customer_id_var,
"last_order_id": last_order_id_var,
"current_reservation_id": current_reservation_id_var,
"customer_name": customer_name_var,
"preferred_contact_method": preferred_contact_method_var
}
def create_order_management_topic(variables) -> Topic:
"""Create the Order Management topic with its actions."""
# Create the topic
topic = Topic(
name="Order Management",
description="Handle order-related queries and actions",
scope="Handle order-related queries and actions",
)
# Set instructions
topic.instructions = [
"Process order requests efficiently",
"Validate order details before confirmation",
"Provide clear order status updates",
]
# Get variables needed for this topic
customer_variable = variables["customer_id"]
order_id_variable = variables["last_order_id"]
# Create and configure actions
place_order_action = Action(
name="Place Order",
description="Place an order for a product",
inputs=[
Input(
name="product_id",
description="ID of the product to order",
data_type="String",
required=True,
),
Input(
name="quantity",
description="Quantity of the product",
data_type="Number",
required=True,
),
Input(
name="customer_id",
description="ID of the customer placing the order",
data_type="String",
required=True,
),
],
outputs=[
Output(
name="status",
description="Status of the order placement",
data_type="String",
required=True,
),
Output(
name="order_id",
description="Unique identifier for the order",
data_type="String",
required=True,
),
Output(
name="message",
description="Status message for the order",
data_type="String",
required=True,
),
],
example_output=Output(
status="success",
order_id="12345",
message="Order placed successfully",
),
)
# Map customer_id input parameter to customer_id variable
place_order_action.attribute_mappings.append(
AttributeMapping(
action_parameter="customer_id",
variable=customer_variable,
direction="input"
)
)
# Map order_id output to last_order_id variable
place_order_action.attribute_mappings.append(
AttributeMapping(
action_parameter="order_id",
variable=order_id_variable,
direction="output"
)
)
check_order_action = Action(
name="Check Order Status",
description="Check the status of an order",
inputs=[
Input(
name="order_id",
description="ID of the order to check",
data_type="String",
required=True,
)
],
outputs=[
Output(
name="status",
description="Status of the order status check",
data_type="String",
required=True,
),
Output(
name="order_status",
description="Current status of the order",
data_type="String",
required=True,
),
Output(
name="estimated_delivery",
description="Estimated delivery date",
data_type="String",
required=True,
),
],
example_output=Output(
status="success",
order_status="processing",
estimated_delivery="2024-03-20",
),
)
# Map order_id input to last_order_id variable
check_order_action.attribute_mappings.append(
AttributeMapping(
action_parameter="order_id",
variable=order_id_variable,
direction="input"
)
)
# Set the actions for the topic
topic.actions = [place_order_action, check_order_action]
return topic
def create_reservation_management_topic(variables) -> Topic:
"""Create the Reservation Management topic with its actions."""
# Create the topic
topic = Topic(
name="Reservation Management",
description="Handle reservation-related queries and actions",
scope="Handle reservation-related queries and actions",
)
# Set instructions
topic.instructions = [
"Process reservation requests efficiently",
"Check availability before confirming reservations",
"Handle reservation modifications and cancellations",
]
# Get variables needed for this topic
customer_variable = variables["customer_id"]
reservation_id_variable = variables["current_reservation_id"]
# Create and configure actions
make_reservation_action = Action(
name="Make Reservation",
description="Make a reservation for a table",
inputs=[
Input(
name="date",
description="Reservation date",
data_type="Date",
required=True,
),
Input(
name="time",
description="Reservation time",
data_type="Time",
required=True,
),
Input(
name="party_size",
description="Number of people",
data_type="Number",
required=True,
),
Input(
name="customer_id",
description="ID of the customer making the reservation",
data_type="String",
required=True,
),
],
outputs=[
Output(
name="status",
description="Status of the reservation request",
data_type="String",
required=True,
),
Output(
name="reservation_id",
description="Unique identifier for the reservation",
data_type="String",
required=True,
),
Output(
name="confirmation",
description="Confirmation message for the reservation",
data_type="String",
required=True,
),
],
example_output=Output(
status="success",
reservation_id="R123",
confirmation="Reservation confirmed",
),
)
# Map customer_id input parameter to customer_id variable
make_reservation_action.attribute_mappings.append(
AttributeMapping(
action_parameter="customer_id",
variable=customer_variable,
direction="input"
)
)
# Map reservation_id output to current_reservation_id variable
make_reservation_action.attribute_mappings.append(
AttributeMapping(
action_parameter="reservation_id",
variable=reservation_id_variable,
direction="output"
)
)
# Set the actions for the topic
topic.actions = [make_reservation_action]
return topic
def main():
# Initialize AgentForce client
parser = argparse.ArgumentParser(
description="Create an AgentForce agent from JSON file"
)
parser.add_argument("--username", required=True, help="Salesforce username")
parser.add_argument("--password", required=True, help="Salesforce password")
parser.add_argument(
"--security_token",
required=False,
help="Salesforce security token",
default=None,
)
args = parser.parse_args()
# Create all variables first
variables = create_agent_variables()
# Create the agent
agent = Agent(
name="Order Management Agent",
description="An agent that helps customers manage their orders and reservations",
agent_type="External",
company_name="Salesforce",
)
# Set sample utterances for the agent
agent.sample_utterances = [
"I want to place an order",
"Check my order status",
"I need to make a reservation",
"What's my order tracking number?",
"Can I modify my reservation?",
]
# Set system messages for the agent
agent.system_messages = [
SystemMessage(
message="You are a helpful order management assistant.", msg_type="system"
),
SystemMessage(
message="Always be professional and courteous.", msg_type="system"
),
]
# Set variables for the agent using the pre-defined variables
agent.variables = list(variables.values())
# Create topics with the pre-defined variables
order_topic = create_order_management_topic(variables)
reservation_topic = create_reservation_management_topic(variables)
# Set topics for the agent
agent.topics = [order_topic, reservation_topic]
try:
# Create the agent in Salesforce
print(f"Attempting to create agent: {agent.name}...")
auth = BasicAuth(
username=args.username,
password=args.password,
security_token=args.security_token,
)
agentforce = Agentforce(auth=auth)
result = agentforce.create(agent)
# --- Check if create() returned None (early failure) ---
if result is None:
raise RuntimeError(
f"Agent '{agent.name}' creation failed before deployment could start. Check logs for authentication or setup errors."
)
# --- Check deployment status ---
deployment_status = result.get("deployResult", {}).get("status")
deployment_id = result.get("id", "N/A")
if deployment_status == "Succeeded":
print(
f"Agent '{agent.name}' deployed successfully. Deployment ID: {deployment_id}"
)
else:
error_message = f"Agent '{agent.name}' deployment did not succeed. Status: {deployment_status}. Full Result: {result}"
raise RuntimeError(error_message) # Raise exception on failure
except Exception as e:
# Catch and re-raise other exceptions during the process
raise RuntimeError(f"Error during agent creation process: {str(e)}")
if __name__ == "__main__":
main()