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
Add support for conversation model endpoints + multi search with conversations
ozanarmagan405b0037da43
diff --git a/include/collection.h b/include/collection.hindex 4bb6a5a3..3c460349 100644--- a/include/collection.h+++ b/include/collection.h@@ -289,7 +289,6 @@ private:Option<uint32_t> get_reference_doc_id(const std::string& ref_collection_name, const uint32_t& seq_id) const;- static void hide_credential(nlohmann::json& json, const std::string& credential_name);public:@@ -319,9 +318,6 @@ public:static constexpr const char* COLLECTION_SEPARATORS = "token_separators";static constexpr const char* REFERENCE_HELPER_FIELD_SUFFIX = "_sequence_id";-- static constexpr const char* COLLECTION_QA = "qa";-// methodsCollection() = delete;@@ -331,7 +327,7 @@ public:const std::string& default_sorting_field,const float max_memory_ratio, const std::string& fallback_field_type,const std::vector<std::string>& symbols_to_index, const std::vector<std::string>& token_separators,- const bool enable_nested_fields, const nlohmann::json& qa);+ const bool enable_nested_fields);~Collection();@@ -500,6 +496,7 @@ public:const std::string& stopwords_set="",const std::vector<std::string>& facet_return_parent = {},const bool converstaion = false,+ const int conversation_model_id = -1,const std::string& system_prompt = "",int conversation_id = -1) const;@@ -611,6 +608,8 @@ public:Option<uint32_t> get_sort_indexed_field_value(const std::string& field_name, const uint32_t& seq_id) const;+ static void hide_credential(nlohmann::json& json, const std::string& credential_name);+friend class filter_result_iterator_t;};diff --git a/include/collection_manager.h b/include/collection_manager.hindex 3be6a06e..828421fc 100644--- a/include/collection_manager.h+++ b/include/collection_manager.h@@ -158,7 +158,7 @@ public:const std::string& fallback_field_type = "",const std::vector<std::string>& symbols_to_index = {},const std::vector<std::string>& token_separators = {},- const bool enable_nested_fields = false, const nlohmann::json& qa = nlohmann::json::object());+ const bool enable_nested_fields = false);locked_resource_view_t<Collection> get_collection(const std::string & collection_name) const;diff --git a/include/conversation_manager.h b/include/conversation_manager.hindex e94ea249..3e3787b1 100644--- a/include/conversation_manager.h+++ b/include/conversation_manager.h@@ -2,8 +2,11 @@#include <string>#include <unordered_map>+#include <shared_mutex>+#include <mutex>#include <json.hpp>#include "option.h"+#include "store.h"class ConversationManager {@@ -17,7 +20,16 @@ class ConversationManager {static Option<nlohmann::json> delete_conversation(int conversation_id);static Option<nlohmann::json> get_all_conversations();static constexpr size_t MAX_TOKENS = 3000;+ static Option<int> init(Store* store);private:static inline std::unordered_map<int, nlohmann::json> conversations;static inline int conversation_id = 0;+ static inline std::shared_mutex conversations_mutex;++ static constexpr char* CONVERSATION_NEXT_ID = "$CNVN";+ static constexpr char* CONVERSATION_RPEFIX = "$CNVP";++ static inline Store* store;++ static const std::string get_conversation_key(int conversation_id);};\ No newline at end of filediff --git a/include/qa_model.h b/include/conversation_model.hsimilarity index 92%rename from include/qa_model.hrename to include/conversation_model.hindex 5f611e2b..24644564 100644--- a/include/qa_model.h+++ b/include/conversation_model.h@@ -6,9 +6,9 @@-class QAModel {+class ConversationModel {public:- virtual ~QAModel() {};+ virtual ~ConversationModel() {};static Option<std::string> get_answer(const std::string& context, const std::string& prompt, const std::string& system_prompt, const nlohmann::json& model_config);static Option<nlohmann::json> parse_conversation_history(const nlohmann::json& conversation, const nlohmann::json& model_config);static Option<bool> validate_model(const nlohmann::json& model_config);@@ -19,7 +19,7 @@ class QAModel {};-class OpenAIQAModel : public QAModel {+class OpenAIConversationModel : public ConversationModel {public:static Option<std::string> get_answer(const std::string& context, const std::string& prompt, const std::string& system_prompt, const nlohmann::json& model_config);static Option<bool> validate_model(const nlohmann::json& model_config);@@ -31,7 +31,7 @@ class OpenAIQAModel : public QAModel {Rewrite the follow-up question on top of a human-assistant conversation history as a standalone question that encompasses all pertinent context.)";// prevent instantiation- OpenAIQAModel() = delete;+ OpenAIConversationModel() = delete;private:static constexpr char* OPENAI_LIST_MODELS = "https://api.openai.com/v1/models";static constexpr char* OPENAI_CHAT_COMPLETION = "https://api.openai.com/v1/chat/completions";diff --git a/include/conversation_model_manager.h b/include/conversation_model_manager.hnew file mode 100644index 00000000..c695c552--- /dev/null+++ b/include/conversation_model_manager.h@@ -0,0 +1,37 @@+#pragma once++#include <shared_mutex>+#include <mutex>+#include <unordered_map>+#include <json.hpp>+#include <option.h>+#include "store.h"+++class ConversationModelManager+{+ public:+ ConversationModelManager() = delete;+ ConversationModelManager(const ConversationModelManager&) = delete;+ ConversationModelManager(ConversationModelManager&&) = delete;+ ConversationModelManager& operator=(const ConversationModelManager&) = delete;++ static Option<nlohmann::json> get_model(const uint32_t model_id);+ static Option<nlohmann::json> add_model(nlohmann::json model);+ static Option<nlohmann::json> delete_model(const uint32_t model_id);+ static Option<nlohmann::json> update_model(const uint32_t model_id, nlohmann::json model);+ static Option<nlohmann::json> get_all_models();+++ static Option<int> init(Store* store);+ private:+ static inline std::unordered_map<uint32_t, nlohmann::json> models;+ static inline uint32_t model_id = 0;+ static inline std::shared_mutex models_mutex;++ static constexpr char* MODEL_NEXT_ID = "$CVMN";+ static constexpr char* MODEL_KEY_PREFIX = "$CVMP";++ static inline Store* store;+ static const std::string get_model_key(uint32_t model_id);+};\ No newline at end of filediff --git a/include/core_api.h b/include/core_api.hindex f8891e10..a44940a1 100644--- a/include/core_api.h+++ b/include/core_api.h@@ -191,3 +191,14 @@ bool get_conversations(const std::shared_ptr<http_req>& req, const std::shared_pbool 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 post_conversation_model(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);++bool get_conversation_model(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);++bool del_conversation_model(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);++bool get_conversation_models(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);++bool put_conversation_model(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);\ No newline at end of filediff --git a/src/collection.cpp b/src/collection.cppindex 4373e3f2..04d7f141 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -19,8 +19,9 @@#include "vector_query_ops.h"#include "text_embedder_manager.h"#include "stopwords_manager.h"-#include "qa_model.h"+#include "conversation_model.h"#include "conversation_manager.h"+#include "conversation_model_manager.h"const std::string override_t::MATCH_EXACT = "exact";const std::string override_t::MATCH_CONTAINS = "contains";@@ -46,14 +47,14 @@ Collection::Collection(const std::string& name, const uint32_t collection_id, coconst float max_memory_ratio, const std::string& fallback_field_type,const std::vector<std::string>& symbols_to_index,const std::vector<std::string>& token_separators,- const bool enable_nested_fields, const nlohmann::json& qa):+ const bool enable_nested_fields):name(name), collection_id(collection_id), created_at(created_at),next_seq_id(next_seq_id), store(store),fields(fields), default_sorting_field(default_sorting_field), enable_nested_fields(enable_nested_fields),max_memory_ratio(max_memory_ratio),fallback_field_type(fallback_field_type), dynamic_fields({}),symbols_to_index(to_char_array(symbols_to_index)), token_separators(to_char_array(token_separators)),- index(init_index()), qa(qa) {+ index(init_index()) {this->num_documents = 0;}@@ -289,13 +290,6 @@ nlohmann::json Collection::get_summary_json() const {json_response["fields"] = fields_arr;json_response["default_sorting_field"] = default_sorting_field;- json_response["qa"] = qa;- hide_credential(json_response["qa"], "api_key");- hide_credential(json_response["qa"], "access_token");- hide_credential(json_response["qa"], "refresh_token");- hide_credential(json_response["qa"], "client_id");- hide_credential(json_response["qa"], "client_secret");- hide_credential(json_response["qa"], "project_id");return json_response;}@@ -1398,6 +1392,7 @@ Option<nlohmann::json> Collection::search(std::string raw_query,const std::string& stopwords_set,const std::vector<std::string>& facet_return_parent,const bool conversation,+ const int conversation_model_id,const std::string& system_prompt,int conversation_id) const {@@ -1482,6 +1477,18 @@ Option<nlohmann::json> Collection::search(std::string raw_query,size_t num_embed_fields = 0;std::string query = raw_query;+ if(conversation) {+ if(conversation_model_id == -1) {+ return Option<nlohmann::json>(400, "Conversation is enabled but no conversation model ID is provided.");+ }++ auto conversation_model_op = ConversationModelManager::get_model(conversation_model_id);++ if(!conversation_model_op.ok()) {+ return Option<nlohmann::json>(400, conversation_model_op.error());+ }+ }+if(conversation_id >= 0) {if(!conversation) {return Option<nlohmann::json>(400, "Conversation ID provided but conversation is not enabled for this collection.");@@ -1498,13 +1505,13 @@ Option<nlohmann::json> Collection::search(std::string raw_query,return Option<nlohmann::json>(400, truncate_conversation_history.error());}- auto standalone_question_op = QAModel::get_standalone_question(truncate_conversation_history.get(), raw_query, this->qa);+ auto conversation_model_op = ConversationModelManager::get_model(conversation_model_id);++ auto standalone_question_op = ConversationModel::get_standalone_question(truncate_conversation_history.get(), raw_query, conversation_model_op.get());if(!standalone_question_op.ok()) {return Option<nlohmann::json>(400, standalone_question_op.error());}query = standalone_question_op.get();-- LOG(INFO) << "Conversation ID: " << conversation_id << ", query: " << query;}for(size_t i = 0; i < raw_search_fields.size(); i++) {@@ -1610,20 +1617,6 @@ Option<nlohmann::json> Collection::search(std::string raw_query,return Option<nlohmann::json>(400, error);}- if(conversation) {- if(vector_query.field_name.empty()) {- std::string error = "Conversation search is only supported for semantic and hybrid search.";- return Option<nlohmann::json>(400, error);- }-- if(this->qa.empty()) {- std::string error = "Conversation search is not enabled for this collection.";- return Option<nlohmann::json>(400, error);- }- }---for(const std::string & field_name: processed_search_fields) {field search_field = search_schema.at(field_name);if(!search_field.index) {@@ -2345,20 +2338,22 @@ Option<nlohmann::json> Collection::search(std::string raw_query,}}+ auto conversation_model = ConversationModelManager::get_model(conversation_id).get();+bool has_conversation_history = conversation_id >= 0;- auto qa_op = QAModel::get_answer(docs_array.dump(0), raw_query, system_prompt, this->qa);+ auto qa_op = ConversationModel::get_answer(docs_array.dump(0), raw_query, system_prompt, conversation_model);if(!qa_op.ok()) {return Option<nlohmann::json>(qa_op.code(), qa_op.error());}result["conversation"]["answer"] = qa_op.get();result["conversation"]["conversation_id"] = conversation_id;- auto formatted_question_op = QAModel::format_question(raw_query, this->qa);+ auto formatted_question_op = ConversationModel::format_question(raw_query, conversation_model);if(!formatted_question_op.ok()) {return Option<nlohmann::json>(formatted_question_op.code(), formatted_question_op.error());}- auto formatted_answer_op = QAModel::format_answer(qa_op.get(), this->qa);+ auto formatted_answer_op = ConversationModel::format_answer(qa_op.get(), conversation_model);if(!formatted_answer_op.ok()) {return Option<nlohmann::json>(formatted_answer_op.code(), formatted_answer_op.error());}@@ -4699,13 +4694,6 @@ Option<bool> Collection::validate_alter_payload(nlohmann::json& schema_changes,return Option<bool>(400, "Only `fields` can be updated at the moment.");}- if(schema_changes.count("qa") != 0) {- auto qa_op = QAModel::validate_model(schema_changes["qa"]);- if(!qa_op.ok()) {- return qa_op;- }- }-const std::string err_msg = "The `fields` value should be an array of objects containing ""the field `name` and other properties.";diff --git a/src/collection_manager.cpp b/src/collection_manager.cppindex 1cac95fc..5addcfa3 100644--- a/src/collection_manager.cpp+++ b/src/collection_manager.cpp@@ -9,7 +9,7 @@#include "logger.h"#include "magic_enum.hpp"#include "stopwords_manager.h"-#include "qa_model.h"+#include "conversation_model.h"constexpr const size_t CollectionManager::DEFAULT_NUM_MEMORY_SHARDS;@@ -128,12 +128,6 @@ Collection* CollectionManager::init_collection(const nlohmann::json & collectionstd::vector<std::string> symbols_to_index;std::vector<std::string> token_separators;- nlohmann::json qa = nlohmann::json::object();-- if(collection_meta.count(Collection::COLLECTION_QA) != 0) {- qa = collection_meta[Collection::COLLECTION_QA];- }-if(collection_meta.count(Collection::COLLECTION_SYMBOLS_TO_INDEX) != 0) {symbols_to_index = collection_meta[Collection::COLLECTION_SYMBOLS_TO_INDEX].get<std::vector<std::string>>();}@@ -155,7 +149,7 @@ Collection* CollectionManager::init_collection(const nlohmann::json & collectionfallback_field_type,symbols_to_index,token_separators,- enable_nested_fields, qa);+ enable_nested_fields);return collection;}@@ -423,8 +417,7 @@ Option<Collection*> CollectionManager::create_collection(const std::string& nameconst std::string& fallback_field_type,const std::vector<std::string>& symbols_to_index,const std::vector<std::string>& token_separators,- const bool enable_nested_fields,- const nlohmann::json& qa) {+ const bool enable_nested_fields) {std::unique_lock lock(coll_create_mutex);if(store->contains(Collection::get_meta_key(name))) {@@ -458,13 +451,12 @@ Option<Collection*> CollectionManager::create_collection(const std::string& namecollection_meta[Collection::COLLECTION_SYMBOLS_TO_INDEX] = symbols_to_index;collection_meta[Collection::COLLECTION_SEPARATORS] = token_separators;collection_meta[Collection::COLLECTION_ENABLE_NESTED_FIELDS] = enable_nested_fields;- collection_meta[Collection::COLLECTION_QA] = qa;Collection* new_collection = new Collection(name, next_collection_id, created_at, 0, store, fields,default_sorting_field,this->max_memory_ratio, fallback_field_type,symbols_to_index, token_separators,- enable_nested_fields, qa);+ enable_nested_fields);next_collection_id++;rocksdb::WriteBatch batch;@@ -975,6 +967,7 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& reconst char *CONVERSATION = "conversation";const char *CONVERSATION_ID = "conversation_id";const char *SYSTEM_PROMPT = "system_prompt";+ const char *CONVERSATION_MODEL_ID = "conversation_model_id";// enrich params with values from embedded paramsfor(auto& item: embedded_params.items()) {@@ -1098,6 +1091,7 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& rebool conversation = false;size_t conversation_id = std::numeric_limits<size_t>::max();std::string system_prompt;+ size_t conversation_model_id = std::numeric_limits<size_t>::max();std::unordered_map<std::string, size_t*> unsigned_int_values = {{MIN_LEN_1TYPO, &min_len_1typo},@@ -1124,6 +1118,7 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& re{REMOTE_EMBEDDING_TIMEOUT_MS, &remote_embedding_timeout_ms},{REMOTE_EMBEDDING_NUM_TRIES, &remote_embedding_num_tries},{CONVERSATION_ID, &conversation_id},+ {CONVERSATION_MODEL_ID, &conversation_model_id},};std::unordered_map<std::string, std::string*> str_values = {@@ -1349,6 +1344,7 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& restopwords_set,facet_return_parent,conversation,+ (conversation_model_id == std::numeric_limits<size_t>::max()) ? -1 : static_cast<int>(conversation_model_id),system_prompt,(conversation_id == std::numeric_limits<size_t>::max()) ? -1 : static_cast<int>(conversation_id));@@ -1510,14 +1506,6 @@ Option<Collection*> CollectionManager::create_collection(nlohmann::json& req_jsoreturn Option<Collection*>(parse_op.code(), parse_op.error());}- if(req_json.count("qa") != 0) {- auto validate_qa_res = QAModel::validate_model(req_json["qa"]);- if(!validate_qa_res.ok()) {- return Option<Collection*>(validate_qa_res.code(), validate_qa_res.error());- }- } else {- req_json["qa"] = nlohmann::json::object();- }const auto created_at = static_cast<uint64_t>(std::time(nullptr));@@ -1526,7 +1514,7 @@ Option<Collection*> CollectionManager::create_collection(nlohmann::json& req_jsofallback_field_type,req_json[SYMBOLS_TO_INDEX],req_json[TOKEN_SEPARATORS],- req_json[ENABLE_NESTED_FIELDS], req_json["qa"]);+ req_json[ENABLE_NESTED_FIELDS]);}Option<bool> CollectionManager::load_collection(const nlohmann::json &collection_meta,diff --git a/src/conversation_manager.cpp b/src/conversation_manager.cppindex 641698ae..1a3784f4 100644--- a/src/conversation_manager.cpp+++ b/src/conversation_manager.cpp@@ -2,15 +2,28 @@Option<int> ConversationManager::create_conversation(const nlohmann::json& conversation) {+ std::unique_lock lock(conversations_mutex);if(!conversation.is_array()) {return Option<int>(400, "Conversation is not an array");}+ auto conversation_key = get_conversation_key(conversation_id);+ nlohmann::json conversation_store_json;+ conversation_store_json["id"] = conversation_id;+ conversation_store_json["conversation"] = conversation;+ bool insert_op = store->insert(conversation_key, conversation_store_json.dump(0));+ if(!insert_op) {+ return Option<int>(500, "Error while inserting conversation into the store");+ }+ store->increment(std::string(CONVERSATION_NEXT_ID), 1);+conversations[conversation_id] = conversation;+return Option<int>(conversation_id++);}Option<nlohmann::json> ConversationManager::get_conversation(int conversation_id) {+ std::shared_lock lock(conversations_mutex);auto conversation = conversations.find(conversation_id);if (conversation == conversations.end()) {return Option<nlohmann::json>(404, "Conversation not found");@@ -20,23 +33,36 @@ Option<nlohmann::json> ConversationManager::get_conversation(int conversation_id}Option<bool> ConversationManager::append_conversation(int conversation_id, const nlohmann::json& message) {- auto conversation = conversations.find(conversation_id);- if (conversation == conversations.end()) {+ std::unique_lock lock(conversations_mutex);+ auto conversation_it = conversations.find(conversation_id);+ if (conversation_it == conversations.end()) {return Option<bool>(404, "Conversation not found");}- if(!conversation->second.is_array()) {+ if(!conversation_it->second.is_array()) {return Option<bool>(400, "Conversation is not an array");}+ nlohmann::json conversation = nlohmann::json::object();+ conversation["id"] = conversation_id;+ conversation["conversation"] = conversation_it->second;+if(!message.is_array()) {- conversation->second.push_back(message);+ conversation["conversation"].push_back(message);} else {for(auto& m : message) {- conversation->second.push_back(m);+ conversation["conversation"].push_back(m);}}+ auto conversation_key = get_conversation_key(conversation_id);+ bool insert_op = store->insert(conversation_key, conversation.dump(0));+ if(!insert_op) {+ return Option<bool>(500, "Error while inserting conversation into the store");+ }++ conversations[conversation_id] = conversation["conversation"];+return Option<bool>(true);}@@ -63,6 +89,7 @@ Option<nlohmann::json> ConversationManager::truncate_conversation(nlohmann::json}Option<nlohmann::json> ConversationManager::delete_conversation(int conversation_id) {+ std::unique_lock lock(conversations_mutex);auto conversation = conversations.find(conversation_id);if (conversation == conversations.end()) {return Option<nlohmann::json>(404, "Conversation not found");@@ -70,11 +97,18 @@ Option<nlohmann::json> ConversationManager::delete_conversation(int conversationOption<nlohmann::json> conversation_res(*conversation);+ auto conversation_key = get_conversation_key(conversation_id);+ bool delete_op = store->remove(conversation_key);+ if(!delete_op) {+ return Option<nlohmann::json>(500, "Error while deleting conversation from the store");+ }+conversations.erase(conversation);return conversation_res;}Option<nlohmann::json> ConversationManager::get_all_conversations() {+ std::shared_lock lock(conversations_mutex);nlohmann::json all_conversations = nlohmann::json::array();for(auto& conversation : conversations) {nlohmann::json conversation_json;@@ -84,4 +118,38 @@ Option<nlohmann::json> ConversationManager::get_all_conversations() {}return Option<nlohmann::json>(all_conversations);+}++Option<int> ConversationManager::init(Store* store) {+ std::unique_lock lock(conversations_mutex);+ ConversationManager::store = store;++ std::string last_id_str;+ StoreStatus last_id_str_status = store->get(std::string(CONVERSATION_NEXT_ID), last_id_str);++ if(last_id_str_status == StoreStatus::ERROR) {+ return Option<int>(500, "Error while loading conversations next id from the store");+ } else if(last_id_str_status == StoreStatus::FOUND) {+ conversation_id = StringUtils::deserialize_uint32_t(last_id_str);+ } else {+ conversation_id = 0;+ }++ std::vector<std::string> conversation_strs;+ store->scan_fill(std::string(CONVERSATION_RPEFIX) + "_", std::string(CONVERSATION_RPEFIX) + "`", conversation_strs);++ size_t loaded_conversations = 0;++ for(auto& conversation_str : conversation_strs) {+ nlohmann::json conversation_json = nlohmann::json::parse(conversation_str);+ int conversation_id = conversation_json["id"];+ conversations[conversation_id] = conversation_json["conversation"];+ loaded_conversations++;+ }++ return Option<int>(loaded_conversations);+}++const std::string ConversationManager::get_conversation_key(int conversation_id) {+ return std::string(CONVERSATION_RPEFIX) + "_" + StringUtils::serialize_uint32_t(conversation_id);}\ No newline at end of filediff --git a/src/qa_model.cpp b/src/conversation_model.cppsimilarity index 85%rename from src/qa_model.cpprename to src/conversation_model.cppindex 0ba9f6cd..5480795e 100644--- a/src/qa_model.cpp+++ b/src/conversation_model.cpp@@ -1,4 +1,4 @@-#include "qa_model.h"+#include "conversation_model.h"#include "text_embedder_manager.h"#include "text_embedder_remote.h"#include "conversation_manager.h"@@ -12,7 +12,7 @@ const std::string get_model_namespace(const std::string& model_name) {}}-Option<bool> QAModel::validate_model(const nlohmann::json& model_config) {+Option<bool> ConversationModel::validate_model(const nlohmann::json& model_config) {// check model_name is exists and it is a stringif(model_config.count("model_name") == 0 || !model_config["model_name"].is_string()) {return Option<bool>(400, "Property `qa.model_name` is not provided or not a string.");@@ -20,65 +20,67 @@ Option<bool> QAModel::validate_model(const nlohmann::json& model_config) {const std::string model_namespace = get_model_namespace(model_config["model_name"].get<std::string>());if(model_namespace == "openai") {- return OpenAIQAModel::validate_model(model_config);+ return OpenAIConversationModel::validate_model(model_config);}return Option<bool>(400, "Model namespace `" + model_namespace + "` is not supported.");}-Option<std::string> QAModel::get_answer(const std::string& context, const std::string& prompt,+Option<std::string> 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<std::string>());if(model_namespace == "openai") {- return OpenAIQAModel::get_answer(context, prompt, system_prompt, model_config);+ return OpenAIConversationModel::get_answer(context, prompt, system_prompt, model_config);}throw Option<std::string>(400, "Model namespace " + model_namespace + " is not supported.");}-Option<nlohmann::json> QAModel::parse_conversation_history(const nlohmann::json& conversation, const nlohmann::json& model_config) {+Option<nlohmann::json> 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<std::string>());if(model_namespace == "openai") {- return OpenAIQAModel::parse_conversation_history(conversation);+ return OpenAIConversationModel::parse_conversation_history(conversation);}throw Option<nlohmann::json>(400, "Model namespace " + model_namespace + " is not supported.");}-Option<std::string> QAModel::get_standalone_question(const nlohmann::json& conversation_history, const std::string& question, const nlohmann::json& model_config) {+Option<std::string> 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<std::string>());if(model_namespace == "openai") {- return OpenAIQAModel::get_standalone_question(conversation_history, question, model_config);+ return OpenAIConversationModel::get_standalone_question(conversation_history, question, model_config);}throw Option<std::string>(400, "Model namespace " + model_namespace + " is not supported.");}-Option<nlohmann::json> QAModel::format_question(const std::string& message, const nlohmann::json& model_config) {+Option<nlohmann::json> 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<std::string>());if(model_namespace == "openai") {- return OpenAIQAModel::format_question(message);+ return OpenAIConversationModel::format_question(message);}throw Option<nlohmann::json>(400, "Model namespace " + model_namespace + " is not supported.");}-Option<nlohmann::json> QAModel::format_answer(const std::string& message, const nlohmann::json& model_config) {+Option<nlohmann::json> 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<std::string>());if(model_namespace == "openai") {- return OpenAIQAModel::format_answer(message);+ return OpenAIConversationModel::format_answer(message);}throw Option<nlohmann::json>(400, "Model namespace " + model_namespace + " is not supported.");}-Option<bool> OpenAIQAModel::validate_model(const nlohmann::json& model_config) {+Option<bool> OpenAIConversationModel::validate_model(const nlohmann::json& model_config) {if(model_config.count("api_key") == 0) {return Option<bool>(400, "API key is not provided");}@@ -163,10 +165,11 @@ Option<bool> OpenAIQAModel::validate_model(const nlohmann::json& model_config) {return Option<bool>(true);}-Option<std::string> OpenAIQAModel::get_answer(const std::string& context, const std::string& prompt,+Option<std::string> 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<std::string>());const std::string api_key = model_config["api_key"].get<std::string>();+std::unordered_map<std::string, std::string> headers;std::map<std::string, std::string> res_headers;headers["Authorization"] = "Bearer " + api_key;@@ -217,7 +220,7 @@ Option<std::string> OpenAIQAModel::get_answer(const std::string& context, constreturn Option<std::string>(json_res["choices"][0]["message"]["content"].get<std::string>());}-Option<nlohmann::json> OpenAIQAModel::parse_conversation_history(const nlohmann::json& conversation) {+Option<nlohmann::json> OpenAIConversationModel::parse_conversation_history(const nlohmann::json& conversation) {if(!conversation.is_array()) {return Option<nlohmann::json>(400, "Conversation is not an array");}@@ -244,7 +247,7 @@ Option<nlohmann::json> OpenAIQAModel::parse_conversation_history(const nlohmann:return Option<nlohmann::json>(messages);}-Option<std::string> OpenAIQAModel::get_standalone_question(const nlohmann::json& conversation_history,+Option<std::string> 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<std::string>());const std::string api_key = model_config["api_key"].get<std::string>();@@ -306,13 +309,13 @@ Option<std::string> OpenAIQAModel::get_standalone_question(const nlohmann::json&return Option<std::string>(json_res["choices"][0]["message"]["content"].get<std::string>());}-Option<nlohmann::json> OpenAIQAModel::format_question(const std::string& message) {+Option<nlohmann::json> OpenAIConversationModel::format_question(const std::string& message) {nlohmann::json json = nlohmann::json::object();json["user"] = message;return Option<nlohmann::json>(json);}-Option<nlohmann::json> OpenAIQAModel::format_answer(const std::string& message) {+Option<nlohmann::json> OpenAIConversationModel::format_answer(const std::string& message) {nlohmann::json json = nlohmann::json::object();json["assistant"] = message;return Option<nlohmann::json>(json);diff --git a/src/conversation_model_manager.cpp b/src/conversation_model_manager.cppnew file mode 100644index 00000000..2aea347d--- /dev/null+++ b/src/conversation_model_manager.cpp@@ -0,0 +1,115 @@+#include "conversation_model_manager.h"+#include "conversation_model.h"++Option<nlohmann::json> ConversationModelManager::get_model(const uint32_t model_id) {+ std::shared_lock lock(models_mutex);+ auto it = models.find(model_id);+ if (it == models.end()) {+ return Option<nlohmann::json>(404, "Model not found");+ }++ return Option<nlohmann::json>(it->second);+}++Option<nlohmann::json> ConversationModelManager::add_model(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());+ }+ model["id"] = model_id;++ 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;++ return Option<nlohmann::json>(model);+}++Option<nlohmann::json> ConversationModelManager::delete_model(const uint32_t model_id) {+ std::unique_lock lock(models_mutex);+ auto it = models.find(model_id);+ if (it == models.end()) {+ return Option<nlohmann::json>(404, "Model not found");+ }++ nlohmann::json model = it->second;++ auto model_key = get_model_key(model_id);+ bool delete_op = store->remove(model_key);++ models.erase(it);+ return Option<nlohmann::json>(model);+}++Option<nlohmann::json> ConversationModelManager::get_all_models() {+ std::shared_lock lock(models_mutex);+ nlohmann::json models_json = nlohmann::json::array();+ for (auto& [id, model] : models) {+ models_json.push_back(model);+ }++ return Option<nlohmann::json>(models_json);+}++Option<nlohmann::json> ConversationModelManager::update_model(const uint32_t 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;++ 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;++ return Option<nlohmann::json>(model);+}++Option<int> ConversationModelManager::init(Store* store) {+ std::unique_lock lock(models_mutex);+ ConversationModelManager::store = store;++ std::string last_id_str;+ StoreStatus last_id_str_status = store->get(std::string(MODEL_NEXT_ID), last_id_str);++ if(last_id_str_status == StoreStatus::ERROR) {+ return Option<int>(500, "Error while loading conversations next id from the store");+ } else if(last_id_str_status == StoreStatus::FOUND) {+ model_id = StringUtils::deserialize_uint32_t(last_id_str);+ } else {+ model_id = 0;+ }++ std::vector<std::string> model_strs;+ store->scan_fill(std::string(MODEL_KEY_PREFIX) + "_", std::string(MODEL_KEY_PREFIX) + "`", model_strs);++ int loaded_models = 0;+ for(auto& model_str : model_strs) {+ nlohmann::json model_json = nlohmann::json::parse(model_str);+ int model_id = model_json["id"];+ models[model_id] = model_json;+ loaded_models++;+ }++ return Option<int>(loaded_models);+}++const std::string ConversationModelManager::get_model_key(uint32_t model_id) {+ return std::string(MODEL_KEY_PREFIX) + "_" + StringUtils::serialize_uint32_t(model_id);+}\ No newline at end of filediff --git a/src/core_api.cpp b/src/core_api.cppindex 6be064fc..d747794f 100644--- a/src/core_api.cpp+++ b/src/core_api.cpp@@ -18,6 +18,8 @@#include "http_proxy.h"#include "include/stopwords_manager.h"#include "conversation_manager.h"+#include "conversation_model_manager.h"+#include "conversation_model.h"using namespace std::chrono_literals;@@ -523,6 +525,75 @@ bool post_multi_search(const std::shared_ptr<http_req>& req, const std::shared_p}}+ bool conversation = orig_req_params["conversation"] == "true";+ bool conversation_history = orig_req_params.find("conversation_id") != orig_req_params.end();+ std::string common_query;++ if(!conversation && conversation_history) {+ res->set_400("`conversation_id` can only be used if `conversation` is enabled.");+ return false;+ }++ if(conversation) {+ if(orig_req_params.find("q") == orig_req_params.end()) {+ res->set_400("`q` parameter has to be common for all searches if conversation is enabled.");+ return false;+ }++ if(orig_req_params.find("conversation_model_id") == orig_req_params.end()) {+ res->set_400("`conversation_model_id` is needed if conversation is enabled.");+ return false;+ }++ if(StringUtils::is_uint32_t(orig_req_params["conversation_model_id"])) {+ uint32_t conversation_model_id = std::stoul(orig_req_params["conversation_model_id"]);+ auto conversation_model = ConversationModelManager::get_model(conversation_model_id);++ if(!conversation_model.ok()) {+ res->set_400("`conversation_model_id` is invalid.");+ return false;+ }+ } else {+ res->set_400("`conversation_model_id` is invalid.");+ return false;+ }++ if(conversation_history) {+ if(!StringUtils::is_uint32_t(orig_req_params["conversation_id"])) {+ res->set_400("`conversation_id` is invalid.");+ return false;+ }++ uint32_t conversation_id = std::stoul(orig_req_params["conversation_id"]);++ auto conversation_history = ConversationManager::get_conversation(conversation_id);++ if(!conversation_history.ok()) {+ res->set_400("`conversation_id` is invalid.");+ return false;+ }+ }++ common_query = orig_req_params["q"];++ if(conversation_history) {+ auto conversation_model_id = std::stoul(orig_req_params["conversation_model_id"]);+ auto conversation_id = std::stoul(orig_req_params["conversation_id"]);+ auto conversation_model = ConversationModelManager::get_model(conversation_model_id).get();+ auto conversation_history = ConversationManager::get_conversation(conversation_id).get();+ auto generate_standalone_q = ConversationModel::get_standalone_question(conversation_history, common_query, conversation_model);++ if(!generate_standalone_q.ok()) {+ res->set_400(generate_standalone_q.error());+ return false;+ }++ orig_req_params["q"] = generate_standalone_q.get();+ }+ }+++for(size_t i = 0; i < searches.size(); i++) {auto& search_params = searches[i];@@ -533,6 +604,7 @@ bool post_multi_search(const std::shared_ptr<http_req>& req, const std::shared_preq->params = orig_req_params;+for(auto& search_item: search_params.items()) {if(search_item.key() == "cache_ttl") {// cache ttl can be applied only from an embedded key: cannot be a multi search param@@ -547,12 +619,28 @@ bool post_multi_search(const std::shared_ptr<http_req>& req, const std::shared_p}}+ if(req->params.count("conversation") != 0) {+ req->params.erase("conversation");+ }++ if(req->params.count("conversation_id") != 0) {+ req->params.erase("conversation_id");+ }++ if(req->params.count("conversation_model_id") != 0) {+ req->params.erase("conversation_model_id");+ }+std::string results_json_str;Option<bool> search_op = CollectionManager::do_search(req->params, req->embedded_params_vec[i],results_json_str, req->conn_ts);if(search_op.ok()) {- response["results"].push_back(nlohmann::json::parse(results_json_str));+ auto results_json = nlohmann::json::parse(results_json_str);+ if(conversation) {+ results_json["request_params"]["q"] = common_query;+ }+ response["results"].push_back(results_json);} else {if(search_op.code() == 408) {res->set(search_op.code(), search_op.error());@@ -566,6 +654,111 @@ bool post_multi_search(const std::shared_ptr<http_req>& req, const std::shared_p}}+ if(conversation) {+ nlohmann::json result_docs_arr = nlohmann::json::array();+ for(const auto& result : response["results"]) {+ nlohmann::json result_docs = nlohmann::json::array();++ if(result.contains("grouped_hits")) {+ for(const auto& grouped_hit : result["grouped_hits"]) {+ for(const auto& hit : grouped_hit["hits"]) {+ result_docs.push_back(hit["document"]);+ }+ }+ }+ else {+ for(const auto& hit : result["hits"]) {+ result_docs.push_back(hit["document"]);+ }+ }++ result_docs_arr.push_back(result_docs);+ }++ // We have to pop a document from the search result with max size+ // Until we do not exceed MAX_TOKENS limit+ while(ConversationManager::get_token_count(result_docs_arr) > ConversationManager::MAX_TOKENS) {+ // sort the result_docs_arr by size descending+ std::sort(result_docs_arr.begin(), result_docs_arr.end(), [](const auto& a, const auto& b) {+ return a.size() > b.size();+ });++ // pop the last element from first array+ if(result_docs_arr.size() > 0 && result_docs_arr[0].size() > 0) {+ result_docs_arr[0].erase(result_docs_arr[0].size() - 1);+ }+ }+++ // Make result_docs_arr 1D+ nlohmann::json result_docs = nlohmann::json::array();+ for(const auto& result_doc : result_docs_arr) {+ for(const auto& doc : result_doc) {+ result_docs.push_back(doc);+ }+ }++ auto conversation_model_id = std::stoul(orig_req_params["conversation_model_id"]);+ auto conversation_model = ConversationModelManager::get_model(conversation_model_id).get();++ auto prompt = req->params["q"];+ auto system_prompt = req->params["system_prompt"];++ auto answer_op = ConversationModel::get_answer(result_docs.dump(0), prompt, system_prompt, conversation_model);++ if(!answer_op.ok()) {+ res->set_400(answer_op.error());+ return false;+ }++ response["conversation"] = nlohmann::json::object();+ response["conversation"]["query"] = common_query;+ response["conversation"]["answer"] = answer_op.get();++ auto formatted_question_op = ConversationModel::format_question(common_query, conversation_model);+ if(!formatted_question_op.ok()) {+ res->set_400(formatted_question_op.error());+ return false;+ }+++ auto formatted_answer_op = ConversationModel::format_answer(answer_op.get(), conversation_model);+ if(!formatted_answer_op.ok()) {+ res->set_400(formatted_answer_op.error());+ return false;+ }++ if(conversation_history) {+ uint32_t conversation_id = std::stoul(orig_req_params["conversation_id"]);+ ConversationManager::append_conversation(conversation_id, formatted_question_op.get());+ ConversationManager::append_conversation(conversation_id, formatted_answer_op.get());+ auto get_conversation_op = ConversationManager::get_conversation(conversation_id);+ if(!get_conversation_op.ok()) {+ res->set_400(get_conversation_op.error());+ return false;+ }++ auto conversation_history = get_conversation_op.get();++ response["conversation"]["conversation_history"] = conversation_history;+ response["conversation"]["conversation_id"] = conversation_id;+ } else {+ nlohmann::json conversation_history = nlohmann::json::array();+ conversation_history.push_back(formatted_question_op.get());+ conversation_history.push_back(formatted_answer_op.get());+ response["conversation"]["conversation_history"] = conversation_history;++ auto create_conversation_op = ConversationManager::create_conversation(conversation_history);+ if(!create_conversation_op.ok()) {+ res->set_400(create_conversation_op.error());+ return false;+ }++ response["conversation"]["conversation_id"] = create_conversation_op.get();+ }++ }+res->set_200(response.dump());// we will cache only successful requests@@ -2381,4 +2574,133 @@ bool get_conversations(const std::shared_ptr<http_req>& req, const std::shared_pres->set_200(conversations_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;++ 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;+ }++ if(!req_json.is_object()) {+ res->set_400("Bad JSON.");+ return false;+ }++ auto add_model_op = ConversationModelManager::add_model(req_json);++ 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");++ res->set_200(model.dump());+ return true;+}++bool get_conversation_model(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {+ if(!StringUtils::is_uint32_t(req->params["id"])) {+ res->set_400("Invalid ID.");+ return false;+ }+ const int model_id = std::stoi(req->params["id"]);++ auto model_op = ConversationModelManager::get_model(model_id);++ if(!model_op.ok()) {+ res->set(model_op.code(), model_op.error());+ return false;+ }++ auto model = model_op.get();+ Collection::hide_credential(model, "api_key");++ res->set_200(model.dump());+ return true;+}++bool get_conversation_models(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {+ auto models_op = ConversationModelManager::get_all_models();++ if(!models_op.ok()) {+ res->set(models_op.code(), models_op.error());+ return false;+ }++ auto models = models_op.get();++ for(auto& model: models) {+ Collection::hide_credential(model, "api_key");+ }++ res->set_200(models.dump());+ return true;+}+++bool del_conversation_model(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {+ if(!StringUtils::is_uint32_t(req->params["id"])) {+ res->set_400("Invalid ID.");+ return false;+ }+ const int model_id = std::stoi(req->params["id"]);++ auto model_op = ConversationModelManager::delete_model(model_id);++ if(!model_op.ok()) {+ res->set(model_op.code(), model_op.error());+ return false;+ }++ auto model = model_op.get();++ Collection::hide_credential(model, "api_key");++ res->set_200(model.dump());+ return true;+}++bool put_conversation_model(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {+ if(!StringUtils::is_uint32_t(req->params["id"])) {+ res->set_400("Invalid ID.");+ return false;+ }+ const int model_id = std::stoi(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;+ }++ if(!req_json.is_object()) {+ res->set_400("Bad JSON.");+ return false;+ }++ auto model_op = ConversationModelManager::update_model(model_id, req_json);++ if(!model_op.ok()) {+ res->set(model_op.code(), model_op.error());+ return false;+ }++ auto model = model_op.get();++ Collection::hide_credential(model, "api_key");++ res->set_200(model.dump());+ return true;}\ No newline at end of filediff --git a/src/field.cpp b/src/field.cppindex 783e3a91..81169a8d 100644--- a/src/field.cpp+++ b/src/field.cpp@@ -2,7 +2,7 @@#include "field.h"#include "magic_enum.hpp"#include "text_embedder_manager.h"-#include "qa_model.h"+#include "conversation_model.h"#include <stack>#include <collection_manager.h>#include <regex>diff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cppindex 0f8072ff..0f4e7bc2 100644--- a/src/main/typesense_server.cpp+++ b/src/main/typesense_server.cpp@@ -94,6 +94,12 @@ void master_server_routes() {server->post("/operations/cache/clear", post_clear_cache, false, false);server->post("/operations/db/compact", post_compact_db, false, false);server->post("/operations/reset_peers", post_reset_peers, false, false);++ server->post("/conversations/models", post_conversation_model);+ server->get("/conversations/models", get_conversation_models);+ server->get("/conversations/models/:id", get_conversation_model);+ server->del("/conversations/models/:id", del_conversation_model);+server->get("/conversations", get_conversations);server->get("/conversations/:id", get_conversation);diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cppindex 3c96e25b..8f83219c 100644--- a/src/typesense_server_utils.cpp+++ b/src/typesense_server_utils.cpp@@ -22,6 +22,8 @@#include "file_utils.h"#include "threadpool.h"#include "stopwords_manager.h"+#include "conversation_manager.h"+#include "conversation_model_manager.h"#ifndef ASAN_BUILD#include "jemalloc.h"@@ -437,6 +439,22 @@ int run_server(const Config & config, const std::string & version, void (*master}TextEmbedderManager::set_model_dir(config.get_data_dir() + "/models");+ auto conversations_init = ConversationManager::init(&meta_store);++ if(!conversations_init.ok()) {+ LOG(INFO) << "Failed to initialize conversation manager: " << conversations_init.error();+ } else {+ LOG(INFO) << "Loaded " << conversations_init.get() << "(s) conversations.";+ }++ auto conversation_models_init = ConversationModelManager::init(&meta_store);++ if(!conversation_models_init.ok()) {+ LOG(INFO) << "Failed to initialize conversation model manager: " << conversation_models_init.error();+ } else {+ LOG(INFO) << "Loaded " << conversation_models_init.get() << "(s) conversation models.";+ }+// first we start the peering serviceReplicationState replication_state(server, batch_indexer, &store,