-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtool_calling_multistep.cpp
More file actions
273 lines (237 loc) · 9.58 KB
/
tool_calling_multistep.cpp
File metadata and controls
273 lines (237 loc) · 9.58 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
/**
* Multi-Step Tool Calling Example - AI SDK C++
*
* This example demonstrates multi-step tool calling functionality.
* It shows how to:
* - Enable multi-step tool calling with maxSteps
* - Chain tool calls together
* - Monitor progress with step callbacks
* - Handle complex workflows
*
* Usage:
* export OPENAI_API_KEY=your_key_here
* ./tool_calling_multistep
*/
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <string>
#include <ai/openai.h>
#include <ai/tools.h>
// Simulated database of user profiles
std::map<std::string, ai::JsonValue> user_database = {
{"alice",
{{"name", "Alice Johnson"},
{"email", "alice@example.com"},
{"city", "San Francisco"}}},
{"bob",
{{"name", "Bob Smith"},
{"email", "bob@example.com"},
{"city", "New York"}}},
{"charlie",
{{"name", "Charlie Brown"},
{"email", "charlie@example.com"},
{"city", "Paris"}}}};
// Tool: Look up user information
ai::JsonValue lookup_user(const ai::JsonValue& args,
const ai::ToolExecutionContext& context) {
std::string user_id = args["user_id"].get<std::string>();
std::cout << "🔍 Looking up user: " << user_id << std::endl;
auto it = user_database.find(user_id);
if (it != user_database.end()) {
return it->second;
} else {
return ai::JsonValue{{"error", "User not found"}};
}
}
// Tool: Get weather for a location
ai::JsonValue get_weather(const ai::JsonValue& args,
const ai::ToolExecutionContext& context) {
std::string location = args["location"].get<std::string>();
std::cout << "🌤️ Getting weather for: " << location << std::endl;
// Simulate weather API call
std::srand(std::time(nullptr) + location.length()); // Add some variety
int temperature = 50 + (std::rand() % 50);
std::vector<std::string> conditions = {"Sunny", "Cloudy", "Rainy",
"Partly cloudy"};
std::string condition = conditions[std::rand() % conditions.size()];
return ai::JsonValue{{"location", location},
{"temperature", temperature},
{"condition", condition},
{"unit", "Fahrenheit"}};
}
// Tool: Send email notification
ai::JsonValue send_email(const ai::JsonValue& args,
const ai::ToolExecutionContext& context) {
std::string to = args["to"].get<std::string>();
std::string subject = args["subject"].get<std::string>();
std::string body = args["body"].get<std::string>();
std::cout << "📧 Sending email to: " << to << std::endl;
std::cout << " Subject: " << subject << std::endl;
// Simulate email sending
return ai::JsonValue{{"status", "sent"},
{"message_id", "msg_" + std::to_string(std::rand())},
{"recipient", to}};
}
// Tool: Get city recommendations
ai::JsonValue get_recommendations(const ai::JsonValue& args,
const ai::ToolExecutionContext& context) {
std::string city = args["city"].get<std::string>();
std::string weather_condition =
args.contains("weather") ? args["weather"].get<std::string>() : "unknown";
std::cout << "💡 Getting recommendations for: " << city
<< " (weather: " << weather_condition << ")" << std::endl;
std::vector<std::string> indoor_activities = {
"Visit museums", "Go shopping", "Try local restaurants", "See a movie"};
std::vector<std::string> outdoor_activities = {
"Walk in parks", "Visit landmarks", "Take photos", "Outdoor dining"};
ai::JsonValue activities;
if (weather_condition == "Rainy") {
activities = indoor_activities;
} else {
// Mix of indoor and outdoor
activities = ai::JsonValue::array();
for (int i = 0; i < 2; ++i) {
activities.push_back(indoor_activities[i]);
activities.push_back(outdoor_activities[i]);
}
}
return ai::JsonValue{{"city", city},
{"weather_condition", weather_condition},
{"recommendations", activities}};
}
int main() {
std::cout << "AI SDK C++ - Multi-Step Tool Calling Example\n";
std::cout << "=============================================\n\n";
// Create OpenAI client
auto client = ai::openai::create_client();
// Define tools
ai::ToolSet tools = {
{"lookup_user", ai::create_simple_tool(
"lookup_user", "Look up user information by user ID",
{{"user_id", "string"}}, lookup_user)},
{"get_weather", ai::create_simple_tool(
"get_weather", "Get current weather for a location",
{{"location", "string"}}, get_weather)},
{"send_email",
ai::create_simple_tool(
"send_email", "Send an email notification",
{{"to", "string"}, {"subject", "string"}, {"body", "string"}},
send_email)},
{"get_recommendations",
ai::create_simple_tool("get_recommendations",
"Get activity recommendations for a city, "
"optionally considering weather",
{{"city", "string"}, {"weather", "string"}},
get_recommendations)}};
// Step callback to monitor progress
auto step_callback = [](const ai::GenerateStep& step) {
std::cout << "\n--- Step Completed ---\n";
std::cout << "Text: " << step.text << "\n";
std::cout << "Tool calls: " << step.tool_calls.size() << "\n";
std::cout << "Tool results: " << step.tool_results.size() << "\n";
std::cout << "Finish reason: ";
switch (step.finish_reason) {
case ai::kFinishReasonStop:
std::cout << "stop";
break;
case ai::kFinishReasonLength:
std::cout << "length";
break;
case ai::kFinishReasonToolCalls:
std::cout << "tool_calls";
break;
case ai::kFinishReasonError:
std::cout << "error";
break;
default:
std::cout << "unknown";
break;
}
std::cout << "\n----------------------\n\n";
};
// Example 1: Multi-step workflow
std::cout << "1. Multi-Step Workflow Example:\n";
std::cout << "Task: Look up user 'alice', get weather for her city, get "
"recommendations, and send her an email\n\n";
ai::GenerateOptions options1;
options1.model = ai::openai::models::kGpt4o;
options1.prompt = R"(
Please help me with this task:
1. Look up the user 'alice' to get her information
2. Get the current weather for her city
3. Get activity recommendations for her city based on the weather
4. Send her an email with a personalized message about the weather and recommendations
Use the available tools to complete this task step by step.
)";
options1.tools = tools;
options1.max_steps = 6; // Allow multiple steps
options1.max_tokens = 200;
options1.on_step_finish = step_callback;
auto result1 = client.generate_text(options1);
if (result1) {
std::cout << "Final Result:\n";
std::cout << "=============\n";
std::cout << "Text: " << result1.text << "\n";
std::cout << "Total Steps: " << result1.steps.size() << "\n";
std::cout << "Total Tool Calls: " << result1.get_all_tool_calls().size()
<< "\n";
std::cout << "Total Tool Results: " << result1.get_all_tool_results().size()
<< "\n";
std::cout << "Total Tokens: " << result1.usage.total_tokens << "\n\n";
// Show the workflow breakdown
std::cout << "Workflow Breakdown:\n";
for (size_t i = 0; i < result1.steps.size(); ++i) {
const auto& step = result1.steps[i];
std::cout << " Step " << (i + 1) << ": ";
if (!step.tool_calls.empty()) {
std::cout << "Called " << step.tool_calls.size() << " tool(s): ";
for (const auto& call : step.tool_calls) {
std::cout << call.tool_name << " ";
}
} else {
std::cout << "Generated text response";
}
std::cout << "\n";
}
} else {
std::cout << "Error: " << result1.error_message() << "\n\n";
}
// Example 2: User choice workflow
std::cout << "\n2. Interactive Multi-Step Example:\n";
std::cout
<< "Task: Look up user 'bob' and create a personalized travel report\n\n";
ai::GenerateOptions options2;
options2.model = ai::openai::models::kGpt4o;
options2.prompt = R"(
Create a personalized travel report for user 'bob':
1. Look up Bob's profile to get his location
2. Get the current weather for his city
3. Get recommendations for activities based on the weather
4. Create a summary report and ask if he'd like it emailed to him
Be conversational and helpful in your responses.
)";
options2.tools = tools;
options2.max_steps = 5;
options2.max_tokens = 250;
auto result2 = client.generate_text(options2);
if (result2) {
std::cout << "Interactive Report:\n";
std::cout << "==================\n";
std::cout << result2.text << "\n";
std::cout << "Steps taken: " << result2.steps.size() << "\n";
std::cout << "Tokens used: " << result2.usage.total_tokens << "\n\n";
} else {
std::cout << "Error: " << result2.error_message() << "\n\n";
}
std::cout << "Multi-step tool calling examples completed!\n";
std::cout << "\nKey features demonstrated:\n";
std::cout << " ✓ Multi-step tool calling with max_steps\n";
std::cout << " ✓ Tool chaining and workflow coordination\n";
std::cout << " ✓ Step-by-step progress monitoring\n";
std::cout << " ✓ Complex task breakdown\n";
std::cout << " ✓ Tool result integration across steps\n";
std::cout << " ✓ Error handling in multi-step workflows\n";
return 0;
}