#pragma once #include "llama.h" #include "common.h" // Change JSON_ASSERT from assert() to GGML_ASSERT: #define JSON_ASSERT GGML_ASSERT #include #include "chat.h" #include #include #include #include #define DEFAULT_OAICOMPAT_MODEL "gpt-3.5-turbo-0613" using json = nlohmann::ordered_json; // https://community.openai.com/t/openai-chat-list-of-error-codes-and-types/357791/11 enum error_type { ERROR_TYPE_INVALID_REQUEST, ERROR_TYPE_AUTHENTICATION, ERROR_TYPE_SERVER, ERROR_TYPE_NOT_FOUND, ERROR_TYPE_PERMISSION, ERROR_TYPE_UNAVAILABLE, // custom error ERROR_TYPE_NOT_SUPPORTED, // custom error }; extern bool server_verbose; extern bool server_log_json; #ifndef SERVER_VERBOSE #define SERVER_VERBOSE 1 #endif #if SERVER_VERBOSE != 1 #define LOG_VERBOSE(MSG, ...) #else #define LOG_VERBOSE(MSG, ...) \ do \ { \ if (server_verbose) \ { \ server_log("VERB", __func__, __LINE__, MSG, __VA_ARGS__); \ } \ } while (0) #endif #define LOG_ERROR( MSG, ...) server_log("ERR", __func__, __LINE__, MSG, __VA_ARGS__) #define LOG_WARNING(MSG, ...) server_log("WARN", __func__, __LINE__, MSG, __VA_ARGS__) #define LOG_INFO( MSG, ...) server_log("INFO", __func__, __LINE__, MSG, __VA_ARGS__) static inline void server_log(const char * level, const char * function, int line, const char * message, const json & extra); template static T json_value(const json & body, const std::string & key, const T & default_value) { // Fallback null to default value if (body.contains(key) && !body.at(key).is_null()) { try { return body.at(key); } catch (NLOHMANN_JSON_NAMESPACE::detail::type_error const& err) { std::stringstream ss; ss << "Wrong type supplied for parameter '" << key << "'. Expected '" << json(default_value).type_name() << "', using default value: "<< err.what(); LOG_WARNING(ss.str().c_str(), body); return default_value; } } else { return default_value; } } // thin wrapper around common_grammar_trigger with (de)serialization functions struct server_grammar_trigger { common_grammar_trigger value; server_grammar_trigger() = default; server_grammar_trigger(const common_grammar_trigger& value) : value(value) {} server_grammar_trigger(const json& in) { value.type = (common_grammar_trigger_type)in.at("type").get(); value.value = in.at("value").get(); if (value.type == COMMON_GRAMMAR_TRIGGER_TYPE_TOKEN) { value.token = (llama_token)in.at("token").get(); } } json to_json() const { json out{ {"type", (int)value.type}, {"value", value.value}, }; if (value.type == COMMON_GRAMMAR_TRIGGER_TYPE_TOKEN) { out["token"] = (int)value.token; } return out; } }; static inline void server_log(const char * level, const char * function, int line, const char * message, const json & extra) { std::stringstream ss_tid; ss_tid << std::this_thread::get_id(); json log = json{ {"tid", ss_tid.str()}, {"timestamp", time(nullptr)}, }; if (server_log_json) { log.merge_patch({ {"level", level}, {"function", function}, {"line", line}, {"msg", message}, }); if (!extra.empty()) { log.merge_patch(extra); } printf("%s\n", log.dump(-1, ' ', false, json::error_handler_t::replace).c_str()); } else { char buf[1024]; snprintf(buf, 1024, "%4s [%24s] %s", level, function, message); if (!extra.empty()) { log.merge_patch(extra); } std::stringstream ss; ss << buf << " |"; for (const auto & el : log.items()) { const std::string value = el.value().dump(-1, ' ', false, json::error_handler_t::replace); ss << " " << el.key() << "=" << value; } const std::string str = ss.str(); printf("%.*s\n", (int)str.size(), str.data()); } fflush(stdout); } // // chat template utils // // // base64 utils (TODO: move to common in the future) // static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; static inline bool is_base64(uint8_t c) { return (isalnum(c) || (c == '+') || (c == '/')); } static inline std::vector base64_decode(const std::string & encoded_string) { int i = 0; int j = 0; int in_ = 0; int in_len = encoded_string.size(); uint8_t char_array_4[4]; uint8_t char_array_3[3]; std::vector ret; while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { char_array_4[i++] = encoded_string[in_]; in_++; if (i == 4) { for (i = 0; i < 4; i++) { char_array_4[i] = base64_chars.find(char_array_4[i]); } char_array_3[0] = ((char_array_4[0] ) << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (i = 0; (i < 3); i++) { ret.push_back(char_array_3[i]); } i = 0; } } if (i) { for (j = i; j < 4; j++) { char_array_4[j] = 0; } for (j = 0; j < 4; j++) { char_array_4[j] = base64_chars.find(char_array_4[j]); } char_array_3[0] = ((char_array_4[0] ) << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (j = 0; j < i - 1; j++) { ret.push_back(char_array_3[j]); } } return ret; } // // random string / id // static std::string random_string() { static const std::string str("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"); std::random_device rd; std::mt19937 generator(rd()); std::string result(32, ' '); for (int i = 0; i < 32; ++i) { result[i] = str[generator() % str.size()]; } return result; } static std::string gen_chatcmplid() { std::stringstream chatcmplid; chatcmplid << "chatcmpl-" << random_string(); return chatcmplid.str(); } static std::string gen_tool_call_id() { return random_string(); } // // other common utils // static size_t common_part(const std::vector & a, const std::vector & b) { size_t i; for (i = 0; i < a.size() && i < b.size() && a[i] == b[i]; i++) {} return i; } static size_t common_part(const std::string & a, const std::string & b) { size_t i; for (i = 0; i < a.size() && i < b.size() && a[i] == b[i]; i++) {} return i; } // return the last index of character that can form a valid string // if the last character is potentially cut in half, return the index before the cut // if validate_utf8(text) == text.size(), then the whole text is valid utf8 static size_t validate_utf8(const std::string& text) { size_t len = text.size(); if (len == 0) return 0; // Check the last few bytes to see if a multi-byte character is cut off for (size_t i = 1; i <= 4 && i <= len; ++i) { unsigned char c = text[len - i]; // Check for start of a multi-byte sequence from the end if ((c & 0xE0) == 0xC0) { // 2-byte character start: 110xxxxx // Needs at least 2 bytes if (i < 2) return len - i; } else if ((c & 0xF0) == 0xE0) { // 3-byte character start: 1110xxxx // Needs at least 3 bytes if (i < 3) return len - i; } else if ((c & 0xF8) == 0xF0) { // 4-byte character start: 11110xxx // Needs at least 4 bytes if (i < 4) return len - i; } } // If no cut-off multi-byte character is found, return full length return len; } // TODO: reuse llama_detokenize template static std::string tokens_to_str(llama_context * ctx, Iter begin, Iter end) { std::string ret; for (; begin != end; ++begin) { ret += llama_token_to_piece(ctx, *begin); } return ret; } // format incomplete utf-8 multibyte character for output static std::string tokens_to_output_formatted_string(const llama_context * ctx, const llama_token token) { std::string out = token == -1 ? "" : llama_token_to_piece(ctx, token); // if the size is 1 and first bit is 1, meaning it's a partial character // (size > 1 meaning it's already a known token) if (out.size() == 1 && (out[0] & 0x80) == 0x80) { std::stringstream ss; ss << std::hex << (out[0] & 0xff); std::string res(ss.str()); out = "byte: \\x" + res; } return out; } struct completion_token_output { llama_token tok; std::string text_to_send; float prob; struct prob_info { llama_token tok; std::string txt; float prob; }; std::vector probs; json to_json(bool post_sampling_probs) const { json probs_for_token = json::array(); for (const auto& p : probs) { std::string txt(p.txt); txt.resize(validate_utf8(txt)); probs_for_token.push_back(json{ {"id", p.tok}, {"token", txt}, {"bytes", str_to_bytes(p.txt)}, { post_sampling_probs ? "prob" : "logprob", post_sampling_probs ? p.prob : logarithm(p.prob) }, }); } return probs_for_token; } static float logarithm(float x) { // nlohmann::json converts -inf to null, so we need to prevent that return x == 0.0f ? std::numeric_limits::lowest() : std::log(x); } static std::vector str_to_bytes(const std::string& str) { std::vector bytes; for (unsigned char c : str) { bytes.push_back(c); } return bytes; } static json probs_vector_to_json(const std::vector& probs, bool post_sampling_probs) { json out = json::array(); for (const auto& p : probs) { std::string txt(p.text_to_send); txt.resize(validate_utf8(txt)); out.push_back(json{ {"id", p.tok}, {"token", txt}, {"bytes", str_to_bytes(p.text_to_send)}, { post_sampling_probs ? "prob" : "logprob", post_sampling_probs ? p.prob : logarithm(p.prob) }, { post_sampling_probs ? "top_probs" : "top_logprobs", p.to_json(post_sampling_probs) }, }); } return out; } }; // convert a vector of completion_token_output to json static json probs_vector_to_json(const llama_context * ctx, const std::vector & probs) { json out = json::array(); for (const auto & prob : probs) { json probs_for_token = json::array(); for (const auto & p : prob.probs) { const std::string tok_str = tokens_to_output_formatted_string(ctx, p.tok); probs_for_token.push_back(json { {"tok_str", tok_str}, {"prob", p.prob}, }); } const std::string tok_str = tokens_to_output_formatted_string(ctx, prob.tok); out.push_back(json { {"content", tok_str}, {"probs", probs_for_token}, }); } return out; } // // OAI utils // // used by /completions endpoint static json oaicompat_chat_params_parse(const json& body) { json llama_params; if (!body.contains("prompt")) { throw std::runtime_error("\"prompt\" is required"); } // Handle "stop" field if (body.contains("stop") && body.at("stop").is_string()) { llama_params["stop"] = json::array({ body.at("stop").get() }); } else { llama_params["stop"] = json_value(body, "stop", json::array()); } // Handle "n" field int n_choices = json_value(body, "n", 1); if (n_choices != 1) { throw std::runtime_error("Only one completion choice is allowed"); } // Handle "echo" field if (json_value(body, "echo", false)) { throw std::runtime_error("Only no echo is supported"); } // Params supported by OAI but unsupported by llama.cpp static const std::vector unsupported_params{ "best_of", "suffix" }; for (const auto& param : unsupported_params) { if (body.contains(param)) { throw std::runtime_error("Unsupported param: " + param); } } // Copy remaining properties to llama_params for (const auto& item : body.items()) { // Exception: if "n_predict" is present, we overwrite the value specified earlier by "max_tokens" if (!llama_params.contains(item.key()) || item.key() == "n_predict") { llama_params[item.key()] = item.value(); } } return llama_params; } struct oaicompat_parser_options { bool use_jinja; bool prefill_assistant; common_reasoning_format reasoning_format; std::map chat_template_kwargs; common_chat_templates* tmpls; bool allow_image; bool allow_audio; bool enable_thinking = true; }; // used by /chat/completions endpoint static json oaicompat_chat_params_parse( const struct llama_model* model, const json& body, /* openai api json semantics */ const oaicompat_parser_options& opt) { json llama_params; llama_params["__oaicompat"] = true; auto tools = json_value(body, "tools", json()); auto has_tools = tools.is_array() && !tools.empty(); auto stream = json_value(body, "stream", false); auto tool_choice = json_value(body, "tool_choice", std::string("auto")); /* if (tools.is_array() && !tools.empty()) { if (stream) { throw std::runtime_error("Cannot use tools with stream"); } if (!use_jinja) { throw std::runtime_error("tools param requires --jinja flag"); } } if (!use_jinja) { if (body.contains("tool_choice") && !body.at("tool_choice").is_null()) { throw std::runtime_error("Unsupported param: tool_choice"); } }*/ if (!opt.use_jinja) { if (has_tools) { throw std::runtime_error("tools param requires --jinja flag"); } if (tool_choice != "auto") { throw std::runtime_error("tool_choice param requires --jinja flag"); } } // Handle "stop" field if (body.contains("stop") && body.at("stop").is_string()) { llama_params["stop"] = json::array({ body.at("stop").get() }); } else { llama_params["stop"] = json_value(body, "stop", json::array()); } auto json_schema = json_value(body, "json_schema", json()); auto grammar = json_value(body, "grammar", std::string()); if (!json_schema.is_null() && !grammar.empty()) { throw std::runtime_error("Cannot use both json_schema and grammar"); } // Handle "response_format" field if (body.contains("response_format")) { json response_format = json_value(body, "response_format", json::object()); std::string response_type = json_value(response_format, "type", std::string()); if (response_type == "json_object") { json_schema = json_value(response_format, "schema", json::object()); } else if (response_type == "json_schema") { auto schema_wrapper = json_value(response_format, "json_schema", json::object()); json_schema = json_value(schema_wrapper, "schema", json::object()); } else if (!response_type.empty() && response_type != "text") { json_schema = json_value(json_schema, "schema", json::object()); } } common_chat_templates_inputs inputs; inputs.messages = common_chat_msgs_parse_oaicompat(body.at("messages")); inputs.tools = common_chat_tools_parse_oaicompat(tools); inputs.tool_choice = common_chat_tool_choice_parse_oaicompat(tool_choice); inputs.json_schema = json_schema.is_null() ? "" : json_schema.dump(); inputs.grammar = grammar; inputs.use_jinja = opt.use_jinja; inputs.parallel_tool_calls = json_value(body, "parallel_tool_calls", false); inputs.add_generation_prompt = json_value(body, "add_generation_prompt", true); inputs.reasoning_format = opt.reasoning_format; inputs.enable_thinking = opt.enable_thinking; if (!inputs.tools.empty() && inputs.tool_choice != COMMON_CHAT_TOOL_CHOICE_NONE) { if (body.contains("grammar")) { throw std::runtime_error("Cannot use custom grammar constraints with tools."); } llama_params["parse_tool_calls"] = true; } // merge the template args provided from command line with the args provided in the user request auto chat_template_kwargs_object = json_value(body, "chat_template_kwargs", json::object()); inputs.chat_template_kwargs = opt.chat_template_kwargs; for (const auto& item : chat_template_kwargs_object.items()) { inputs.chat_template_kwargs[item.key()] = item.value().dump(); } // parse the "enable_thinking" kwarg to override the default value auto enable_thinking_kwarg = json_value(inputs.chat_template_kwargs, "enable_thinking", std::string("")); if (enable_thinking_kwarg == "true") { inputs.enable_thinking = true; } else if (enable_thinking_kwarg == "false") { inputs.enable_thinking = false; } else if (!enable_thinking_kwarg.empty() && enable_thinking_kwarg[0] == '"') { throw std::runtime_error("invalid type for \"enable_thinking\" (expected boolean, got string)"); } /*"whether to prefill the assistant's response if the last message is an assistant message (default: prefill enabled)\n" "when this flag is set, if the last message is an assistant message then it will be treated as a full message and not prefilled\n"*/ bool prefill_assistant_message = !inputs.messages.empty() && inputs.messages.back().role == "assistant" &&opt.prefill_assistant; common_chat_msg last_message; if (prefill_assistant_message) { last_message = inputs.messages.back(); inputs.messages.pop_back(); /* sanity check, max one assistant message at the end of the list */ if (!inputs.messages.empty() && inputs.messages.back().role == "assistant") { throw std::runtime_error("Cannot have 2 or more assistant messages at the end of the list."); } /* TODO: test this properly */ inputs.reasoning_format = COMMON_REASONING_FORMAT_NONE; if (inputs.enable_thinking) { throw std::runtime_error("Assistant response prefill is incompatible with enable_thinking."); } inputs.add_generation_prompt = true; } // Apply chat template to the list of messages auto chat_params = common_chat_templates_apply(opt.tmpls, inputs); llama_params["chat_format"] = static_cast(chat_params.format); llama_params["prompt"] = chat_params.prompt; llama_params["grammar"] = chat_params.grammar; llama_params["grammar_lazy"] = chat_params.grammar_lazy; auto grammar_triggers = json::array(); for (const auto& trigger : chat_params.grammar_triggers) { grammar_triggers.push_back(trigger.to_json()); } llama_params["grammar_triggers"] = grammar_triggers; llama_params["preserved_tokens"] = chat_params.preserved_tokens; llama_params["thinking_forced_open"] = chat_params.thinking_forced_open; for (const auto& stop : chat_params.additional_stops) { llama_params["stop"].push_back(stop); } // Handle "n" field int n_choices = json_value(body, "n", 1); if (n_choices != 1) { throw std::runtime_error("Only one completion choice is allowed"); } // Handle "logprobs" field // TODO: The response format of this option is not yet OAI-compatible, but seems like no one really using it; We may need to fix it in the future if (json_value(body, "logprobs", false)) { if (has_tools && stream) { throw std::runtime_error("logprobs is not supported with tools + stream"); } llama_params["n_probs"] = json_value(body, "top_logprobs", 20); } else if (body.contains("top_logprobs") && !body.at("top_logprobs").is_null()) { throw std::runtime_error("top_logprobs requires logprobs to be set to true"); } // Copy remaining properties to llama_params // This allows user to use llama.cpp-specific params like "mirostat", "tfs_z",... via OAI endpoint. // See "launch_slot_with_task()" for a complete list of params supported by llama.cpp for (const auto& item : body.items()) { // Exception: if "n_predict" is present, we overwrite the value specified earlier by "max_tokens" if (!llama_params.contains(item.key()) || item.key() == "n_predict") { llama_params[item.key()] = item.value(); } } return llama_params; } // get value by path(key1 / key2) static json json_get_nested_values(const std::vector& paths, const json& js) { json result = json::object(); for (const std::string& path : paths) { json current = js; const auto keys = string_split(path, /*separator*/ '/'); bool valid_path = true; for (const std::string& k : keys) { if (valid_path && current.is_object() && current.contains(k)) { current = current[k]; } else { valid_path = false; } } if (valid_path) { result[path] = current; } } return result; } static json format_tokenizer_response(const std::vector & tokens) { return json { {"tokens", tokens} }; } static json format_detokenized_response(const std::string & content) { return json { {"content", content} }; } static json format_error_response(const std::string & message, const enum error_type type) { std::string type_str; int code = 500; switch (type) { case ERROR_TYPE_INVALID_REQUEST: type_str = "invalid_request_error"; code = 400; break; case ERROR_TYPE_AUTHENTICATION: type_str = "authentication_error"; code = 401; break; case ERROR_TYPE_NOT_FOUND: type_str = "not_found_error"; code = 404; break; case ERROR_TYPE_SERVER: type_str = "server_error"; code = 500; break; case ERROR_TYPE_PERMISSION: type_str = "permission_error"; code = 403; break; case ERROR_TYPE_NOT_SUPPORTED: type_str = "not_supported_error"; code = 501; break; case ERROR_TYPE_UNAVAILABLE: type_str = "unavailable_error"; code = 503; break; } return json { {"code", code}, {"message", message}, {"type", type_str}, }; } struct token_probabilities { float sampled_token_p; std::vector cur; }; static token_probabilities get_token_probabilities(llama_context * ctx, int idx, llama_token sampled_token_id, int n_sorted) { const auto * logits = llama_get_logits_ith(ctx, idx); const int n_vocab = llama_n_vocab(llama_get_model(ctx)); n_sorted = std::min(n_sorted, n_vocab); std::vector> sorted(n_vocab); for (llama_token token_id = 0; token_id < n_vocab; token_id++) sorted[token_id] = {logits[token_id], token_id}; std::partial_sort(sorted.begin(), sorted.begin() + n_sorted, sorted.end(), std::greater>{}); float max_l = sorted.front().first; float cum_sum = 0.0f; float sampled_token_p = 0.0f; bool sampled_token_found = false; std::vector cur(n_sorted); for (int i = 0; i < n_vocab; ++i) { float p = expf(sorted[i].first - max_l); cum_sum += p; if (i < n_sorted) { cur[i] = {sorted[i].second, sorted[i].first, p}; } if (!sampled_token_found && sorted[i].second == sampled_token_id) { sampled_token_p = p; sampled_token_found = true; } } for (int i = n_sorted; i < n_vocab; ++i) cum_sum += expf(sorted[i].first - max_l); float inv_cum_sum = 1/cum_sum; for (int i = 0; i < n_sorted; ++i) cur[i].p *= inv_cum_sum; sampled_token_p *= inv_cum_sum; return {sampled_token_p, cur}; }