#include "conversation_model.h" #include "text_embedder_manager.h" #include "text_embedder_remote.h" #include "conversation_manager.h" const std::string get_model_namespace(const std::string& model_name) { if(model_name.find("/") != std::string::npos) { return model_name.substr(0, model_name.find("/")); } else { return ""; } } Option ConversationModel::validate_model(const nlohmann::json& model_config) { // check model_name is exists and it is a string if(model_config.count("model_name") == 0 || !model_config["model_name"].is_string()) { return Option(400, "Property `model_name` is not provided or not a string."); } const std::string model_namespace = get_model_namespace(model_config["model_name"].get()); if(model_namespace == "openai") { return OpenAIConversationModel::validate_model(model_config); } return Option(400, "Model namespace `" + model_namespace + "` is not supported."); } Option ConversationModel::get_answer(const std::string& context, const std::string& prompt, const std::string& system_prompt, const nlohmann::json& model_config) { const std::string model_namespace = get_model_namespace(model_config["model_name"].get()); if(model_namespace == "openai") { return OpenAIConversationModel::get_answer(context, prompt, system_prompt, model_config); } throw Option(400, "Model namespace " + model_namespace + " is not supported."); } Option ConversationModel::parse_conversation_history(const nlohmann::json& conversation, const nlohmann::json& model_config) { const std::string model_namespace = get_model_namespace(model_config["model_name"].get()); if(model_namespace == "openai") { return OpenAIConversationModel::parse_conversation_history(conversation); } throw Option(400, "Model namespace " + model_namespace + " is not supported."); } Option ConversationModel::get_standalone_question(const nlohmann::json& conversation_history, const std::string& question, const nlohmann::json& model_config) { const std::string model_namespace = get_model_namespace(model_config["model_name"].get()); if(model_namespace == "openai") { return OpenAIConversationModel::get_standalone_question(conversation_history, question, model_config); } throw Option(400, "Model namespace " + model_namespace + " is not supported."); } Option ConversationModel::format_question(const std::string& message, const nlohmann::json& model_config) { const std::string model_namespace = get_model_namespace(model_config["model_name"].get()); if(model_namespace == "openai") { return OpenAIConversationModel::format_question(message); } throw Option(400, "Model namespace " + model_namespace + " is not supported."); } Option ConversationModel::format_answer(const std::string& message, const nlohmann::json& model_config) { const std::string model_namespace = get_model_namespace(model_config["model_name"].get()); if(model_namespace == "openai") { return OpenAIConversationModel::format_answer(message); } throw Option(400, "Model namespace " + model_namespace + " is not supported."); } Option OpenAIConversationModel::validate_model(const nlohmann::json& model_config) { if(model_config.count("api_key") == 0) { return Option(400, "API key is not provided"); } if(!model_config["api_key"].is_string()) { return Option(400, "API key is not a string"); } std::unordered_map headers; std::map res_headers; headers["Authorization"] = "Bearer " + model_config["api_key"].get(); headers["Content-Type"] = "application/json"; std::string res; auto res_code = RemoteEmbedder::call_remote_api("GET", OPENAI_LIST_MODELS, "", res, res_headers, headers); if(res_code == 408) { return Option(408, "OpenAI API timeout."); } if (res_code != 200) { nlohmann::json json_res; try { json_res = nlohmann::json::parse(res); } catch (const std::exception& e) { return Option(400, "OpenAI API error: " + res); } if(json_res.count("error") == 0 || json_res["error"].count("message") == 0) { return Option(400, "OpenAI API error: " + res); } return Option(400, "OpenAI API error: " + nlohmann::json::parse(res)["error"]["message"].get()); } nlohmann::json models_json; try { models_json = nlohmann::json::parse(res); } catch (const std::exception& e) { return Option(400, "Got malformed response from OpenAI API."); } bool found = false; // extract model name by removing "openai/" prefix auto model_name_without_namespace = TextEmbedderManager::get_model_name_without_namespace(model_config["model_name"].get()); for (auto& model : models_json["data"]) { if (model["id"] == model_name_without_namespace) { found = true; break; } } if(!found) { return Option(400, "Property `model_name` is not a valid OpenAI model."); } nlohmann::json req_body; req_body["model"] = model_name_without_namespace; req_body["messages"] = R"([ { "role":"user", "content":"hello" } ])"_json; std::string chat_res; res_code = RemoteEmbedder::call_remote_api("POST", OPENAI_CHAT_COMPLETION, req_body.dump(), chat_res, res_headers, headers); if(res_code == 408) { return Option(408, "OpenAI API timeout."); } if (res_code != 200) { nlohmann::json json_res; try { json_res = nlohmann::json::parse(chat_res); } catch (const std::exception& e) { return Option(400, "OpenAI API error: " + chat_res); } if(json_res.count("error") == 0 || json_res["error"].count("message") == 0) { return Option(400, "OpenAI API error: " + chat_res); } return Option(400, "OpenAI API error: " + nlohmann::json::parse(res)["error"]["message"].get()); } return Option(true); } Option OpenAIConversationModel::get_answer(const std::string& context, const std::string& prompt, const std::string& system_prompt, const nlohmann::json& model_config) { const std::string model_name = TextEmbedderManager::get_model_name_without_namespace(model_config["model_name"].get()); const std::string api_key = model_config["api_key"].get(); std::unordered_map headers; std::map res_headers; headers["Authorization"] = "Bearer " + api_key; headers["Content-Type"] = "application/json"; nlohmann::json req_body; req_body["model"] = model_name; req_body["messages"] = nlohmann::json::array(); if(!system_prompt.empty()) { nlohmann::json system_message = nlohmann::json::object(); system_message["role"] = "system"; system_message["content"] = system_prompt; req_body["messages"].push_back(system_message); } nlohmann::json message = nlohmann::json::object(); message["role"] = "user"; message["content"] = "\n" + context + "\n\n\n" + prompt + "\n\n"; req_body["messages"].push_back(message); std::string res; auto res_code = RemoteEmbedder::call_remote_api("POST", OPENAI_CHAT_COMPLETION, req_body.dump(), res, res_headers, headers); if(res_code == 408) { throw Option(400, "OpenAI API timeout."); } if (res_code != 200) { nlohmann::json json_res; try { json_res = nlohmann::json::parse(res); } catch (const std::exception& e) { throw Option(400, "OpenAI API error: " + res); } if(json_res.count("error") == 0 || json_res["error"].count("message") == 0) { throw Option(400, "OpenAI API error: " + res); } throw Option(400, "OpenAI API error: " + nlohmann::json::parse(res)["error"]["message"].get()); } nlohmann::json json_res; try { json_res = nlohmann::json::parse(res); } catch (const std::exception& e) { throw Option(400, "Got malformed response from OpenAI API."); } return Option(json_res["choices"][0]["message"]["content"].get()); } Option OpenAIConversationModel::parse_conversation_history(const nlohmann::json& conversation) { if(!conversation.is_array()) { return Option(400, "Conversation is not an array"); } nlohmann::json messages = nlohmann::json::array(); for(auto& message : conversation) { if(!message.is_object()) { return Option(400, "Message is not an object"); } if(message.count("role") == 0 || !message["role"].is_string()) { return Option(400, "Message role is not provided or not a string"); } if(message.count("content") == 0 || !message["content"].is_string()) { return Option(400, "Message content is not provided or not a string"); } auto parsed_message = nlohmann::json::object(); parsed_message[message["role"].get()] = message["content"].get(); messages.push_back(parsed_message); } return Option(messages); } Option OpenAIConversationModel::get_standalone_question(const nlohmann::json& conversation_history, const std::string& question, const nlohmann::json& model_config) { const std::string model_name = TextEmbedderManager::get_model_name_without_namespace(model_config["model_name"].get()); const std::string api_key = model_config["api_key"].get(); std::unordered_map headers; std::map res_headers; headers["Authorization"] = "Bearer " + api_key; headers["Content-Type"] = "application/json"; nlohmann::json req_body; req_body["model"] = model_name; req_body["messages"] = nlohmann::json::array(); std::string res; std::string standalone_question = STANDALONE_QUESTION_PROMPT; standalone_question += "\n\n\n"; for(auto& message : conversation_history["conversation"]) { if(message.count("user") == 0 && message.count("assistant") == 0) { return Option(400, "Conversation history is not valid"); } standalone_question += message.dump(0) + "\n"; } standalone_question += "\n\n\n" + question; standalone_question += "\n\n\n"; nlohmann::json message = nlohmann::json::object(); message["role"] = "user"; message["content"] = standalone_question; req_body["messages"].push_back(message); auto res_code = RemoteEmbedder::call_remote_api("POST", OPENAI_CHAT_COMPLETION, req_body.dump(), res, res_headers, headers); if(res_code == 408) { return Option(400, "OpenAI API timeout."); } if (res_code != 200) { nlohmann::json json_res; try { json_res = nlohmann::json::parse(res); } catch (const std::exception& e) { return Option(400, "OpenAI API error: " + res); } if(json_res.count("error") == 0 || json_res["error"].count("message") == 0) { return Option(400, "OpenAI API error: " + res); } return Option(400, "OpenAI API error: " + nlohmann::json::parse(res)["error"]["message"].get()); } nlohmann::json json_res; try { json_res = nlohmann::json::parse(res); } catch (const std::exception& e) { return Option(400, "Got malformed response from OpenAI API."); } return Option(json_res["choices"][0]["message"]["content"].get()); } Option OpenAIConversationModel::format_question(const std::string& message) { nlohmann::json json = nlohmann::json::object(); json["user"] = message; return Option(json); } Option OpenAIConversationModel::format_answer(const std::string& message) { nlohmann::json json = nlohmann::json::object(); json["assistant"] = message; return Option(json); }