Skip to content

Commit abd274a

Browse files
author
ochafik
committed
Copy minja from google/minja@58f0ca6
1 parent d79d8f3 commit abd274a

File tree

2 files changed

+3005
-0
lines changed

2 files changed

+3005
-0
lines changed

common/chat-template.hpp

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
/*
2+
Copyright 2024 Google LLC
3+
4+
Use of this source code is governed by an MIT-style
5+
license that can be found in the LICENSE file or at
6+
https://opensource.org/licenses/MIT.
7+
*/
8+
// SPDX-License-Identifier: MIT
9+
#pragma once
10+
11+
#include "minja.hpp"
12+
#include <json.hpp>
13+
#include <string>
14+
#include <vector>
15+
16+
using json = nlohmann::ordered_json;
17+
18+
namespace minja {
19+
20+
class chat_template {
21+
public:
22+
23+
private:
24+
bool supports_tools_ = true;
25+
// Meta-Llama-3.1-8B-Instruct's template expects arguments to be an object.
26+
// Most other templates (and OpenAI's API) expect the arguments object to be stringified.
27+
bool requires_object_arguments_ = false;
28+
bool supports_system_role_ = true;
29+
bool supports_parallel_tool_calls_ = false;
30+
std::string source_;
31+
std::string bos_token_;
32+
std::string eos_token_;
33+
std::shared_ptr<minja::TemplateNode> template_root_;
34+
35+
std::string try_render(
36+
const nlohmann::ordered_json & messages,
37+
const nlohmann::ordered_json & tools,
38+
bool add_generation_prompt,
39+
const nlohmann::ordered_json & extra_context = nlohmann::ordered_json()) const
40+
{
41+
try {
42+
auto prompt = apply(messages, tools, add_generation_prompt, extra_context);
43+
// fprintf(stderr, "Prompt: %s\n", prompt.c_str());
44+
return prompt;
45+
} catch (const std::exception & e) {
46+
// fprintf(stderr, "Error: %s\n", e.what());
47+
return "";
48+
}
49+
}
50+
51+
public:
52+
chat_template(const std::string & source, const std::string & bos_token, const std::string & eos_token)
53+
: source_(source), bos_token_(bos_token), eos_token_(eos_token)
54+
{
55+
template_root_ = minja::Parser::parse(source_, {
56+
/* .trim_blocks = */ true,
57+
/* .lstrip_blocks = */ true,
58+
/* .keep_trailing_newline = */ false,
59+
});
60+
supports_tools_ = source.find("tools") != std::string::npos;
61+
62+
auto renders_string_arguments =
63+
try_render({
64+
{
65+
{"role", "user"},
66+
{"content", "Hey"}
67+
},
68+
{
69+
{"role", "assistant"},
70+
{"tool_calls", json::array({
71+
{
72+
{"id", "call_1___"},
73+
{"type", "function"},
74+
{"function", {
75+
{"arguments", "{\"code\": \"print('Hello, World!')\"}"},
76+
{"name", "ipython"},
77+
}},
78+
},
79+
})},
80+
}
81+
}, {}, false).find("{\"code\": \"print") != std::string::npos;
82+
if (!renders_string_arguments) {
83+
auto renders_object_arguments =
84+
try_render({
85+
{
86+
{"role", "user"},
87+
{"content", "Hey"}
88+
},
89+
{
90+
{"role", "assistant"},
91+
{"tool_calls", json::array({
92+
{
93+
{"id", "call_1___"},
94+
{"type", "function"},
95+
{"function", {
96+
{"arguments", {
97+
{"code", "print('Hello, World!')"},
98+
}},
99+
{"name", "ipython"},
100+
}},
101+
},
102+
})},
103+
}
104+
}, {}, false).find("{\"code\": \"print") != std::string::npos;
105+
requires_object_arguments_ = renders_object_arguments;
106+
}
107+
supports_parallel_tool_calls_ = source.find("tool_call_id") != std::string::npos;
108+
109+
supports_system_role_ = try_render({
110+
{{"role", "system"}, {"content", "<System Needle>"}},
111+
{{"role", "user"}, {"content", "Hey"}}
112+
}, {}, false).find("<System Needle>") != std::string::npos;
113+
}
114+
115+
const std::string & source() const { return source_; }
116+
bool supports_tools() const { return supports_tools_; }
117+
bool supports_parallel_tool_calls() const { return supports_parallel_tool_calls_; }
118+
119+
std::string apply(
120+
const nlohmann::ordered_json & messages,
121+
const nlohmann::ordered_json & tools,
122+
bool add_generation_prompt,
123+
const nlohmann::ordered_json & extra_context = nlohmann::ordered_json()) const
124+
{
125+
json actual_messages;
126+
127+
// First, "fix" messages so they have a chance to be rendered correctly by the template
128+
129+
if (requires_object_arguments_ || !supports_system_role_ || !supports_tools_) {
130+
actual_messages = json::array();
131+
132+
std::string pending_system;
133+
auto flush_sys = [&]() {
134+
if (!pending_system.empty()) {
135+
actual_messages.push_back({
136+
{"role", "user"},
137+
{"content", pending_system},
138+
});
139+
pending_system.clear();
140+
}
141+
};
142+
for (const auto & message_ : messages) {
143+
auto message = message_;
144+
if (!message.contains("role") || !message.contains("content")) {
145+
throw std::runtime_error("message must have 'role' and 'content' fields: " + message.dump());
146+
}
147+
std::string role = message.at("role");
148+
149+
if (message.contains("tool_calls")) {
150+
if (requires_object_arguments_ || !supports_tools_) {
151+
for (auto & tool_call : message.at("tool_calls")) {
152+
if (tool_call["type"] == "function") {
153+
auto & function = tool_call.at("function");
154+
std::string arguments = function.at("arguments");
155+
function["arguments"] = json::parse(arguments);
156+
}
157+
}
158+
}
159+
if (!supports_tools_) {
160+
auto content = message.at("content");
161+
auto tool_calls = json::array();
162+
for (const auto & tool_call : message.at("tool_calls")) {
163+
if (tool_call.at("type") != "function") {
164+
continue;
165+
}
166+
const auto & function = tool_call.at("function");
167+
auto tc = json {
168+
{"name", function.at("name")},
169+
{"arguments", function.at("arguments")},
170+
};
171+
if (tool_call.contains("id")) {
172+
tc["id"] = tool_call["id"];
173+
}
174+
tool_calls.push_back(tc);
175+
}
176+
auto obj = json {
177+
{"tool_calls", tool_calls},
178+
};
179+
if (!content.is_null() && content != "") {
180+
obj["content"] = content;
181+
}
182+
message["content"] = obj.dump(2);
183+
message.erase("tool_calls");
184+
}
185+
}
186+
if (!supports_tools_ && role == "tool") {
187+
message["role"] = "user";
188+
auto obj = json {
189+
{"tool_response", {
190+
{"tool", message.at("name")},
191+
{"content", message.at("content")},
192+
}},
193+
};
194+
if (message.contains("tool_call_id")) {
195+
obj["tool_response"]["tool_call_id"] = message.at("tool_call_id");
196+
}
197+
message["content"] = obj.dump(2);
198+
message.erase("name");
199+
}
200+
201+
if (!message["content"].is_null() && !supports_system_role_) {
202+
std::string content = message.at("content");
203+
if (role == "system") {
204+
if (!pending_system.empty()) pending_system += "\n";
205+
pending_system += content;
206+
continue;
207+
} else {
208+
if (role == "user") {
209+
if (!pending_system.empty()) {
210+
message["content"] = pending_system + (content.empty() ? "" : "\n" + content);
211+
pending_system.clear();
212+
}
213+
} else {
214+
flush_sys();
215+
}
216+
}
217+
}
218+
actual_messages.push_back(message);
219+
}
220+
flush_sys();
221+
} else {
222+
actual_messages = messages;
223+
}
224+
225+
auto context = minja::Context::make(json({
226+
{"messages", actual_messages},
227+
{"add_generation_prompt", add_generation_prompt},
228+
{"bos_token", bos_token_},
229+
{"eos_token", eos_token_},
230+
}));
231+
232+
if (!tools.is_null()) {
233+
auto tools_val = minja::Value(tools);
234+
context->set("tools", tools_val);
235+
}
236+
if (!extra_context.is_null()) {
237+
for (auto & kv : extra_context.items()) {
238+
minja::Value val(kv.value());
239+
context->set(kv.key(), val);
240+
}
241+
}
242+
243+
return template_root_->render(context);
244+
}
245+
};
246+
247+
} // namespace minja

0 commit comments

Comments
 (0)