-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathfunction-calling.ts
More file actions
214 lines (196 loc) · 6.37 KB
/
function-calling.ts
File metadata and controls
214 lines (196 loc) · 6.37 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
/**
* Function Calling — LLM dynamically invokes worker functions
*
* Demonstrates the agentic pattern where an LLM decides which tool/function
* to call based on user input. Uses llmChatCompleteTask with tool definitions
* and a dynamic task to execute the chosen function.
*
* Prerequisites:
* - An LLM integration configured in Conductor
*
* Run:
* CONDUCTOR_SERVER_URL=http://localhost:8080 npx ts-node examples/agentic-workflows/function-calling.ts
*/
import {
OrkesClients,
ConductorWorkflow,
TaskHandler,
worker,
llmChatCompleteTask,
simpleTask,
switchTask,
inlineTask,
Role,
} from "../../src/sdk";
import type { Task } from "../../src/open-api";
// ── Tool workers ────────────────────────────────────────────────────
const _getWeather = worker({ taskDefName: "fn_get_weather", registerTaskDef: true })(
async (task: Task) => {
const city = (task.inputData?.city as string) ?? "Unknown";
// Simulate weather API
const weather = {
city,
temperature: Math.round(15 + Math.random() * 20),
condition: ["Sunny", "Cloudy", "Rainy", "Windy"][
Math.floor(Math.random() * 4)
],
humidity: Math.round(30 + Math.random() * 50),
};
return { status: "COMPLETED", outputData: weather };
}
);
const _getStockPrice = worker({ taskDefName: "fn_get_stock_price", registerTaskDef: true })(
async (task: Task) => {
const symbol = (task.inputData?.symbol as string) ?? "AAPL";
// Simulate stock API
const price = {
symbol,
price: Math.round(100 + Math.random() * 200 * 100) / 100,
change: Math.round((Math.random() * 10 - 5) * 100) / 100,
currency: "USD",
};
return { status: "COMPLETED", outputData: price };
}
);
const _calculate = worker({ taskDefName: "fn_calculate", registerTaskDef: true })(
async (task: Task) => {
const expression = (task.inputData?.expression as string) ?? "0";
let result: number;
try {
// Simple safe evaluation (in production, use a proper math parser)
result = Function(`"use strict"; return (${expression})`)();
} catch {
return {
status: "FAILED",
outputData: { error: `Invalid expression: ${expression}` },
};
}
return { status: "COMPLETED", outputData: { expression, result } };
}
);
// ── Main ────────────────────────────────────────────────────────────
async function main() {
const clients = await OrkesClients.from();
const workflowClient = clients.getWorkflowClient();
const client = clients.getClient();
const provider = process.env.LLM_PROVIDER ?? "openai_integration";
const model = process.env.LLM_MODEL ?? "gpt-4o";
// ── Build the agentic workflow ────────────────────────────────────
const wf = new ConductorWorkflow(workflowClient, "function_calling_example")
.description("LLM picks which function to call based on user query");
// Step 1: LLM decides which function to call
wf.add(
llmChatCompleteTask("decide_ref", provider, model, {
messages: [
{
role: Role.SYSTEM,
message: `You are a helpful assistant with access to these tools:
1. get_weather(city: string) - Get current weather for a city
2. get_stock_price(symbol: string) - Get current stock price
3. calculate(expression: string) - Evaluate a math expression
Based on the user's query, respond with ONLY a JSON object:
{"function": "<function_name>", "args": {"<key>": "<value>"}}`,
},
{
role: Role.USER,
message: "${workflow.input.query}",
},
],
temperature: 0,
maxTokens: 200,
})
);
// Step 2: Parse LLM response
wf.add(
inlineTask(
"parse_ref",
`(function() {
var text = $.decide_ref.output.result;
try {
var parsed = JSON.parse(text);
return parsed;
} catch(e) {
return { function: "unknown", args: {} };
}
})()`,
"javascript"
)
);
// Step 3: Route to the appropriate function
wf.add(
switchTask(
"route_ref",
"${parse_ref.output.result.function}",
{
get_weather: [
simpleTask("weather_ref", "fn_get_weather", {
city: "${parse_ref.output.result.args.city}",
}),
],
get_stock_price: [
simpleTask("stock_ref", "fn_get_stock_price", {
symbol: "${parse_ref.output.result.args.symbol}",
}),
],
calculate: [
simpleTask("calc_ref", "fn_calculate", {
expression: "${parse_ref.output.result.args.expression}",
}),
],
},
[
inlineTask(
"unknown_ref",
'(function() { return { error: "Unknown function requested" }; })()',
"javascript"
),
]
)
);
// Step 4: LLM summarizes the result
wf.add(
llmChatCompleteTask("summarize_ref", provider, model, {
messages: [
{
role: Role.SYSTEM,
message: "Summarize the tool result in a natural, conversational way.",
},
{
role: Role.USER,
message:
'User asked: "${workflow.input.query}". Tool result: ${route_ref.output}',
},
],
temperature: 0.5,
maxTokens: 200,
})
);
wf.outputParameters({
query: "${workflow.input.query}",
functionCalled: "${parse_ref.output.result.function}",
summary: "${summarize_ref.output.result}",
});
await wf.register(true);
console.log("Registered workflow:", wf.getName());
// Start workers
const handler = new TaskHandler({ client, scanForDecorated: true });
await handler.startWorkers();
// Execute with different queries
const queries = [
"What's the weather like in Tokyo?",
"What's the current price of TSLA stock?",
"Calculate 42 * 17 + 3",
];
for (const query of queries) {
console.log(`\nQuery: "${query}"`);
const run = await wf.execute({ query });
console.log("Status:", run.status);
console.log("Output:", JSON.stringify(run.output, null, 2));
}
await handler.stopWorkers();
process.exit(0);
}
main().catch((err) => {
console.error(err);
process.exit(1);
});