MCPcopy Create free account
hub / github.com/ClickHouse/ai-sdk-cpp / main

Function main

examples/basic_chat.cpp:22–140  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20#include <ai/openai.h>
21
22int main() {
23 std::cout << "AI SDK C++ - Basic Chat Example\n";
24 std::cout << "================================\n\n";
25
26 // Example 1: Simple text generation with OpenAI
27 std::cout << "1. Generating text with OpenAI GPT-5.4:\n";
28 std::cout << "Question: What is the capital of France?\n\n";
29
30 auto client1 = ai::openai::create_client();
31 ai::GenerateOptions options1;
32 options1.model = ai::openai::models::kGpt54;
33 options1.prompt =
34 "What is the capital of France? Please provide a brief answer.";
35
36 auto result1 = client1.generate_text(options1);
37
38 if (result1) {
39 std::cout << "Answer: " << result1.text << "\n";
40 std::cout << "Usage: " << result1.usage.total_tokens << " tokens\n";
41 std::cout << "Finish reason: " << result1.finishReasonToString() << "\n\n";
42 } else {
43 std::cout << "Error: " << result1.error_message() << "\n\n";
44 }
45
46 // Example 2: Text generation with system prompt
47 std::cout << "2. Generating text with system prompt:\n";
48 std::cout << "System: You are a helpful math tutor who explains concepts "
49 "clearly.\n";
50 std::cout << "Question: Explain what a prime number is.\n\n";
51
52 ai::GenerateOptions options2;
53 options2.model = ai::openai::models::kGpt54;
54 options2.system =
55 "You are a helpful math tutor who explains concepts clearly.";
56 options2.prompt =
57 "Explain what a prime number is in simple terms with an example.";
58
59 auto result2 = client1.generate_text(options2);
60
61 if (result2) {
62 std::cout << "Answer: " << result2.text << "\n";
63 std::cout << "Usage: " << result2.usage.total_tokens << " tokens\n\n";
64 } else {
65 std::cout << "Error: " << result2.error_message() << "\n\n";
66 }
67
68 // Example 3: Conversation with messages
69 std::cout << "3. Multi-turn conversation:\n";
70
71 ai::Messages conversation = {
72 ai::Message::system("You are a friendly assistant who likes to help with "
73 "coding questions."),
74 ai::Message::user(
75 "What is the difference between a vector and a list in C++?"),
76 ai::Message::assistant(
77 "Great question! In C++, std::vector and std::list are both "
78 "containers but with different characteristics..."),
79 ai::Message::user(

Callers

nothing calls this directly

Calls 4

finishReasonToStringMethod · 0.80
create_clientFunction · 0.50
generate_textMethod · 0.45
error_messageMethod · 0.45

Tested by

no test coverage detected