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

Method generate_text

tests/utils/mock_anthropic_client.cpp:45–113  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

43}
44
45GenerateResult ControllableAnthropicClient::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 AnthropicNetworkFailureSimulator::createNetworkErrorResult(
56 AnthropicNetworkFailureSimulator::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
70 // Anthropic response format
71 if (json_response["type"] == "message") {
72 result.text = json_response["content"][0]["text"];
73
74 // Map Anthropic stop reasons to our enum
75 std::string stop_reason = json_response.value("stop_reason", "");
76 if (stop_reason == "end_turn") {
77 result.finish_reason = kFinishReasonStop;
78 } else if (stop_reason == "max_tokens") {
79 result.finish_reason = kFinishReasonLength;
80 } else if (stop_reason == "stop_sequence") {
81 result.finish_reason = kFinishReasonStop;
82 } else if (stop_reason == "tool_use") {
83 result.finish_reason = kFinishReasonToolCalls;
84 } else {
85 result.finish_reason = kFinishReasonError;
86 }
87
88 if (json_response.contains("usage")) {
89 result.usage.prompt_tokens = json_response["usage"]["input_tokens"];
90 result.usage.completion_tokens =
91 json_response["usage"]["output_tokens"];
92 result.usage.total_tokens =
93 result.usage.prompt_tokens + result.usage.completion_tokens;
94 }
95
96 if (json_response.contains("id")) {
97 result.id = json_response["id"];
98 }
99
100 if (json_response.contains("model")) {
101 result.model = json_response["model"];
102 }

Callers

nothing calls this directly

Calls 1

GenerateResultClass · 0.85

Tested by

no test coverage detected