mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-15 01:54:43 +00:00
C++ code formatting
This commit is contained in:
@@ -51,10 +51,9 @@ class HttpWebClient final : public WebClient {
|
||||
* @param headers Additional request headers.
|
||||
* @return Response body on HTTP 2xx; throws std::runtime_error otherwise.
|
||||
*/
|
||||
std::string Post(
|
||||
const std::string& url, const std::string& body,
|
||||
const std::vector<std::pair<std::string, std::string>>& headers)
|
||||
override;
|
||||
std::string Post(const std::string& url, const std::string& body,
|
||||
const std::vector<std::pair<std::string, std::string>>&
|
||||
headers) override;
|
||||
|
||||
/**
|
||||
* @brief Percent-encodes a single URI component (query parameter value or
|
||||
|
||||
@@ -121,8 +121,8 @@ std::optional<ApplicationOptions> ParseArguments(
|
||||
|
||||
// Enforce mutual exclusivity before any further configuration is
|
||||
// applied.
|
||||
const int selected_generator_count =
|
||||
(use_mocked ? 1 : 0) + (!model_path.empty() ? 1 : 0) +
|
||||
const int selected_generator_count = (use_mocked ? 1 : 0) +
|
||||
(!model_path.empty() ? 1 : 0) +
|
||||
(use_openai ? 1 : 0);
|
||||
|
||||
if (selected_generator_count > 1) {
|
||||
@@ -190,7 +190,8 @@ std::optional<ApplicationOptions> ParseArguments(
|
||||
: use_openai ? GeneratorMode::kOpenAI
|
||||
: GeneratorMode::kLlama;
|
||||
options.generator.model_path = model_path;
|
||||
options.generator.openai_model = var_map["openai-model"].as<std::string>();
|
||||
options.generator.openai_model =
|
||||
var_map["openai-model"].as<std::string>();
|
||||
|
||||
// Only populate sampling config when the user explicitly overrides at
|
||||
// least one value. Leaving it as std::nullopt lets LlamaGenerator fall
|
||||
|
||||
@@ -43,6 +43,5 @@ std::vector<City> BiergartenPipelineOrchestrator::QueryLocations() {
|
||||
" Locations available: {}\n Sampled Locations: {}",
|
||||
all_locations.size(), sampled_locations.size())});
|
||||
|
||||
|
||||
return sampled_locations;
|
||||
}
|
||||
@@ -8,14 +8,13 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <boost/json.hpp>
|
||||
#include <cctype>
|
||||
#include <format>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <boost/json.hpp>
|
||||
|
||||
namespace {
|
||||
std::string Trim(std::string_view value) {
|
||||
constexpr std::string_view whitespace = " \t\n\r\f\v";
|
||||
@@ -55,7 +54,8 @@ std::string CondenseWhitespace(std::string_view text) {
|
||||
return out;
|
||||
}
|
||||
|
||||
// Keeps truncation past the string's midpoint, preserving the critical opening content.
|
||||
// Keeps truncation past the string's midpoint, preserving the critical opening
|
||||
// content.
|
||||
constexpr size_t kTruncationGuardDivisor = 2;
|
||||
|
||||
bool ReadRequiredTrimmedStringField(const boost::json::object& obj,
|
||||
@@ -64,8 +64,7 @@ bool ReadRequiredTrimmedStringField(const boost::json::object& obj,
|
||||
const boost::json::value* field = obj.if_contains(key);
|
||||
if (field == nullptr || !field->is_string()) {
|
||||
if (error_out != nullptr) {
|
||||
*error_out =
|
||||
std::format("Missing or invalid string field: {}", key);
|
||||
*error_out = std::format("Missing or invalid string field: {}", key);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -6,9 +6,11 @@
|
||||
*
|
||||
* API docs last accessed 2026-08-23.
|
||||
* Docs: https://platform.openai.com/docs/api-reference/chat/create
|
||||
* Structured Outputs: https://platform.openai.com/docs/guides/structured-outputs
|
||||
* Structured Outputs:
|
||||
* https://platform.openai.com/docs/guides/structured-outputs
|
||||
*/
|
||||
|
||||
#include <boost/json.hpp>
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
@@ -16,8 +18,6 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/json.hpp>
|
||||
|
||||
#include "data_generation/openai_generator.h"
|
||||
|
||||
namespace {
|
||||
@@ -48,7 +48,6 @@ std::string OpenAIGenerator::CallChatCompletionsApi(
|
||||
const std::string& system_prompt, const std::string& user_prompt,
|
||||
std::string_view json_schema, std::string_view schema_name,
|
||||
int max_tokens) {
|
||||
|
||||
boost::system::error_code schema_error;
|
||||
boost::json::value schema_value =
|
||||
boost::json::parse(json_schema, schema_error);
|
||||
@@ -60,7 +59,8 @@ std::string OpenAIGenerator::CallChatCompletionsApi(
|
||||
schema_error.message());
|
||||
}
|
||||
|
||||
// response_format: https://platform.openai.com/docs/guides/structured-outputs
|
||||
// response_format:
|
||||
// https://platform.openai.com/docs/guides/structured-outputs
|
||||
boost::json::object json_schema_wrapper;
|
||||
json_schema_wrapper["name"] = std::string(schema_name);
|
||||
json_schema_wrapper["strict"] = true;
|
||||
@@ -127,14 +127,14 @@ std::string OpenAIGenerator::CallChatCompletionsApi(
|
||||
message_field != nullptr && message_field->is_string()) {
|
||||
error_message = std::string(message_field->as_string());
|
||||
}
|
||||
throw std::runtime_error(
|
||||
std::format("OpenAIGenerator: API request failed: {}", error_message));
|
||||
throw std::runtime_error(std::format(
|
||||
"OpenAIGenerator: API request failed: {}", error_message));
|
||||
}
|
||||
|
||||
const boost::json::value* choices_field = response_obj.
|
||||
if_contains("choices");
|
||||
bool choices_field_missing_or_invalid =
|
||||
choices_field == nullptr || !choices_field->is_array() ||
|
||||
const boost::json::value* choices_field =
|
||||
response_obj.if_contains("choices");
|
||||
bool choices_field_missing_or_invalid = choices_field == nullptr ||
|
||||
!choices_field->is_array() ||
|
||||
choices_field->get_array().empty();
|
||||
|
||||
if (choices_field_missing_or_invalid) {
|
||||
@@ -161,12 +161,12 @@ std::string OpenAIGenerator::CallChatCompletionsApi(
|
||||
|
||||
// Refusal field: https://platform.openai.com/docs/guides/structured-outputs
|
||||
const boost::json::value* refusal_field = message_obj.if_contains("refusal");
|
||||
bool model_refused_request =
|
||||
refusal_field != nullptr && refusal_field->is_string() &&
|
||||
bool model_refused_request = refusal_field != nullptr &&
|
||||
refusal_field->is_string() &&
|
||||
!refusal_field->as_string().empty();
|
||||
if (model_refused_request) {
|
||||
throw std::runtime_error(std::format(
|
||||
"OpenAIGenerator: request refused by safety policies: {}",
|
||||
throw std::runtime_error(
|
||||
std::format("OpenAIGenerator: request refused by safety policies: {}",
|
||||
std::string(refusal_field->as_string())));
|
||||
}
|
||||
|
||||
|
||||
@@ -17,8 +17,7 @@
|
||||
|
||||
namespace {
|
||||
|
||||
std::string FormatLocalLanguageCodes(
|
||||
const std::vector<std::string>& codes) {
|
||||
std::string FormatLocalLanguageCodes(const std::vector<std::string>& codes) {
|
||||
if (codes.empty()) {
|
||||
return "Not provided";
|
||||
}
|
||||
@@ -36,7 +35,8 @@ std::string FormatLocalLanguageCodes(
|
||||
|
||||
constexpr int kBreweryMaxTokens = 2800;
|
||||
|
||||
// Structured outputs already guarantee schema-valid JSON, so retries here only cover transient failures or the model returning placeholder text.
|
||||
// Structured outputs already guarantee schema-valid JSON, so retries here only
|
||||
// cover transient failures or the model returning placeholder text.
|
||||
constexpr int kMaxAttempts = 2;
|
||||
|
||||
} // namespace
|
||||
@@ -51,7 +51,8 @@ BreweryResult OpenAIGenerator::GenerateBrewery(
|
||||
FormatLocalLanguageCodes(location.local_languages);
|
||||
|
||||
/**
|
||||
* Loads the backend-agnostic brewery system prompt via the injected prompt directory.
|
||||
* Loads the backend-agnostic brewery system prompt via the injected prompt
|
||||
* directory.
|
||||
*/
|
||||
const std::string system_prompt =
|
||||
prompt_directory_->Load("BREWERY_GENERATION");
|
||||
@@ -70,9 +71,9 @@ BreweryResult OpenAIGenerator::GenerateBrewery(
|
||||
std::string last_error;
|
||||
|
||||
for (int attempt = 0; attempt < kMaxAttempts; ++attempt) {
|
||||
raw = CallChatCompletionsApi(system_prompt, user_prompt,
|
||||
kBreweryJsonSchema, "brewery_result",
|
||||
kBreweryMaxTokens);
|
||||
raw =
|
||||
CallChatCompletionsApi(system_prompt, user_prompt, kBreweryJsonSchema,
|
||||
"brewery_result", kBreweryMaxTokens);
|
||||
if (logger_) {
|
||||
logger_->Log({.level = LogLevel::Debug,
|
||||
.phase = PipelinePhase::BreweryAndBeerGeneration,
|
||||
|
||||
@@ -16,7 +16,8 @@
|
||||
namespace {
|
||||
constexpr int kUserMaxTokens = 1200;
|
||||
|
||||
// Structured outputs already guarantee schema-valid JSON, so retries here only cover transient failures or placeholder text.
|
||||
// Structured outputs already guarantee schema-valid JSON, so retries here only
|
||||
// cover transient failures or placeholder text.
|
||||
constexpr int kMaxAttempts = 2;
|
||||
} // namespace
|
||||
|
||||
@@ -47,8 +48,7 @@ UserResult OpenAIGenerator::GenerateUser(const EnrichedCity& city,
|
||||
|
||||
const std::string retry_context = std::format(
|
||||
"Name: {} {}\nCity: {}, {}\nPersona: {}", name.first_name,
|
||||
name.last_name, city.location.city, city.location.country,
|
||||
persona.name);
|
||||
name.last_name, city.location.city, city.location.country, persona.name);
|
||||
|
||||
std::string raw;
|
||||
std::string last_error;
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
* @brief OpenAIGenerator constructor.
|
||||
*/
|
||||
|
||||
#include "data_generation/openai_generator.h"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "data_generation/openai_generator.h"
|
||||
|
||||
OpenAIGenerator::OpenAIGenerator(
|
||||
std::string api_key, std::string model, std::shared_ptr<ILogger> logger,
|
||||
std::unique_ptr<IPromptDirectory> prompt_directory,
|
||||
|
||||
@@ -166,10 +166,10 @@ int main(const int argc, char** argv) {
|
||||
inj.template create<std::unique_ptr<WebClient>>(),
|
||||
log_producer);
|
||||
}),
|
||||
di::bind<DataGenerator>().to(
|
||||
[&options, &model_path, &sampling, &prompt_directory,
|
||||
&log_producer](
|
||||
const auto& inj) -> std::unique_ptr<DataGenerator> {
|
||||
di::bind<DataGenerator>().to([&options, &model_path, &sampling,
|
||||
&prompt_directory,
|
||||
&log_producer](const auto& inj)
|
||||
-> std::unique_ptr<DataGenerator> {
|
||||
if (options.generator.mode == GeneratorMode::kMock) {
|
||||
log_producer->Log({.level = LogLevel::Info,
|
||||
.phase = PipelinePhase::Startup,
|
||||
@@ -179,8 +179,7 @@ int main(const int argc, char** argv) {
|
||||
}
|
||||
|
||||
if (options.generator.mode == GeneratorMode::kOpenAI) {
|
||||
log_producer->Log(
|
||||
{.level = LogLevel::Info,
|
||||
log_producer->Log({.level = LogLevel::Info,
|
||||
.phase = PipelinePhase::Startup,
|
||||
.message = std::format(
|
||||
"Generator: OpenAIGenerator | model={}",
|
||||
@@ -194,7 +193,8 @@ int main(const int argc, char** argv) {
|
||||
}
|
||||
|
||||
#ifdef BIERGARTEN_MOCK_ONLY
|
||||
// Only reachable when mode == Llama, which requires llama.cpp and is excluded from a BIERGARTEN_MOCK_ONLY build.
|
||||
// Only reachable when mode == Llama, which requires llama.cpp and
|
||||
// is excluded from a BIERGARTEN_MOCK_ONLY build.
|
||||
throw std::runtime_error(
|
||||
"LlamaGenerator is unavailable in a BIERGARTEN_MOCK_ONLY "
|
||||
"build");
|
||||
|
||||
@@ -125,8 +125,7 @@ const LocationsList& CuratedJsonDataService::LoadLocations() {
|
||||
if (!cache_.locations.empty()) {
|
||||
return cache_.locations;
|
||||
}
|
||||
boost::json::value root_val =
|
||||
ParseJsonFile(filepaths_.locations_path);
|
||||
boost::json::value root_val = ParseJsonFile(filepaths_.locations_path);
|
||||
|
||||
if (!root_val.is_object()) {
|
||||
throw std::runtime_error(
|
||||
@@ -135,14 +134,12 @@ const LocationsList& CuratedJsonDataService::LoadLocations() {
|
||||
|
||||
boost::json::object root_obj = root_val.as_object();
|
||||
|
||||
if (!root_obj.contains("cities"))
|
||||
{
|
||||
if (!root_obj.contains("cities")) {
|
||||
throw std::runtime_error(
|
||||
"Invalid locations JSON: root object must contain a 'cities' key");
|
||||
}
|
||||
|
||||
if (!root_obj["cities"].is_array())
|
||||
{
|
||||
if (!root_obj["cities"].is_array()) {
|
||||
throw std::runtime_error(
|
||||
"Invalid locations JSON: 'cities' key must be an array");
|
||||
}
|
||||
@@ -179,8 +176,7 @@ const PersonasList& CuratedJsonDataService::LoadPersonas() {
|
||||
return cache_.personas;
|
||||
}
|
||||
|
||||
const boost::json::value root =
|
||||
ParseJsonFile(filepaths_.personas_path);
|
||||
const boost::json::value root = ParseJsonFile(filepaths_.personas_path);
|
||||
|
||||
if (!root.is_array()) {
|
||||
throw std::runtime_error(
|
||||
@@ -215,8 +211,7 @@ const ForenamesByCountryMap& CuratedJsonDataService::LoadForenamesByCountry() {
|
||||
return cache_.forenames_by_country;
|
||||
}
|
||||
|
||||
const boost::json::value root =
|
||||
ParseJsonFile(filepaths_.forenames_path);
|
||||
const boost::json::value root = ParseJsonFile(filepaths_.forenames_path);
|
||||
|
||||
if (!root.is_object()) {
|
||||
throw std::runtime_error(
|
||||
@@ -263,8 +258,7 @@ const SurnamesByCountryMap& CuratedJsonDataService::LoadSurnamesByCountry() {
|
||||
return cache_.surnames_by_country;
|
||||
}
|
||||
|
||||
const boost::json::value root =
|
||||
ParseJsonFile(filepaths_.surnames_path);
|
||||
const boost::json::value root = ParseJsonFile(filepaths_.surnames_path);
|
||||
|
||||
if (!root.is_object()) {
|
||||
throw std::runtime_error(
|
||||
|
||||
@@ -180,8 +180,8 @@ uint64_t SqliteExportService::ProcessRecord(const BreweryRecord& brewery) {
|
||||
sqlite_export_service_internal::Bind(
|
||||
insert_brewery_address_stmt_,
|
||||
sqlite_export_service_internal::BoundParam<double>{
|
||||
.index = sqlite_export_service_internal::
|
||||
kBreweryAddressLatitudeBindIndex,
|
||||
.index =
|
||||
sqlite_export_service_internal::kBreweryAddressLatitudeBindIndex,
|
||||
.value = brewery.address.latitude,
|
||||
.action = "Failed to bind SQLite brewery address latitude"});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user