fix: stabilize Gemma 4 brewery generation

remove misleading turn-token output guidance from the brewery prompt
extract the last balanced JSON object before validation
keep README model setup and run instructions aligned
preserve Gemma 4 sampling defaults and local model usage
This commit is contained in:
Aaron Po
2026-04-10 22:25:26 -04:00
parent 902bda6eb9
commit 8c572a2d07
5 changed files with 74 additions and 44 deletions

View File

@@ -26,8 +26,9 @@ auto ExtractFinalJsonPayload(std::string raw_response) -> std::string {
return text.substr(first, last - first + 1);
};
static const std::array<std::string_view, 4> separator_tokens = {
"<|turn|>", "<turn|>", "<channel|>", "<|channel|>"};
static const std::array<std::string_view, 6> separator_tokens = {
"<|think|>", "<think|>", "<|turn|>",
"<turn|>", "<channel|>", "<|channel|>"};
std::size_t separator_pos = std::string::npos;
std::size_t separator_length = 0;
@@ -46,18 +47,13 @@ auto ExtractFinalJsonPayload(std::string raw_response) -> std::string {
}
const std::string_view trimmed = trim(raw_response);
const std::size_t first_brace = trimmed.find('{');
if (first_brace == std::string_view::npos) {
return std::string(trimmed);
std::string json_candidate =
ExtractLastJsonObjectPublic(std::string(trimmed));
if (!json_candidate.empty()) {
return ExtractLastJsonObjectPublic(std::string(trimmed));
}
const std::size_t last_brace = trimmed.find_last_of('}');
if (last_brace == std::string_view::npos || last_brace < first_brace) {
return std::string(trimmed.substr(first_brace));
}
return std::string(
trimmed.substr(first_brace, last_brace - first_brace + 1));
return std::string(trimmed);
}
} // namespace
@@ -147,9 +143,11 @@ auto LlamaGenerator::GenerateBrewery(const BreweryLocation& location,
// limits.
prompt =
"Your previous response was invalid. Error: " + validation_error +
"\nReturn ONLY valid JSON with this exact schema: "
"{\"name\": \"string\", \"description\": \"string\"}."
"\nDo not include markdown, comments, or extra keys.";
"\nReturn ONLY valid JSON with exactly these keys: "
"{\"name\": \"<brewery name>\", "
"\"description\": \"<single-paragraph description>\"}."
"\nDo not include markdown, comments, extra keys, or literal "
"placeholder values.";
prompt += "\n\n";
prompt += retry_location;
}