Skip to content

Commit

Permalink
Update minja to google/minja@b8437df
Browse files Browse the repository at this point in the history
  • Loading branch information
ochafik committed Jan 20, 2025
1 parent 8a7c89e commit 8347da9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
2 changes: 2 additions & 0 deletions common/chat-template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class chat_template {
}

const std::string & source() const { return source_; }
const std::string & bos_token() const { return bos_token_; }
const std::string & eos_token() const { return eos_token_; }
bool supports_tools() const { return supports_tools_; }
bool supports_parallel_tool_calls() const { return supports_parallel_tool_calls_; }

Expand Down
25 changes: 16 additions & 9 deletions common/minja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,11 @@ class Value : public std::enable_shared_from_this<Value> {
throw std::runtime_error("contains can only be called on arrays and objects: " + dump());
}
}
void erase(size_t index) {
if (array_) throw std::runtime_error("Value is not an array: " + dump());
Value pop(size_t index) {
if (!array_) throw std::runtime_error("Value is not an array: " + dump());
auto value = array_->at(index);
array_->erase(array_->begin() + index);
}
void erase(const std::string & key) {
if (object_) throw std::runtime_error("Value is not an object: " + dump());
object_->erase(key);
return value;
}
const Value& at(const Value & index) const {
return const_cast<Value*>(this)->at(index);
Expand Down Expand Up @@ -1353,6 +1351,15 @@ class MethodCallExpr : public Expression {
if (index < 0 || index > (int64_t) obj.size()) throw std::runtime_error("Index out of range for insert method");
obj.insert(index, vargs.args[1]);
return Value();
} else if (method->get_name() == "pop") {
vargs.expectArgs("pop method", {0, 1}, {0, 0});
if (vargs.args.empty()) {
return obj.pop(obj.size() - 1);
} else {
auto index = vargs.args[0].get<int64_t>();
if (index < 0 || index >= (int64_t) obj.size()) throw std::runtime_error("Index out of range for pop method");
return obj.pop(index);
}
}
} else if (obj.is_object()) {
if (method->get_name() == "items") {
Expand Down Expand Up @@ -2539,7 +2546,7 @@ inline std::shared_ptr<Context> Context::builtins() {
}));
globals.set("namespace", Value::callable([=](const std::shared_ptr<Context> &, ArgumentsValue & args) {
auto ns = Value::object();
args.expectArgs("namespace", {0, 0}, {0, (std::numeric_limits<size_t>::max)()});
args.expectArgs("namespace", {0, 0}, {0, std::numeric_limits<size_t>::max()});
for (auto & [name, value] : args.kwargs) {
ns.set(name, value);
}
Expand Down Expand Up @@ -2594,7 +2601,7 @@ inline std::shared_ptr<Context> Context::builtins() {
};
// https://jinja.palletsprojects.com/en/3.0.x/templates/#jinja-filters.reject
globals.set("reject", Value::callable([=](const std::shared_ptr<Context> & context, ArgumentsValue & args) {
args.expectArgs("reject", {2, (std::numeric_limits<size_t>::max)()}, {0, 0});
args.expectArgs("reject", {2, std::numeric_limits<size_t>::max()}, {0, 0});
auto & items = args.args[0];
auto filter_fn = context->get(args.args[1]);
if (filter_fn.is_null()) throw std::runtime_error("Undefined filter: " + args.args[1].dump());
Expand Down Expand Up @@ -2665,7 +2672,7 @@ inline std::shared_ptr<Context> Context::builtins() {
return out;
}));
globals.set("selectattr", Value::callable([=](const std::shared_ptr<Context> & context, ArgumentsValue & args) {
args.expectArgs("selectattr", {2, (std::numeric_limits<size_t>::max)()}, {0, 0});
args.expectArgs("selectattr", {2, std::numeric_limits<size_t>::max()}, {0, 0});
auto & items = args.args[0];
if (items.is_null())
return Value::array();
Expand Down

0 comments on commit 8347da9

Please sign in to comment.