Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support mistralai/Mistral-Nemo-Instruct-2407 #2

Merged
merged 3 commits into from
Dec 4, 2024
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
43 changes: 31 additions & 12 deletions include/minja/minja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ class Value : public std::enable_shared_from_this<Value> {
}
Value get(const Value& key) {
if (array_) {
if (!key.is_number_integer()) {
return Value();
}
auto index = key.get<int>();
return array_->at(index < 0 ? array_->size() + index : index);
} else if (object_) {
Expand All @@ -236,7 +239,7 @@ class Value : public std::enable_shared_from_this<Value> {
if (it == object_->end()) return Value();
return it->second;
}
throw std::runtime_error("Value is not an array or object: " + dump());
return Value();
}
void set(const Value& key, const Value& value) {
if (!object_) throw std::runtime_error("Value is not an object: " + dump());
Expand Down Expand Up @@ -618,7 +621,7 @@ class Expression {
Value evaluate(const std::shared_ptr<Context> & context) const {
try {
return do_evaluate(context);
} catch (const std::runtime_error & e) {
} catch (const std::exception & e) {
std::ostringstream out;
out << e.what();
if (location.source) out << error_location_suffix(*location.source, location.pos);
Expand Down Expand Up @@ -769,7 +772,7 @@ class TemplateNode {
void render(std::ostringstream & out, const std::shared_ptr<Context> & context) const {
try {
do_render(out, context);
} catch (const std::runtime_error & e) {
} catch (const std::exception & e) {
std::ostringstream err;
err << e.what();
if (location_.source) err << error_location_suffix(*location_.source, location_.pos);
Expand Down Expand Up @@ -1092,15 +1095,24 @@ class SubscriptExpr : public Expression {
if (!index) throw std::runtime_error("SubscriptExpr.index is null");
auto target_value = base->evaluate(context);
if (auto slice = dynamic_cast<SliceExpr*>(index.get())) {
if (!target_value.is_array()) throw std::runtime_error("Subscripting non-array");

auto start = slice->start ? slice->start->evaluate(context).get<size_t>() : 0;
auto end = slice->end ? slice->end->evaluate(context).get<size_t>() : target_value.size();
auto result = Value::array();
for (auto i = start; i < end; ++i) {
result.push_back(target_value.at(i));
auto start = slice->start ? slice->start->evaluate(context).get<int64_t>() : 0;
auto end = slice->end ? slice->end->evaluate(context).get<int64_t>() : (int64_t) target_value.size();
if (target_value.is_string()) {
std::string s = target_value.get<std::string>();
if (start < 0) start = s.size() + start;
if (end < 0) end = s.size() + end;
return s.substr(start, end - start);
} else if (target_value.is_array()) {
if (start < 0) start = target_value.size() + start;
if (end < 0) end = target_value.size() + end;
auto result = Value::array();
for (auto i = start; i < end; ++i) {
result.push_back(target_value.at(i));
}
return result;
} else {
throw std::runtime_error(target_value.is_null() ? "Cannot subscript null" : "Subscripting only supported on arrays and strings");
}
return result;
} else {
auto index_value = index->evaluate(context);
if (target_value.is_null()) {
Expand Down Expand Up @@ -1247,6 +1259,9 @@ class MethodCallExpr : public Expression {
if (!object) throw std::runtime_error("MethodCallExpr.object is null");
if (!method) throw std::runtime_error("MethodCallExpr.method is null");
auto obj = object->evaluate(context);
if (obj.is_null()) {
throw std::runtime_error("Trying to call method '" + method->get_name() + "' on null");
}
if (obj.is_array()) {
if (method->get_name() == "append") {
args.expectArgs("append method", {1, 1}, {0, 0});
Expand Down Expand Up @@ -2140,7 +2155,7 @@ class Parser {
}
}
return tokens;
} catch (const std::runtime_error & e) {
} catch (const std::exception & e) {
throw std::runtime_error(e.what() + error_location_suffix(*template_str, std::distance(start, it)));
}
}
Expand Down Expand Up @@ -2403,6 +2418,10 @@ inline std::shared_ptr<Context> Context::builtins() {
globals.set("safe", simple_function("safe", { "value" }, [](const std::shared_ptr<Context> &, Value & args) -> Value {
return args.at("value");
}));
globals.set("string", simple_function("string", { "value" }, [](const std::shared_ptr<Context> &, Value & args) -> Value {
auto & items = args.at("value");
return items.to_str();
}));
globals.set("list", simple_function("list", { "items" }, [](const std::shared_ptr<Context> &, Value & args) -> Value {
auto & items = args.at("items");
if (!items.is_array()) throw std::runtime_error("object is not iterable");
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ set(MODEL_IDS
# "google/gemma-2-2b-it"
# "mistralai/Mistral-7B-Instruct-v0.2"
# "mistralai/Mixtral-8x7B-Instruct-v0.1"
# "mistralai/Mistral-Nemo-Instruct-2407",
# "CohereForAI/c4ai-command-r-plus"
)

Expand Down
11 changes: 7 additions & 4 deletions tests/contexts/tool_use.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"content": "",
"tool_calls": [
{
"id": "call_1",
"id": "call_1___",
"type": "function",
"function": {
"arguments": "{\"code\": \"print('Hello, World!')\"}",
Expand All @@ -20,6 +20,7 @@
},
{
"role": "tool",
"tool_call_id": "call_1___",
"name": "ipython",
"content": "{\"stdout\": \"Hello, World!\"}"
},
Expand All @@ -36,7 +37,7 @@
"content": "",
"tool_calls": [
{
"id": "call_2",
"id": "call_2___",
"type": "function",
"function": {
"arguments": "{\"condition\":true}",
Expand All @@ -47,6 +48,7 @@
},
{
"role": "tool",
"tool_call_id": "call_2___",
"name": "test",
"content": "true"
},
Expand All @@ -63,7 +65,7 @@
"content": "",
"tool_calls": [
{
"id": "call_3",
"id": "call_3___",
"type": "function",
"function": {
"arguments": "{\"query\": \"what is truth anyway am I right?\"}",
Expand All @@ -74,6 +76,7 @@
},
{
"role": "tool",
"tool_call_id": "call_3___",
"name": "brave_search",
"content": "{\"title\":\"Truth: don't ask the web, ask an LLM instead!\",\"url\":\"https://en.wikipedia.org/wiki/Truth\"}"
},
Expand Down Expand Up @@ -161,4 +164,4 @@
}
}
]
}
}
9 changes: 9 additions & 0 deletions tests/test-syntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ TEST(SyntaxTest, SimpleCases) {
EXPECT_EQ(
"a b",
render(R"( {{- 'a' -}}{{ ' ' }}{{- 'b' -}} )", {}, {}));
EXPECT_EQ(
"bc",
render(R"({{ "abcd"[1:-1] }})", {}, {}));
EXPECT_EQ(
"[1, 2]",
render(R"({{ [0, 1, 2, 3][1:-1] }})", {}, {}));
EXPECT_EQ(
"9",
render(R"({{ "123456789" | length }})", {}, {}));
EXPECT_EQ(
" end",
render(R"( {%- if True %}{%- endif %}{{ ' ' }}{%- for x in [] %}foo{% endfor %}end)", {}, {}));
Expand Down
Loading