utils.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. #pragma once
  2. #include "llama.h"
  3. #include "common.h"
  4. // Change JSON_ASSERT from assert() to GGML_ASSERT:
  5. #define JSON_ASSERT GGML_ASSERT
  6. #include "json.hpp"
  7. #include <string>
  8. #include <vector>
  9. #include <sstream>
  10. #include <random>
  11. #define DEFAULT_OAICOMPAT_MODEL "gpt-3.5-turbo-0613"
  12. using json = nlohmann::ordered_json;
  13. // https://community.openai.com/t/openai-chat-list-of-error-codes-and-types/357791/11
  14. enum error_type {
  15. ERROR_TYPE_INVALID_REQUEST,
  16. ERROR_TYPE_AUTHENTICATION,
  17. ERROR_TYPE_SERVER,
  18. ERROR_TYPE_NOT_FOUND,
  19. ERROR_TYPE_PERMISSION,
  20. ERROR_TYPE_UNAVAILABLE, // custom error
  21. ERROR_TYPE_NOT_SUPPORTED, // custom error
  22. };
  23. extern bool server_verbose;
  24. extern bool server_log_json;
  25. #ifndef SERVER_VERBOSE
  26. #define SERVER_VERBOSE 1
  27. #endif
  28. #if SERVER_VERBOSE != 1
  29. #define LOG_VERBOSE(MSG, ...)
  30. #else
  31. #define LOG_VERBOSE(MSG, ...) \
  32. do \
  33. { \
  34. if (server_verbose) \
  35. { \
  36. server_log("VERB", __func__, __LINE__, MSG, __VA_ARGS__); \
  37. } \
  38. } while (0)
  39. #endif
  40. #define LOG_ERROR( MSG, ...) server_log("ERR", __func__, __LINE__, MSG, __VA_ARGS__)
  41. #define LOG_WARNING(MSG, ...) server_log("WARN", __func__, __LINE__, MSG, __VA_ARGS__)
  42. #define LOG_INFO( MSG, ...) server_log("INFO", __func__, __LINE__, MSG, __VA_ARGS__)
  43. static inline void server_log(const char * level, const char * function, int line, const char * message, const json & extra);
  44. template <typename T>
  45. static T json_value(const json & body, const std::string & key, const T & default_value) {
  46. // Fallback null to default value
  47. if (body.contains(key) && !body.at(key).is_null()) {
  48. try {
  49. return body.at(key);
  50. } catch (NLOHMANN_JSON_NAMESPACE::detail::type_error const &) {
  51. std::stringstream ss;
  52. ss << "Wrong type supplied for parameter '" << key << "'. Expected '" << json(default_value).type_name() << "', using default value.";
  53. LOG_WARNING(ss.str().c_str(), body);
  54. return default_value;
  55. }
  56. } else {
  57. return default_value;
  58. }
  59. }
  60. static inline void server_log(const char * level, const char * function, int line, const char * message, const json & extra) {
  61. std::stringstream ss_tid;
  62. ss_tid << std::this_thread::get_id();
  63. json log = json{
  64. {"tid", ss_tid.str()},
  65. {"timestamp", time(nullptr)},
  66. };
  67. if (server_log_json) {
  68. log.merge_patch({
  69. {"level", level},
  70. {"function", function},
  71. {"line", line},
  72. {"msg", message},
  73. });
  74. if (!extra.empty()) {
  75. log.merge_patch(extra);
  76. }
  77. printf("%s\n", log.dump(-1, ' ', false, json::error_handler_t::replace).c_str());
  78. } else {
  79. char buf[1024];
  80. snprintf(buf, 1024, "%4s [%24s] %s", level, function, message);
  81. if (!extra.empty()) {
  82. log.merge_patch(extra);
  83. }
  84. std::stringstream ss;
  85. ss << buf << " |";
  86. for (const auto & el : log.items())
  87. {
  88. const std::string value = el.value().dump(-1, ' ', false, json::error_handler_t::replace);
  89. ss << " " << el.key() << "=" << value;
  90. }
  91. const std::string str = ss.str();
  92. printf("%.*s\n", (int)str.size(), str.data());
  93. }
  94. fflush(stdout);
  95. }
  96. //
  97. // chat template utils
  98. //
  99. // Format given chat. If tmpl is empty, we take the template from model metadata
  100. inline std::string format_chat(const struct llama_model * model, const std::string & tmpl, const std::vector<json> & messages) {
  101. size_t alloc_size = 0;
  102. // vector holding all allocated string to be passed to llama_chat_apply_template
  103. std::vector<std::string> str(messages.size() * 2);
  104. std::vector<llama_chat_message> chat(messages.size());
  105. for (size_t i = 0; i < messages.size(); ++i) {
  106. const auto & curr_msg = messages[i];
  107. str[i*2 + 0] = json_value(curr_msg, "role", std::string(""));
  108. str[i*2 + 1] = json_value(curr_msg, "content", std::string(""));
  109. alloc_size += str[i*2 + 1].length();
  110. chat[i].role = str[i*2 + 0].c_str();
  111. chat[i].content = str[i*2 + 1].c_str();
  112. }
  113. const char * ptr_tmpl = tmpl.empty() ? nullptr : tmpl.c_str();
  114. std::vector<char> buf(alloc_size * 2);
  115. // run the first time to get the total output length
  116. int32_t res = llama_chat_apply_template(model, ptr_tmpl, chat.data(), chat.size(), true, buf.data(), buf.size());
  117. // if it turns out that our buffer is too small, we resize it
  118. if ((size_t) res > buf.size()) {
  119. buf.resize(res);
  120. res = llama_chat_apply_template(model, ptr_tmpl, chat.data(), chat.size(), true, buf.data(), buf.size());
  121. }
  122. const std::string formatted_chat(buf.data(), res);
  123. LOG_VERBOSE("formatted_chat", {{"text", formatted_chat.c_str()}});
  124. return formatted_chat;
  125. }
  126. //
  127. // base64 utils (TODO: move to common in the future)
  128. //
  129. static const std::string base64_chars =
  130. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  131. "abcdefghijklmnopqrstuvwxyz"
  132. "0123456789+/";
  133. static inline bool is_base64(uint8_t c) {
  134. return (isalnum(c) || (c == '+') || (c == '/'));
  135. }
  136. static inline std::vector<uint8_t> base64_decode(const std::string & encoded_string) {
  137. int i = 0;
  138. int j = 0;
  139. int in_ = 0;
  140. int in_len = encoded_string.size();
  141. uint8_t char_array_4[4];
  142. uint8_t char_array_3[3];
  143. std::vector<uint8_t> ret;
  144. while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
  145. char_array_4[i++] = encoded_string[in_]; in_++;
  146. if (i == 4) {
  147. for (i = 0; i < 4; i++) {
  148. char_array_4[i] = base64_chars.find(char_array_4[i]);
  149. }
  150. char_array_3[0] = ((char_array_4[0] ) << 2) + ((char_array_4[1] & 0x30) >> 4);
  151. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  152. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  153. for (i = 0; (i < 3); i++) {
  154. ret.push_back(char_array_3[i]);
  155. }
  156. i = 0;
  157. }
  158. }
  159. if (i) {
  160. for (j = i; j < 4; j++) {
  161. char_array_4[j] = 0;
  162. }
  163. for (j = 0; j < 4; j++) {
  164. char_array_4[j] = base64_chars.find(char_array_4[j]);
  165. }
  166. char_array_3[0] = ((char_array_4[0] ) << 2) + ((char_array_4[1] & 0x30) >> 4);
  167. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  168. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  169. for (j = 0; j < i - 1; j++) {
  170. ret.push_back(char_array_3[j]);
  171. }
  172. }
  173. return ret;
  174. }
  175. //
  176. // random string / id
  177. //
  178. static std::string random_string() {
  179. static const std::string str("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
  180. std::random_device rd;
  181. std::mt19937 generator(rd());
  182. std::string result(32, ' ');
  183. for (int i = 0; i < 32; ++i) {
  184. result[i] = str[generator() % str.size()];
  185. }
  186. return result;
  187. }
  188. static std::string gen_chatcmplid() {
  189. std::stringstream chatcmplid;
  190. chatcmplid << "chatcmpl-" << random_string();
  191. return chatcmplid.str();
  192. }
  193. //
  194. // other common utils
  195. //
  196. static size_t common_part(const std::vector<llama_token> & a, const std::vector<llama_token> & b) {
  197. size_t i;
  198. for (i = 0; i < a.size() && i < b.size() && a[i] == b[i]; i++) {}
  199. return i;
  200. }
  201. static bool ends_with(const std::string & str, const std::string & suffix) {
  202. return str.size() >= suffix.size() && 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
  203. }
  204. static size_t find_partial_stop_string(const std::string &stop, const std::string &text) {
  205. if (!text.empty() && !stop.empty()) {
  206. const char text_last_char = text.back();
  207. for (int64_t char_index = stop.size() - 1; char_index >= 0; char_index--) {
  208. if (stop[char_index] == text_last_char) {
  209. const std::string current_partial = stop.substr(0, char_index + 1);
  210. if (ends_with(text, current_partial)) {
  211. return text.size() - char_index - 1;
  212. }
  213. }
  214. }
  215. }
  216. return std::string::npos;
  217. }
  218. // TODO: reuse llama_detokenize
  219. template <class Iter>
  220. static std::string tokens_to_str(llama_context * ctx, Iter begin, Iter end) {
  221. std::string ret;
  222. for (; begin != end; ++begin) {
  223. ret += llama_token_to_piece(ctx, *begin);
  224. }
  225. return ret;
  226. }
  227. // format incomplete utf-8 multibyte character for output
  228. static std::string tokens_to_output_formatted_string(const llama_context * ctx, const llama_token token) {
  229. std::string out = token == -1 ? "" : llama_token_to_piece(ctx, token);
  230. // if the size is 1 and first bit is 1, meaning it's a partial character
  231. // (size > 1 meaning it's already a known token)
  232. if (out.size() == 1 && (out[0] & 0x80) == 0x80) {
  233. std::stringstream ss;
  234. ss << std::hex << (out[0] & 0xff);
  235. std::string res(ss.str());
  236. out = "byte: \\x" + res;
  237. }
  238. return out;
  239. }
  240. struct completion_token_output {
  241. llama_token tok;
  242. std::string text_to_send;
  243. struct token_prob {
  244. llama_token tok;
  245. float prob;
  246. };
  247. std::vector<token_prob> probs;
  248. };
  249. // convert a vector of completion_token_output to json
  250. static json probs_vector_to_json(const llama_context * ctx, const std::vector<completion_token_output> & probs) {
  251. json out = json::array();
  252. for (const auto & prob : probs) {
  253. json probs_for_token = json::array();
  254. for (const auto & p : prob.probs) {
  255. const std::string tok_str = tokens_to_output_formatted_string(ctx, p.tok);
  256. probs_for_token.push_back(json {
  257. {"tok_str", tok_str},
  258. {"prob", p.prob},
  259. });
  260. }
  261. const std::string tok_str = tokens_to_output_formatted_string(ctx, prob.tok);
  262. out.push_back(json {
  263. {"content", tok_str},
  264. {"probs", probs_for_token},
  265. });
  266. }
  267. return out;
  268. }
  269. //
  270. // OAI utils
  271. //
  272. static json oaicompat_completion_params_parse(
  273. const struct llama_model * model,
  274. const json & body, /* openai api json semantics */
  275. const std::string & chat_template) {
  276. json llama_params;
  277. llama_params["__oaicompat"] = true;
  278. // Map OpenAI parameters to llama.cpp parameters
  279. //
  280. // For parameters that are defined by the OpenAI documentation (e.g.
  281. // temperature), we explicitly specify OpenAI's intended default; we
  282. // need to do that because sometimes OpenAI disagrees with llama.cpp
  283. //
  284. // https://platform.openai.com/docs/api-reference/chat/create
  285. llama_sampling_params default_sparams;
  286. llama_params["model"] = json_value(body, "model", std::string("unknown"));
  287. llama_params["frequency_penalty"] = json_value(body, "frequency_penalty", 0.0);
  288. llama_params["logit_bias"] = json_value(body, "logit_bias", json::object());
  289. llama_params["n_predict"] = json_value(body, "max_tokens", -1);
  290. llama_params["presence_penalty"] = json_value(body, "presence_penalty", 0.0);
  291. llama_params["seed"] = json_value(body, "seed", LLAMA_DEFAULT_SEED);
  292. llama_params["stream"] = json_value(body, "stream", false);
  293. llama_params["temperature"] = json_value(body, "temperature", 1.0);
  294. llama_params["top_p"] = json_value(body, "top_p", 1.0);
  295. // Apply chat template to the list of messages
  296. llama_params["prompt"] = format_chat(model, chat_template, body.at("messages"));
  297. // Handle "stop" field
  298. if (body.contains("stop") && body.at("stop").is_string()) {
  299. llama_params["stop"] = json::array({body.at("stop").get<std::string>()});
  300. } else {
  301. llama_params["stop"] = json_value(body, "stop", json::array());
  302. }
  303. // Handle "response_format" field
  304. if (body.contains("response_format")) {
  305. json response_format = json_value(body, "response_format", json::object());
  306. std::string response_type = json_value(response_format, "type", std::string());
  307. if (response_type == "json_object") {
  308. llama_params["json_schema"] = json_value(response_format, "schema", json::object());
  309. } else if (!response_type.empty() && response_type != "text") {
  310. throw std::runtime_error("response_format type must be one of \"text\" or \"json_object\", but got: " + response_type);
  311. }
  312. }
  313. // Handle "n" field
  314. int n_choices = json_value(body, "n", 1);
  315. if (n_choices != 1) {
  316. throw std::runtime_error("Only one completion choice is allowed");
  317. }
  318. // Handle "logprobs" field
  319. // 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
  320. if (body.contains("logprobs")) {
  321. llama_params["n_probs"] = json_value(body, "top_logprobs", 20);
  322. } else if (body.contains("top_logprobs")) {
  323. throw std::runtime_error("top_logprobs requires logprobs to be set to true");
  324. }
  325. // Params supported by OAI but unsupported by llama.cpp
  326. static const std::vector<std::string> unsupported_params { "tools", "tool_choice" };
  327. for (auto & param : unsupported_params) {
  328. if (body.contains(param)) {
  329. throw std::runtime_error("Unsupported param: " + param);
  330. }
  331. }
  332. // Copy remaining properties to llama_params
  333. // This allows user to use llama.cpp-specific params like "mirostat", "tfs_z",... via OAI endpoint.
  334. // See "launch_slot_with_task()" for a complete list of params supported by llama.cpp
  335. for (const auto & item : body.items()) {
  336. // Exception: if "n_predict" is present, we overwrite the value specified earlier by "max_tokens"
  337. if (!llama_params.contains(item.key()) || item.key() == "n_predict") {
  338. llama_params[item.key()] = item.value();
  339. }
  340. }
  341. return llama_params;
  342. }
  343. static json format_final_response_oaicompat(const json & request, json result, const std::string & completion_id, bool streaming = false) {
  344. bool stopped_word = result.count("stopped_word") != 0;
  345. bool stopped_eos = json_value(result, "stopped_eos", false);
  346. int num_tokens_predicted = json_value(result, "tokens_predicted", 0);
  347. int num_prompt_tokens = json_value(result, "tokens_evaluated", 0);
  348. std::string content = json_value(result, "content", std::string(""));
  349. std::string finish_reason = "length";
  350. if (stopped_word || stopped_eos) {
  351. finish_reason = "stop";
  352. }
  353. json choices =
  354. streaming ? json::array({json{{"finish_reason", finish_reason},
  355. {"index", 0},
  356. {"delta", json::object()}}})
  357. : json::array({json{{"finish_reason", finish_reason},
  358. {"index", 0},
  359. {"message", json{{"content", content},
  360. {"role", "assistant"}}}}});
  361. std::time_t t = std::time(0);
  362. json res = json {
  363. {"choices", choices},
  364. {"created", t},
  365. {"model",
  366. json_value(request, "model", std::string(DEFAULT_OAICOMPAT_MODEL))},
  367. {"object", streaming ? "chat.completion.chunk" : "chat.completion"},
  368. {"usage", json {
  369. {"completion_tokens", num_tokens_predicted},
  370. {"prompt_tokens", num_prompt_tokens},
  371. {"total_tokens", num_tokens_predicted + num_prompt_tokens}
  372. }},
  373. {"id", completion_id}
  374. };
  375. if (server_verbose) {
  376. res["__verbose"] = result;
  377. }
  378. if (result.contains("completion_probabilities")) {
  379. res["completion_probabilities"] = json_value(result, "completion_probabilities", json::array());
  380. }
  381. return res;
  382. }
  383. // return value is vector as there is one case where we might need to generate two responses
  384. static std::vector<json> format_partial_response_oaicompat(json result, const std::string & completion_id) {
  385. if (!result.contains("model") || !result.contains("oaicompat_token_ctr")) {
  386. return std::vector<json>({result});
  387. }
  388. bool first = json_value(result, "oaicompat_token_ctr", 0) == 0;
  389. std::string modelname = json_value(result, "model", std::string(DEFAULT_OAICOMPAT_MODEL));
  390. bool stopped_word = json_value(result, "stopped_word", false);
  391. bool stopped_eos = json_value(result, "stopped_eos", false);
  392. bool stopped_limit = json_value(result, "stopped_limit", false);
  393. std::string content = json_value(result, "content", std::string(""));
  394. std::string finish_reason;
  395. if (stopped_word || stopped_eos) {
  396. finish_reason = "stop";
  397. }
  398. if (stopped_limit) {
  399. finish_reason = "length";
  400. }
  401. std::time_t t = std::time(0);
  402. json choices;
  403. if (!finish_reason.empty()) {
  404. choices = json::array({json{{"finish_reason", finish_reason},
  405. {"index", 0},
  406. {"delta", json::object()}}});
  407. } else {
  408. if (first) {
  409. if (content.empty()) {
  410. choices = json::array({json{{"finish_reason", nullptr},
  411. {"index", 0},
  412. {"delta", json{{"role", "assistant"}}}}});
  413. } else {
  414. // We have to send this as two updates to conform to openai behavior
  415. json initial_ret = json{{"choices", json::array({json{
  416. {"finish_reason", nullptr},
  417. {"index", 0},
  418. {"delta", json{
  419. {"role", "assistant"}
  420. }}}})},
  421. {"created", t},
  422. {"id", completion_id},
  423. {"model", modelname},
  424. {"object", "chat.completion.chunk"}};
  425. json second_ret = json{
  426. {"choices", json::array({json{{"finish_reason", nullptr},
  427. {"index", 0},
  428. {"delta", json{
  429. {"content", content}}}
  430. }})},
  431. {"created", t},
  432. {"id", completion_id},
  433. {"model", modelname},
  434. {"object", "chat.completion.chunk"}};
  435. return std::vector<json>({initial_ret, second_ret});
  436. }
  437. } else {
  438. // Some idiosyncrasy in task processing logic makes several trailing calls
  439. // with empty content, we ignore these at the calee site.
  440. if (content.empty()) {
  441. return std::vector<json>({json::object()});
  442. }
  443. choices = json::array({json{
  444. {"finish_reason", nullptr},
  445. {"index", 0},
  446. {"delta",
  447. json{
  448. {"content", content},
  449. }},
  450. }});
  451. }
  452. }
  453. json ret = json {
  454. {"choices", choices},
  455. {"created", t},
  456. {"id", completion_id},
  457. {"model", modelname},
  458. {"object", "chat.completion.chunk"}
  459. };
  460. if (!finish_reason.empty()) {
  461. int num_tokens_predicted = json_value(result, "tokens_predicted", 0);
  462. int num_prompt_tokens = json_value(result, "tokens_evaluated", 0);
  463. ret.push_back({"usage", json {
  464. {"completion_tokens", num_tokens_predicted},
  465. {"prompt_tokens", num_prompt_tokens},
  466. {"total_tokens", num_tokens_predicted + num_prompt_tokens}
  467. }});
  468. }
  469. return std::vector<json>({ret});
  470. }
  471. static json format_embeddings_response_oaicompat(const json & request, const json & embeddings) {
  472. json data = json::array();
  473. int i = 0;
  474. for (auto & elem : embeddings) {
  475. data.push_back(json{
  476. {"embedding", json_value(elem, "embedding", json::array())},
  477. {"index", i++},
  478. {"object", "embedding"}
  479. });
  480. }
  481. json res = json {
  482. {"model", json_value(request, "model", std::string(DEFAULT_OAICOMPAT_MODEL))},
  483. {"object", "list"},
  484. {"usage", json {
  485. {"prompt_tokens", 0},
  486. {"total_tokens", 0}
  487. }},
  488. {"data", data}
  489. };
  490. return res;
  491. }
  492. static json format_tokenizer_response(const std::vector<llama_token> & tokens) {
  493. return json {
  494. {"tokens", tokens}
  495. };
  496. }
  497. static json format_detokenized_response(const std::string & content) {
  498. return json {
  499. {"content", content}
  500. };
  501. }
  502. static json format_error_response(const std::string & message, const enum error_type type) {
  503. std::string type_str;
  504. int code = 500;
  505. switch (type) {
  506. case ERROR_TYPE_INVALID_REQUEST:
  507. type_str = "invalid_request_error";
  508. code = 400;
  509. break;
  510. case ERROR_TYPE_AUTHENTICATION:
  511. type_str = "authentication_error";
  512. code = 401;
  513. break;
  514. case ERROR_TYPE_NOT_FOUND:
  515. type_str = "not_found_error";
  516. code = 404;
  517. break;
  518. case ERROR_TYPE_SERVER:
  519. type_str = "server_error";
  520. code = 500;
  521. break;
  522. case ERROR_TYPE_PERMISSION:
  523. type_str = "permission_error";
  524. code = 403;
  525. break;
  526. case ERROR_TYPE_NOT_SUPPORTED:
  527. type_str = "not_supported_error";
  528. code = 501;
  529. break;
  530. case ERROR_TYPE_UNAVAILABLE:
  531. type_str = "unavailable_error";
  532. code = 503;
  533. break;
  534. }
  535. return json {
  536. {"code", code},
  537. {"message", message},
  538. {"type", type_str},
  539. };
  540. }