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

Method generate_text

tests/utils/mock_openai_client.cpp:44–93  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

42 call_count_ = 0;
43}
44
45GenerateResult ControllableOpenAIClient::generate_text(
46 const GenerateOptions& options) {
47 last_generate_options_ = options;
48 call_count_++;
49
50 if (should_timeout_) {
51 return GenerateResult("Network timeout");
52 }
53
54 if (should_fail_) {
55 return NetworkFailureSimulator::createNetworkErrorResult(
56 NetworkFailureSimulator::FailureType::kConnectionRefused);
57 }
58
59 if (predefined_status_code_ != 200) {
60 return GenerateResult("HTTP " + std::to_string(predefined_status_code_) +
61 " error: " + predefined_response_);
62 }
63
64 // Parse predefined response and create result
65 try {
66 auto json_response = nlohmann::json::parse(predefined_response_);
67
68 GenerateResult result;
69 result.text = json_response["choices"][0]["message"]["content"];
70 result.finish_reason = kFinishReasonStop;
71
72 if (json_response.contains("usage")) {
73 result.usage.prompt_tokens = json_response["usage"]["prompt_tokens"];
74 result.usage.completion_tokens =
75 json_response["usage"]["completion_tokens"];
76 result.usage.total_tokens = json_response["usage"]["total_tokens"];
77 }
78
79 if (json_response.contains("id")) {
80 result.id = json_response["id"];
81 }
82
83 if (json_response.contains("model")) {
84 result.model = json_response["model"];
85 }
86
87 result.provider_metadata = predefined_response_;
88
89 return result;
90
91 } catch (const std::exception& e) {
92 return GenerateResult("Failed to parse response: " + std::string(e.what()));
93 }
94}
95
96StreamResult ControllableOpenAIClient::stream_text(

Callers 8

TEST_FFunction · 0.45
TEST_FFunction · 0.45
executeSQLGenerationMethod · 0.45
TEST_PFunction · 0.45
TEST_FFunction · 0.45
TEST_PFunction · 0.45
TEST_FFunction · 0.45
TEST_FFunction · 0.45

Calls 1

GenerateResultClass · 0.85

Tested by 8

TEST_FFunction · 0.36
TEST_FFunction · 0.36
executeSQLGenerationMethod · 0.36
TEST_PFunction · 0.36
TEST_FFunction · 0.36
TEST_PFunction · 0.36
TEST_FFunction · 0.36
TEST_FFunction · 0.36