Skip to content

armgit / PS5LM

Public Imported from GitHub
0

llama.cpp on a jailbroken PS5: run any GGUF model natively on the console, starting with Qwen 3.8

Patch llama-server: fold later system messages for the Anthropic API

Abhiramdc5aedf75d08
diff --git a/patches/0004-anthropic-system-messages.patch b/patches/0004-anthropic-system-messages.patch
new file mode 100644
index 0000000..8ff5f6b
--- /dev/null
+++ b/patches/0004-anthropic-system-messages.patch
@@ -0,0 +1,48 @@
+PS5LM: fold system messages that are not first (Claude Code sends some) into the leading one, so templates like Qwen's accept the Anthropic API.
+
+diff --git a/tools/server/server-chat.cpp b/tools/server/server-chat.cpp
+index a3a4ea152..0dd4b6c33 100644
+--- a/tools/server/server-chat.cpp
++++ b/tools/server/server-chat.cpp
+@@ -543,6 +543,41 @@ json server_chat_convert_anthropic_to_oai(const json & body) {
+ }
+ }
+
++ // PS5LM: some clients (Claude Code) send system messages inside
++ // `messages`; templates such as Qwen's refuse a system message that is not
++ // first. Fold every later one into the leading system message.
++ {
++ json merged = json::array();
++ std::string extra;
++ for (const auto & m : oai_messages) {
++ const std::string r = json_value(m, "role", std::string());
++ if ((r == "system" || r == "developer") && !merged.empty()) {
++ const json & c = m.contains("content") ? m.at("content") : json();
++ if (c.is_string()) {
++ extra += "\n\n" + c.get<std::string>();
++ } else if (c.is_array()) {
++ for (const auto & part : c) {
++ extra += "\n\n" + json_value(part, "text", std::string());
++ }
++ }
++ continue;
++ }
++ merged.push_back(m);
++ }
++ if (!extra.empty()) {
++ if (!merged.empty() && json_value(merged[0], "role", std::string()) == "system" && merged[0].at("content").is_string()) {
++ merged[0]["content"] = merged[0].at("content").get<std::string>() + extra;
++ } else {
++ json front = json::array();
++ front.push_back({ { "role", "system" }, { "content", extra.substr(2) } });
++ for (auto & m : merged) {
++ front.push_back(std::move(m));
++ }
++ merged = std::move(front);
++ }
++ }
++ oai_messages = std::move(merged);
++ }
+ oai_body["messages"] = oai_messages;
+
+ // Convert tools