Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions app/npu_tool_grammar.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

#include <nlohmann/json.hpp>

#include <cstddef>
#include <stdexcept>
#include <string>
#include <vector>
Expand Down Expand Up @@ -178,6 +179,33 @@ inline std::string tool_call_gbnf(const nlohmann::json& tools, const nlohmann::j
// (app/unified.cpp, Engine::generate dispatches `if (lane)` first), and telling that reader about `PrivateRequest`
// sends them to code their request never touched, with a way out (an env var `unified.cpp` never reads) that does
// nothing for the process that just refused them.
//
// engine#3287: tools that are only OFFERED are dropped, not refused. A request whose `tool_choice` is absent, "auto" or
// "none" asks for "a call, or a plain answer", and this file's own header says what that is worth on any route: "auto"
// installs no grammar even on the per-op forward, so a plain answer is a correct reply to it, not the silently wrong
// shape #3193 was about. Clients send tools that way on every message (the Lemonade web app offers its 19 tools with
// every chat), so refusing them made the NPU unusable from the Lemonade page, and the appliance needed a proxy that
// stripped them in front of lemond. The tools are removed BEFORE the prompt is rendered, so the template does not spend
// prefill on definitions the route cannot act on (19 of them are thousands of tokens). A request that DEMANDS a call
// (tool_choice "required" or a named function) keeps its tools and is still refused by npu_constraint_refusal below,
// as is a caller's json_schema. Returns how many tools were dropped (0: the body is unchanged); the handler says so in
// the X-1bit-Tools-Ignored response header and on stderr, so the drop is visible rather than silent.
template <class Json>
inline std::size_t npu_drop_offered_tools(Json& body) {
if (!body.is_object() || !body.contains("tools") || !body["tools"].is_array() || body["tools"].empty()) return 0;
if (body.contains("tool_choice") && !body["tool_choice"].is_null()) {
const auto& c = body["tool_choice"];
if (!c.is_string()) return 0; // {"type": "function", ...}: a named call is demanded
const std::string s = c.template get<std::string>();
if (s != "auto" && s != "none") return 0; // "required", or a value this code does not know: left to the refusal
}
const std::size_t n = body["tools"].size();
body.erase("tools");
body.erase("tool_choice");
body.erase("parallel_tool_calls");
return n;
}

inline std::string npu_constraint_refusal(const nlohmann::json& body, const std::string& route, bool private_route) {
// `tools: []` is not a constraint: many clients send the key with an empty list on every request, and refusing
// those would break ordinary chats. Only a non-empty list (or a real tool_choice) asks for something the route
Expand Down
16 changes: 15 additions & 1 deletion app/unified.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,18 @@ void handle_generate(Engine& e, const httplib::Request& req, httplib::Response&
// engine#3193: a private route's session generates its own tokens and cannot carry a tool grammar, so a
// request with `tools`/`tool_choice` used to be answered as prose -- silently the wrong shape, where the
// per-op forward emits a real tool_calls. Refused rather than ignored, like the over-context case below.
// engine#3287: except tools that are only OFFERED (tool_choice absent/"auto"/"none"), which are dropped first: a plain
// answer is a correct reply to those on every route (npu_tool_grammar.h), and the Lemonade page offers 19 of them
// with every chat. A demanded call ("required", a named function) still reaches the refusal.
std::size_t tools_ignored = 0;
if (chat) {
tools_ignored = npu::npu_drop_offered_tools(body);
if (tools_ignored) {
res.set_header("X-1bit-Tools-Ignored", std::to_string(tools_ignored));
std::fprintf(stderr, "1bit unified: %s: ignored %zu offered tool(s) (tool_choice auto/none): this route answers "
"without tool calls (engine#3287)\n", e.id.c_str(), tools_ignored);
}
}
if (chat) {
// engine#3193: which NPU path this model is on decides the sentence, because the two have different
// (equally real) reasons and different ways out -- e.priv is set only on the private route.
Expand All @@ -234,7 +246,9 @@ void handle_generate(Engine& e, const httplib::Request& req, httplib::Response&
if (chat && e.tmpl.present() && npu::renderer_built()) {
// engine#3193: the container's template, not ChatML. `request_template_vars` carries the request's
// enable_thinking/tools exactly as the per-op forward passes them, so the two routes agree token for token.
const nlohmann::ordered_json ordered = nlohmann::ordered_json::parse(req.body);
nlohmann::ordered_json ordered = nlohmann::ordered_json::parse(req.body);
// the same drop as above, on the copy the template sees: dropped tools must not reach the prompt
if (tools_ignored) npu::npu_drop_offered_tools(ordered);
prompt = npu::render_chat_template(e.tmpl, npu::messages_for_template(ordered.at("messages")),
/*add_generation_prompt=*/true,
npu::request_template_vars(ordered, thinking), e.bos_text, e.eos_text);
Expand Down
56 changes: 56 additions & 0 deletions tests/npu_private_route_tools_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <string>

using onebit::npu::npu_constraint_refusal;
using onebit::npu::npu_drop_offered_tools;
using json = nlohmann::json;

namespace {
Expand Down Expand Up @@ -132,6 +133,61 @@ int main() {
check(npu_constraint_refusal(body, route, true).empty(), "an ordinary chat request is not refused");
}

// engine#3287: what the HANDLER does first. Tools that are only offered are dropped, so the request reaches the
// route as an ordinary chat; a demanded call keeps its tools and the refusal above still answers it.
{
json body = {{"messages", json::array()}, {"tools", tools}};
check(npu_drop_offered_tools(body) == 1, "tools with no tool_choice are offered, so they are dropped (count 1)");
check(!body.contains("tools") && npu_constraint_refusal(body, route, true).empty(),
"and the dropped request is an ordinary chat: no refusal");
}
{
json body = {{"messages", json::array()}, {"tools", tools}, {"tool_choice", "auto"}, {"parallel_tool_calls", true}};
check(npu_drop_offered_tools(body) == 1, "tool_choice auto offers, so the tools are dropped");
check(!body.contains("tool_choice") && !body.contains("parallel_tool_calls"),
"and tool_choice and parallel_tool_calls go with them");
check(npu_constraint_refusal(body, route, true).empty(), "leaving nothing to refuse");
}
{
json body = {{"messages", json::array()}, {"tools", tools}, {"tool_choice", "none"}};
check(npu_drop_offered_tools(body) == 1 && npu_constraint_refusal(body, route, true).empty(),
"tool_choice none asks for no call: dropped, and not refused");
}
{
json body = {{"messages", json::array()}, {"tools", tools}, {"tool_choice", "required"}};
check(npu_drop_offered_tools(body) == 0 && body.contains("tools"),
"tool_choice required DEMANDS a call, so the tools are kept");
check(!npu_constraint_refusal(body, route, true).empty(), "and the request is still refused (#3193)");
}
{
json body = {{"messages", json::array()}, {"tools", tools},
{"tool_choice", {{"type", "function"}, {"function", {{"name", "get_weather"}}}}}};
check(npu_drop_offered_tools(body) == 0 && !npu_constraint_refusal(body, route, true).empty(),
"a named function is demanded too: kept and refused");
}
{
json body = {{"messages", json::array()}, {"tools", tools}, {"tool_choice", "whatever"}};
check(npu_drop_offered_tools(body) == 0, "an unknown tool_choice string is not treated as an offer");
}
{
json body = {{"messages", json::array()}, {"tools", json::array()}};
check(npu_drop_offered_tools(body) == 0 && body.contains("tools"), "tools: [] is left alone (nothing to drop)");
}
{
json body = {{"messages", json::array()}, {"tools", tools},
{"response_format", {{"type", "json_schema"},
{"json_schema", {{"name", "w"}, {"schema", {{"type", "object"}}}}}}}};
check(npu_drop_offered_tools(body) == 1 && !npu_constraint_refusal(body, route, true).empty(),
"dropping offered tools does not excuse a caller's json_schema: still refused");
}
{
// the handler drops on a second, ORDERED parse for the template; the same function must work there
nlohmann::ordered_json body = nlohmann::ordered_json::parse(R"({"messages":[],"tools":[{"type":"function",
"function":{"name":"a"}},{"type":"function","function":{"name":"b"}}],"tool_choice":"auto"})");
check(npu_drop_offered_tools(body) == 2 && !body.contains("tools"),
"on the ordered copy the template renders, too (count 2, tools gone)");
}

std::printf("npu_private_route_tools: %s\n", bad ? "FAIL" : "ok");
return bad ? 1 : 0;
}