-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_anthropic.cpp
More file actions
67 lines (53 loc) · 2.09 KB
/
Copy pathtest_anthropic.cpp
File metadata and controls
67 lines (53 loc) · 2.09 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
#include "ai/logger.h"
#include <iostream>
#include <ai/ai.h>
int main() {
try {
// Enable debug logging
ai::logger::install_logger(std::make_shared<ai::logger::ConsoleLogger>(
ai::logger::LogLevel::kLogLevelInfo));
// Create Anthropic client
auto client = ai::anthropic::create_client();
// Test simple generation
std::cout << "Testing Anthropic text generation...\n\n";
ai::GenerateOptions options(ai::anthropic::models::kClaudeHaiku45,
"You are a helpful assistant.",
"Why is the sky blue? Give a short answer.");
auto result = client.generate_text(options);
if (result) {
std::cout << "Response: " << result.text << "\n";
std::cout << "Model: " << result.model.value_or("unknown") << "\n";
std::cout << "Tokens used: " << result.usage.total_tokens << "\n";
std::cout << "Finish reason: " << result.finishReasonToString() << "\n";
} else {
std::cout << "Error: " << result.error_message() << "\n";
}
// Test streaming
std::cout << "\nTesting streaming...\n";
ai::GenerateOptions stream_opts(
ai::anthropic::models::kClaudeHaiku45,
"Count from 1 to 5 slowly and with each number say 'tick'");
ai::StreamOptions stream_options(stream_opts);
auto stream = client.stream_text(stream_options);
for (const auto& event : stream) {
if (event.is_text_delta()) {
std::cout << event.text_delta << std::flush;
} else if (event.is_error()) {
std::cout << "\nStream error: " << event.error.value_or("unknown")
<< "\n";
} else if (event.is_finish()) {
std::cout << "\n\nStream finished.\n";
if (event.usage.has_value()) {
std::cout << "Total tokens: " << event.usage->total_tokens << "\n";
} else {
std::cout << "Note: Token usage data may not be available in "
"streaming mode.\n";
}
}
}
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
return 1;
}
return 0;
}