-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagentic_workflow.rs
More file actions
505 lines (452 loc) · 18.8 KB
/
agentic_workflow.rs
File metadata and controls
505 lines (452 loc) · 18.8 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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// Copyright {{.Year}} Conductor OSS
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
use conductor::{
client::ConductorClient,
configuration::Configuration,
models::{
ChatMessage, StartWorkflowRequest, TaskDef, WorkflowDef, WorkflowStatus, WorkflowTask,
WorkflowTimeoutPolicy,
},
};
// Configuration
const LLM_PROVIDER: &str = "openai";
const LLM_MODEL: &str = "gpt-4o-mini";
/// Tool definitions in OpenAI function calling format
fn get_tool_definitions() -> serde_json::Value {
serde_json::json!([
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a specific location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state/country, e.g., 'San Francisco, CA'"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature unit"
}
},
"required": ["location"]
}
}
},
{
"type": "function",
"function": {
"name": "calculate",
"description": "Perform mathematical calculations",
"parameters": {
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": "Mathematical expression to evaluate, e.g., '2 + 2 * 3'"
}
},
"required": ["expression"]
}
}
},
{
"type": "function",
"function": {
"name": "search_knowledge",
"description": "Search the internal knowledge base for information",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Search query"
},
"max_results": {
"type": "integer",
"description": "Maximum number of results to return",
"default": 5
}
},
"required": ["query"]
}
}
},
{
"type": "function",
"function": {
"name": "no_tool_needed",
"description": "Use this when the user's question can be answered directly without any tools",
"parameters": {
"type": "object",
"properties": {
"answer": {
"type": "string",
"description": "Direct answer to the user's question"
}
},
"required": ["answer"]
}
}
}
])
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
println!("Agentic Workflow Example - Conductor Rust SDK\n");
println!("{}", "=".repeat(80));
// Initialize the client
let config = Configuration::default();
let client = ConductorClient::new(config)?;
let metadata_client = client.metadata_client();
let workflow_client = client.workflow_client();
// ==========================================================================
// Register Tool Workers (Task Definitions)
// ==========================================================================
println!("\nREGISTERING TOOL TASK DEFINITIONS");
println!("{}", "=".repeat(80));
println!();
let tool_tasks = vec![
TaskDef::new("get_weather").with_description(
"Gets weather for a location. Worker should return temperature and conditions.",
),
TaskDef::new("calculate").with_description(
"Evaluates a mathematical expression. Worker should return the result.",
),
TaskDef::new("search_knowledge")
.with_description("Searches knowledge base. Worker should return matching documents."),
];
for task in &tool_tasks {
match metadata_client.register_task_def(task).await {
Ok(_) => println!(" Registered task: {}", task.name),
Err(e) => println!(" Task {} may already exist: {}", task.name, e),
}
}
// ==========================================================================
// Create the Agent Workflow
// ==========================================================================
println!("\n{}", "=".repeat(80));
println!("CREATING AGENT WORKFLOW");
println!("{}", "=".repeat(80));
println!();
let workflow_name = "rust_ai_agent";
// Step 1: LLM reasoning - decide which tool to use
let system_prompt = r#"You are a helpful AI assistant with access to tools.
Analyze the user's question and decide which tool to use.
Available tools:
- get_weather: Get current weather for a location
- calculate: Perform mathematical calculations
- search_knowledge: Search the internal knowledge base
- no_tool_needed: Answer directly without tools
You MUST call exactly one function. Choose the most appropriate tool based on the user's question."#;
let reasoning_task =
WorkflowTask::llm_chat_complete("agent_reasoning_ref", LLM_PROVIDER, LLM_MODEL)
.with_messages(vec![
ChatMessage::system(system_prompt),
ChatMessage::user("${workflow.input.question}"),
])
.with_input_param("tools", get_tool_definitions())
.with_input_param("tool_choice", "required") // Force tool use
.with_temperature(0.0) // Deterministic for tool selection
.with_max_tokens(500);
// Step 2: Parse the tool call response
let parse_script = r#"
(function(){
var output = $.llm_output;
var toolCall = null;
// Handle different response formats
if (output.tool_calls && output.tool_calls.length > 0) {
toolCall = output.tool_calls[0];
} else if (output.function_call) {
toolCall = {
function: output.function_call
};
}
if (toolCall) {
var funcName = toolCall.function.name;
var args = typeof toolCall.function.arguments === 'string'
? JSON.parse(toolCall.function.arguments)
: toolCall.function.arguments;
return {
tool_name: funcName,
tool_args: args,
has_tool_call: true
};
}
// Fallback - direct answer
return {
tool_name: 'no_tool_needed',
tool_args: { answer: output.content || output.result || 'I could not process your request.' },
has_tool_call: false
};
})();
"#;
let parse_task = WorkflowTask::inline("parse_tool_call_ref", parse_script)
.with_input_param("llm_output", "${agent_reasoning_ref.output}");
// Step 3: Switch based on tool selection
let weather_task = WorkflowTask::simple("get_weather", "get_weather_ref")
.with_input_param(
"location",
"${parse_tool_call_ref.output.result.tool_args.location}",
)
.with_input_param(
"unit",
"${parse_tool_call_ref.output.result.tool_args.unit}",
);
let calculate_task = WorkflowTask::simple("calculate", "calculate_ref").with_input_param(
"expression",
"${parse_tool_call_ref.output.result.tool_args.expression}",
);
let search_task = WorkflowTask::simple("search_knowledge", "search_knowledge_ref")
.with_input_param(
"query",
"${parse_tool_call_ref.output.result.tool_args.query}",
)
.with_input_param(
"max_results",
"${parse_tool_call_ref.output.result.tool_args.max_results}",
);
let direct_answer_task = WorkflowTask::inline(
"direct_answer_ref",
"(function(){ return { result: $.answer }; })();",
)
.with_input_param(
"answer",
"${parse_tool_call_ref.output.result.tool_args.answer}",
);
let tool_switch = WorkflowTask::switch_value_param(
"tool_router_ref",
"${parse_tool_call_ref.output.result.tool_name}",
)
.with_switch_case("get_weather", vec![weather_task])
.with_switch_case("calculate", vec![calculate_task])
.with_switch_case("search_knowledge", vec![search_task])
.with_switch_case("no_tool_needed", vec![direct_answer_task])
.with_default_case(vec![WorkflowTask::inline(
"unknown_tool_ref",
"(function(){ return { error: 'Unknown tool: ' + $.tool_name }; })();",
)
.with_input_param(
"tool_name",
"${parse_tool_call_ref.output.result.tool_name}",
)]);
// Step 4: Format the final response
let format_script = r#"
(function(){
var toolName = $.tool_name;
var toolResult = $.tool_result;
var question = $.question;
// Build response context
var context = 'Tool used: ' + toolName + '\n';
context += 'Result: ' + JSON.stringify(toolResult, null, 2);
return {
tool_used: toolName,
tool_output: toolResult,
question: question,
summary: context
};
})();
"#;
let format_task = WorkflowTask::inline("format_response_ref", format_script)
.with_input_param(
"tool_name",
"${parse_tool_call_ref.output.result.tool_name}",
)
.with_input_param("tool_result", "${tool_router_ref.output}")
.with_input_param("question", "${workflow.input.question}");
// Build the agent workflow
let workflow = WorkflowDef::new(workflow_name)
.with_description("AI Agent: Analyzes questions and uses appropriate tools")
.with_version(1)
.with_task(reasoning_task)
.with_task(parse_task)
.with_task(tool_switch)
.with_task(format_task)
.with_input_parameters(vec!["question".to_string()])
.with_output_param(
"tool_used",
"${format_response_ref.output.result.tool_used}",
)
.with_output_param(
"tool_output",
"${format_response_ref.output.result.tool_output}",
)
.with_output_param("summary", "${format_response_ref.output.result.summary}")
.with_timeout(120, WorkflowTimeoutPolicy::TimeOutWf);
println!("Workflow: {}", workflow.name);
println!("Description: {:?}", workflow.description);
println!();
println!("Agent Pipeline:");
println!(" 1. agent_reasoning_ref - LLM decides which tool to use");
println!(" 2. parse_tool_call_ref - Parse tool call from LLM response");
println!(" 3. tool_router_ref - Route to appropriate tool worker");
println!(" 4. format_response_ref - Format final response");
println!();
// Register the workflow
println!("Registering workflow...");
metadata_client
.register_or_update_workflow_def(&workflow, true)
.await?;
println!(" Workflow registered: {}", workflow_name);
// ==========================================================================
// Display Tool Definitions
// ==========================================================================
println!("\n{}", "=".repeat(80));
println!("TOOL DEFINITIONS");
println!("{}", "=".repeat(80));
println!();
let tools = get_tool_definitions();
if let Some(arr) = tools.as_array() {
for tool in arr {
if let Some(func) = tool.get("function") {
let name = func.get("name").and_then(|n| n.as_str()).unwrap_or("?");
let desc = func
.get("description")
.and_then(|d| d.as_str())
.unwrap_or("?");
println!(" {} - {}", name, desc);
}
}
}
// ==========================================================================
// Example Queries
// ==========================================================================
println!("\n{}", "=".repeat(80));
println!("EXAMPLE QUERIES");
println!("{}", "=".repeat(80));
println!();
let example_queries = [
"What's the weather like in San Francisco?",
"Calculate 15% of 250",
"What is the capital of France?",
"Search for information about Conductor workflows",
];
for (i, query) in example_queries.iter().enumerate() {
println!(" {}. {}", i + 1, query);
}
// ==========================================================================
// Run Example (Weather Query)
// ==========================================================================
println!("\n{}", "=".repeat(80));
println!("RUNNING EXAMPLE QUERY");
println!("{}", "=".repeat(80));
println!();
let test_question = "What's the weather like in Tokyo?";
println!("Question: {}", test_question);
println!();
let request = StartWorkflowRequest::new(workflow_name)
.with_version(1)
.with_input_value("question", test_question);
match workflow_client.start_workflow(&request).await {
Ok(workflow_id) => {
println!("Workflow started: {}", workflow_id);
println!();
// Poll for completion
println!("Waiting for agent to process (max 30s)...");
let start = std::time::Instant::now();
let timeout = std::time::Duration::from_secs(30);
loop {
if start.elapsed() > timeout {
println!(" Timeout - the agent is still processing.");
println!(" This may happen if tool workers are not running.");
break;
}
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
match workflow_client.get_workflow(&workflow_id, true).await {
Ok(wf) => {
let status = wf.status;
print!("\r Status: {:?} ", status);
if status == WorkflowStatus::Completed {
println!();
println!();
println!("{}", "-".repeat(60));
println!("AGENT RESPONSE");
println!("{}", "-".repeat(60));
if let Some(tool) = wf.output.get("tool_used") {
println!("Tool Selected: {}", tool);
}
if let Some(output) = wf.output.get("tool_output") {
println!("Tool Output: {}", serde_json::to_string_pretty(output)?);
}
if let Some(summary) = wf.output.get("summary") {
println!("\nSummary:\n{}", summary.as_str().unwrap_or("N/A"));
}
break;
} else if matches!(
status,
WorkflowStatus::Failed
| WorkflowStatus::Terminated
| WorkflowStatus::TimedOut
) {
println!();
println!(" Agent workflow failed: {:?}", status);
if let Some(reason) = wf.reason_for_incompletion {
println!(" Reason: {}", reason);
}
println!();
println!(" Note: This is expected if:");
println!(" - Tool workers are not running");
println!(" - LLM integration is not configured");
break;
}
}
Err(e) => {
println!(" Error checking status: {}", e);
break;
}
}
}
}
Err(e) => {
println!(" Could not start agent workflow: {}", e);
println!();
println!(" This is expected if LLM integration is not configured.");
}
}
// ==========================================================================
// How to Implement Tool Workers
// ==========================================================================
println!("\n{}", "=".repeat(80));
println!("IMPLEMENTING TOOL WORKERS");
println!("{}", "=".repeat(80));
println!();
println!("To make the agent fully functional, implement workers for each tool:");
println!();
println!("```rust");
println!("#[worker(get_weather)]");
println!("async fn weather_worker(input: GetWeatherInput) -> TaskResult {{");
println!(" // Call weather API");
println!(" let weather = fetch_weather(&input.location).await?;");
println!(" TaskResult::completed_with_output(json!({{");
println!(" \"temperature\": weather.temp,");
println!(" \"conditions\": weather.conditions");
println!(" }}))");
println!("}}");
println!("```");
println!();
println!("See examples/worker_example.rs for worker implementation patterns.");
// ==========================================================================
// Cleanup
// ==========================================================================
println!("\n{}", "=".repeat(80));
println!("CLEANUP");
println!("{}", "=".repeat(80));
println!();
match metadata_client.delete_workflow_def(workflow_name, 1).await {
Ok(_) => println!(" Deleted workflow: {}", workflow_name),
Err(e) => println!(" Could not delete workflow: {}", e),
}
// Note: We don't delete task definitions as they may be used by other workflows
println!();
println!("Agentic workflow example completed!");
println!();
println!("Key Concepts Demonstrated:");
println!(" 1. LLM with function/tool calling for decision making");
println!(" 2. Switch tasks for routing to different tool implementations");
println!(" 3. JavaScript inline tasks for parsing and formatting");
println!(" 4. Task workers as tool implementations");
Ok(())
}