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
Fix conversation and conversation model (#1869)
Ozan Armağan039585760187
diff --git a/include/conversation_manager.h b/include/conversation_manager.hindex a049c0b1..2d9d90db 100644--- a/include/conversation_manager.h+++ b/include/conversation_manager.h@@ -10,11 +10,10 @@#include "store.h"#include "sole.hpp"#include "collection_manager.h"-+#include "conversation_model_manager.h"class ConversationManager {public:-ConversationManager(const ConversationManager&) = delete;ConversationManager(ConversationManager&&) = delete;ConversationManager& operator=(const ConversationManager&) = delete;@@ -23,14 +22,12 @@ class ConversationManager {static ConversationManager instance;return instance;}- Option<std::string> add_conversation(const nlohmann::json& conversation, const std::string& history_collection, const std::string& id = "");+ Option<std::string> add_conversation(const nlohmann::json& conversation, const nlohmann::json& model, const std::string& id = "");Option<nlohmann::json> get_conversation(const std::string& conversation_id);static Option<nlohmann::json> truncate_conversation(nlohmann::json conversation, size_t limit);- Option<nlohmann::json> update_conversation(nlohmann::json conversation);Option<nlohmann::json> delete_conversation(const std::string& conversation_id);Option<bool> check_conversation_exists(const std::string& conversation_id);Option<std::unordered_set<std::string>> get_conversation_ids();- Option<nlohmann::json> get_all_conversations();static constexpr size_t MAX_TOKENS = 3000;Option<bool> init(ReplicationState* raft_server);void clear_expired_conversations();@@ -45,17 +42,16 @@ class ConversationManager {Option<bool> add_history_collection(const std::string& collection);Option<bool> remove_history_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;ReplicationState* raft_server;- static constexpr size_t CONVERSATION_TTL = 60 * 60 * 24;size_t TTL_OFFSET = 0;- size_t MAX_CONVERSATIONS_TO_DELETE_ONCE = 5;std::atomic<bool> quit = false;std::condition_variable cv;- std::unordered_map<std::string, uint32_t> history_collection_map;- std::unordered_map<std::string, std::string> conversation_mapper;++ Option<nlohmann::json> delete_conversation_unsafe(const std::string& conversation_id);};\ No newline at end of filediff --git a/include/conversation_model_manager.h b/include/conversation_model_manager.hindex 1c00e35d..e8ace218 100644--- a/include/conversation_model_manager.h+++ b/include/conversation_model_manager.h@@ -24,6 +24,12 @@ class ConversationModelManagerstatic Option<nlohmann::json> get_all_models();static Option<int> init(Store* store);static Option<nlohmann::json> migrate_model(nlohmann::json model);+ static std::unordered_set<std::string> get_history_collections();+ // For testing Purpose only+ static void insert_model_for_testing(const std::string& model_id, nlohmann::json model) {+ std::unique_lock lock(models_mutex);+ models[model_id] = model;+ }private:static inline std::unordered_map<std::string, nlohmann::json> models;static inline std::shared_mutex models_mutex;diff --git a/include/core_api.h b/include/core_api.hindex ace6c0dd..d485a8cf 100644--- a/include/core_api.h+++ b/include/core_api.h@@ -200,13 +200,6 @@ void init_api(uint32_t cache_num_entries);bool post_proxy(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);-bool get_conversations(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);--bool get_conversation(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);--bool del_conversation(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);--bool put_conversation(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);bool post_conversation_model(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);diff --git a/src/collection.cpp b/src/collection.cppindex afc97923..079368dd 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -84,8 +84,6 @@ Collection::~Collection() {VQModelManager::get_instance().delete_model(vq_model->get_model_name());}}-- ConversationManager::get_instance().remove_history_collection(name);}uint32_t Collection::get_next_seq_id() {@@ -2920,7 +2918,7 @@ Option<nlohmann::json> Collection::search(std::string raw_query,conversation_history.push_back(formatted_question_op.get());conversation_history.push_back(formatted_answer_op.get());- auto add_conversation_op = ConversationManager::get_instance().add_conversation(conversation_history, conversation_model["history_collection"].get<std::string>(), conversation_id);+ auto add_conversation_op = ConversationManager::get_instance().add_conversation(conversation_history, conversation_model, conversation_id);if(!add_conversation_op.ok()) {return Option<nlohmann::json>(add_conversation_op.code(), add_conversation_op.error());}diff --git a/src/conversation_manager.cpp b/src/conversation_manager.cppindex cd6dbba0..2a875253 100644--- a/src/conversation_manager.cpp+++ b/src/conversation_manager.cpp@@ -4,7 +4,7 @@#include "http_client.h"#include "core_api.h"-Option<std::string> ConversationManager::add_conversation(const nlohmann::json& conversation, const std::string& history_collection, const std::string& id) {+Option<std::string> ConversationManager::add_conversation(const nlohmann::json& conversation, const nlohmann::json& model, const std::string& id) {std::unique_lock lock(conversations_mutex);if(!conversation.is_array()) {return Option<std::string>(400, "Conversation is not an array");@@ -18,6 +18,7 @@ Option<std::string> ConversationManager::add_conversation(const nlohmann::json&}std::string conversation_id = id.empty() ? sole::uuid4().str() : id;+ std::string history_collection = model["history_collection"].get<std::string>();auto collection = CollectionManager::get_instance().get_collection(history_collection).get();if(!collection) {@@ -46,6 +47,7 @@ Option<std::string> ConversationManager::add_conversation(const nlohmann::json&message_json["role"] = message_it.key();message_json["message"] = message_it.value();message_json["timestamp"] = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();+ message_json["model_id"] = model["id"];body += message_json.dump(-1) + "\n";}@@ -62,7 +64,6 @@ Option<std::string> ConversationManager::add_conversation(const nlohmann::json&return Option<std::string>(resp->status_code, resp->body);}- conversation_mapper[conversation_id] = history_collection;return Option<std::string>(conversation_id);}@@ -82,7 +83,6 @@ Option<std::string> ConversationManager::add_conversation(const nlohmann::json&LOG(ERROR) << "Status: " << status;return Option<std::string>(400, "Error while creating conversation");} else {- conversation_mapper[conversation_id] = history_collection;return Option<std::string>(conversation_id);}} else {@@ -117,8 +117,14 @@ Option<nlohmann::json> ConversationManager::get_conversation(const std::string&message[hit["document"]["role"]] = hit["document"]["message"];res["conversation"].push_back(message);}++ // swap every two elements+ for(size_t i = 0; i < res["conversation"].size() - 1; i += 2) {+ res["conversation"].at(i).swap(res["conversation"].at(i + 1));+ }++res["id"] = conversation_id;- res["ttl"] = CONVERSATION_TTL;res["last_updated"] = (search_res_json["hits"].size() > 0) ? search_res_json["hits"][search_res_json["hits"].size() - 1]["document"]["timestamp"].get<uint32_t>() : 0;if(total > 250) {@@ -161,9 +167,7 @@ Option<nlohmann::json> ConversationManager::truncate_conversation(nlohmann::jsonreturn Option<nlohmann::json>(conversation);}-Option<nlohmann::json> ConversationManager::delete_conversation(const std::string& conversation_id) {- std::unique_lock lock(conversations_mutex);-+Option<nlohmann::json> ConversationManager::delete_conversation_unsafe(const std::string& conversation_id) {auto conversation_exists = check_conversation_exists(conversation_id);if(!conversation_exists.ok()) {return Option<nlohmann::json>(conversation_exists.code(), conversation_exists.error());@@ -190,7 +194,6 @@ Option<nlohmann::json> ConversationManager::delete_conversation(const std::strinnlohmann::json res_json;res_json["id"] = conversation_id;- conversation_mapper.erase(conversation_id);return Option<nlohmann::json>(res_json);}@@ -214,30 +217,15 @@ Option<nlohmann::json> ConversationManager::delete_conversation(const std::strin} else {nlohmann::json res_json;res_json["conversation_id"] = conversation_id;- conversation_mapper.erase(conversation_id);return Option<nlohmann::json>(res_json);}-}--Option<nlohmann::json> ConversationManager::get_all_conversations() {- auto conversation_ids_op = get_conversation_ids();- if(!conversation_ids_op.ok()) {- return Option<nlohmann::json>(conversation_ids_op.code(), conversation_ids_op.error());- }- auto conversation_ids = conversation_ids_op.get();- nlohmann::json res;- res = nlohmann::json::array();+}- for(auto& conversation_id : conversation_ids) {- auto conversation_op = get_conversation(conversation_id);- if(!conversation_op.ok()) {- return Option<nlohmann::json>(conversation_op.code(), conversation_op.error());- }- res.push_back(conversation_op.get());- }+Option<nlohmann::json> ConversationManager::delete_conversation(const std::string& conversation_id) {+ std::unique_lock lock(conversations_mutex);+ return delete_conversation_unsafe(conversation_id);+}- return Option<nlohmann::json>(res);-}Option<bool> ConversationManager::init(ReplicationState* raft_server) {@@ -255,123 +243,39 @@ Option<bool> ConversationManager::init(ReplicationState* raft_server) {void ConversationManager::clear_expired_conversations() {std::unique_lock lock(conversations_mutex);- int cleared_conversations = 0;- auto conversation_ids_op = get_conversation_ids();- if(!conversation_ids_op.ok()) {- LOG(ERROR) << conversation_ids_op.error();+ auto models_op = ConversationModelManager::get_all_models();+ if(!models_op.ok()) {+ LOG(ERROR) << "Error while getting conversation models: " << models_op.error();return;}- auto conversation_ids = conversation_ids_op.get();- std::vector<std::string> conversation_ids_to_delete;- std::vector<sort_by> sort_by_vec = {{"timestamp", sort_field_const::desc}};-- for(auto& conversation_id : conversation_ids) {- auto collection_op = get_history_collection(conversation_id);- if(!collection_op.ok()) {- LOG(ERROR) << collection_op.error();- continue;- }- auto collection = collection_op.get();- auto search_res = collection->search("*", {}, "conversation_id:" + conversation_id, {}, sort_by_vec, {}, 1);- if(!search_res.ok()) {- LOG(ERROR) << "Error while searching conversation store: " << search_res.error();- continue;- }- auto search_res_json = search_res.get();- if(search_res_json["hits"].size() == 0) {- continue;- }- auto last_updated = search_res_json["hits"][0]["document"]["timestamp"].get<uint32_t>();- if(std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count() - last_updated + TTL_OFFSET > CONVERSATION_TTL) {- conversation_ids_to_delete.push_back(conversation_id);- }-- if(conversation_ids_to_delete.size() >= MAX_CONVERSATIONS_TO_DELETE_ONCE) {- break;- }- }-- if(conversation_ids_to_delete.empty()) {- return;- }+ auto models = models_op.get();- for(auto& conversation_id : conversation_ids_to_delete) {- lock.unlock();- auto delete_op = delete_conversation(conversation_id);- lock.lock();- if(!delete_op.ok()) {- LOG(ERROR) << delete_op.error();+ for(auto& model : models) {+ if(model.count("history_collection") == 0) {continue;}- cleared_conversations++;- }-- LOG(INFO) << "Cleared " << cleared_conversations << " expired conversations";-}--Option<nlohmann::json> ConversationManager::update_conversation(nlohmann::json conversation) {- std::unique_lock lock(conversations_mutex);- if(!conversation.is_object()) {- return Option<nlohmann::json>(400, "Conversation is not an object");- }- if(conversation.count("id") == 0) {- return Option<nlohmann::json>(400, "Conversation is missing id");- }-- if(!conversation["id"].is_string()) {- return Option<nlohmann::json>(400, "Conversation id must be a string");- }+ auto history_collection = model["history_collection"].get<std::string>();+ auto ttl = model["ttl"].get<uint64_t>();- if(conversation.count("conversation") == 0) {- return Option<nlohmann::json>(400, "Conversation is missing conversation");- }-- if(!conversation["conversation"].is_array()) {- return Option<nlohmann::json>(400, "Conversation history is not an array");- }+ auto collection = CollectionManager::get_instance().get_collection(history_collection).get();- for(auto& message : conversation["conversation"]) {- if(!message.is_object()) {- return Option<nlohmann::json>(400, "Message is not an object");- }+ std::shared_ptr<http_req> req = std::make_shared<http_req>();+ std::shared_ptr<http_res> resp = std::make_shared<http_res>(nullptr);+ req->params["collection"] = history_collection;+ req->params["filter_by"] = "timestamp:<" + std::to_string(std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count() - ttl + TTL_OFFSET) + "&&model_id:=" + model["id"].get<std::string>();- const auto& message_it = message.items().begin();- if(message_it == message.items().end()) {- return Option<nlohmann::json>(400, "Message is empty");- }+ auto api_res = del_remove_documents(req, resp);- if(!message_it.value().is_string()) {- return Option<nlohmann::json>(400, "Role and message must be strings");+ if(!api_res) {+ LOG(ERROR) << "Error while deleting expired conversations: " << resp->body;}- }-- const std::string& conversation_id = conversation["id"];-- auto conversation_exists = check_conversation_exists(conversation_id);- if(!conversation_exists.ok()) {- return Option<nlohmann::json>(conversation_exists.code(), conversation_exists.error());- }-- auto delete_op = delete_conversation(conversation_id);- if(!delete_op.ok()) {- return Option<nlohmann::json>(delete_op.code(), delete_op.error());- }- auto create_op = add_conversation(conversation["conversation"], conversation_id);- if(!create_op.ok()) {- return Option<nlohmann::json>(create_op.code(), create_op.error());}-- auto actual_conversation_op = get_conversation(conversation_id);- if(!actual_conversation_op.ok()) {- return Option<nlohmann::json>(actual_conversation_op.code(), actual_conversation_op.error());- }-- return Option<nlohmann::json>(actual_conversation_op.get());}+void ConversationManager::run() {while(!quit) {std::unique_lock lock(conversations_mutex);@@ -416,10 +320,6 @@ Option<bool> ConversationManager::validate_conversation_store_schema(Collection*return Option<bool>(400, "`conversation_id` field must be a string");}- if(!schema.at("conversation_id").facet) {- return Option<bool>(400, "`conversation_id` field must be a facet");- }-if(schema.at("role").type != field_types::STRING) {return Option<bool>(400, "`role` field must be a string");}@@ -436,6 +336,14 @@ Option<bool> ConversationManager::validate_conversation_store_schema(Collection*return Option<bool>(400, "`timestamp` field must be a sort field");}+ if(schema.count("model_id") == 0) {+ return Option<bool>(400, "Schema is missing `model_id` field");+ }++ if(schema.at("model_id").type != field_types::STRING) {+ return Option<bool>(400, "`model_id` field must be a string");+ }+return Option<bool>(true);}@@ -463,71 +371,12 @@ Option<bool> ConversationManager::check_conversation_exists(const std::string& creturn Option<bool>(true);}-Option<std::unordered_set<std::string>> ConversationManager::get_conversation_ids() {- std::unordered_set<std::string> conversation_ids;- for(const auto& conversation_id : conversation_mapper) {- conversation_ids.insert(conversation_id.first);- }-- return Option<std::unordered_set<std::string>>(conversation_ids);-}--Option<bool> ConversationManager::add_history_collection(const std::string& collection) {- std::unique_lock lock(conversations_mutex);- if(history_collection_map.count(collection) > 0) {- history_collection_map[collection]++;- } else {- auto collection_ptr = CollectionManager::get_instance().get_collection(collection).get();- if(!collection_ptr) {- return Option<bool>(404, "Collection not found");- }-- auto validate_op = validate_conversation_store_schema(collection_ptr);- if(!validate_op.ok()) {- return Option<bool>(validate_op.code(), validate_op.error());- }-- history_collection_map[collection] = 1;- }-- return Option<bool>(true);-}--Option<bool> ConversationManager::remove_history_collection(const std::string& collection) {- std::unique_lock lock(conversations_mutex);- if(history_collection_map.count(collection) == 0) {- return Option<bool>(404, "Collection not found");- }-- history_collection_map[collection]--;-- if(history_collection_map[collection] == 0) {- std::vector<std::string> conversations_to_delete;- for(auto& conversation : conversation_mapper) {- if(conversation.second == collection) {- conversations_to_delete.push_back(conversation.first);- }- }- for(auto conversation_id : conversations_to_delete) {- conversation_mapper.erase(conversation_id);- }- history_collection_map.erase(collection);- }-- return Option<bool>(true);-}Option<Collection*> ConversationManager::get_history_collection(const std::string& conversation_id) {- if(conversation_mapper.count(conversation_id) > 0) {- auto collection = CollectionManager::get_instance().get_collection(conversation_mapper[conversation_id]).get();- if(collection) {- return Option<Collection*>(collection);- }- }-- for(auto& collection : history_collection_map) {- auto collection_ptr = CollectionManager::get_instance().get_collection(collection.first).get();+ auto history_collections = ConversationModelManager::get_history_collections();+ for(auto& collection : history_collections) {+ auto collection_ptr = CollectionManager::get_instance().get_collection(collection).get();if(!collection_ptr) {continue;}@@ -539,7 +388,6 @@ Option<Collection*> ConversationManager::get_history_collection(const std::strinauto search_res_json = search_res.get();if(search_res_json["found"].get<uint32_t>() > 0) {- conversation_mapper[conversation_id] = collection.first;return Option<Collection*>(collection_ptr);}}@@ -558,5 +406,9 @@ Option<bool> ConversationManager::validate_conversation_store_collection(const sreturn Option<bool>(validate_op.code(), validate_op.error());}+ return 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 d7bc613b..251f1f0f 100644--- a/src/conversation_model.cpp+++ b/src/conversation_model.cpp@@ -28,13 +28,19 @@ Option<bool> ConversationModel::validate_model(const nlohmann::json& model_confireturn Option<bool>(400, "Property `max_bytes` is not provided or not a positive integer.");}- if(model_config.count("history_collection") == 0 || !model_config["history_collection"].is_string()) {- return Option<bool>(400, "Property `history_collection` is not provided or not a string.");+ 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()) {+ return Option<bool>(400, "Property `ttl` is not a positive integer.");}const std::string model_namespace = get_model_namespace(model_config["model_name"].get<std::string>());diff --git a/src/conversation_model_manager.cpp b/src/conversation_model_manager.cppindex c7a62601..f16d7c45 100644--- a/src/conversation_model_manager.cpp+++ b/src/conversation_model_manager.cpp@@ -18,6 +18,12 @@ Option<nlohmann::json> ConversationModelManager::add_model(nlohmann::json model,}Option<nlohmann::json> ConversationModelManager::add_model_unsafe(nlohmann::json model, const std::string& model_id) {+ // check if model exists+ if (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());@@ -25,15 +31,26 @@ Option<nlohmann::json> ConversationModelManager::add_model_unsafe(nlohmann::jsonmodel["id"] = model_id.empty() ? sole::uuid4().str() : model_id;- auto model_key = get_model_key(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"] = 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");}- models[model_id] = model;+ models[model["id"]] = model;- ConversationManager::get_instance().add_history_collection(model["history_collection"]);return Option<nlohmann::json>(model);}@@ -52,10 +69,10 @@ Option<nlohmann::json> ConversationModelManager::delete_model_unsafe(const std::auto model_key = get_model_key(model_id);bool delete_op = store->remove(model_key);-- if(model.count("history_collection") != 0) {- ConversationManager::get_instance().remove_history_collection(model["history_collection"].get<std::string>());+ if(!delete_op) {+ return Option<nlohmann::json>(500, "Error while deleting model from the store");}+models.erase(it);return Option<nlohmann::json>(model);}@@ -72,32 +89,41 @@ Option<nlohmann::json> ConversationModelManager::get_all_models() {Option<nlohmann::json> ConversationModelManager::update_model(const std::string& model_id, nlohmann::json model) {std::unique_lock lock(models_mutex);- auto validate_res = ConversationModel::validate_model(model);- if (!validate_res.ok()) {- return Option<nlohmann::json>(validate_res.code(), validate_res.error());- }-auto it = models.find(model_id);if (it == models.end()) {return Option<nlohmann::json>(404, "Model not found");}- model["id"] = model_id;+ nlohmann::json model_copy = it->second;++ for (auto& [key, value] : model.items()) {+ model_copy[key] = value;+ }++ auto validate_res = ConversationModel::validate_model(model_copy);+ if (!validate_res.ok()) {+ 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.dump(0));+ 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(it->second["history_collection"] != model["history_collection"]) {- ConversationManager::get_instance().remove_history_collection(it->second["history_collection"]);- ConversationManager::get_instance().add_history_collection(model["history_collection"]);+ 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;- return Option<nlohmann::json>(model);+ models[model_id] = model_copy;++ return Option<nlohmann::json>(model_copy);}Option<int> ConversationModelManager::init(Store* store) {@@ -118,18 +144,22 @@ Option<int> ConversationModelManager::init(Store* store) {// Migrate cloudflare models to new namespace convention, change namespace from `cf` to `cloudflare`if(EmbedderManager::get_model_namespace(model_json["model_name"]) == "cf") {- auto delete_op = delete_model(model_id);- if(!delete_op.ok()) {- return Option<int>(delete_op.code(), delete_op.error());+ 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();}-- model_json["model_name"] = "cloudflare/" + EmbedderManager::get_model_name_without_namespace(model_json["model_name"]);- auto add_res = add_model(model_json, model_id);+ auto add_res = add_model_unsafe(model_json, model_id);if(!add_res.ok()) {return Option<int>(add_res.code(), add_res.error());}}+// Migrate models that don't have a conversation collectionif(model_json.count("history_collection") == 0) {auto migrate_op = migrate_model(model_json);@@ -140,7 +170,7 @@ Option<int> ConversationModelManager::init(Store* store) {}models[model_id] = model_json;- ConversationManager::get_instance().add_history_collection(model_json["history_collection"].get<std::string>());+ ConversationManager::get_instance().initialize_history_collection(model_json["history_collection"].get<std::string>());loaded_models++;}@@ -166,8 +196,7 @@ Option<Collection*> ConversationModelManager::get_default_history_collection() {"fields": [{"name": "conversation_id",- "type": "string",- "facet": true+ "type": "string"},{"name": "role",@@ -183,6 +212,10 @@ Option<Collection*> ConversationModelManager::get_default_history_collection() {"name": "timestamp","type": "int32","sort": true+ },+ {+ "name": "model_id",+ "type": "string"}]})"_json;@@ -213,3 +246,14 @@ Option<nlohmann::json> ConversationModelManager::migrate_model(nlohmann::json mo}return Option<nlohmann::json>(model);}++std::unordered_set<std::string> ConversationModelManager::get_history_collections() {+ std::unordered_set<std::string> collections;+ for(auto& [id, model] : models) {+ if(model.find("history_collection") == model.end()) {+ continue;+ }+ collections.insert(model["history_collection"].get<std::string>());+ }+ return collections;+}\ No newline at end of filediff --git a/src/core_api.cpp b/src/core_api.cppindex 43998b22..a0b99ea4 100644--- a/src/core_api.cpp+++ b/src/core_api.cpp@@ -973,7 +973,7 @@ bool post_multi_search(const std::shared_ptr<http_req>& req, const std::shared_pnew_conversation_history.push_back(formatted_answer_op.get());std::string conversation_id = conversation_history ? orig_req_params["conversation_id"] : "";- auto add_conversation_op = ConversationManager::get_instance().add_conversation(new_conversation_history, conversation_model["history_collection"], conversation_id);+ auto add_conversation_op = ConversationManager::get_instance().add_conversation(new_conversation_history, conversation_model, conversation_id);if(!add_conversation_op.ok()) {res->set_400(add_conversation_op.error());return false;@@ -2981,73 +2981,6 @@ bool post_proxy(const std::shared_ptr<http_req>& req, const std::shared_ptr<http}-bool get_conversation(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {- std::string conversation_id = req->params["id"];-- auto conversation_op = ConversationManager::get_instance().get_conversation(conversation_id);-- if(!conversation_op.ok()) {- res->set(conversation_op.code(), conversation_op.error());- return false;- }-- res->set_200(conversation_op.get().dump());- return true;-}---bool del_conversation(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {- std::string conversation_id = req->params["id"];-- auto conversation_op = ConversationManager::get_instance().delete_conversation(conversation_id);-- if(!conversation_op.ok()) {- res->set(conversation_op.code(), conversation_op.error());- return false;- }-- res->set_200(conversation_op.get().dump());- return true;-}--bool get_conversations(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {- auto conversations_op = ConversationManager::get_instance().get_all_conversations();-- if(!conversations_op.ok()) {- res->set(conversations_op.code(), conversations_op.error());- return false;- }-- res->set_200(conversations_op.get().dump());- return true;-}--bool put_conversation(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {- std::string conversation_id = req->params["id"];-- nlohmann::json req_json;-- try {- req_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;- }-- req_json["id"] = conversation_id;-- auto conversation_op = ConversationManager::get_instance().update_conversation(req_json);-- if(!conversation_op.ok()) {- res->set(conversation_op.code(), conversation_op.error());- return false;- }-- res->set_200(conversation_op.get().dump());- return true;-}-bool post_conversation_model(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {nlohmann::json req_json;diff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cppindex 93e4263c..6e554a1f 100644--- a/src/main/typesense_server.cpp+++ b/src/main/typesense_server.cpp@@ -102,12 +102,7 @@ void master_server_routes() {server->get("/conversations/models/:id", get_conversation_model);server->put("/conversations/models/:id", put_conversation_model);server->del("/conversations/models/:id", del_conversation_model);-- server->get("/conversations", get_conversations);- server->get("/conversations/:id", get_conversation);- server->del("/conversations/:id", del_conversation);- server->put("/conversations/:id", put_conversation);-+server->get("/limits", get_rate_limits);server->get("/limits/active", get_active_throttles);server->get("/limits/exceeds", get_limit_exceed_counts);diff --git a/test/collection_vector_search_test.cpp b/test/collection_vector_search_test.cppindex cdc98e91..920c0b0f 100644--- a/test/collection_vector_search_test.cpp+++ b/test/collection_vector_search_test.cpp@@ -20,7 +20,6 @@ protected:std::vector<std::string> query_fields;std::vector<sort_by> sort_fields;-void setupCollection() {std::string state_dir_path = "/tmp/typesense_test/collection_vector_search";LOG(INFO) << "Truncating and creating: " << state_dir_path;@@ -51,10 +50,13 @@ protected:"name": "timestamp","type": "int32","sort": true+ },+ {+ "name": "model_id",+ "type": "string"}]})"_json;-collectionManager.create_collection(schema_json);}@@ -3165,8 +3167,8 @@ TEST_F(CollectionVectorTest, TestQAConversation) {auto conversation_model_config = R"({"model_name": "openai/gpt-3.5-turbo",- "max_bytes: 1000,- "history_collection": "conversation_store",+ "max_bytes": 1000,+ "history_collection": "conversation_store"})"_json;conversation_model_config["api_key"] = api_key;@@ -3178,7 +3180,6 @@ TEST_F(CollectionVectorTest, TestQAConversation) {auto coll = collection_create_op.get();auto model_add_op = ConversationModelManager::add_model(conversation_model_config);-ASSERT_TRUE(model_add_op.ok());auto add_op = coll->add(R"({@@ -3215,8 +3216,7 @@ TEST_F(CollectionVectorTest, TestQAConversation) {0, 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"]);-+ true, 0, max_score, 100, 0, 0, "exhaustive", 30000, 2, "", {}, {}, "right_to_left", true, true, true, model_add_op.get()["id"].get<std::string>());ASSERT_TRUE(results_op.ok());auto results = results_op.get();@@ -3788,9 +3788,9 @@ TEST_F(CollectionVectorTest, InvalidMultiSearchConversation) {TEST_F(CollectionVectorTest, TestMigratingConversationModel) {auto conversation_model_config = R"({+ "id": "0","model_name": "openai/gpt-3.5-turbo",- "max_bytes": 1000,- "history_collection": "conversation_store"+ "max_bytes": 1000})"_json;if (std::getenv("api_key") == nullptr) {@@ -3799,6 +3799,7 @@ TEST_F(CollectionVectorTest, TestMigratingConversationModel) {}auto api_key = std::string(std::getenv("api_key"));+ conversation_model_config["api_key"] = api_key;auto migrate_res = ConversationModelManager::migrate_model(conversation_model_config);ASSERT_TRUE(migrate_res.ok());@@ -3809,6 +3810,54 @@ TEST_F(CollectionVectorTest, TestMigratingConversationModel) {ASSERT_TRUE(collection != nullptr);}+TEST_F(CollectionVectorTest, TestPartiallyUpdateConversationModel) {+ auto schema_json =+ R"({+ "name": "Products",+ "fields": [+ {"name": "product_name", "type": "string", "infix": true},+ {"name": "category", "type": "string"},+ {"name": "embedding", "type":"float[]", "embed":{"from": ["product_name", "category"], "model_config": {"model_name": "ts/e5-small"}}}+ ]+ })"_json;++ EmbedderManager::set_model_dir("/tmp/typesense_test/models");++ if (std::getenv("api_key") == nullptr) {+ LOG(INFO) << "Skipping test as api_key is not set.";+ return;+ }++ auto api_key = std::string(std::getenv("api_key"));++ auto conversation_model_config = R"({+ "model_name": "openai/gpt-3.5-turbo",+ "max_bytes: 1000,+ "history_collection": "conversation_store",+ })"_json;++ conversation_model_config["api_key"] = api_key;++ auto collection_create_op = collectionManager.create_collection(schema_json);++ ASSERT_TRUE(collection_create_op.ok());++ auto coll = collection_create_op.get();++ auto model_add_op = ConversationModelManager::add_model(conversation_model_config);+ ASSERT_TRUE(model_add_op.ok());++ auto model_id = model_add_op.get()["id"];++ auto update_op = ConversationModelManager::update_model(model_id, R"({"max_bytes": 2000})"_json);+ ASSERT_TRUE(update_op.ok());++ auto updated_model = update_op.get();+ ASSERT_EQ(updated_model["max_bytes"], 2000);+ ASSERT_EQ(updated_model["history_collection"], "conversation_store");+ ASSERT_EQ(updated_model["model_name"], "openai/gpt-3.5-turbo");+}+TEST_F(CollectionVectorTest, TestVectorQueryQs) {auto schema_json =diff --git a/test/conversation_test.cpp b/test/conversation_test.cppindex d8096e50..729bfd53 100644--- a/test/conversation_test.cpp+++ b/test/conversation_test.cpp@@ -7,6 +7,11 @@ class ConversationTest : public ::testing::Test {CollectionManager & collectionManager = CollectionManager::get_instance();Store* store;std::atomic<bool> quit = false;+ nlohmann::json model = R"({+ "id": "0",+ "history_collection": "conversation_store",+ "ttl": 86400+ })"_json;void SetUp() override {std::string state_dir_path = "/tmp/typesense_test/conversation_test";system(("rm -rf "+state_dir_path+" && mkdir -p "+state_dir_path).c_str());@@ -35,16 +40,19 @@ class ConversationTest : public ::testing::Test {"name": "timestamp","type": "int32","sort": true+ },+ {+ "name": "model_id",+ "type": "string"}]})"_json;collectionManager.create_collection(schema_json);- ConversationManager::get_instance().add_history_collection("conversation_store");+ ConversationModelManager::insert_model_for_testing("0", model);}void TearDown() override {- ConversationManager::get_instance().remove_history_collection("conversation_store");collectionManager.dispose();delete store;}@@ -53,7 +61,7 @@ class ConversationTest : public ::testing::Test {TEST_F(ConversationTest, CreateConversation) {nlohmann::json conversation = nlohmann::json::array();- auto create_res = ConversationManager::get_instance().add_conversation(conversation, "conversation_store");+ auto create_res = ConversationManager::get_instance().add_conversation(conversation, model);ASSERT_TRUE(create_res.ok());}@@ -77,12 +85,12 @@ TEST_F(ConversationTest, AppendConversation) {nlohmann::json message = nlohmann::json::object();message["user"] = "Hello";conversation.push_back(message);- auto create_res = ConversationManager::get_instance().add_conversation(conversation, "conversation_store");+ auto create_res = ConversationManager::get_instance().add_conversation(conversation, model);ASSERT_TRUE(create_res.ok());std::string conversation_id = create_res.get();-- auto append_res = ConversationManager::get_instance().add_conversation(conversation, "conversation_store", conversation_id);+ LOG(INFO) << conversation_id;+ auto append_res = ConversationManager::get_instance().add_conversation(conversation, model, conversation_id);ASSERT_TRUE(append_res.ok());ASSERT_EQ(append_res.get(), conversation_id);@@ -101,14 +109,14 @@ TEST_F(ConversationTest, AppendInvalidConversation) {nlohmann::json conversation = nlohmann::json::array();nlohmann::json message = nlohmann::json::object();message["user"] = "Hello";- auto create_res = ConversationManager::get_instance().add_conversation(conversation, "conversation_store");+ auto create_res = ConversationManager::get_instance().add_conversation(conversation, model);ASSERT_TRUE(create_res.ok());std::string conversation_id = create_res.get();message = "invalid";- auto append_res = ConversationManager::get_instance().add_conversation(message, "conversation_store", conversation_id);+ auto append_res = ConversationManager::get_instance().add_conversation(message, model, conversation_id);ASSERT_FALSE(append_res.ok());ASSERT_EQ(append_res.code(), 400);ASSERT_EQ(append_res.error(), "Conversation is not an array");@@ -119,7 +127,7 @@ TEST_F(ConversationTest, DeleteConversation) {nlohmann::json message = nlohmann::json::object();message["user"] = "Hello";conversation.push_back(message);- auto create_res = ConversationManager::get_instance().add_conversation(conversation, "conversation_store");+ auto create_res = ConversationManager::get_instance().add_conversation(conversation, model);ASSERT_TRUE(create_res.ok());std::string conversation_id = create_res.get();LOG(INFO) << conversation_id;@@ -187,7 +195,7 @@ TEST_F(ConversationTest, TestConversationExpire) {nlohmann::json message = nlohmann::json::object();message["user"] = "Hello";conversation.push_back(message);- auto create_res = ConversationManager::get_instance().add_conversation(conversation, "conversation_store");+ auto create_res = ConversationManager::get_instance().add_conversation(conversation, model);ASSERT_TRUE(create_res.ok());std::string conversation_id = create_res.get();@@ -213,128 +221,6 @@ TEST_F(ConversationTest, TestConversationExpire) {ConversationManager::get_instance()._set_ttl_offset(0);}-TEST_F(ConversationTest, TestRemoveConversationCollection) {- nlohmann::json schema_json = R"({- "name": "conversation_store2",- "fields": [- {- "name": "conversation_id",- "type": "string",- "facet": true- },- {- "name": "role",- "type": "string"- },- {- "name": "message",- "type": "string"- },- {- "name": "timestamp",- "type": "int32",- "sort": true- }- ]- })"_json;- LOG(INFO) << "Creating collection";- collectionManager.create_collection(schema_json);- ConversationManager::get_instance().add_history_collection("conversation_store2");- LOG(INFO) << "Collection created";- nlohmann::json conversation = nlohmann::json::array();- nlohmann::json message = nlohmann::json::object();- message["user"] = "Hello";- conversation.push_back(message);-- LOG(INFO) << "Adding conversation";- auto create_res = ConversationManager::get_instance().add_conversation(conversation, "conversation_store2");- ASSERT_TRUE(create_res.ok());-- LOG(INFO) << "Getting conversation";- auto get_res = ConversationManager::get_instance().get_conversation(create_res.get());- ASSERT_TRUE(get_res.ok());- ASSERT_EQ(get_res.get()["conversation"].size(), 1);--- LOG(INFO) << "Removing collection";- auto remove_res = ConversationManager::get_instance().remove_history_collection("conversation_store2");- ASSERT_TRUE(remove_res.ok());-- LOG(INFO) << "Getting conversation";- get_res = ConversationManager::get_instance().get_conversation(create_res.get());- ASSERT_FALSE(get_res.ok());- ASSERT_EQ(get_res.code(), 404);- ASSERT_EQ(get_res.error(), "Conversation not found");-- LOG(INFO) << "Adding collection again";- ConversationManager::get_instance().add_history_collection("conversation_store2");--- LOG(INFO) << "Adding conversation";- get_res = ConversationManager::get_instance().get_conversation(create_res.get());- ASSERT_TRUE(get_res.ok());- ASSERT_EQ(get_res.get()["conversation"].size(), 1);-- LOG(INFO) << "Dropping collection";- collectionManager.drop_collection("conversation_store2");-- LOG(INFO) << "Getting conversation";- get_res = ConversationManager::get_instance().get_conversation(create_res.get());- ASSERT_FALSE(get_res.ok());- ASSERT_EQ(get_res.code(), 404);- ASSERT_EQ(get_res.error(), "Conversation not found");-- LOG(INFO) << "TestRemoveConversationCollection done";-}--TEST_F(ConversationTest, TestMultipleRefConversationCollection) {- nlohmann::json schema_json = R"({- "name": "conversation_store2",- "fields": [- {- "name": "conversation_id",- "type": "string",- "facet": true- },- {- "name": "role",- "type": "string"- },- {- "name": "message",- "type": "string"- },- {- "name": "timestamp",- "type": "int32",- "sort": true- }- ]- })"_json;-- collectionManager.create_collection(schema_json);- ConversationManager::get_instance().add_history_collection("conversation_store2");- ConversationManager::get_instance().add_history_collection("conversation_store2");-- nlohmann::json conversation = nlohmann::json::array();- nlohmann::json message = nlohmann::json::object();- message["user"] = "Hello";- conversation.push_back(message);-- auto create_res = ConversationManager::get_instance().add_conversation(conversation, "conversation_store2");- ASSERT_TRUE(create_res.ok());-- auto get_res = ConversationManager::get_instance().get_conversation(create_res.get());- ASSERT_TRUE(get_res.ok());- ASSERT_EQ(get_res.get()["conversation"].size(), 1);-- auto remove_res = ConversationManager::get_instance().remove_history_collection("conversation_store2");- ASSERT_TRUE(remove_res.ok());-- get_res = ConversationManager::get_instance().get_conversation(create_res.get());- ASSERT_TRUE(get_res.ok());- ASSERT_EQ(get_res.get()["conversation"].size(), 1);-}TEST_F(ConversationTest, TestInvalidConversationCollection) {nlohmann::json schema_json = R"({@@ -347,9 +233,10 @@ TEST_F(ConversationTest, TestInvalidConversationCollection) {]})"_json;- collectionManager.create_collection(schema_json);- auto res = ConversationManager::get_instance().add_history_collection("conversation_store2");+ auto coll = collectionManager.create_collection(schema_json).get();+ auto res = ConversationManager::get_instance().validate_conversation_store_schema(coll);ASSERT_FALSE(res.ok());ASSERT_EQ(res.code(), 400);ASSERT_EQ(res.error(), "Schema is missing `conversation_id` field");}+diff --git a/test/core_api_utils_test.cpp b/test/core_api_utils_test.cppindex 3be8b6f5..2bd68a20 100644--- a/test/core_api_utils_test.cpp+++ b/test/core_api_utils_test.cpp@@ -54,7 +54,6 @@ protected:})"_json;collectionManager.create_collection(schema_json);- ConversationManager::get_instance().add_history_collection("conversation_store");}virtual void SetUp() {@@ -1341,20 +1340,6 @@ TEST_F(CoreAPIUtilsTest, TestGetConversations) {auto req = std::make_shared<http_req>();auto resp = std::make_shared<http_res>(nullptr);- req->params["id"] = "0";-- get_conversations(req, resp);-- ASSERT_EQ(200, resp->status_code);-- nlohmann::json res_json = nlohmann::json::parse(resp->body);- ASSERT_TRUE(res_json.is_array());- ASSERT_EQ(0, res_json.size());-- get_conversation(req, resp);-- ASSERT_EQ(404, resp->status_code);-auto schema_json =R"({"name": "Products",@@ -1432,41 +1417,11 @@ TEST_F(CoreAPIUtilsTest, TestGetConversations) {ASSERT_TRUE(results_op.ok());- get_conversations(req, resp);-- ASSERT_EQ(200, resp->status_code);-- res_json = nlohmann::json::parse(resp->body);-- ASSERT_TRUE(res_json.is_array());- ASSERT_EQ(1, res_json.size());-- ASSERT_TRUE(res_json[0]["conversation"].is_array());- ASSERT_EQ(2, res_json[0]["conversation"].size());- ASSERT_EQ("how many products are there for clothing category?", res_json[0]["conversation"][0]["user"].get<std::string>());-- req->params["id"] = res_json[0]["id"].get<std::string>();- get_conversation(req, resp);-- ASSERT_EQ(200, resp->status_code);-- res_json = nlohmann::json::parse(resp->body);-- ASSERT_TRUE(res_json.is_object());- ASSERT_TRUE(res_json["conversation"].is_array());- ASSERT_EQ(2, res_json["conversation"].size());- ASSERT_EQ("how many products are there for clothing category?", res_json["conversation"][0]["user"].get<std::string>());-- del_conversation(req, resp);- ASSERT_EQ(200, resp->status_code);-- get_conversations(req, resp);- ASSERT_EQ(200, resp->status_code);-- res_json = nlohmann::json::parse(resp->body);- ASSERT_TRUE(res_json.is_array());- ASSERT_EQ(0, res_json.size());+ 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_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);}