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

Function main

examples/langfuse_tracing.cpp:54–119  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

52} // namespace
53
54int main() {
55 const char* lf_pk = std::getenv("LANGFUSE_PUBLIC_KEY");
56 const char* lf_sk = std::getenv("LANGFUSE_SECRET_KEY");
57 if (!lf_pk || !lf_sk) {
58 std::cerr << "Set LANGFUSE_PUBLIC_KEY and LANGFUSE_SECRET_KEY first.\n";
59 return 1;
60 }
61
62 const char* host = std::getenv("LANGFUSE_HOST");
63 if (!host || !*host)
64 host = std::getenv("LANGFUSE_BASE_URL");
65 if (!host || !*host)
66 host = "https://cloud.langfuse.com";
67
68 ai::langfuse::Tracer tracer({
69 .host = host,
70 .public_key = lf_pk,
71 .secret_key = lf_sk,
72 .environment = "ai-sdk-cpp-example",
73 });
74 if (!tracer.is_valid()) {
75 std::cerr << "Langfuse tracer not configured.\n";
76 return 1;
77 }
78
79 auto client = ai::openai::create_client();
80 if (!client.is_valid()) {
81 std::cerr << "OpenAI client not configured (set OPENAI_API_KEY).\n";
82 return 1;
83 }
84
85 ai::ToolSet tools;
86 tools["lookup_user"] = ai::create_tool(
87 "Look up a user's profile by id",
88 ai::create_object_schema({{"user_id", "string"}}), lookup_user);
89 tools["get_weather"] = ai::create_tool(
90 "Get the current weather for a location",
91 ai::create_object_schema({{"location", "string"}}), get_weather);
92
93 ai::GenerateOptions options;
94 options.model = ai::openai::models::kGpt4oMini;
95 options.system =
96 "You are a concise assistant. Use the available tools when helpful.";
97 options.prompt = "Look up alice and tell me the weather where she lives.";
98 options.tools = std::move(tools);
99 options.max_steps = 4;
100 options.temperature = 0.0;
101
102 auto trace = tracer.start_trace("langfuse_tracing_example");
103 trace->set_input(options.prompt);
104 trace->set_metadata({{"example", "langfuse_tracing"}, {"sdk", "ai-sdk-cpp"}});
105
106 auto result = ai::langfuse::generate_text(client, std::move(options), *trace);
107
108 if (result) {
109 std::cout << "Output: " << result.text << "\n";
110 trace->set_output(result.text);
111 } else {

Callers

nothing calls this directly

Calls 11

create_toolFunction · 0.85
create_object_schemaFunction · 0.85
generate_textFunction · 0.85
start_traceMethod · 0.80
set_inputMethod · 0.80
set_metadataMethod · 0.80
set_outputMethod · 0.80
create_clientFunction · 0.50
is_validMethod · 0.45
error_messageMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected