Improve type safety, update logging, remove unused paths

This commit is contained in:
Aaron Po
2026-04-18 19:18:21 -04:00
parent 8a6cbe5efd
commit 1b242e86b5
4 changed files with 18 additions and 22 deletions

View File

@@ -44,8 +44,6 @@ hex ::= [0-9a-fA-F]
)json_brewery"; )json_brewery";
static constexpr int kBreweryInitialMaxTokens = 2800; static constexpr int kBreweryInitialMaxTokens = 2800;
static constexpr int kBreweryTruncationRetryTokenBump = 700;
static constexpr int kBreweryMaxTokensCeiling = 5000;
BreweryResult LlamaGenerator::GenerateBrewery( BreweryResult LlamaGenerator::GenerateBrewery(
const Location& location, const std::string& region_context) { const Location& location, const std::string& region_context) {
@@ -98,7 +96,7 @@ BreweryResult LlamaGenerator::GenerateBrewery(
// Generate brewery data from LLM // Generate brewery data from LLM
raw = this->Infer(system_prompt, user_prompt, max_tokens, raw = this->Infer(system_prompt, user_prompt, max_tokens,
kBreweryJsonGrammar); kBreweryJsonGrammar);
spdlog::info("LlamaGenerator: raw output (attempt {}): {}", attempt + 1, spdlog::debug("LlamaGenerator: raw output (attempt {}): {}", attempt + 1,
raw); raw);
// Validate output: parse JSON and check required fields // Validate output: parse JSON and check required fields
@@ -123,18 +121,6 @@ BreweryResult LlamaGenerator::GenerateBrewery(
spdlog::warn("LlamaGenerator: malformed brewery JSON (attempt {}): {}", spdlog::warn("LlamaGenerator: malformed brewery JSON (attempt {}): {}",
attempt + 1, *validation_error); attempt + 1, *validation_error);
if (last_error == "JSON parse error: incomplete JSON") {
const int previous_max_tokens = max_tokens;
max_tokens = std::min(max_tokens + kBreweryTruncationRetryTokenBump,
kBreweryMaxTokensCeiling);
spdlog::info(
"LlamaGenerator: detected truncated JSON; increasing max_tokens from "
"{} to {} and retrying",
previous_max_tokens, max_tokens);
continue;
}
// Update prompt with error details to guide LLM toward correct output. // Update prompt with error details to guide LLM toward correct output.
user_prompt = std::format( user_prompt = std::format(
"Your previous response was invalid. Error: {}\nReturn the thought " "Your previous response was invalid. Error: {}\nReturn the thought "

View File

@@ -41,7 +41,7 @@ static std::string CondenseWhitespace(std::string_view text) {
bool pending_space = false; bool pending_space = false;
for (const char chr : text) { for (const char chr : text) {
if (std::isspace(chr) != 0) { if (std::isspace(static_cast<unsigned char>(chr)) != 0) {
if (!out.empty()) { if (!out.empty()) {
pending_space = true; pending_space = true;
} }

View File

@@ -9,6 +9,7 @@
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#include <string>
#include <string_view> #include <string_view>
#include <boost/json.hpp> #include <boost/json.hpp>

View File

@@ -6,6 +6,7 @@
#include "web_client/curl_web_client.h" #include "web_client/curl_web_client.h"
#include <cstdint> #include <cstdint>
#include <limits>
#include <memory> #include <memory>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
@@ -14,9 +15,9 @@
using CurlHandle = std::unique_ptr<CURL, decltype(&curl_easy_cleanup)>; using CurlHandle = std::unique_ptr<CURL, decltype(&curl_easy_cleanup)>;
static constexpr int64_t kConnectionTimeout = 10; static constexpr long kConnectionTimeout = 10;
static constexpr int64_t kRequestTimeout = 30; static constexpr long kRequestTimeout = 30;
static constexpr int64_t kOkHttpStatus = 200; static constexpr int32_t kOkHttpStatus = 200;
static CurlHandle CreateHandle() { static CurlHandle CreateHandle() {
CURL* handle = curl_easy_init(); CURL* handle = curl_easy_init();
@@ -64,8 +65,16 @@ std::string CURLWebClient::Get(const std::string& url) {
throw std::runtime_error(error); throw std::runtime_error(error);
} }
int64_t http_code = 0; long curl_http_code = 0;
curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, &http_code); curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, &curl_http_code);
if (curl_http_code < std::numeric_limits<int32_t>::min() ||
curl_http_code > std::numeric_limits<int32_t>::max()) {
throw std::runtime_error("[CURLWebClient] Invalid HTTP status code: " +
std::to_string(curl_http_code));
}
const int32_t http_code = static_cast<int32_t>(curl_http_code);
if (http_code != kOkHttpStatus) { if (http_code != kOkHttpStatus) {
const std::string error = "[CURLWebClient] HTTP error " + const std::string error = "[CURLWebClient] HTTP error " +