-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic.cpp
More file actions
61 lines (50 loc) · 1.61 KB
/
basic.cpp
File metadata and controls
61 lines (50 loc) · 1.61 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
// Basic usage example - demonstrates both streaming and non-streaming modes
import mcpplibs.llmapi;
import std;
#include "print.hpp"
using namespace mcpplibs::llmapi;
int main() {
auto apiKey = std::getenv("OPENAI_API_KEY");
if (!apiKey) {
println("Error: OPENAI_API_KEY not set");
return 1;
}
auto client = Client(Config{
.apiKey = apiKey,
.model = "gpt-4o-mini",
});
client.system("You are a helpful assistant.");
println("=== llmapi Basic Usage Demo ===");
println();
try {
// Example 1: Non-streaming request
println("[Example 1] Non-streaming mode:");
println("Question: What is the capital of China?");
println();
auto resp = client.chat("What is the capital of China?");
println("Answer: ", resp.text());
println();
// Example 2: Streaming request
println("[Example 2] Streaming mode:");
println("Question: Convince me to use modern C++ (100 words)");
println();
client.clear();
client.system("You are a helpful assistant.");
print("Answer: ");
auto resp2 = client.chat_stream("Convince me to use modern C++ (100 words)",
[](std::string_view chunk) {
print(chunk);
});
println();
println();
println("[Verification] Answer length: ", resp2.text().size(), " chars");
println();
} catch (const std::exception& e) {
println();
println("Error: ", e.what());
println();
return 1;
}
println("=== Demo Complete ===");
return 0;
}