forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjinja_utils.h
More file actions
27 lines (23 loc) · 927 Bytes
/
jinja_utils.h
File metadata and controls
27 lines (23 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#pragma once
#include <json/value.h>
#include <string>
#include "extensions/template_renderer.h"
#include "utils/chat-template.hpp"
#include "utils/result.hpp"
namespace jinja {
inline cpp::result<std::string, std::string> RenderTemplate(
std::string& tmpl, const Json::Value& data, const std::string& bos_token,
const std::string& eos_token, bool add_bos_token, bool add_eos_token,
bool add_generation_prompt = true) {
try {
auto converted_json =
extensions::TemplateRenderer().ConvertJsonValue(data);
minja::chat_template chat_tmpl(tmpl, add_bos_token ? bos_token : "",
add_eos_token ? eos_token : "");
return chat_tmpl.apply(converted_json["messages"], {},
add_generation_prompt);
} catch (const std::exception& e) {
return cpp::fail("Failed to render template: " + std::string(e.what()));
}
}
} // namespace jinja