Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Refactor conversation model.
Kishore Nallan02ed6a471b75
diff --git a/include/conversation_manager.h b/include/conversation_manager.hindex 0bec6eef..088ec9fb 100644--- a/include/conversation_manager.h+++ b/include/conversation_manager.h@@ -40,7 +40,6 @@ class ConversationManager {Option<bool> validate_conversation_store_schema(Collection* collection);Option<bool> validate_conversation_store_collection(const std::string& collection);Option<Collection*> get_history_collection(const std::string& conversation_id);- Option<bool> initialize_history_collection(const std::string& collection);private:ConversationManager() {}std::mutex conversations_mutex;diff --git a/include/conversation_model_manager.h b/include/conversation_model_manager.hindex e8ace218..6c800e66 100644--- a/include/conversation_model_manager.h+++ b/include/conversation_model_manager.h@@ -18,12 +18,13 @@ class ConversationModelManagerConversationModelManager& operator=(const ConversationModelManager&) = delete;static Option<nlohmann::json> get_model(const std::string& model_id);- static Option<nlohmann::json> add_model(nlohmann::json model, const std::string& model_id = "");+ static Option<bool> add_model(nlohmann::json& model, const std::string& model_id,+ const bool write_to_disk);static Option<nlohmann::json> delete_model(const std::string& model_id);static Option<nlohmann::json> update_model(const std::string& model_id, nlohmann::json model);static Option<nlohmann::json> get_all_models();static Option<int> init(Store* store);- static Option<nlohmann::json> migrate_model(nlohmann::json model);+ static bool migrate_model(nlohmann::json& model);static std::unordered_set<std::string> get_history_collections();// For testing Purpose onlystatic void insert_model_for_testing(const std::string& model_id, nlohmann::json model) {@@ -39,8 +40,7 @@ class ConversationModelManagerstatic inline int64_t DEFAULT_HISTORY_COLLECTION_SUFFIX = 0;static inline Store* store;static const std::string get_model_key(const std::string& model_id);- static Option<Collection*> get_default_history_collection();+ static Option<Collection*> create_default_history_collection(const std::string& model_id);static Option<nlohmann::json> delete_model_unsafe(const std::string& model_id);- static Option<nlohmann::json> add_model_unsafe(nlohmann::json model, const std::string& model_id);};\ No newline at end of filediff --git a/src/conversation_manager.cpp b/src/conversation_manager.cppindex 3cccc505..bec7e3df 100644--- a/src/conversation_manager.cpp+++ b/src/conversation_manager.cpp@@ -226,7 +226,6 @@ Option<nlohmann::json> ConversationManager::delete_conversation(const std::strinreturn delete_conversation_unsafe(conversation_id);}-Option<bool> ConversationManager::init(ReplicationState* raft_server) {if(raft_server == nullptr) {@@ -238,8 +237,6 @@ Option<bool> ConversationManager::init(ReplicationState* raft_server) {return Option<bool>(true);}--void ConversationManager::clear_expired_conversations() {std::unique_lock lock(conversations_mutex);// Only leader can delete expired conversations@@ -425,7 +422,3 @@ Option<bool> ConversationManager::validate_conversation_store_collection(const sreturn Option<bool>(true);}--Option<bool> ConversationManager::initialize_history_collection(const std::string& collection) {- return Option<bool>(true);-}\ No newline at end of filediff --git a/src/conversation_model.cpp b/src/conversation_model.cppindex 251f1f0f..1159d595 100644--- a/src/conversation_model.cpp+++ b/src/conversation_model.cpp@@ -15,7 +15,7 @@ const std::string get_model_namespace(const std::string& model_name) {}Option<bool> ConversationModel::validate_model(const nlohmann::json& model_config) {- // check model_name is exists and it is a string+ // check model_name exists and it is a stringif(model_config.count("model_name") == 0 || !model_config["model_name"].is_string()) {return Option<bool>(400, "Property `model_name` is not provided or not a string.");}@@ -24,19 +24,18 @@ Option<bool> ConversationModel::validate_model(const nlohmann::json& model_confireturn Option<bool>(400, "Property `system_prompt` is not a string.");}+ if(model_config.count("history_collection") == 0 || !model_config["history_collection"].is_string()) {+ return Option<bool>(400, "Property `history_collection` is missing or is not a string.");+ }+if(model_config.count("max_bytes") == 0 || !model_config["max_bytes"].is_number_unsigned() || model_config["max_bytes"].get<size_t>() == 0) {return Option<bool>(400, "Property `max_bytes` is not provided or not a positive integer.");}- if(model_config.count("history_collection") != 0) {- if(!model_config["history_collection"].is_string()) {- return Option<bool>(400, "Property `history_collection` is not a string.");- }-- auto validate_converson_collection_op = ConversationManager::get_instance().validate_conversation_store_collection(model_config["history_collection"].get<std::string>());- if(!validate_converson_collection_op.ok()) {- return Option<bool>(400, validate_converson_collection_op.error());- }+ auto validate_converson_collection_op = ConversationManager::get_instance()+ .validate_conversation_store_collection(model_config["history_collection"].get<std::string>());+ if(!validate_converson_collection_op.ok()) {+ return Option<bool>(400, validate_converson_collection_op.error());}if(model_config.count("ttl") != 0 && !model_config["ttl"].is_number_unsigned()) {diff --git a/src/conversation_model_manager.cpp b/src/conversation_model_manager.cppindex a6ee9fe9..92a8fefb 100644--- a/src/conversation_model_manager.cpp+++ b/src/conversation_model_manager.cpp@@ -12,46 +12,36 @@ Option<nlohmann::json> ConversationModelManager::get_model(const std::string& moreturn Option<nlohmann::json>(it->second);}-Option<nlohmann::json> ConversationModelManager::add_model(nlohmann::json model, const std::string& model_id) {+Option<bool> ConversationModelManager::add_model(nlohmann::json& model, const std::string& model_id,+ const bool write_to_disk) {std::unique_lock lock(models_mutex);- return add_model_unsafe(model, model_id);-}-Option<nlohmann::json> ConversationModelManager::add_model_unsafe(nlohmann::json model, const std::string& model_id) {- // check if model existsif (models.find(model_id) != models.end()) {- return Option<nlohmann::json>(409, "Model already exists");- }--- auto validate_res = ConversationModel::validate_model(model);- if (!validate_res.ok()) {- return Option<nlohmann::json>(validate_res.code(), validate_res.error());+ return Option<bool>(409, "Model already exists");}model["id"] = model_id.empty() ? sole::uuid4().str() : model_id;- if(model.count("history_collection") == 0) {- auto default_collection = get_default_history_collection();- if(!default_collection.ok()) {- return Option<nlohmann::json>(default_collection.code(), default_collection.error());- }- model["history_collection"] = default_collection.get()->get_name();- }-if(model.count("ttl") == 0) {model["ttl"] = (uint64_t)(60 * 60 * 24);}- auto model_key = get_model_key(model["id"]);- bool insert_op = store->insert(model_key, model.dump(0));- if(!insert_op) {- return Option<nlohmann::json>(500, "Error while inserting model into the store");+ auto validate_res = ConversationModel::validate_model(model);+ if (!validate_res.ok()) {+ return Option<bool>(validate_res.code(), validate_res.error());}models[model["id"]] = model;- return Option<nlohmann::json>(model);+ if(write_to_disk) {+ auto model_key = get_model_key(model["id"]);+ bool insert_op = store->insert(model_key, model.dump(0));+ if(!insert_op) {+ return Option<bool>(500, "Error while inserting model into the store");+ }+ }++ return Option<bool>(true);}Option<nlohmann::json> ConversationModelManager::delete_model(const std::string& model_id) {@@ -105,29 +95,18 @@ Option<nlohmann::json> ConversationModelManager::update_model(const std::string&return Option<nlohmann::json>(validate_res.code(), validate_res.error());}-auto model_key = get_model_key(model_id);bool insert_op = store->insert(model_key, model_copy.dump(0));if(!insert_op) {return Option<nlohmann::json>(500, "Error while inserting model into the store");}- if(model_copy.count("history_collection") == 0) {- auto default_collection = get_default_history_collection();- if(!default_collection.ok()) {- return Option<nlohmann::json>(default_collection.code(), default_collection.error());- }- model_copy["history_collection"] = default_collection.get()->get_name();- }--models[model_id] = model_copy;return Option<nlohmann::json>(model_copy);}Option<int> ConversationModelManager::init(Store* store) {- std::unique_lock lock(models_mutex);ConversationModelManager::store = store;std::vector<std::string> model_strs;@@ -140,37 +119,17 @@ Option<int> ConversationModelManager::init(Store* store) {int loaded_models = 0;for(auto& model_str : model_strs) {nlohmann::json model_json = nlohmann::json::parse(model_str);- std::string model_id = model_json["id"];-- // Migrate cloudflare models to new namespace convention, change namespace from `cf` to `cloudflare`- if(EmbedderManager::get_model_namespace(model_json["model_name"]) == "cf") {- model_json["model_name"] = "cloudflare/@cf/" + EmbedderManager::get_model_name_without_namespace(model_json["model_name"]);- if(model_json.find("history_collection") == model_json.end()) {- auto default_collection = get_default_history_collection();- if(!default_collection.ok()) {- LOG(INFO) << "Error while creating default history collection for model " << model_id << ": " << default_collection.error();- continue;- }- model_json["history_collection"] = default_collection.get()->get_name();- }- auto add_res = add_model_unsafe(model_json, model_id);- if(!add_res.ok()) {- return Option<int>(add_res.code(), add_res.error());- }- }+ const std::string& model_id = model_json["id"];+ // handle model format changes+ auto has_migration = migrate_model(model_json);- // Migrate models that don't have a conversation collection- if(model_json.count("history_collection") == 0) {- auto migrate_op = migrate_model(model_json);- if(!migrate_op.ok()) {- return Option<int>(migrate_op.code(), migrate_op.error());- }- model_json = migrate_op.get();+ // write to disk only when a migration has been done on model data+ auto add_op = add_model(model_json, model_id, has_migration);+ if(!add_op.ok()) {+ return Option<int>(add_op.code(), add_op.error());}- models[model_id] = model_json;- ConversationManager::get_instance().initialize_history_collection(model_json["history_collection"].get<std::string>());loaded_models++;}@@ -181,17 +140,14 @@ const std::string ConversationModelManager::get_model_key(const std::string& modreturn std::string(MODEL_KEY_PREFIX) + "_" + model_id;}+Option<Collection*> ConversationModelManager::create_default_history_collection(const std::string& model_id) {+ std::string collection_name = "ts_conversation_history_" + model_id;-Option<Collection*> ConversationModelManager::get_default_history_collection() {- int64_t time_epoch;- if(DEFAULT_HISTORY_COLLECTION_SUFFIX == 0) {- time_epoch = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();- DEFAULT_HISTORY_COLLECTION_SUFFIX = time_epoch;- } else {- time_epoch = DEFAULT_HISTORY_COLLECTION_SUFFIX;+ auto get_res = CollectionManager::get_instance().get_collection(collection_name).get();+ if(get_res) {+ return Option<Collection*>(get_res);}-- std::string collection_id = "default_conversation_history_" + std::to_string(time_epoch);+nlohmann::json schema_json = R"({"fields": [{@@ -219,12 +175,8 @@ Option<Collection*> ConversationModelManager::get_default_history_collection() {}]})"_json;- schema_json["name"] = collection_id;- auto get_res = CollectionManager::get_instance().get_collection(collection_id).get();- if(get_res) {- return Option<Collection*>(get_res);- }+ schema_json["name"] = collection_name;auto create_res = CollectionManager::get_instance().create_collection(schema_json);if(!create_res.ok()) {@@ -233,23 +185,35 @@ Option<Collection*> ConversationModelManager::get_default_history_collection() {return Option<Collection*>(create_res.get());}-Option<nlohmann::json> ConversationModelManager::migrate_model(nlohmann::json model) {+bool ConversationModelManager::migrate_model(nlohmann::json& model) {+ // handles missing fields and format changesauto model_id = model["id"];- auto default_collection = get_default_history_collection();- if(!default_collection.ok()) {- return Option<nlohmann::json>(default_collection.code(), default_collection.error());+ bool has_model_change = false;++ // Migrate cloudflare models to new namespace convention, change namespace from `cf` to `cloudflare`+ if(EmbedderManager::get_model_namespace(model["model_name"]) == "cf") {+ model["model_name"] = "cloudflare/@cf/" + EmbedderManager::get_model_name_without_namespace(model["model_name"]);+ has_model_change = true;+ }++ if(model.count("history_collection") == 0) {+ auto default_collection_op = create_default_history_collection(model_id);+ if(!default_collection_op.ok()) {+ LOG(INFO) << "Error while creating default history collection for model " << model_id << ": "+ << default_collection_op.error();+ return false;+ }++ model["history_collection"] = default_collection_op.get()->get_name();+ has_model_change = true;}- model["history_collection"] = default_collection.get()->get_name();if(model.count("ttl") == 0) {model["ttl"] = (uint64_t)(60 * 60 * 24);+ has_model_change = true;}- auto add_res = add_model_unsafe(model, model_id);- if(!add_res.ok()) {- return Option<nlohmann::json>(add_res.code(), add_res.error());- }- return Option<nlohmann::json>(model);+ return has_model_change;}std::unordered_set<std::string> ConversationModelManager::get_history_collections() {diff --git a/src/core_api.cpp b/src/core_api.cppindex a0b99ea4..0c81112b 100644--- a/src/core_api.cpp+++ b/src/core_api.cpp@@ -2982,34 +2982,33 @@ bool post_proxy(const std::shared_ptr<http_req>& req, const std::shared_ptr<httpbool post_conversation_model(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {- nlohmann::json req_json;+ nlohmann::json model_json;try {- req_json = nlohmann::json::parse(req->body);+ model_json = nlohmann::json::parse(req->body);} catch(const nlohmann::json::parse_error& e) {LOG(ERROR) << "JSON error: " << e.what();res->set_400("Bad JSON.");return false;}- if(!req_json.is_object()) {+ if(!model_json.is_object()) {res->set_400("Bad JSON.");return false;}std::string model_id = req->metadata;- auto add_model_op = ConversationModelManager::add_model(req_json, model_id);+ auto add_model_op = ConversationModelManager::add_model(model_json, model_id, true);if(!add_model_op.ok()) {res->set(add_model_op.code(), add_model_op.error());return false;}- auto model = add_model_op.get();- Collection::hide_credential(model, "api_key");+ Collection::hide_credential(model_json, "api_key");- res->set_200(model.dump());+ res->set_200(model_json.dump());return true;}diff --git a/test/collection_vector_search_test.cpp b/test/collection_vector_search_test.cppindex aefa449c..b2475e0d 100644--- a/test/collection_vector_search_test.cpp+++ b/test/collection_vector_search_test.cpp@@ -3180,7 +3180,7 @@ TEST_F(CollectionVectorTest, TestQAConversation) {auto coll = collection_create_op.get();- auto model_add_op = ConversationModelManager::add_model(conversation_model_config);+ auto model_add_op = ConversationModelManager::add_model(conversation_model_config, "", true);ASSERT_TRUE(model_add_op.ok());auto add_op = coll->add(R"({@@ -3214,10 +3214,11 @@ TEST_F(CollectionVectorTest, TestQAConversation) {auto results_op = coll->search("how many products are there for clothing category?", {"embedding"},"", {}, {}, {2}, 10,1, FREQUENCY, {true},- 0, spp::sparse_hash_set<std::string>(), {},+ 0, spp::sparse_hash_set<std::string>(), spp::sparse_hash_set<std::string>(),10, "", 30, 4, "", 1, "", "", {}, 3, "<mark>", "</mark>", {}, 4294967295UL, true, false,true, "", false, 6000000UL, 4, 7, fallback, 4, {off}, 32767UL, 32767UL, 2, 2, false, "",- true, 0, max_score, 100, 0, 0, "exhaustive", 30000, 2, "", {}, {}, "right_to_left", true, true, true, model_add_op.get()["id"].get<std::string>());+ true, 0, max_score, 100, 0, 0, "exhaustive", 30000, 2, "", {}, {}, "right_to_left", true, true, true,+ conversation_model_config["id"].get<std::string>());ASSERT_TRUE(results_op.ok());auto results = results_op.get();@@ -3716,11 +3717,11 @@ TEST_F(CollectionVectorTest, InvalidMultiSearchConversation) {conversation_model_config["api_key"] = api_key;- auto model_add_op = ConversationModelManager::add_model(conversation_model_config);+ auto model_add_op = ConversationModelManager::add_model(conversation_model_config, "", true);ASSERT_TRUE(model_add_op.ok());- auto model_id = model_add_op.get()["id"];+ std::string model_id = conversation_model_config["id"];auto collection_create_op = collectionManager.create_collection(schema_json);ASSERT_TRUE(collection_create_op.ok());@@ -3752,7 +3753,7 @@ TEST_F(CollectionVectorTest, InvalidMultiSearchConversation) {ASSERT_EQ(res_json["message"], "`q` parameter cannot be used in POST body if `conversation` is enabled. Please set `q` as a query parameter in the request, instead of inside the POST body");search_body["searches"][0].erase("q");- search_body["searches"][0]["conversation_model_id"] = to_string(model_id);+ search_body["searches"][0]["conversation_model_id"] = model_id;req->body = search_body.dump();@@ -3803,9 +3804,8 @@ TEST_F(CollectionVectorTest, TestMigratingConversationModel) {conversation_model_config["api_key"] = api_key;auto migrate_res = ConversationModelManager::migrate_model(conversation_model_config);- ASSERT_TRUE(migrate_res.ok());- auto migrated_model = migrate_res.get();- ASSERT_TRUE(migrated_model.count("history_collection") == 1);+ ASSERT_TRUE(migrate_res);+ ASSERT_TRUE(conversation_model_config.count("history_collection") == 1);auto collection = CollectionManager::get_instance().get_collection("conversation_store").get();ASSERT_TRUE(collection != nullptr);@@ -3845,10 +3845,10 @@ TEST_F(CollectionVectorTest, TestPartiallyUpdateConversationModel) {auto coll = collection_create_op.get();- auto model_add_op = ConversationModelManager::add_model(conversation_model_config);+ auto model_add_op = ConversationModelManager::add_model(conversation_model_config, "", true);ASSERT_TRUE(model_add_op.ok());- auto model_id = model_add_op.get()["id"];+ std::string model_id = conversation_model_config["id"];auto update_op = ConversationModelManager::update_model(model_id, R"({"max_bytes": 2000})"_json);ASSERT_TRUE(update_op.ok());diff --git a/test/core_api_utils_test.cpp b/test/core_api_utils_test.cppindex eb01c365..3376a4c5 100644--- a/test/core_api_utils_test.cpp+++ b/test/core_api_utils_test.cpp@@ -1400,18 +1400,18 @@ TEST_F(CoreAPIUtilsTest, TestGetConversations) {model_config["api_key"] = api_key;- auto add_model_op = ConversationModelManager::add_model(model_config);+ auto add_model_op = ConversationModelManager::add_model(model_config, "", true);ASSERT_TRUE(add_model_op.ok());- LOG(INFO) << "Model id: " << add_model_op.get();+ LOG(INFO) << "Model id: " << model_config["id"];- auto model_id = add_model_op.get()["id"].get<std::string>();+ auto model_id = model_config["id"].get<std::string>();auto results_op = coll->search("how many products are there for clothing category?", {"embedding"},"", {}, {}, {2}, 10,1, FREQUENCY, {true},- 0, spp::sparse_hash_set<std::string>(), {},+ 0, spp::sparse_hash_set<std::string>(), spp::sparse_hash_set<std::string>(),10, "", 30, 4, "", 1, "", "", {}, 3, "<mark>", "</mark>", {}, 4294967295UL, true, false,true, "", false, 6000000UL, 4, 7, fallback, 4, {off}, 32767UL, 32767UL, 2, 2, false, "",true, 0, max_score, 100, 0, 0, "exhaustive", 30000, 2, "", {}, {}, "right_to_left", true, true, true, model_id);@@ -1420,7 +1420,8 @@ TEST_F(CoreAPIUtilsTest, TestGetConversations) {auto id = results_op.get()["conversation"]["id"].get<std::string>();- auto history_collection = ConversationManager::get_instance().get_history_collection(add_model_op.get()["history_collection"].get<std::string>()).get();+ auto history_collection = ConversationManager::get_instance()+ .get_history_collection(model_config["history_collection"].get<std::string>()).get();auto history_search_res = history_collection->search(id, {"conversation_id"}, "", {}, {}, {0}).get();ASSERT_EQ(2, history_search_res["hits"].size());auto del_res = ConversationModelManager::delete_model(model_id);