mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-06-01 01:54:00 +00:00
Code format updates
This commit is contained in:
@@ -36,7 +36,6 @@ std::string PrepareRegionContextPublic(std::string_view region_context,
|
|||||||
std::pair<std::string, std::string> ParseTwoLineResponsePublic(
|
std::pair<std::string, std::string> ParseTwoLineResponsePublic(
|
||||||
const std::string& raw, const std::string& error_message);
|
const std::string& raw, const std::string& error_message);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Applies model chat template to system and user prompts.
|
* @brief Applies model chat template to system and user prompts.
|
||||||
*
|
*
|
||||||
@@ -68,7 +67,8 @@ void AppendTokenPiecePublic(const llama_vocab* vocab, llama_token token,
|
|||||||
* @return Validation error message if invalid, or std::nullopt on success.
|
* @return Validation error message if invalid, or std::nullopt on success.
|
||||||
*/
|
*/
|
||||||
std::optional<std::string> ValidateBreweryJsonPublic(
|
std::optional<std::string> ValidateBreweryJsonPublic(
|
||||||
const std::string& raw, std::string& name_out, std::string& description_out);
|
const std::string& raw, std::string& name_out,
|
||||||
|
std::string& description_out);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Extracts the last balanced JSON object from text.
|
* @brief Extracts the last balanced JSON object from text.
|
||||||
|
|||||||
@@ -44,8 +44,8 @@ class MockGenerator final : public DataGenerator {
|
|||||||
*/
|
*/
|
||||||
static std::size_t DeterministicHash(const Location& location);
|
static std::size_t DeterministicHash(const Location& location);
|
||||||
|
|
||||||
static constexpr std::array<std::string_view, 18> kBreweryAdjectives =
|
static constexpr std::array<std::string_view, 18> kBreweryAdjectives = {
|
||||||
{"Craft", "Heritage", "Local", "Artisan", "Pioneer", "Golden",
|
"Craft", "Heritage", "Local", "Artisan", "Pioneer", "Golden",
|
||||||
"Modern", "Classic", "Summit", "Northern", "Riverstone", "Barrel",
|
"Modern", "Classic", "Summit", "Northern", "Riverstone", "Barrel",
|
||||||
"Hinterland", "Harbor", "Wild", "Granite", "Copper", "Maple"};
|
"Hinterland", "Harbor", "Wild", "Granite", "Copper", "Maple"};
|
||||||
|
|
||||||
@@ -55,8 +55,7 @@ class MockGenerator final : public DataGenerator {
|
|||||||
"Project", "Foundry", "Malthouse", "Public House", "Co-op",
|
"Project", "Foundry", "Malthouse", "Public House", "Co-op",
|
||||||
"Lab", "Beer Hall", "Guild"};
|
"Lab", "Beer Hall", "Guild"};
|
||||||
|
|
||||||
static constexpr std::array<std::string_view, 18>
|
static constexpr std::array<std::string_view, 18> kBreweryDescriptions = {
|
||||||
kBreweryDescriptions = {
|
|
||||||
"Handcrafted pale ales and seasonal IPAs with local ingredients.",
|
"Handcrafted pale ales and seasonal IPAs with local ingredients.",
|
||||||
"Traditional lagers and experimental sours in small batches.",
|
"Traditional lagers and experimental sours in small batches.",
|
||||||
"Award-winning stouts and wildly hoppy blonde ales.",
|
"Award-winning stouts and wildly hoppy blonde ales.",
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ bool BiergartenDataGenerator::Run() {
|
|||||||
spdlog::info("[Pipeline] Context for '{}' ({}) gathered:\n{}",
|
spdlog::info("[Pipeline] Context for '{}' ({}) gathered:\n{}",
|
||||||
city.city, city.country, region_context);
|
city.city, city.country, region_context);
|
||||||
|
|
||||||
enriched.push_back(EnrichedCity{.location = city,
|
enriched.push_back(
|
||||||
.region_context = region_context});
|
EnrichedCity{.location = city, .region_context = region_context});
|
||||||
} catch (const std::exception& exception) {
|
} catch (const std::exception& exception) {
|
||||||
++skipped_count;
|
++skipped_count;
|
||||||
spdlog::warn(
|
spdlog::warn(
|
||||||
|
|||||||
@@ -35,8 +35,7 @@ static std::string ExtractFinalJsonPayload(std::string raw_response) {
|
|||||||
for (const std::string_view token : separator_tokens) {
|
for (const std::string_view token : separator_tokens) {
|
||||||
const std::size_t candidate_pos = raw_response.rfind(token);
|
const std::size_t candidate_pos = raw_response.rfind(token);
|
||||||
if (candidate_pos != std::string::npos &&
|
if (candidate_pos != std::string::npos &&
|
||||||
(separator_pos == std::string::npos ||
|
(separator_pos == std::string::npos || candidate_pos > separator_pos)) {
|
||||||
candidate_pos > separator_pos)) {
|
|
||||||
separator_pos = candidate_pos;
|
separator_pos = candidate_pos;
|
||||||
separator_length = token.size();
|
separator_length = token.size();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -104,8 +104,7 @@ static std::string ToChatPrompt(const llama_model* model,
|
|||||||
std::vector<char> buffer(std::max<std::size_t>(
|
std::vector<char> buffer(std::max<std::size_t>(
|
||||||
1024, (system_prompt.size() + user_prompt.size()) * 4));
|
1024, (system_prompt.size() + user_prompt.size()) * 4));
|
||||||
|
|
||||||
auto apply_template_with_resize =
|
auto apply_template_with_resize = [&](const llama_chat_message* chat_messages,
|
||||||
[&](const llama_chat_message* chat_messages,
|
|
||||||
int32_t message_count) -> int32_t {
|
int32_t message_count) -> int32_t {
|
||||||
int32_t result = llama_chat_apply_template(
|
int32_t result = llama_chat_apply_template(
|
||||||
tmpl, chat_messages, message_count, true, buffer.data(),
|
tmpl, chat_messages, message_count, true, buffer.data(),
|
||||||
@@ -117,8 +116,8 @@ static std::string ToChatPrompt(const llama_model* model,
|
|||||||
|
|
||||||
if (result >= static_cast<int32_t>(buffer.size())) {
|
if (result >= static_cast<int32_t>(buffer.size())) {
|
||||||
buffer.resize(static_cast<std::size_t>(result) + 1);
|
buffer.resize(static_cast<std::size_t>(result) + 1);
|
||||||
result = llama_chat_apply_template(
|
result = llama_chat_apply_template(tmpl, chat_messages, message_count,
|
||||||
tmpl, chat_messages, message_count, true, buffer.data(),
|
true, buffer.data(),
|
||||||
static_cast<int32_t>(buffer.size()));
|
static_cast<int32_t>(buffer.size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,14 +157,14 @@ static std::string ToChatPrompt(const llama_model* model,
|
|||||||
static void AppendTokenPiece(const llama_vocab* vocab, llama_token token,
|
static void AppendTokenPiece(const llama_vocab* vocab, llama_token token,
|
||||||
std::string& output) {
|
std::string& output) {
|
||||||
std::array<char, 256> buffer{};
|
std::array<char, 256> buffer{};
|
||||||
int32_t bytes = llama_token_to_piece(vocab, token, buffer.data(),
|
int32_t bytes =
|
||||||
buffer.size(), 0, true);
|
llama_token_to_piece(vocab, token, buffer.data(), buffer.size(), 0, true);
|
||||||
|
|
||||||
if (bytes < 0) {
|
if (bytes < 0) {
|
||||||
std::vector<char> dynamic_buffer(static_cast<std::size_t>(-bytes));
|
std::vector<char> dynamic_buffer(static_cast<std::size_t>(-bytes));
|
||||||
bytes = llama_token_to_piece(vocab, token, dynamic_buffer.data(),
|
bytes = llama_token_to_piece(vocab, token, dynamic_buffer.data(),
|
||||||
static_cast<int32_t>(dynamic_buffer.size()),
|
static_cast<int32_t>(dynamic_buffer.size()), 0,
|
||||||
0, true);
|
true);
|
||||||
if (bytes < 0) {
|
if (bytes < 0) {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"LlamaGenerator: failed to decode sampled token piece");
|
"LlamaGenerator: failed to decode sampled token piece");
|
||||||
|
|||||||
@@ -44,8 +44,7 @@ std::string LlamaGenerator::LoadBrewerySystemPrompt(
|
|||||||
prompt_file.close();
|
prompt_file.close();
|
||||||
|
|
||||||
if (prompt.empty()) {
|
if (prompt.empty()) {
|
||||||
spdlog::error(
|
spdlog::error("LlamaGenerator: Brewery system prompt file '{}' is empty",
|
||||||
"LlamaGenerator: Brewery system prompt file '{}' is empty",
|
|
||||||
prompt_path.string());
|
prompt_path.string());
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"LlamaGenerator: empty brewery system prompt file: " +
|
"LlamaGenerator: empty brewery system prompt file: " +
|
||||||
|
|||||||
@@ -31,8 +31,8 @@ BreweryResult MockGenerator::GenerateBrewery(
|
|||||||
const std::string country_suffix =
|
const std::string country_suffix =
|
||||||
location.country.empty() ? std::string{}
|
location.country.empty() ? std::string{}
|
||||||
: std::format(", {}", location.country);
|
: std::format(", {}", location.country);
|
||||||
const std::string description = std::format(
|
const std::string description =
|
||||||
"{} Located in {}{}{}.", base_description, location.city,
|
std::format("{} Located in {}{}{}.", base_description, location.city,
|
||||||
state_suffix, country_suffix);
|
state_suffix, country_suffix);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ static std::string ReadRequiredString(const boost::json::object& object,
|
|||||||
const char* key) {
|
const char* key) {
|
||||||
const boost::json::value* value = object.if_contains(key);
|
const boost::json::value* value = object.if_contains(key);
|
||||||
if (value == nullptr || !value->is_string()) {
|
if (value == nullptr || !value->is_string()) {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(std::string("Missing or invalid string field: ") +
|
||||||
std::string("Missing or invalid string field: ") + key);
|
key);
|
||||||
}
|
}
|
||||||
const std::string_view text = value->as_string();
|
const std::string_view text = value->as_string();
|
||||||
return std::string(text);
|
return std::string(text);
|
||||||
@@ -29,8 +29,8 @@ static double ReadRequiredNumber(const boost::json::object& object,
|
|||||||
const char* key) {
|
const char* key) {
|
||||||
const boost::json::value* value = object.if_contains(key);
|
const boost::json::value* value = object.if_contains(key);
|
||||||
if (value == nullptr || !value->is_number()) {
|
if (value == nullptr || !value->is_number()) {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(std::string("Missing or invalid numeric field: ") +
|
||||||
std::string("Missing or invalid numeric field: ") + key);
|
key);
|
||||||
}
|
}
|
||||||
return value->to_number<double>();
|
return value->to_number<double>();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,8 +34,7 @@ namespace di = boost::di;
|
|||||||
* @return Parsed ApplicationOptions if parsing succeeded, std::nullopt
|
* @return Parsed ApplicationOptions if parsing succeeded, std::nullopt
|
||||||
* otherwise.
|
* otherwise.
|
||||||
*/
|
*/
|
||||||
std::optional<ApplicationOptions> ParseArguments(const int argc,
|
std::optional<ApplicationOptions> ParseArguments(const int argc, char** argv) {
|
||||||
char** argv) {
|
|
||||||
prog_opts::options_description desc("Pipeline Options");
|
prog_opts::options_description desc("Pipeline Options");
|
||||||
|
|
||||||
auto opt = desc.add_options();
|
auto opt = desc.add_options();
|
||||||
@@ -149,8 +148,8 @@ int main(const int argc, char** argv) {
|
|||||||
di::bind<ApplicationOptions>().to(options),
|
di::bind<ApplicationOptions>().to(options),
|
||||||
di::bind<IEnrichmentService>().to<WikipediaService>(),
|
di::bind<IEnrichmentService>().to<WikipediaService>(),
|
||||||
di::bind<std::string>().to(options.model_path),
|
di::bind<std::string>().to(options.model_path),
|
||||||
di::bind<DataGenerator>().to([options](const auto& inj)
|
di::bind<DataGenerator>().to(
|
||||||
-> std::unique_ptr<DataGenerator> {
|
[options](const auto& inj) -> std::unique_ptr<DataGenerator> {
|
||||||
if (options.use_mocked) {
|
if (options.use_mocked) {
|
||||||
spdlog::info(
|
spdlog::info(
|
||||||
"[Generator] Using MockGenerator (no model path provided)");
|
"[Generator] Using MockGenerator (no model path provided)");
|
||||||
|
|||||||
@@ -34,8 +34,7 @@ std::string WikipediaService::FetchExtract(std::string_view query) {
|
|||||||
if (!pages.empty()) {
|
if (!pages.empty()) {
|
||||||
auto& page = pages.begin()->value().get_object();
|
auto& page = pages.begin()->value().get_object();
|
||||||
if (page.contains("extract") && page.at("extract").is_string()) {
|
if (page.contains("extract") && page.at("extract").is_string()) {
|
||||||
const std::string_view extract_view =
|
const std::string_view extract_view = page.at("extract").as_string();
|
||||||
page.at("extract").as_string();
|
|
||||||
std::string extract(extract_view);
|
std::string extract(extract_view);
|
||||||
|
|
||||||
spdlog::debug("WikipediaService fetched {} chars for '{}'",
|
spdlog::debug("WikipediaService fetched {} chars for '{}'",
|
||||||
|
|||||||
@@ -37,8 +37,7 @@ static void set_common_get_options(CURL* curl, const std::string& url) {
|
|||||||
|
|
||||||
// curl write callback that appends response data into a std::string
|
// curl write callback that appends response data into a std::string
|
||||||
static size_t WriteCallbackString(void* contents, const size_t size,
|
static size_t WriteCallbackString(void* contents, const size_t size,
|
||||||
const size_t nmemb,
|
const size_t nmemb, void* userp) {
|
||||||
void* userp) {
|
|
||||||
const size_t real_size = size * nmemb;
|
const size_t real_size = size * nmemb;
|
||||||
auto* str = static_cast<std::string*>(userp);
|
auto* str = static_cast<std::string*>(userp);
|
||||||
str->append(static_cast<char*>(contents), real_size);
|
str->append(static_cast<char*>(contents), real_size);
|
||||||
@@ -68,8 +67,7 @@ std::string CURLWebClient::Get(const std::string& url) {
|
|||||||
|
|
||||||
if (httpCode != 200) {
|
if (httpCode != 200) {
|
||||||
const std::string error = "[CURLWebClient] HTTP error " +
|
const std::string error = "[CURLWebClient] HTTP error " +
|
||||||
std::to_string(httpCode) +
|
std::to_string(httpCode) + " for URL " + url;
|
||||||
" for URL " + url;
|
|
||||||
throw std::runtime_error(error);
|
throw std::runtime_error(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user