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

Function main

examples/embeddings_example.cpp:61–286  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

59}
60
61int main() {
62 std::cout << "AI SDK C++ - Embeddings Example\n";
63 std::cout << "================================\n\n";
64
65 // Create OpenAI client
66 auto client = ai::openai::create_client();
67 if (!client.is_valid()) {
68 std::cerr << "Error: Failed to create OpenAI client. Make sure "
69 "OPENAI_API_KEY is set.\n";
70 return 1;
71 }
72
73 // Example 1: Basic single text embedding
74 std::cout << "1. Single Text Embedding:\n";
75 std::cout << "Text: \"Hello, world!\"\n\n";
76
77 nlohmann::json input1 = "Hello, world!";
78 ai::EmbeddingOptions options1("text-embedding-3-small", input1);
79 auto result1 = client.embeddings(options1);
80
81 if (result1) {
82 auto embedding = result1.data[0]["embedding"];
83 std::cout << "✓ Successfully generated embedding\n";
84 std::cout << " Dimensions: " << embedding.size() << "\n";
85 std::cout << " Token usage: " << result1.usage.total_tokens << " tokens\n";
86 std::cout << " First 5 values: [";
87 for (size_t i = 0; i < std::min(size_t(5), embedding.size()); ++i) {
88 std::cout << std::fixed << std::setprecision(6)
89 << embedding[i].get<double>();
90 if (i < 4)
91 std::cout << ", ";
92 }
93 std::cout << ", ...]\n\n";
94 } else {
95 std::cout << "✗ Error: " << result1.error_message() << "\n\n";
96 }
97
98 // Example 2: Multiple texts embedding
99 std::cout << "2. Multiple Texts Embedding:\n";
100 nlohmann::json input2 = nlohmann::json::array(
101 {"sunny day at the beach", "rainy afternoon in the city",
102 "snowy night in the mountains"});
103
104 ai::EmbeddingOptions options2("text-embedding-3-small", input2);
105 auto result2 = client.embeddings(options2);
106
107 if (result2) {
108 std::cout << "✓ Successfully generated " << result2.data.size()
109 << " embeddings\n";
110 std::cout << " Token usage: " << result2.usage.total_tokens << " tokens\n";
111 for (size_t i = 0; i < result2.data.size(); ++i) {
112 std::cout << " Embedding " << i + 1
113 << " dimensions: " << result2.data[i]["embedding"].size()
114 << "\n";
115 }
116 std::cout << "\n";
117 } else {
118 std::cout << "✗ Error: " << result2.error_message() << "\n\n";

Callers

nothing calls this directly

Calls 8

extract_embeddingFunction · 0.85
cosine_similarityFunction · 0.85
beginMethod · 0.80
create_clientFunction · 0.50
is_validMethod · 0.45
embeddingsMethod · 0.45
error_messageMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected