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

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