-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchat.cpp
More file actions
54 lines (43 loc) · 1.14 KB
/
chat.cpp
File metadata and controls
54 lines (43 loc) · 1.14 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
// Simple and elegant AI chat CLI tool using streaming
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("AI Chat CLI - Type 'quit' to exit");
println();
while (true) {
print("You: ");
std::string input;
std::getline(std::cin, input);
if (input == "quit" || input == "q") {
println();
println("Bye!");
break;
}
if (input.empty()) continue;
try {
print("\nAI: ");
client.chat_stream(input, [](std::string_view chunk) {
print(chunk);
});
println();
println();
} catch (const std::exception& e) {
println();
println("Error: ", e.what());
println();
}
}
return 0;
}