Skip to content
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
12 changes: 6 additions & 6 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
[submodule "gpt4all-chat/deps/QXlsx"]
path = gpt4all-chat/deps/QXlsx
url = https://github.com/nomic-ai/QXlsx.git
[submodule "gpt4all-chat/deps/Jinja2Cpp"]
path = gpt4all-chat/deps/Jinja2Cpp
url = https://github.com/nomic-ai/jinja2cpp.git
[submodule "gpt4all-chat/deps/rapidjson"]
path = gpt4all-chat/deps/rapidjson
url = https://github.com/nomic-ai/rapidjson.git
[submodule "gpt4all-chat/deps/minja"]
path = gpt4all-chat/deps/minja
url = https://github.com/nomic-ai/minja.git
[submodule "gpt4all-chat/deps/json"]
path = gpt4all-chat/deps/json
url = https://github.com/nlohmann/json.git
3 changes: 3 additions & 0 deletions gpt4all-chat/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
### Added
- Support DeepSeek-R1 Qwen models ([#3431](https://github.com/nomic-ai/gpt4all/pull/3431))

### Changed
- Use minja instead of Jinja2Cpp for significantly improved template compatibility ([#3433](https://github.com/nomic-ai/gpt4all/pull/3433))

### Fixed
- Fix regression while using localdocs with server API ([#3410](https://github.com/nomic-ai/gpt4all/pull/3410))
- Don't show system messages in server chat view ([#3411](https://github.com/nomic-ai/gpt4all/pull/3411))
Expand Down
7 changes: 5 additions & 2 deletions gpt4all-chat/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if (MSVC)
# Enable accurate __cplusplus macro to fix errors in Jinja2Cpp
# Enable accurate __cplusplus macro
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/Zc:__cplusplus>)
endif()

Expand Down Expand Up @@ -437,7 +437,10 @@ else()
target_link_libraries(chat PRIVATE pdfium)
endif()
target_link_libraries(chat
PRIVATE llmodel SingleApplication fmt::fmt duckx::duckx QXlsx jinja2cpp)
PRIVATE llmodel SingleApplication fmt::fmt duckx::duckx QXlsx)
target_include_directories(chat PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/deps/json/include)
target_include_directories(chat PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/deps/json/include/nlohmann)
target_include_directories(chat PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/deps/minja/include)

if (APPLE)
target_link_libraries(chat PRIVATE ${COCOA_LIBRARY})
Expand Down
9 changes: 0 additions & 9 deletions gpt4all-chat/deps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ add_subdirectory(DuckX)
set(QT_VERSION_MAJOR 6)
add_subdirectory(QXlsx/QXlsx)

# forked dependency of Jinja2Cpp
set(RAPIDJSON_BUILD_DOC OFF)
set(RAPIDJSON_BUILD_EXAMPLES OFF)
set(RAPIDJSON_BUILD_TESTS OFF)
set(RAPIDJSON_ENABLE_INSTRUMENTATION_OPT OFF)
add_subdirectory(rapidjson)

add_subdirectory(Jinja2Cpp)

if (NOT GPT4ALL_USING_QTPDF)
# If we do not use QtPDF, we need to get PDFium.
set(GPT4ALL_PDFIUM_TAG "chromium/6954")
Expand Down
1 change: 0 additions & 1 deletion gpt4all-chat/deps/Jinja2Cpp
Submodule Jinja2Cpp deleted from ce10f7
1 change: 1 addition & 0 deletions gpt4all-chat/deps/json
Submodule json added at 606b63
1 change: 1 addition & 0 deletions gpt4all-chat/deps/minja
Submodule minja added at 491f5c
1 change: 0 additions & 1 deletion gpt4all-chat/deps/rapidjson
Submodule rapidjson deleted from 9b547e
98 changes: 38 additions & 60 deletions gpt4all-chat/src/chatllm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
#include "toolcallparser.h"

#include <fmt/format.h>

#include <jinja2cpp/error_info.h>
#include <jinja2cpp/template.h>
#include <jinja2cpp/template_env.h>
#include <jinja2cpp/user_callable.h>
#include <jinja2cpp/value.h>
#include <minja/minja.hpp>
#include <nlohmann/json.hpp>

#include <QDataStream>
#include <QDebug>
Expand Down Expand Up @@ -60,59 +56,40 @@
using namespace Qt::Literals::StringLiterals;
using namespace ToolEnums;
namespace ranges = std::ranges;
using json = nlohmann::ordered_json;

//#define DEBUG
//#define DEBUG_MODEL_LOADING

static std::string jinjaGetStringArg(const jinja2::ValuesMap &args, const std::string &name)
{
auto arg = args.find(name);
if (arg == args.end() || !arg->second.isString())
throw std::runtime_error(fmt::format("'{}' argument to raise_exception() must be a string", name));
return arg->second.asString();
}

// NOTE: not threadsafe
static jinja2::TemplateEnv *jinjaEnv()
static const std::shared_ptr<minja::Context> &jinjaEnv()
{
static std::optional<jinja2::TemplateEnv> environment;
static std::shared_ptr<minja::Context> environment;
if (!environment) {
auto &env = environment.emplace();
auto &settings = env.GetSettings();
settings.trimBlocks = true;
settings.lstripBlocks = true;
env.AddGlobal("raise_exception", jinja2::UserCallable(
/*callable*/ [](auto &params) -> jinja2::Value {
auto message = jinjaGetStringArg(params.args, "message");
throw std::runtime_error(fmt::format("Jinja template error: {}", message));
},
/*argsInfo*/ { jinja2::ArgInfo("message", /*isMandatory*/ true) }
));
env.AddGlobal("strftime_now", jinja2::UserCallable(
/*callable*/ [](auto &params) -> jinja2::Value {
environment = minja::Context::builtins();
environment->set("strftime_now", minja::simple_function(
"strftime_now", { "format" },
[](const std::shared_ptr<minja::Context> &, minja::Value &args) -> minja::Value {
auto format = args.at("format").get<std::string>();
using Clock = std::chrono::system_clock;
auto format = jinjaGetStringArg(params.args, "format");
time_t nowUnix = Clock::to_time_t(Clock::now());
auto localDate = *std::localtime(&nowUnix);
std::ostringstream ss;
ss << std::put_time(&localDate, format.c_str());
return ss.str();
},
/*argsInfo*/ { jinja2::ArgInfo("format", /*isMandatory*/ true) }
}
));
env.AddGlobal("regex_replace", jinja2::UserCallable(
/*callable*/ [](auto &params) -> jinja2::Value {
auto str = jinjaGetStringArg(params.args, "str" );
auto pattern = jinjaGetStringArg(params.args, "pattern");
auto repl = jinjaGetStringArg(params.args, "repl" );
environment->set("regex_replace", minja::simple_function(
"regex_replace", { "str", "pattern", "repl" },
[](const std::shared_ptr<minja::Context> &, minja::Value &args) -> minja::Value {
auto str = args.at("str" ).get<std::string>();
auto pattern = args.at("pattern").get<std::string>();
auto repl = args.at("repl" ).get<std::string>();
return std::regex_replace(str, std::regex(pattern), repl);
},
/*argsInfo*/ { jinja2::ArgInfo("str", /*isMandatory*/ true),
jinja2::ArgInfo("pattern", /*isMandatory*/ true),
jinja2::ArgInfo("repl", /*isMandatory*/ true) }
}
));
}
return &*environment;
return environment;
}

class LLModelStore {
Expand Down Expand Up @@ -772,19 +749,18 @@ static uint parseJinjaTemplateVersion(QStringView tmpl)
return 0;
}

static auto loadJinjaTemplate(
std::optional<jinja2::Template> &tmpl /*out*/, const std::string &source
) -> jinja2::Result<void>
static std::shared_ptr<minja::TemplateNode> loadJinjaTemplate(const std::string &source)
{
tmpl.emplace(jinjaEnv());
return tmpl->Load(source);
return minja::Parser::parse(source, { .trim_blocks = true, .lstrip_blocks = true, .keep_trailing_newline = false });
}

std::optional<std::string> ChatLLM::checkJinjaTemplateError(const std::string &source)
{
std::optional<jinja2::Template> tmpl;
if (auto res = loadJinjaTemplate(tmpl, source); !res)
return res.error().ToString();
try {
loadJinjaTemplate(source);
} catch (const std::runtime_error &e) {
return e.what();
}
return std::nullopt;
}

Expand Down Expand Up @@ -816,13 +792,13 @@ std::string ChatLLM::applyJinjaTemplate(std::span<const MessageItem> items) cons
uint version = parseJinjaTemplateVersion(chatTemplate);

auto makeMap = [version](const MessageItem &item) {
return jinja2::GenericMap([msg = std::make_shared<JinjaMessage>(version, item)] { return msg.get(); });
return JinjaMessage(version, item).AsJson();
};

std::unique_ptr<MessageItem> systemItem;
bool useSystem = !isAllSpace(systemMessage);

jinja2::ValuesList messages;
json::array_t messages;
messages.reserve(useSystem + items.size());
if (useSystem) {
systemItem = std::make_unique<MessageItem>(MessageItem::Type::System, systemMessage.toUtf8());
Expand All @@ -831,27 +807,29 @@ std::string ChatLLM::applyJinjaTemplate(std::span<const MessageItem> items) cons
for (auto &item : items)
messages.emplace_back(makeMap(item));

jinja2::ValuesList toolList;
json::array_t toolList;
const int toolCount = ToolModel::globalInstance()->count();
for (int i = 0; i < toolCount; ++i) {
Tool *t = ToolModel::globalInstance()->get(i);
toolList.push_back(t->jinjaValue());
}

jinja2::ValuesMap params {
json::object_t params {
{ "messages", std::move(messages) },
{ "add_generation_prompt", true },
{ "toolList", toolList },
};
for (auto &[name, token] : model->specialTokens())
params.emplace(std::move(name), std::move(token));

std::optional<jinja2::Template> tmpl;
auto maybeRendered = loadJinjaTemplate(tmpl, chatTemplate.toStdString())
.and_then([&] { return tmpl->RenderAsString(params); });
if (!maybeRendered)
throw std::runtime_error(fmt::format("Failed to parse chat template: {}", maybeRendered.error().ToString()));
return *maybeRendered;
try {
auto tmpl = loadJinjaTemplate(chatTemplate.toStdString());
auto context = minja::Context::make(minja::Value(std::move(params)), jinjaEnv());
return tmpl->render(context);
} catch (const std::runtime_error &e) {
throw std::runtime_error(fmt::format("Failed to parse chat template: {}", e.what()));
}
Q_UNREACHABLE();
}

auto ChatLLM::promptInternalChat(const QStringList &enabledCollections, const LLModel::PromptContext &ctx,
Expand Down
Loading