Repository: ggerganov/llama.cpp
File: common/chat.cpp (C++)

Code before:
            data.preserved_tokens = {
                "<think>",
                "</think>",
                "<tool_call>",
                "</tool_call>",
            };
        });
    }

    return data;
}

static common_chat_params common_chat_params_init_translate_gemma(const common_chat_template & tmpl, const struct templates_params & inputs) {
    common_chat_params data;

    // This template does not support tools or reasoning
    // we just need to transform the messages into the correct schema

    templates_params inputs_new = inputs;
    json & messages = inputs_new.messages;

    // default to chat_template_kwargs, or en-GB if not specified
    std::string default_src_lang = inputs.extra_context.contains("source_lang_code")
        ? inputs.extra_context["source_lang_code"].get<std::string>() : "en-GB";
    std::string default_tgt_lang = inputs.extra_context.contains("target_lang_code")
        ? inputs.extra_context["target_lang_code"].get<std::string>() : "en-GB";

    GGML_ASSERT(messages.is_array());
    for (auto & message : messages) {
        if (message.contains("role") && message["role"].get<std::string>() != "user") {
            continue;
        }
        if (!message.contains("content")) {
            message["content"] = json::array();
        }
        if (message.contains("content") && !message["content"].is_array()) {
            auto content_str = message["content"].get<std::string>();
            // default to en-GB if not specified (to make common_chat_format_example works)
            auto src_lang = message.contains("source_lang_code")
                        ? message["source_lang_code"].get<std::string>() : default_src_lang;
            auto tgt_lang = message.contains("target_lang_code")
                        ? message["target_lang_code"].get<std::string>() : default_tgt_lang;
            message["content"] = json::array({
                json{
                    {"type", "text"},
                    {"text", content_str},
                    {"source_lang_code", src_lang},
                    {"target_lang_code", tgt_lang},
                }
            });
        }

Review comment (suggestion):
Maybe `value()` is a little easier to read here?
https://json.nlohmann.me/api/basic_json/value/

```suggestion
    std::string default_src_lang = inputs.extra_context.value("source_lang_code", "en-GB");
    std::string default_tgt_lang = inputs.extra_context.value("target_lang_code", "en-GB");
```

Code after:
            data.preserved_tokens = {
                "<think>",
                "</think>",
                "<tool_call>",
                "</tool_call>",
            };
        });
    }

    return data;
}

static common_chat_params common_chat_params_init_translate_gemma(const common_chat_template & tmpl, const struct templates_params & inputs) {
    common_chat_params data;

    // This template does not support tools or reasoning
    // we just need to transform the messages into the correct schema

    templates_params inputs_new = inputs;
    json & messages = inputs_new.messages;

    // default to chat_template_kwargs, or en-GB if not specified
    std::string default_src_lang = inputs.extra_context.value("source_lang_code", "en-GB");
    std::string default_tgt_lang = inputs.extra_context.value("target_lang_code", "en-GB");

    GGML_ASSERT(messages.is_array());
    for (auto & message : messages) {
        if (message.contains("role") && message["role"].get<std::string>() != "user") {
            continue;
        }
        if (!message.contains("content")) {
            message["content"] = json::array();
        }
        if (message.contains("content") && !message["content"].is_array()) {
            auto content_str = message["content"].get<std::string>();
            // default to en-GB if not specified (to make common_chat_format_example works)
            auto src_lang = message.contains("source_lang_code")
                        ? message["source_lang_code"].get<std::string>() : default_src_lang;
            auto tgt_lang = message.contains("target_lang_code")
                        ? message["target_lang_code"].get<std::string>() : default_tgt_lang;
            message["content"] = json::array({
                json{
                    {"type", "text"},
                    {"text", content_str},
                    {"source_lang_code", src_lang},
                    {"target_lang_code", tgt_lang},
                }
            });
        }
    }