diff --git a/abi/rad_builder.h b/abi/rad_builder.h index e122636d..2480458f 100644 --- a/abi/rad_builder.h +++ b/abi/rad_builder.h @@ -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); diff --git a/core/build/rad_builder.cpp b/core/build/rad_builder.cpp index 41915494..dc549198 100644 --- a/core/build/rad_builder.cpp +++ b/core/build/rad_builder.cpp @@ -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; diff --git a/core/build/startup.cpp b/core/build/startup.cpp index fe5178ad..b08ee817 100644 --- a/core/build/startup.cpp +++ b/core/build/startup.cpp @@ -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); diff --git a/core/build/startup.h b/core/build/startup.h index d84b7ab7..78fe62d1 100644 --- a/core/build/startup.h +++ b/core/build/startup.h @@ -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. */ diff --git a/core/engine.cpp b/core/engine.cpp index dd8db874..9c35f125 100644 --- a/core/engine.cpp +++ b/core/engine.cpp @@ -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 r, std::string* why) override { diff --git a/core/server/audio_gen.cpp b/core/server/audio_gen.cpp index 78c8e93b..13eb7f05 100644 --- a/core/server/audio_gen.cpp +++ b/core/server/audio_gen.cpp @@ -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() == 1.0)) return fail(err_unsupported("speed", "this model keeps its own tempo; the instructions " diff --git a/tests/speech_test.cpp b/tests/speech_test.cpp index 6cb7536f..71109a6e 100644 --- a/tests/speech_test.cpp +++ b/tests/speech_test.cpp @@ -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 = "{input}"; m.gen.inf.default_steps = 0;