-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_tool_integration.cpp
More file actions
203 lines (164 loc) · 6.72 KB
/
Copy pathtest_tool_integration.cpp
File metadata and controls
203 lines (164 loc) · 6.72 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
/**
* Tool Integration Test - AI SDK C++
*
* A simple test to verify that tool calling works with the provider
* implementations. This example tests the integration between the tool system
* and the OpenAI/Anthropic clients.
*
* Usage:
* export OPENAI_API_KEY=your_key_here
* export ANTHROPIC_API_KEY=your_key_here
* ./test_tool_integration
*/
#include <iostream>
#include <string>
#include <ai/anthropic.h>
#include <ai/openai.h>
#include <ai/tools.h>
// Simple test tool
ai::JsonValue simple_test_tool(const ai::JsonValue& args,
const ai::ToolExecutionContext& context) {
std::string input = args["input"].get<std::string>();
std::cout << "🔧 Tool called with input: " << input << std::endl;
return ai::JsonValue{{"result", "Tool executed successfully"},
{"input_received", input},
{"execution_id", context.tool_call_id}};
}
void test_openai_tools() {
std::cout << "\n=== Testing OpenAI Tool Integration ===\n";
auto client = ai::openai::create_client();
// Create a simple tool
ai::ToolSet tools = {
{"test_tool", ai::create_simple_tool(
"test_tool", "A simple test tool that echoes input",
{{"input", "string"}}, simple_test_tool)}};
ai::GenerateOptions options;
options.model = ai::openai::models::kGpt54;
options.prompt = "Please use the test_tool with input 'hello world'";
options.tools = tools;
options.tool_choice =
ai::ToolChoice::specific("test_tool"); // Force tool usage
options.max_tokens = 100;
std::cout << "Making request to OpenAI with forced tool usage...\n";
auto result = client.generate_text(options);
if (result) {
std::cout << "✅ Request successful!\n";
std::cout << "Response: " << result.text << "\n";
std::cout << "Tool calls: " << result.tool_calls.size() << "\n";
std::cout << "Tool results: " << result.tool_results.size() << "\n";
if (!result.tool_calls.empty()) {
const auto& call = result.tool_calls[0];
std::cout << "First tool call: " << call.tool_name << "\n";
std::cout << "Arguments: " << call.arguments.dump() << "\n";
}
if (!result.tool_results.empty()) {
const auto& tool_result = result.tool_results[0];
std::cout << "First tool result: " << tool_result.result.dump() << "\n";
}
} else {
std::cout << "❌ Request failed: " << result.error_message() << "\n";
}
}
void test_anthropic_tools() {
std::cout << "\n=== Testing Anthropic Tool Integration ===\n";
auto client = ai::anthropic::create_client();
// Create a simple tool
ai::ToolSet tools = {
{"test_tool", ai::create_simple_tool(
"test_tool", "A simple test tool that echoes input",
{{"input", "string"}}, simple_test_tool)}};
ai::GenerateOptions options;
options.model = ai::anthropic::models::kClaudeSonnet46;
options.prompt = "Please use the test_tool with input 'hello anthropic'";
options.tools = tools;
options.tool_choice =
ai::ToolChoice::specific("test_tool"); // Force tool usage
options.max_tokens = 100;
std::cout << "Making request to Anthropic with forced tool usage...\n";
auto result = client.generate_text(options);
if (result) {
std::cout << "✅ Request successful!\n";
std::cout << "Response: " << result.text << "\n";
std::cout << "Tool calls: " << result.tool_calls.size() << "\n";
std::cout << "Tool results: " << result.tool_results.size() << "\n";
if (!result.tool_calls.empty()) {
const auto& call = result.tool_calls[0];
std::cout << "First tool call: " << call.tool_name << "\n";
std::cout << "Arguments: " << call.arguments.dump() << "\n";
}
if (!result.tool_results.empty()) {
const auto& tool_result = result.tool_results[0];
std::cout << "First tool result: " << tool_result.result.dump() << "\n";
}
} else {
std::cout << "❌ Request failed: " << result.error_message() << "\n";
}
}
void test_multi_step() {
std::cout << "\n=== Testing Multi-Step Tool Calling ===\n";
auto client = ai::openai::create_client();
// Create tools for multi-step
ai::ToolSet tools = {
{"get_number",
ai::create_simple_tool(
"get_number", "Get a number for calculation",
{{"description", "string"}},
[](const ai::JsonValue& args,
const ai::ToolExecutionContext& context) -> ai::JsonValue {
std::string desc = args["description"].get<std::string>();
std::cout << "🔢 Getting number for: " << desc << std::endl;
return ai::JsonValue{{"number", 42}};
})},
{"calculate",
ai::create_simple_tool(
"calculate", "Perform a calculation",
{{"operation", "string"}, {"a", "number"}, {"b", "number"}},
[](const ai::JsonValue& args,
const ai::ToolExecutionContext& context) -> ai::JsonValue {
std::string op = args["operation"].get<std::string>();
double a = args["a"].get<double>();
double b = args["b"].get<double>();
std::cout << "🧮 Calculating: " << a << " " << op << " " << b
<< std::endl;
double result = 0;
if (op == "add")
result = a + b;
else if (op == "multiply")
result = a * b;
return ai::JsonValue{{"result", result}};
})}};
ai::GenerateOptions options;
options.model = ai::openai::models::kGpt54;
options.prompt = "Get a number and then multiply it by 2. Show me the steps.";
options.tools = tools;
options.max_steps = 5; // Enable multi-step
options.max_tokens = 200;
std::cout << "Making multi-step request...\n";
auto result = client.generate_text(options);
if (result) {
std::cout << "✅ Multi-step request successful!\n";
std::cout << "Final response: " << result.text << "\n";
std::cout << "Total steps: " << result.steps.size() << "\n";
std::cout << "Total tool calls: " << result.get_all_tool_calls().size()
<< "\n";
std::cout << "Total tool results: " << result.get_all_tool_results().size()
<< "\n";
} else {
std::cout << "❌ Multi-step request failed: " << result.error_message()
<< "\n";
}
}
int main() {
std::cout << "AI SDK C++ - Tool Integration Test\n";
std::cout << "===================================\n";
// Test OpenAI integration
test_openai_tools();
// Test Anthropic integration
test_anthropic_tools();
// Test multi-step functionality
test_multi_step();
std::cout << "\n=== Test Summary ===\n";
std::cout << "Tool integration tests completed!\n";
std::cout << "Check the output above for success/failure status.\n";
return 0;
}