|
| 1 | +#include "value.h" |
| 2 | +#include "runtime.h" |
| 3 | +#include "caps.h" |
| 4 | + |
| 5 | +// note: the json dependency is only for defining input in a convenient way |
| 6 | +// we can remove it in the future when we figure out a better way to define inputs using jinja::value |
| 7 | +#include <nlohmann/json.hpp> |
| 8 | + |
| 9 | +#include <functional> |
| 10 | +#include <sstream> |
| 11 | + |
| 12 | +#define FILENAME "jinja-caps" |
| 13 | + |
| 14 | +using json = nlohmann::ordered_json; |
| 15 | + |
| 16 | +namespace jinja { |
| 17 | + |
| 18 | +using caps_json_fn = std::function<json()>; |
| 19 | +using caps_analyze_fn = std::function<void(bool, value &, value &)>; |
| 20 | + |
| 21 | +static void caps_try_execute(jinja::program & prog, |
| 22 | + const caps_json_fn & messages_fn, |
| 23 | + const caps_json_fn & tools_fn, |
| 24 | + const caps_analyze_fn & analyze_fn) { |
| 25 | + context ctx; |
| 26 | + ctx.is_get_stats = true; |
| 27 | + jinja::global_from_json(ctx, json{ |
| 28 | + {"messages", messages_fn()}, |
| 29 | + {"tools", tools_fn()}, |
| 30 | + {"bos_token", ""}, |
| 31 | + {"eos_token", ""}, |
| 32 | + {"add_generation_prompt", true} |
| 33 | + }, true); |
| 34 | + |
| 35 | + auto messages = ctx.get_val("messages"); |
| 36 | + auto tools = ctx.get_val("tools"); |
| 37 | + |
| 38 | + bool success = false; |
| 39 | + try { |
| 40 | + jinja::runtime runtime(ctx); |
| 41 | + runtime.execute(prog); |
| 42 | + success = true; |
| 43 | + } catch (const std::exception & e) { |
| 44 | + JJ_DEBUG("Exception during execution: %s", e.what()); |
| 45 | + // ignore exceptions during capability analysis |
| 46 | + } |
| 47 | + |
| 48 | + analyze_fn(success, messages, tools); |
| 49 | +} |
| 50 | + |
| 51 | +// for debugging only |
| 52 | +static void caps_print_stats(value & v, const std::string & path) { |
| 53 | + std::string ops; |
| 54 | + for (const auto & name : v->stats.ops) { |
| 55 | + ops += name + " "; |
| 56 | + } |
| 57 | + JJ_DEBUG("Value %s, type: %s %s, ops: %s", |
| 58 | + path.c_str(), |
| 59 | + v->type().c_str(), |
| 60 | + v->stats.used ? "(used)" : "", |
| 61 | + ops.c_str()); |
| 62 | +} |
| 63 | + |
| 64 | +std::string caps::to_string() const { |
| 65 | + std::ostringstream ss; |
| 66 | + ss << "Caps(\n"; |
| 67 | + ss << " requires_typed_content=" << requires_typed_content << "\n"; |
| 68 | + ss << " supports_tools=" << supports_tools << "\n"; |
| 69 | + ss << " supports_tool_calls=" << supports_tool_calls << "\n"; |
| 70 | + ss << " supports_parallel_tool_calls=" << supports_parallel_tool_calls << "\n"; |
| 71 | + ss << " supports_system_role=" << supports_system_role << "\n"; |
| 72 | + ss << ")"; |
| 73 | + return ss.str(); |
| 74 | +} |
| 75 | + |
| 76 | +caps caps_get(jinja::program & prog) { |
| 77 | + caps result; |
| 78 | + |
| 79 | + static const auto has_op = [](value & v, const std::string & op_name) { |
| 80 | + return v->stats.ops.find(op_name) != v->stats.ops.end(); |
| 81 | + }; |
| 82 | + |
| 83 | + // case: typed content requirement |
| 84 | + caps_try_execute( |
| 85 | + prog, |
| 86 | + [&]() { |
| 87 | + // messages |
| 88 | + return json::array({ |
| 89 | + { |
| 90 | + {"role", "user"}, |
| 91 | + {"content", "content"} |
| 92 | + } |
| 93 | + }); |
| 94 | + }, |
| 95 | + [&]() { |
| 96 | + // tools |
| 97 | + return json{nullptr}; |
| 98 | + }, |
| 99 | + [&](bool, value & messages, value &) { |
| 100 | + auto & content = messages->at(0)->at("content"); |
| 101 | + caps_print_stats(content, "messages[0].content"); |
| 102 | + if (has_op(content, "selectattr") || has_op(content, "array_access")) { |
| 103 | + // accessed as an array |
| 104 | + result.requires_typed_content = true; |
| 105 | + } |
| 106 | + } |
| 107 | + ); |
| 108 | + |
| 109 | + |
| 110 | + // case: system prompt support |
| 111 | + caps_try_execute( |
| 112 | + prog, |
| 113 | + [&]() { |
| 114 | + // messages |
| 115 | + return json::array({ |
| 116 | + { |
| 117 | + {"role", "system"}, |
| 118 | + {"content", "System message"} |
| 119 | + }, |
| 120 | + { |
| 121 | + {"role", "user"}, |
| 122 | + {"content", "User message"} |
| 123 | + }, |
| 124 | + }); |
| 125 | + }, |
| 126 | + [&]() { |
| 127 | + // tools |
| 128 | + return json::array(); |
| 129 | + }, |
| 130 | + [&](bool, value & messages, value &) { |
| 131 | + auto & content = messages->at(0)->at("content"); |
| 132 | + caps_print_stats(content, "messages[0].content"); |
| 133 | + if (!content->stats.used) { |
| 134 | + result.supports_system_role = false; |
| 135 | + } |
| 136 | + } |
| 137 | + ); |
| 138 | + |
| 139 | + // case: tools support |
| 140 | + caps_try_execute( |
| 141 | + prog, |
| 142 | + [&]() { |
| 143 | + // messages |
| 144 | + return json::array({ |
| 145 | + { |
| 146 | + {"role", "user"}, |
| 147 | + {"content", "User message"}, |
| 148 | + }, |
| 149 | + { |
| 150 | + {"role", "assistant"}, |
| 151 | + {"content", "Assistant message"}, |
| 152 | + {"tool_calls", json::array({ |
| 153 | + { |
| 154 | + {"id", "call1"}, |
| 155 | + {"type", "function"}, |
| 156 | + {"function", { |
| 157 | + {"name", "tool1"}, |
| 158 | + {"arguments", { |
| 159 | + {"arg", "value"} |
| 160 | + }} |
| 161 | + }} |
| 162 | + }, |
| 163 | + { |
| 164 | + {"id", "call2"}, |
| 165 | + {"type", "function"}, |
| 166 | + {"function", { |
| 167 | + {"name", "tool2"}, |
| 168 | + {"arguments", { |
| 169 | + {"arg", "value"} |
| 170 | + }} |
| 171 | + }} |
| 172 | + } |
| 173 | + })} |
| 174 | + }, |
| 175 | + { |
| 176 | + {"role", "user"}, |
| 177 | + {"content", "User message"}, |
| 178 | + }, |
| 179 | + }); |
| 180 | + }, |
| 181 | + [&]() { |
| 182 | + // tools |
| 183 | + return json::array({ |
| 184 | + { |
| 185 | + {"name", "tool"}, |
| 186 | + {"type", "function"}, |
| 187 | + {"function", { |
| 188 | + {"name", "tool"}, |
| 189 | + {"description", "Tool description"}, |
| 190 | + {"parameters", { |
| 191 | + {"type", "object"}, |
| 192 | + {"properties", { |
| 193 | + {"arg", { |
| 194 | + {"type", "string"}, |
| 195 | + {"description", "Arg description"}, |
| 196 | + }}, |
| 197 | + }}, |
| 198 | + {"required", json::array({ "arg" })}, |
| 199 | + }}, |
| 200 | + }}, |
| 201 | + }, |
| 202 | + }); |
| 203 | + }, |
| 204 | + [&](bool success, value & messages, value & tools) { |
| 205 | + if (!success) { |
| 206 | + result.supports_tool_calls = false; |
| 207 | + result.supports_tools = false; |
| 208 | + return; |
| 209 | + } |
| 210 | + |
| 211 | + auto & tool_name = tools->at(0)->at("function")->at("name"); |
| 212 | + caps_print_stats(tool_name, "tools[0].function.name"); |
| 213 | + if (!tool_name->stats.used) { |
| 214 | + result.supports_tools = false; |
| 215 | + } |
| 216 | + |
| 217 | + auto & tool_calls = messages->at(1)->at("tool_calls");; |
| 218 | + caps_print_stats(tool_calls, "messages[1].tool_calls"); |
| 219 | + if (!tool_calls->stats.used) { |
| 220 | + result.supports_tool_calls = false; |
| 221 | + } |
| 222 | + |
| 223 | + // check for second tool call usage |
| 224 | + auto & tool_call_1 = tool_calls->at(1)->at("function"); |
| 225 | + caps_print_stats(tool_call_1, "messages[1].tool_calls[1].function"); |
| 226 | + if (!tool_call_1->stats.used) { |
| 227 | + result.supports_parallel_tool_calls = false; |
| 228 | + } |
| 229 | + } |
| 230 | + ); |
| 231 | + |
| 232 | + JJ_DEBUG("%s\n", result.to_string().c_str()); |
| 233 | + |
| 234 | + return result; |
| 235 | +} |
| 236 | + |
| 237 | +} // namespace jinja |
0 commit comments