core: a speech model without instructions, and a refusal's reason from its text_prepare -- an audio generator may declare tmpl_plain alone (a request's instructions refused by name), and the reason a plugin leaves in the output buffer reaches the caller

This commit is contained in:
zivius committed 2026-10-20 23:07:33 +02:00
1 parent 61abb990ab
commit 832a88faac
7 files changed
+30 -9

No files matched your search

+5 -3
View File
@@ -902,7 +902,8 @@ typedef struct RadAudioGenDecl {
/* The prompt, as text the core tokenises: `{instructions}` is replaced by the request's
* instructions (a piece's description, a voice's) and `{input}` by its input text (lyrics, the
* words to speak), each first rewritten by the plugin's rad_arch_text_prepare when it exports
* one. Special tokens are spelled as text. */
* one. Special tokens are spelled as text. NULL for a model that takes no instructions, which
* then declares `tmpl_plain` and has a request's instructions refused. */
const char* tmpl;
/* The unconditional branch is the conditional prompt with every token but the first
* `neg_keep_head` and the last `neg_keep_tail` replaced by `neg_token`, so the two branches
@@ -1430,8 +1431,9 @@ const RadAudioFormat* rad_arch_audio_format(const RadModelMeta* meta);
*
* `field` is RAD_TEXT_*, `in` NUL-terminated UTF-8. Writes at most `cap` bytes, the NUL included,
* and returns the length the whole result needs without its NUL (call again with more room when
* it is >= cap), or a negative status for text the model cannot take. Called on HTTP threads, so
* it keeps no state. */
* it is >= cap), or a negative status for text the model cannot take, with the reason in `out`
* (NUL-terminated within `cap`) when it has one to give the caller. Called on HTTP threads, so it
* keeps no state. */
enum { RAD_TEXT_INSTRUCTIONS = 1, RAD_TEXT_INPUT = 2 };
int64_t rad_arch_text_prepare(int field, const char* in, char* out, int64_t cap);
+7 -2
View File
@@ -1361,7 +1361,12 @@ int Builder::declare_audio_generator(const RadAudioGenDecl* d) {
: prog_.generator.layout != 0 ? "an image generator" : "a video generator");
return RAD_E_STATE;
}
if (!d->tmpl || !std::strstr(d->tmpl, "{input}") || !std::strstr(d->tmpl, "{instructions}")) {
if (!d->tmpl && !d->tmpl_plain) {
err(RAD_E_INVAL, "declare_audio_generator '%s': no prompt template (tmpl or tmpl_plain)",
nm);
return RAD_E_INVAL;
}
if (d->tmpl && (!std::strstr(d->tmpl, "{input}") || !std::strstr(d->tmpl, "{instructions}"))) {
err(RAD_E_INVAL, "declare_audio_generator '%s': tmpl needs {instructions} and {input}", nm);
return RAD_E_INVAL;
}
@@ -1460,7 +1465,7 @@ int Builder::declare_audio_generator(const RadAudioGenDecl* d) {
!buf_ok(d->audio_out, RAD_F32, win_samples * d->channels, "audio_out"))
return RAD_E_INVAL;
prog_.agen_str.name = nm;
prog_.agen_str.tmpl = d->tmpl;
prog_.agen_str.tmpl = d->tmpl ? d->tmpl : "";
prog_.agen_str.tmpl_plain = d->tmpl_plain ? d->tmpl_plain : "";
prog_.agen_str.neg_tmpl = d->neg_tmpl ? d->neg_tmpl : "";
prog_.audio_gen = *d;
+6 -1
View File
@@ -270,7 +270,12 @@ int rad_tools_text_prepare(const LoadedPlugins& lp, int field, const std::string
std::string buf(in.size() * 2 + 64, '\0');
for (int tries = 0; tries < 2; ++tries) {
const int64_t n = ap->arch_text_prepare(field, in.c_str(), &buf[0], (int64_t)buf.size());
if (n < 0) return (int)n;
if (n < 0) {
/* The plugin's reason, when it left one in the (zeroed) buffer. */
buf.resize(std::strlen(buf.c_str()));
*out = std::move(buf);
return (int)n;
}
if (n < (int64_t)buf.size()) {
buf.resize((size_t)n);
*out = std::move(buf);
+1 -1
View File
@@ -93,7 +93,7 @@ const RadAudioFormat* rad_tools_audio_format(const LoadedPlugins& plugins, const
* (rad_arch_text_prepare): RAD_OK with the result in `*out` (the text unchanged when the plugin
* states no rewrite), or the plugin's negative status. */
int rad_tools_text_prepare(const LoadedPlugins& plugins, int field, const std::string& in,
std::string* out);
std::string* out); /* on a refusal, `out` is its reason or "" */
/* The architecture plugin's video positions (rad_arch_video_positions), or null when it exports
* none. */
+3 -2
View File
@@ -2471,8 +2471,9 @@ int Engine::run_server() {
std::string* why) const override {
const int s = rad_tools_text_prepare(*plugins, field, in, out);
if (s < 0 && why)
*why = s == RAD_E_INVAL ? "the model cannot take this text"
: std::string(rad_strerror(s));
*why = !out->empty() ? *out
: s == RAD_E_INVAL ? "the model cannot take this text"
: std::string(rad_strerror(s));
return s;
}
int submit(std::unique_ptr<gen::AudioRequest> r, std::string* why) override {
+3
View File
@@ -110,6 +110,9 @@ int Server::speech_request(const json& body, const std::string& text,
return fail(err_bad("instructions", "this model needs instructions, as a non-empty "
"string: for a song its genre, mood, vocals and "
"instruments"));
if (!inst.empty() && info.tmpl.empty())
return fail(err_unsupported("instructions", "this model takes no instructions; its "
"direction is written in the input"));
if (body.contains("speed") && !body["speed"].is_null() &&
!(body["speed"].is_number() && body["speed"].get<double>() == 1.0))
return fail(err_unsupported("speed", "this model keeps its own tempo; the instructions "
+5
View File
@@ -189,6 +189,11 @@ TEST(a_model_that_asks_has_its_input_read_with_special_tokens_and_one_branch_has
CHECK_EQ(s.refused({ { "input", "hi" }, { "guidance_scale", 2.0 } }),
std::string("guidance_scale"));
CHECK_EQ(s.post({ { "input", "hi" }, { "guidance_scale", 1.0 } }).status, 200);
/* A model that declares no template with instructions refuses them by name. */
s.gen.inf.tmpl.clear();
CHECK_EQ(s.refused({ { "input", "hi" }, { "instructions", "calm" } }),
std::string("instructions"));
CHECK_EQ(s.post({ { "input", "hi" } }).status, 200);
Speech m;
m.gen.inf.tmpl_plain = "<s>{input}";
m.gen.inf.default_steps = 0;