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
Generate standalone question from chat history + conversation history endpoint
ozanarmagan60be35276f31
diff --git a/include/conversation_manager.h b/include/conversation_manager.hindex 1193691a..e94ea249 100644--- a/include/conversation_manager.h+++ b/include/conversation_manager.h@@ -12,6 +12,11 @@ class ConversationManager {static Option<int> create_conversation(const nlohmann::json& conversation);static Option<nlohmann::json> get_conversation(int conversation_id);static Option<bool> append_conversation(int conversation_id, const nlohmann::json& message);+ static Option<nlohmann::json> truncate_conversation(nlohmann::json conversation);+ static size_t get_token_count(const nlohmann::json& message);+ static Option<nlohmann::json> delete_conversation(int conversation_id);+ static Option<nlohmann::json> get_all_conversations();+ static constexpr size_t MAX_TOKENS = 3000;private:static inline std::unordered_map<int, nlohmann::json> conversations;static inline int conversation_id = 0;diff --git a/include/core_api.h b/include/core_api.hindex 6d4fe6a9..f8891e10 100644--- a/include/core_api.h+++ b/include/core_api.h@@ -184,3 +184,10 @@ 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);diff --git a/include/qa_model.h b/include/qa_model.hindex 058dd43d..5f611e2b 100644--- a/include/qa_model.h+++ b/include/qa_model.h@@ -9,18 +9,27 @@class QAModel {public:virtual ~QAModel() {};- static Option<std::string> get_answer(const std::string& context, const std::string& prompt, const std::string& system_prompt, const nlohmann::json& model_config, int& conversation_id);+ 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);+ static Option<std::string> get_standalone_question(const nlohmann::json& conversation_history, const std::string& question, const nlohmann::json& model_config);+ static Option<nlohmann::json> format_question(const std::string& message, const nlohmann::json& model_config);+ static Option<nlohmann::json> format_answer(const std::string& message, const nlohmann::json& model_config);private:};class OpenAIQAModel : public QAModel {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, int& conversation_id);+ 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);static Option<nlohmann::json> parse_conversation_history(const nlohmann::json& conversation);+ static Option<std::string> get_standalone_question(const nlohmann::json& conversation_history, const std::string& question, const nlohmann::json& model_config);+ static Option<nlohmann::json> format_question(const std::string& message);+ static Option<nlohmann::json> format_answer(const std::string& message);+ static const inline std::string STANDALONE_QUESTION_PROMPT = R"(+ Rewrite the follow-up question on top of a human-assistant conversation history as a standalone question that encompasses all pertinent context.+ )";// prevent instantiationOpenAIQAModel() = delete;private:diff --git a/src/collection.cpp b/src/collection.cppindex de688001..04cba0a3 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -1215,6 +1215,31 @@ Option<nlohmann::json> Collection::search(std::string raw_query,size_t num_embed_fields = 0;std::string query = raw_query;+ if(conversation_id >= 0) {+ if(!conversation) {+ return Option<nlohmann::json>(400, "Conversation ID provided but conversation is not enabled for this collection.");+ }++ auto conversation_history_op = ConversationManager::get_conversation(conversation_id);+ if(!conversation_history_op.ok()) {+ return Option<nlohmann::json>(400, conversation_history_op.error());+ }++ auto truncate_conversation_history = ConversationManager::truncate_conversation(conversation_history_op.get());++ if(!truncate_conversation_history.ok()) {+ 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);+ 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++) {const std::string& field_name = raw_search_fields[i];if(field_name == "id") {@@ -1279,7 +1304,7 @@ Option<nlohmann::json> Collection::search(std::string raw_query,}}- std::string embed_query = embedder_manager.get_query_prefix(search_field.embed[fields::model_config]) + raw_query;+ std::string embed_query = embedder_manager.get_query_prefix(search_field.embed[fields::model_config]) + query;auto embedding_op = embedder->Embed(embed_query, remote_embedding_timeout_ms, remote_embedding_num_tries);if(!embedding_op.success) {if(!embedding_op.error["error"].get<std::string>().empty()) {@@ -1307,6 +1332,17 @@ Option<nlohmann::json> Collection::search(std::string raw_query,query = "*";}++ if(!vector_query.field_name.empty() && vector_query.values.empty() && num_embed_fields == 0) {+ std::string error = "Vector query could not find any embedded fields.";+ return Option<nlohmann::json>(400, error);+ }++ if(!query_by_weights.empty() && processed_search_fields.size() != query_by_weights.size()) {+ std::string error = "Error, query_by_weights.size != query_by.size.";+ 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.";@@ -1319,15 +1355,6 @@ Option<nlohmann::json> Collection::search(std::string raw_query,}}- if(!vector_query.field_name.empty() && vector_query.values.empty() && num_embed_fields == 0) {- std::string error = "Vector query could not find any embedded fields.";- return Option<nlohmann::json>(400, error);- }-- if(!query_by_weights.empty() && processed_search_fields.size() != query_by_weights.size()) {- std::string error = "Error, query_by_weights.size != query_by.size.";- return Option<nlohmann::json>(400, error);- }for(const std::string & field_name: processed_search_fields) {@@ -2030,41 +2057,68 @@ Option<nlohmann::json> Collection::search(std::string raw_query,}if(conversation) {- result["qa"] = nlohmann::json::object();- result["qa"]["query"] = raw_query;- auto embedding_field_it = search_schema.find(vector_query.field_name);- if(embedding_field_it == search_schema.end()) {- return Option<nlohmann::json>(400, "Invalid embedding field name.");+ result["conversation"] = nlohmann::json::object();+ result["conversation"]["query"] = raw_query;++ // remove all fields with vector type from docs_array+ for(const auto& field : search_schema) {+ if(field.type == field_types::FLOAT_ARRAY && field.num_dim > 0) {+ for(auto& doc : docs_array) {+ doc.erase(field.name);+ }+ }}- // for each document, remove embedding field to reduce request size- for(auto& doc : docs_array) {- doc.erase(embedding_field_it->name);+ // remove document with lowest score until total tokens is less than MAX_TOKENS+ while(ConversationManager::get_token_count(docs_array) > ConversationManager::MAX_TOKENS) {+ try {+ docs_array.erase(docs_array.size() - 1);+ } catch(...) {+ return Option<nlohmann::json>(400, "Failed to remove document from search results.");+ }}bool has_conversation_history = conversation_id >= 0;- auto qa_op = QAModel::get_answer(docs_array.dump(), raw_query, system_prompt, this->qa, conversation_id);+ auto qa_op = QAModel::get_answer(docs_array.dump(0), raw_query, system_prompt, this->qa);if(!qa_op.ok()) {return Option<nlohmann::json>(qa_op.code(), qa_op.error());}- result["qa"]["answer"] = qa_op.get();- result["qa"]["system_prompt"] = system_prompt;- result["qa"]["conversation_id"] = conversation_id;+ result["conversation"]["answer"] = qa_op.get();+ result["conversation"]["conversation_id"] = conversation_id;++ auto formatted_question_op = QAModel::format_question(raw_query, this->qa);+ 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);+ if(!formatted_answer_op.ok()) {+ return Option<nlohmann::json>(formatted_answer_op.code(), formatted_answer_op.error());+ }if(has_conversation_history) {+ 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()) {return Option<nlohmann::json>(get_conversation_op.code(), get_conversation_op.error());}auto conversation_history = get_conversation_op.get();-- auto parse_conversation_op = QAModel::parse_conversation_history(conversation_history, this->qa);- if(!parse_conversation_op.ok()) {- return Option<nlohmann::json>(parse_conversation_op.code(), parse_conversation_op.error());++ result["conversation"]["conversation_history"] = conversation_history;+ } else {+ nlohmann::json conversation_history = nlohmann::json::array();+ conversation_history.push_back(formatted_question_op.get());+ conversation_history.push_back(formatted_answer_op.get());+ result["conversation"]["conversation_history"] = conversation_history;++ auto create_conversation_op = ConversationManager::create_conversation(conversation_history);+ if(!create_conversation_op.ok()) {+ return Option<nlohmann::json>(create_conversation_op.code(), create_conversation_op.error());}- result["qa"]["conversation_history"] = parse_conversation_op.get();+ result["conversation"]["conversation_id"] = create_conversation_op.get();}}diff --git a/src/conversation_manager.cpp b/src/conversation_manager.cppindex 7f037e46..641698ae 100644--- a/src/conversation_manager.cpp+++ b/src/conversation_manager.cpp@@ -13,7 +13,7 @@ Option<int> ConversationManager::create_conversation(const nlohmann::json& conveOption<nlohmann::json> ConversationManager::get_conversation(int conversation_id) {auto conversation = conversations.find(conversation_id);if (conversation == conversations.end()) {- return Option<nlohmann::json>(400, "Conversation not found");+ return Option<nlohmann::json>(404, "Conversation not found");}return Option<nlohmann::json>(conversation->second);@@ -22,7 +22,7 @@ Option<nlohmann::json> ConversationManager::get_conversation(int conversation_idOption<bool> ConversationManager::append_conversation(int conversation_id, const nlohmann::json& message) {auto conversation = conversations.find(conversation_id);if (conversation == conversations.end()) {- return Option<bool>(400, "Conversation not found");+ return Option<bool>(404, "Conversation not found");}if(!conversation->second.is_array()) {@@ -39,3 +39,49 @@ Option<bool> ConversationManager::append_conversation(int conversation_id, constreturn Option<bool>(true);}+++size_t ConversationManager::get_token_count(const nlohmann::json& message) {+ // from OpenAI API docs:+ // A helpful rule of thumb is that one token generally corresponds to ~4 characters of text for common English text.+ // This translates to roughly ¾ of a word (so 100 tokens ~= 75 words).++ return message.dump(0).size() / 4;+}++Option<nlohmann::json> ConversationManager::truncate_conversation(nlohmann::json conversation) {+ while(get_token_count(conversation) > MAX_TOKENS) {+ // pop front element from json array+ try {+ conversation.erase(0);+ } catch (std::exception& e) {+ return Option<nlohmann::json>(400, "Conversation history is not an array");+ }+ }++ return Option<nlohmann::json>(conversation);+}++Option<nlohmann::json> ConversationManager::delete_conversation(int conversation_id) {+ auto conversation = conversations.find(conversation_id);+ if (conversation == conversations.end()) {+ return Option<nlohmann::json>(404, "Conversation not found");+ }++ Option<nlohmann::json> conversation_res(*conversation);++ conversations.erase(conversation);+ return conversation_res;+}++Option<nlohmann::json> ConversationManager::get_all_conversations() {+ nlohmann::json all_conversations = nlohmann::json::array();+ for(auto& conversation : conversations) {+ nlohmann::json conversation_json;+ conversation_json["id"] = conversation.first;+ conversation_json["conversation"] = conversation.second;+ all_conversations.push_back(conversation_json);+ }++ return Option<nlohmann::json>(all_conversations);+}\ No newline at end of filediff --git a/src/core_api.cpp b/src/core_api.cppindex 06572514..1b2b75a0 100644--- a/src/core_api.cpp+++ b/src/core_api.cpp@@ -17,6 +17,7 @@#include "event_manager.h"#include "http_proxy.h"#include "include/stopwords_manager.h"+#include "conversation_manager.h"using namespace std::chrono_literals;@@ -2331,3 +2332,53 @@ bool post_proxy(const std::shared_ptr<http_req>& req, const std::shared_ptr<httpres->set_200(response.body);return true;}+++bool get_conversation(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 conversation_id = std::stoi(req->params["id"]);++ auto conversation_op = ConversationManager::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) {+ if(!StringUtils::is_uint32_t(req->params["id"])) {+ res->set_400("Invalid ID.");+ return false;+ }+ const int conversation_id = std::stoi(req->params["id"]);++ auto conversation_op = ConversationManager::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_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;+}\ No newline at end of filediff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cppindex 735b70e8..0f8072ff 100644--- a/src/main/typesense_server.cpp+++ b/src/main/typesense_server.cpp@@ -95,6 +95,10 @@ void master_server_routes() {server->post("/operations/db/compact", post_compact_db, false, false);server->post("/operations/reset_peers", post_reset_peers, false, false);+ server->get("/conversations", get_conversations);+ server->get("/conversations/:id", get_conversation);+ server->del("/conversations/:id", del_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/src/qa_model.cpp b/src/qa_model.cppindex d9a73d73..0ba9f6cd 100644--- a/src/qa_model.cpp+++ b/src/qa_model.cpp@@ -4,13 +4,21 @@#include "conversation_manager.h"+const std::string get_model_namespace(const std::string& model_name) {+ if(model_name.find("/") != std::string::npos) {+ return model_name.substr(0, model_name.find("/"));+ } else {+ return "";+ }+}+Option<bool> QAModel::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.");}- const std::string model_namespace = TextEmbedderManager::get_model_namespace(model_config["model_name"].get<std::string>());+ 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);}@@ -19,18 +27,18 @@ Option<bool> QAModel::validate_model(const nlohmann::json& model_config) {}Option<std::string> QAModel::get_answer(const std::string& context, const std::string& prompt,- const std::string& system_prompt, const nlohmann::json& model_config, int& conversation_id) {- const std::string model_namespace = TextEmbedderManager::get_model_namespace(model_config["model_name"].get<std::string>());+ 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, conversation_id);+ return OpenAIQAModel::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) {- const std::string model_namespace = TextEmbedderManager::get_model_namespace(model_config["model_name"].get<std::string>());+ 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);@@ -39,6 +47,36 @@ Option<nlohmann::json> QAModel::parse_conversation_history(const nlohmann::json&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) {+ 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);+ }++ 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) {+ const std::string model_namespace = get_model_namespace(model_config["model_name"].get<std::string>());++ if(model_namespace == "openai") {+ return OpenAIQAModel::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) {+ const std::string model_namespace = get_model_namespace(model_config["model_name"].get<std::string>());++ if(model_namespace == "openai") {+ return OpenAIQAModel::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) {if(model_config.count("api_key") == 0) {@@ -126,7 +164,7 @@ Option<bool> OpenAIQAModel::validate_model(const nlohmann::json& model_config) {}Option<std::string> OpenAIQAModel::get_answer(const std::string& context, const std::string& prompt,- const std::string& system_prompt, const nlohmann::json& model_config, int& conversation_id) {+ 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;@@ -137,37 +175,17 @@ Option<std::string> OpenAIQAModel::get_answer(const std::string& context, constreq_body["model"] = model_name;req_body["messages"] = nlohmann::json::array();- nlohmann::json current_conversation = nlohmann::json::array();if(!system_prompt.empty()) {nlohmann::json system_message = nlohmann::json::object();system_message["role"] = "system";system_message["content"] = system_prompt;- current_conversation.push_back(system_message);+ req_body["messages"].push_back(system_message);}- if(conversation_id < 0) {- nlohmann::json message = nlohmann::json::object();- message["role"] = "user";- message["content"] = "Data:\n" + context + "\n\nQuestion:\n" + prompt;- current_conversation.push_back(message);- req_body["messages"] = current_conversation;- } else {- auto conversation_history_op = ConversationManager::get_conversation(conversation_id);- if(!conversation_history_op.ok()) {- return Option<std::string>(conversation_history_op.code(), conversation_history_op.error());- }-- auto conversation_history = conversation_history_op.get();- req_body["messages"] = conversation_history;- nlohmann::json message = nlohmann::json::object();- message["role"] = "user";- message["content"] = prompt;- if(current_conversation.size() > 0) {- req_body["messages"].push_back(current_conversation[0]);- }- current_conversation.push_back(message);- req_body["messages"].push_back(message);- }+ nlohmann::json message = nlohmann::json::object();+ message["role"] = "user";+ message["content"] = "<Data>\n" + context + "\n\n<Question>\n" + prompt + "\n\n<Answer>";+ req_body["messages"].push_back(message);std::string res;auto res_code = RemoteEmbedder::call_remote_api("POST", OPENAI_CHAT_COMPLETION, req_body.dump(), res, res_headers, headers);@@ -196,22 +214,6 @@ Option<std::string> OpenAIQAModel::get_answer(const std::string& context, constthrow Option<std::string>(400, "Got malformed response from OpenAI API.");}-- current_conversation.push_back(json_res["choices"][0]["message"]);- if(conversation_id < 0) {- auto conversation_id_op = ConversationManager::create_conversation(current_conversation);- if(!conversation_id_op.ok()) {- return Option<std::string>(conversation_id_op.code(), conversation_id_op.error());- }-- conversation_id = conversation_id_op.get();- } else {- auto append_conversation_op = ConversationManager::append_conversation(conversation_id, current_conversation);- if(!append_conversation_op.ok()) {- return Option<std::string>(append_conversation_op.code(), append_conversation_op.error());- }- }-return Option<std::string>(json_res["choices"][0]["message"]["content"].get<std::string>());}@@ -239,7 +241,80 @@ Option<nlohmann::json> OpenAIQAModel::parse_conversation_history(const nlohmann:messages.push_back(parsed_message);}+ return Option<nlohmann::json>(messages);+}++Option<std::string> OpenAIQAModel::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>();+ std::unordered_map<std::string, std::string> headers;+ std::map<std::string, std::string> res_headers;+ headers["Authorization"] = "Bearer " + api_key;+ headers["Content-Type"] = "application/json";+ nlohmann::json req_body;+ req_body["model"] = model_name;+ req_body["messages"] = nlohmann::json::array();+ std::string res;++ std::string standalone_question = STANDALONE_QUESTION_PROMPT;++ standalone_question += "\n\n<Conversation history>\n";+ for(auto& message : conversation_history) {+ if(message.count("user") == 0 && message.count("assistant") == 0) {+ return Option<std::string>(400, "Conversation history is not valid");+ }++ standalone_question += message.dump(0) + "\n";+ }++ standalone_question += "\n\n<Question>\n" + question;+ standalone_question += "\n\n<Standalone question>\n";+ nlohmann::json message = nlohmann::json::object();+ message["role"] = "user";+ message["content"] = standalone_question;++ req_body["messages"].push_back(message);++ auto res_code = RemoteEmbedder::call_remote_api("POST", OPENAI_CHAT_COMPLETION, req_body.dump(), res, res_headers, headers);++ if(res_code == 408) {+ return Option<std::string>(400, "OpenAI API timeout.");+ }++ if (res_code != 200) {+ nlohmann::json json_res;+ try {+ json_res = nlohmann::json::parse(res);+ } catch (const std::exception& e) {+ return Option<std::string>(400, "OpenAI API error: " + res);+ }+ if(json_res.count("error") == 0 || json_res["error"].count("message") == 0) {+ return Option<std::string>(400, "OpenAI API error: " + res);+ }+ return Option<std::string>(400, "OpenAI API error: " + nlohmann::json::parse(res)["error"]["message"].get<std::string>());+ }++ nlohmann::json json_res;+ try {+ json_res = nlohmann::json::parse(res);+ } catch (const std::exception& e) {+ return Option<std::string>(400, "Got malformed response from OpenAI API.");+ }++ return Option<std::string>(json_res["choices"][0]["message"]["content"].get<std::string>());+}++Option<nlohmann::json> OpenAIQAModel::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) {+ nlohmann::json json = nlohmann::json::object();+ json["assistant"] = message;+ return Option<nlohmann::json>(json);+}- return Option<nlohmann::json>(messages);-}\ No newline at end of filediff --git a/src/text_embedder.cpp b/src/text_embedder.cppindex 61244054..12d4e110 100644--- a/src/text_embedder.cpp+++ b/src/text_embedder.cpp@@ -18,12 +18,12 @@ TextEmbedder::TextEmbedder(const std::string& model_name) {void* handle = dlopen("libonnxruntime_providers_shared.so", RTLD_NOW | RTLD_GLOBAL);if(!handle) {LOG(INFO) << "ONNX shared libs: off";+ // log errorcontinue;}dlclose(handle);- LOG(INFO) << "Using CUDAExecutionProvider";OrtCUDAProviderOptions cuda_options;session_options.AppendExecutionProvider_CUDA(cuda_options);}diff --git a/test/collection_vector_search_test.cpp b/test/collection_vector_search_test.cppindex 059e4437..1f7ee775 100644--- a/test/collection_vector_search_test.cpp+++ b/test/collection_vector_search_test.cpp@@ -5,6 +5,7 @@#include "collection.h"#include <cstdlib>#include <ctime>+#include "conversation_manager.h"class CollectionVectorTest : public ::testing::Test {protected:@@ -1342,3 +1343,227 @@ TEST_F(CollectionVectorTest, HybridSearchReturnAllInfo) {ASSERT_EQ(1, results["hits"][0].count("text_match_info"));ASSERT_EQ(1, results["hits"][0].count("hybrid_search_info"));}+++TEST_F(CollectionVectorTest, CreateCollectionWithQA) {+ 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"}}}+ ],+ "qa": {+ "model_name": "openai/gpt-3.5-turbo"+ }+ })"_json;++ TextEmbedderManager::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"));++ schema_json["qa"]["api_key"] = api_key;++ auto collection_create_op = collectionManager.create_collection(schema_json);++ LOG(INFO) << collection_create_op.error();++ ASSERT_TRUE(collection_create_op.ok());+}++TEST_F(CollectionVectorTest, CreateCollectionWithQAAndNoModelName) {+ 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"}}}+ ],+ "qa": {+ "api_key": "1234"+ }+ })"_json;++ TextEmbedderManager::set_model_dir("/tmp/typesense_test/models");++ auto collection_create_op = collectionManager.create_collection(schema_json);++ ASSERT_FALSE(collection_create_op.ok());+ ASSERT_EQ("Property `qa.model_name` is not provided or not a string.", collection_create_op.error());+}++TEST_F(CollectionVectorTest, CreateCollectionWithQAAndNoApiKey) {+ 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"}}}+ ],+ "qa": {+ "model_name": "openai/gpt-3.5-turbo"+ }+ })"_json;++ TextEmbedderManager::set_model_dir("/tmp/typesense_test/models");++ auto collection_create_op = collectionManager.create_collection(schema_json);++ ASSERT_FALSE(collection_create_op.ok());+ ASSERT_EQ("API key is not provided", collection_create_op.error());+}++TEST_F(CollectionVectorTest, CreateCollectionWithQAAndNoModelNameAndApiKey) {+ 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"}}}+ ],+ "qa": {+ }+ })"_json;++ TextEmbedderManager::set_model_dir("/tmp/typesense_test/models");++ auto collection_create_op = collectionManager.create_collection(schema_json);++ ASSERT_FALSE(collection_create_op.ok());+ ASSERT_EQ("Property `qa.model_name` is not provided or not a string.", collection_create_op.error());+}++TEST_F(CollectionVectorTest, TestQAConversation) {+ 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"}}}+ ],+ "qa": {+ "model_name": "openai/gpt-3.5-turbo"+ }+ })"_json;++ TextEmbedderManager::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"));++ schema_json["qa"]["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 add_op = coll->add(R"({+ "product_name": "moisturizer",+ "category": "beauty"+ })"_json.dump());++ ASSERT_TRUE(add_op.ok());++ add_op = coll->add(R"({+ "product_name": "shampoo",+ "category": "beauty"+ })"_json.dump());++ ASSERT_TRUE(add_op.ok());++ add_op = coll->add(R"({+ "product_name": "shirt",+ "category": "clothing"+ })"_json.dump());++ ASSERT_TRUE(add_op.ok());++ add_op = coll->add(R"({+ "product_name": "pants",+ "category": "clothing"+ })"_json.dump());++ ASSERT_TRUE(add_op.ok());+++ auto results_op = coll->search("how many products are there for clothing category?", {"embedding"},+ "", {}, {}, {2}, 10,+ 1, FREQUENCY, {true},+ 0, spp::sparse_hash_set<std::string>(), {},+ 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, HASH, 30000, 2, "", {}, true);++ ASSERT_TRUE(results_op.ok());++ auto results = results_op.get();++ ASSERT_EQ(4, results["hits"].size());+ ASSERT_TRUE(results.contains("conversation"));+ ASSERT_TRUE(results["conversation"].is_object());+ ASSERT_EQ("how many products are there for clothing category?", results["conversation"]["query"]);+ ASSERT_EQ(0, results["conversation"]["conversation_id"]);+++ // test getting conversation history+ auto history_op = ConversationManager::get_conversation(0);++ ASSERT_TRUE(history_op.ok());++ auto history = history_op.get();++ ASSERT_TRUE(history.is_array());+ ASSERT_EQ(2, history.size());++ ASSERT_EQ("how many products are there for clothing category?", history[0]["user"]);+}++TEST_F(CollectionVectorTest, CollectionWithQAInvalidNamespace) {+ 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"}}}+ ],+ "qa": {+ "model_name": "gpt-3.5-turbo"+ }+ })"_json;++ TextEmbedderManager::set_model_dir("/tmp/typesense_test/models");++ if (std::getenv("api_key") == nullptr) {+ LOG(INFO) << "Skipping test as api_key is not set.";+ return;+ }++ std::string api_key = std::string(std::getenv("api_key"));++ schema_json["qa"]["api_key"] = api_key;++ auto collection_create_op = collectionManager.create_collection(schema_json);++ ASSERT_FALSE(collection_create_op.ok());++ ASSERT_EQ("Model namespace `` is not supported.", collection_create_op.error());+}++diff --git a/test/core_api_utils_test.cpp b/test/core_api_utils_test.cppindex acb597fd..7e7ba18a 100644--- a/test/core_api_utils_test.cpp+++ b/test/core_api_utils_test.cpp@@ -1157,4 +1157,131 @@ TEST_F(CoreAPIUtilsTest, TestProxyTimeout) {ASSERT_EQ(408, resp->status_code);ASSERT_EQ("Server error on remote server. Please try again later.", nlohmann::json::parse(resp->body)["message"]);-}\ No newline at end of file+}++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",+ "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"}}}+ ],+ "qa": {+ "model_name": "openai/gpt-3.5-turbo"+ }+ })"_json;++ TextEmbedderManager::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"));++ schema_json["qa"]["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 add_op = coll->add(R"({+ "product_name": "moisturizer",+ "category": "beauty"+ })"_json.dump());++ ASSERT_TRUE(add_op.ok());++ add_op = coll->add(R"({+ "product_name": "shampoo",+ "category": "beauty"+ })"_json.dump());++ ASSERT_TRUE(add_op.ok());++ add_op = coll->add(R"({+ "product_name": "shirt",+ "category": "clothing"+ })"_json.dump());++ ASSERT_TRUE(add_op.ok());++ add_op = coll->add(R"({+ "product_name": "pants",+ "category": "clothing"+ })"_json.dump());++ ASSERT_TRUE(add_op.ok());+++ auto results_op = coll->search("how many products are there for clothing category?", {"embedding"},+ "", {}, {}, {2}, 10,+ 1, FREQUENCY, {true},+ 0, spp::sparse_hash_set<std::string>(), {},+ 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, HASH, 30000, 2, "", {}, true);++ 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_EQ(0, res_json[0]["id"].get<size_t>());+ 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>());+++ get_conversation(req, resp);++ ASSERT_EQ(200, resp->status_code);++ res_json = nlohmann::json::parse(resp->body);++ ASSERT_TRUE(res_json.is_array());+ ASSERT_EQ(2, res_json.size());+++ ASSERT_EQ("how many products are there for clothing category?", res_json[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());+}