Skip to content

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

Add TTL for conversations

ozanarmagan230308958b2d
diff --git a/include/conversation_manager.h b/include/conversation_manager.h
index 3e3787b1..8b5618d9 100644
--- a/include/conversation_manager.h
+++ b/include/conversation_manager.h
@@ -21,6 +21,10 @@ class ConversationManager {
static Option<nlohmann::json> get_all_conversations();
static constexpr size_t MAX_TOKENS = 3000;
static Option<int> init(Store* store);
+ static void clear_expired_conversations();
+ static void _set_ttl_offset(size_t offset) {
+ TTL_OFFSET = offset;
+ }
private:
static inline std::unordered_map<int, nlohmann::json> conversations;
static inline int conversation_id = 0;
@@ -32,4 +36,7 @@ class ConversationManager {
static inline Store* store;
static const std::string get_conversation_key(int conversation_id);
+
+ static constexpr size_t CONVERSATION_TTL = 60 * 60 * 24;
+ static inline size_t TTL_OFFSET = 0;
};
\ No newline at end of file
diff --git a/src/collection.cpp b/src/collection.cpp
index 10ddedb4..75c2ffa9 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -2341,7 +2341,7 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
}
}
- auto conversation_model = ConversationModelManager::get_model(conversation_id).get();
+ auto conversation_model = ConversationModelManager::get_model(conversation_model_id).get();
bool has_conversation_history = conversation_id >= 0;
auto qa_op = ConversationModel::get_answer(docs_array.dump(0), raw_query, system_prompt, conversation_model);
@@ -2372,17 +2372,23 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
auto conversation_history = get_conversation_op.get();
result["conversation"]["conversation_history"] = conversation_history;
+ result["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());
- 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());
}
+ auto get_conversation_op = ConversationManager::get_conversation(create_conversation_op.get());
+ if(!get_conversation_op.ok()) {
+ return Option<nlohmann::json>(get_conversation_op.code(), get_conversation_op.error());
+ }
+
+ result["conversation"]["conversation_history"] = get_conversation_op.get();
result["conversation"]["conversation_id"] = create_conversation_op.get();
}
}
diff --git a/src/conversation_manager.cpp b/src/conversation_manager.cpp
index 1a3784f4..c4734977 100644
--- a/src/conversation_manager.cpp
+++ b/src/conversation_manager.cpp
@@ -1,4 +1,6 @@
#include "conversation_manager.h"
+#include "logger.h"
+#include <chrono>
Option<int> ConversationManager::create_conversation(const nlohmann::json& conversation) {
@@ -11,13 +13,15 @@ Option<int> ConversationManager::create_conversation(const nlohmann::json& conve
nlohmann::json conversation_store_json;
conversation_store_json["id"] = conversation_id;
conversation_store_json["conversation"] = conversation;
+ conversation_store_json["last_updated"] = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
+ conversation_store_json["ttl"] = CONVERSATION_TTL;
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;
+ conversations[conversation_id] = conversation_store_json;
return Option<int>(conversation_id++);
}
@@ -39,13 +43,11 @@ Option<bool> ConversationManager::append_conversation(int conversation_id, const
return Option<bool>(404, "Conversation not found");
}
- if(!conversation_it->second.is_array()) {
- return Option<bool>(400, "Conversation is not an array");
+ if(!message.is_object() && !message.is_array()) {
+ return Option<bool>(400, "Message is not an object or array");
}
- nlohmann::json conversation = nlohmann::json::object();
- conversation["id"] = conversation_id;
- conversation["conversation"] = conversation_it->second;
+ nlohmann::json conversation = conversation_it->second;
if(!message.is_array()) {
conversation["conversation"].push_back(message);
@@ -55,13 +57,15 @@ Option<bool> ConversationManager::append_conversation(int conversation_id, const
}
}
+ conversation["last_updated"] = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
+
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"];
+ conversations[conversation_id] = conversation;
return Option<bool>(true);
}
@@ -75,7 +79,11 @@ size_t ConversationManager::get_token_count(const nlohmann::json& message) {
return message.dump(0).size() / 4;
}
+// pop front elements until the conversation is less than MAX_TOKENS
Option<nlohmann::json> ConversationManager::truncate_conversation(nlohmann::json conversation) {
+ if(!conversation.is_array()) {
+ return Option<nlohmann::json>(400, "Conversation history is not an array");
+ }
while(get_token_count(conversation) > MAX_TOKENS) {
// pop front element from json array
try {
@@ -95,7 +103,9 @@ Option<nlohmann::json> ConversationManager::delete_conversation(int conversation
return Option<nlohmann::json>(404, "Conversation not found");
}
- Option<nlohmann::json> conversation_res(*conversation);
+
+
+ Option<nlohmann::json> conversation_res(conversation->second);
auto conversation_key = get_conversation_key(conversation_id);
bool delete_op = store->remove(conversation_key);
@@ -111,16 +121,18 @@ 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;
- conversation_json["id"] = conversation.first;
- conversation_json["conversation"] = conversation.second;
- all_conversations.push_back(conversation_json);
+ all_conversations.push_back(conversation.second);
}
return Option<nlohmann::json>(all_conversations);
}
Option<int> ConversationManager::init(Store* store) {
+
+ if(store == nullptr) {
+ return Option<int>(500, "Store is null");
+ }
+
std::unique_lock lock(conversations_mutex);
ConversationManager::store = store;
@@ -142,8 +154,11 @@ Option<int> ConversationManager::init(Store* store) {
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"];
+ if(conversation_json.count("id") == 0) {
+ LOG(INFO) << "key: " << conversation_str << " is missing id";
+ continue;
+ }
+ conversations[conversation_json["id"]] = conversation_json;
loaded_conversations++;
}
@@ -151,5 +166,37 @@ Option<int> ConversationManager::init(Store* store) {
}
const std::string ConversationManager::get_conversation_key(int conversation_id) {
- return std::string(CONVERSATION_RPEFIX) + "_" + StringUtils::serialize_uint32_t(conversation_id);
+ return std::string(CONVERSATION_RPEFIX) + "_" + std::to_string(conversation_id);
+}
+
+void ConversationManager::clear_expired_conversations() {
+ std::unique_lock lock(conversations_mutex);
+
+ int cleared_conversations = 0;
+
+ for(auto& conversation : conversations) {
+ int conversation_id = conversation.first;
+ nlohmann::json conversation_json = conversation.second;
+ if(conversation_json.count("last_updated") == 0 || conversation_json.count("ttl") == 0) {
+ bool delete_op = store->remove(get_conversation_key(conversation_id));
+ if(!delete_op) {
+ LOG(ERROR) << "Error while deleting conversation from the store";
+ }
+ conversations.erase(conversation_id);
+ cleared_conversations++;
+ continue;
+ }
+ int last_updated = conversation_json["last_updated"];
+ int ttl = conversation_json["ttl"];
+ if(last_updated + ttl < TTL_OFFSET + std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count()) {
+ auto delete_op = store->remove(get_conversation_key(conversation_id));
+ if(!delete_op) {
+ LOG(ERROR) << "Error while deleting conversation from the store";
+ }
+ conversations.erase(conversation_id);
+ cleared_conversations++;
+ }
+ }
+
+ LOG(INFO) << "Cleared " << cleared_conversations << " expired conversations";
}
\ No newline at end of file
diff --git a/src/conversation_model.cpp b/src/conversation_model.cpp
index 5480795e..cab826fb 100644
--- a/src/conversation_model.cpp
+++ b/src/conversation_model.cpp
@@ -15,7 +15,7 @@ const std::string get_model_namespace(const std::string& model_name) {
Option<bool> ConversationModel::validate_model(const nlohmann::json& model_config) {
// check model_name is exists and it is a string
if(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.");
+ return Option<bool>(400, "Property `model_name` is not provided or not a string.");
}
const std::string model_namespace = get_model_namespace(model_config["model_name"].get<std::string>());
@@ -130,7 +130,7 @@ Option<bool> OpenAIConversationModel::validate_model(const nlohmann::json& model
}
if(!found) {
- return Option<bool>(400, "Property `qa.model_name` is not a valid OpenAI model.");
+ return Option<bool>(400, "Property `model_name` is not a valid OpenAI model.");
}
nlohmann::json req_body;
@@ -263,7 +263,7 @@ Option<std::string> OpenAIConversationModel::get_standalone_question(const nlohm
std::string standalone_question = STANDALONE_QUESTION_PROMPT;
standalone_question += "\n\n<Conversation history>\n";
- for(auto& message : conversation_history) {
+ for(auto& message : conversation_history["conversation"]) {
if(message.count("user") == 0 && message.count("assistant") == 0) {
return Option<std::string>(400, "Conversation history is not valid");
}
diff --git a/src/conversation_model_manager.cpp b/src/conversation_model_manager.cpp
index 2aea347d..95e71c3d 100644
--- a/src/conversation_model_manager.cpp
+++ b/src/conversation_model_manager.cpp
@@ -111,5 +111,5 @@ Option<int> ConversationModelManager::init(Store* store) {
}
const std::string ConversationModelManager::get_model_key(uint32_t model_id) {
- return std::string(MODEL_KEY_PREFIX) + "_" + StringUtils::serialize_uint32_t(model_id);
+ return std::string(MODEL_KEY_PREFIX) + "_" + std::to_string(model_id);
}
\ No newline at end of file
diff --git a/src/core_api.cpp b/src/core_api.cpp
index d747794f..be5cf7c1 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -729,6 +729,7 @@ bool post_multi_search(const std::shared_ptr<http_req>& req, const std::shared_p
}
if(conversation_history) {
+ LOG(INFO) << "Appending to 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());
@@ -746,7 +747,6 @@ bool post_multi_search(const std::shared_ptr<http_req>& req, const std::shared_p
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()) {
@@ -754,6 +754,13 @@ bool post_multi_search(const std::shared_ptr<http_req>& req, const std::shared_p
return false;
}
+ auto get_conversation_op = ConversationManager::get_conversation(create_conversation_op.get());
+ if(!get_conversation_op.ok()) {
+ res->set_400(get_conversation_op.error());
+ return false;
+ }
+
+ response["conversation"]["conversation_history"] = get_conversation_op.get();
response["conversation"]["conversation_id"] = create_conversation_op.get();
}
diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cpp
index 8f83219c..4979f881 100644
--- a/src/typesense_server_utils.cpp
+++ b/src/typesense_server_utils.cpp
@@ -475,6 +475,18 @@ int run_server(const Config & config, const std::string & version, void (*master
AnalyticsManager::get_instance().run(&replication_state);
});
+ std::thread conersation_garbage_collector_thread([]() {
+ LOG(INFO) << "Conversation garbage collector thread started.";
+ int last_clear_time = 0;
+ while(!brpc::IsAskedToQuit()) {
+ if(last_clear_time + 60 < std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count()) {
+ last_clear_time = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
+ ConversationManager::clear_expired_conversations();
+ }
+ std::this_thread::sleep_for(std::chrono::seconds(5));
+ }
+ });
+
RemoteEmbedder::init(&replication_state);
std::string path_to_nodes = config.get_nodes();
@@ -511,6 +523,10 @@ int run_server(const Config & config, const std::string & version, void (*master
replication_thread_pool.shutdown();
+ LOG(INFO) << "Shutting down conversation garbage collector thread.";
+
+ conersation_garbage_collector_thread.join();
+
server->stop();
});
diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp
index 7cf6e237..e0f382fb 100644
--- a/test/collection_manager_test.cpp
+++ b/test/collection_manager_test.cpp
@@ -255,8 +255,7 @@ TEST_F(CollectionManagerTest, CollectionCreation) {
],
"token_separators":[
"-"
- ],
- "qa": {}
+ ]
}
)"_json;
diff --git a/test/collection_vector_search_test.cpp b/test/collection_vector_search_test.cpp
index 275719f8..19590486 100644
--- a/test/collection_vector_search_test.cpp
+++ b/test/collection_vector_search_test.cpp
@@ -6,6 +6,7 @@
#include <cstdlib>
#include <ctime>
#include "conversation_manager.h"
+#include "conversation_model_manager.h"
class CollectionVectorTest : public ::testing::Test {
protected:
@@ -24,6 +25,9 @@ protected:
store = new Store(state_dir_path);
collectionManager.init(store, 1.0, "auth_key", quit);
collectionManager.load(8, 1000);
+
+ ConversationModelManager::init(store);
+ ConversationManager::init(store);
}
virtual void SetUp() {
@@ -1858,7 +1862,7 @@ TEST_F(CollectionVectorTest, TestMultilingualE5) {
ASSERT_TRUE(semantic_results.ok());
}
-TEST_F(CollectionVectorTest, CreateCollectionWithQA) {
+TEST_F(CollectionVectorTest, TestQAConversation) {
auto schema_json =
R"({
"name": "Products",
@@ -1866,10 +1870,7 @@ TEST_F(CollectionVectorTest, CreateCollectionWithQA) {
{"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");
@@ -1881,104 +1882,11 @@ TEST_F(CollectionVectorTest, CreateCollectionWithQA) {
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"
- }
+ auto conversation_model_config = R"({
+ "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("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;
+ conversation_model_config["api_key"] = api_key;
auto collection_create_op = collectionManager.create_collection(schema_json);
@@ -1986,6 +1894,10 @@ 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"({
"product_name": "moisturizer",
"category": "beauty"
@@ -2014,19 +1926,18 @@ TEST_F(CollectionVectorTest, TestQAConversation) {
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);
-
+ true, 0, max_score, 100, 0, 0, HASH, 30000, 2, "", {}, true, 0);
+
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());
@@ -2041,40 +1952,9 @@ TEST_F(CollectionVectorTest, TestQAConversation) {
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;
+ ASSERT_TRUE(history.is_object());
+ ASSERT_TRUE(history.contains("conversation"));
+ ASSERT_TRUE(history["conversation"].is_array ());
- 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());
-}
+ ASSERT_EQ("how many products are there for clothing category?", history["conversation"][0]["user"]);
+}
\ No newline at end of file
diff --git a/test/conversation_test.cpp b/test/conversation_test.cpp
new file mode 100644
index 00000000..6a08409a
--- /dev/null
+++ b/test/conversation_test.cpp
@@ -0,0 +1,175 @@
+#include <gtest/gtest.h>
+#include "conversation_manager.h"
+
+
+class ConversationTest : public ::testing::Test {
+ protected:
+ 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());
+ store = new Store(state_dir_path);
+ ConversationManager::init(store);
+ }
+
+ void TearDown() override {
+ auto conversations = ConversationManager::get_all_conversations();
+ for (auto& conversation : conversations.get()) {
+ ConversationManager::delete_conversation(conversation["id"]);
+ }
+ delete store;
+ }
+
+ Store* store;
+};
+
+
+TEST_F(ConversationTest, CreateConversation) {
+ nlohmann::json conversation = nlohmann::json::array();
+ auto create_res = ConversationManager::create_conversation(conversation);
+ ASSERT_TRUE(create_res.ok());
+ ASSERT_EQ(create_res.get(), 0);
+}
+
+TEST_F(ConversationTest, CreateConversationInvalidType) {
+ nlohmann::json conversation = nlohmann::json::object();
+ auto create_res = ConversationManager::create_conversation(conversation);
+ ASSERT_FALSE(create_res.ok());
+ ASSERT_EQ(create_res.code(), 400);
+ ASSERT_EQ(create_res.error(), "Conversation is not an array");
+}
+
+TEST_F(ConversationTest, GetInvalidConversation) {
+ auto get_res = ConversationManager::get_conversation(0);
+ ASSERT_FALSE(get_res.ok());
+ ASSERT_EQ(get_res.code(), 404);
+ ASSERT_EQ(get_res.error(), "Conversation not found");
+}
+
+TEST_F(ConversationTest, AppendConversation) {
+ nlohmann::json conversation = nlohmann::json::array();
+ nlohmann::json message = nlohmann::json::object();
+ message["user"] = "Hello";
+ conversation.push_back(message);
+ auto create_res = ConversationManager::create_conversation(conversation);
+
+ ASSERT_TRUE(create_res.ok());
+ ASSERT_EQ(create_res.get(), 0);
+
+ auto append_res = ConversationManager::append_conversation(0, message);
+ ASSERT_TRUE(append_res.ok());
+ ASSERT_EQ(append_res.get(), true);
+
+ auto get_res = ConversationManager::get_conversation(0);
+
+ ASSERT_TRUE(get_res.ok());
+ ASSERT_TRUE(get_res.get()["conversation"].is_array());
+ ASSERT_EQ(get_res.get()["id"], 0);
+ ASSERT_EQ(get_res.get()["conversation"].size(), 2);
+ ASSERT_EQ(get_res.get()["conversation"][0]["user"], "Hello");
+ ASSERT_EQ(get_res.get()["conversation"][1]["user"], "Hello");
+}
+
+
+TEST_F(ConversationTest, AppendInvalidConversation) {
+ nlohmann::json conversation = nlohmann::json::array();
+ nlohmann::json message = nlohmann::json::object();
+ message["user"] = "Hello";
+ auto create_res = ConversationManager::create_conversation(conversation);
+
+ ASSERT_TRUE(create_res.ok());
+ ASSERT_EQ(create_res.get(), 0);
+
+ message = "invalid";
+
+ auto append_res = ConversationManager::append_conversation(0, message);
+ ASSERT_FALSE(append_res.ok());
+ ASSERT_EQ(append_res.code(), 400);
+ ASSERT_EQ(append_res.error(), "Message is not an object or array");
+}
+
+TEST_F(ConversationTest, DeleteConversation) {
+ nlohmann::json conversation = nlohmann::json::array();
+ auto create_res = ConversationManager::create_conversation(conversation);
+ ASSERT_TRUE(create_res.ok());
+ ASSERT_EQ(create_res.get(), 0);
+
+ auto delete_res = ConversationManager::delete_conversation(0);
+ ASSERT_TRUE(delete_res.ok());
+
+ auto delete_res_json = delete_res.get();
+
+ ASSERT_EQ(delete_res_json["id"], 0);
+ ASSERT_TRUE(delete_res_json["conversation"].is_array());
+
+ auto get_res = ConversationManager::get_conversation(0);
+ ASSERT_FALSE(get_res.ok());
+ ASSERT_EQ(get_res.code(), 404);
+ ASSERT_EQ(get_res.error(), "Conversation not found");
+}
+
+TEST_F(ConversationTest, DeleteInvalidConversation) {
+ auto delete_res = ConversationManager::delete_conversation(0);
+ ASSERT_FALSE(delete_res.ok());
+ ASSERT_EQ(delete_res.code(), 404);
+ ASSERT_EQ(delete_res.error(), "Conversation not found");
+}
+
+TEST_F(ConversationTest, TruncateConversation) {
+ nlohmann::json conversation = nlohmann::json::array();
+ nlohmann::json message = nlohmann::json::object();
+ message["user"] = "Hello";
+ for (int i = 0; i < 1000; i++) {
+ conversation.push_back(message);
+ }
+
+ auto truncated = ConversationManager::truncate_conversation(conversation);
+ ASSERT_TRUE(truncated.ok());
+ ASSERT_TRUE(truncated.get().size() < conversation.size());
+}
+
+TEST_F(ConversationTest, TruncateConversationEmpty) {
+ nlohmann::json conversation = nlohmann::json::array();
+ auto truncated = ConversationManager::truncate_conversation(conversation);
+ ASSERT_TRUE(truncated.ok());
+ ASSERT_TRUE(truncated.get().size() == 0);
+}
+
+TEST_F(ConversationTest, TruncateConversationInvalidType) {
+ nlohmann::json conversation = nlohmann::json::object();
+ auto truncated = ConversationManager::truncate_conversation(conversation);
+ ASSERT_FALSE(truncated.ok());
+ ASSERT_EQ(truncated.code(), 400);
+ ASSERT_EQ(truncated.error(), "Conversation history is not an array");
+}
+
+
+TEST_F(ConversationTest, TestConversationExpire) {
+ nlohmann::json conversation = nlohmann::json::array();
+ nlohmann::json message = nlohmann::json::object();
+ message["user"] = "Hello";
+ conversation.push_back(message);
+ auto create_res = ConversationManager::create_conversation(conversation);
+
+ ASSERT_TRUE(create_res.ok());
+ ASSERT_EQ(create_res.get(), 0);
+
+ ConversationManager::clear_expired_conversations();
+
+ auto get_res = ConversationManager::get_conversation(0);
+ ASSERT_TRUE(get_res.ok());
+ ASSERT_TRUE(get_res.get()["conversation"].is_array());
+ ASSERT_EQ(get_res.get()["id"], 0);
+ ASSERT_EQ(get_res.get()["conversation"].size(), 1);
+
+ ConversationManager::_set_ttl_offset(24 * 60 * 60 * 2);
+ ConversationManager::clear_expired_conversations();
+
+ get_res = ConversationManager::get_conversation(0);
+ ASSERT_FALSE(get_res.ok());
+ ASSERT_EQ(get_res.code(), 404);
+ ASSERT_EQ(get_res.error(), "Conversation not found");
+
+ ConversationManager::_set_ttl_offset(0);
+}
+
+
diff --git a/test/core_api_utils_test.cpp b/test/core_api_utils_test.cpp
index 0898f1ea..174b1780 100644
--- a/test/core_api_utils_test.cpp
+++ b/test/core_api_utils_test.cpp
@@ -5,6 +5,8 @@
#include <core_api.h>
#include "core_api_utils.h"
#include "raft_server.h"
+#include "conversation_model_manager.h"
+#include "conversation_manager.h"
class CoreAPIUtilsTest : public ::testing::Test {
protected:
@@ -23,6 +25,9 @@ protected:
store = new Store(state_dir_path);
collectionManager.init(store, 1.0, "auth_key", quit);
collectionManager.load(8, 1000);
+
+ ConversationModelManager::init(store);
+ ConversationManager::init(store);
}
virtual void SetUp() {
@@ -1218,10 +1223,7 @@ TEST_F(CoreAPIUtilsTest, TestGetConversations) {
{"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");
@@ -1233,8 +1235,6 @@ TEST_F(CoreAPIUtilsTest, TestGetConversations) {
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());
@@ -1269,6 +1269,15 @@ TEST_F(CoreAPIUtilsTest, TestGetConversations) {
ASSERT_TRUE(add_op.ok());
+ nlohmann::json model_config = R"({
+ "model_name": "openai/gpt-3.5-turbo"
+ })"_json;
+
+ model_config["api_key"] = api_key;
+
+ auto add_model_op = ConversationModelManager::add_model(model_config);
+
+ ASSERT_TRUE(add_model_op.ok());
auto results_op = coll->search("how many products are there for clothing category?", {"embedding"},
"", {}, {}, {2}, 10,
@@ -1276,7 +1285,7 @@ TEST_F(CoreAPIUtilsTest, TestGetConversations) {
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);
+ true, 0, max_score, 100, 0, 0, HASH, 30000, 2, "", {}, true, 0);
ASSERT_TRUE(results_op.ok());
@@ -1301,11 +1310,13 @@ TEST_F(CoreAPIUtilsTest, TestGetConversations) {
res_json = nlohmann::json::parse(resp->body);
- ASSERT_TRUE(res_json.is_array());
- ASSERT_EQ(2, res_json.size());
+ ASSERT_TRUE(res_json.is_object());
+ ASSERT_EQ(0, res_json["id"].get<size_t>());
+ 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[0]["user"].get<std::string>());
+ ASSERT_EQ("how many products are there for clothing category?", res_json["conversation"][0]["user"].get<std::string>());
del_conversation(req, resp);
@@ -1319,6 +1330,7 @@ TEST_F(CoreAPIUtilsTest, TestGetConversations) {
ASSERT_TRUE(res_json.is_array());
ASSERT_EQ(0, res_json.size());
}
+
TEST_F(CoreAPIUtilsTest, SampleGzipIndexTest) {
Collection *coll_hnstories;
@@ -1373,3 +1385,114 @@ TEST_F(CoreAPIUtilsTest, SampleGzipIndexTest) {
infile.close();
}
+
+TEST_F(CoreAPIUtilsTest, TestConversationModels) {
+ nlohmann::json model_config = R"({
+ "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;
+ }
+
+ model_config["api_key"] = std::string(std::getenv("api_key"));
+
+ auto req = std::make_shared<http_req>();
+ auto resp = std::make_shared<http_res>(nullptr);
+
+ req->body = model_config.dump();
+
+ post_conversation_model(req, resp);
+
+ ASSERT_EQ(200, resp->status_code);
+
+ req->params["id"] = "0";
+
+ get_conversation_model(req, resp);
+
+ ASSERT_EQ(200, resp->status_code);
+ ASSERT_EQ(0, nlohmann::json::parse(resp->body)["id"].get<size_t>());
+
+ get_conversation_models(req, resp);
+
+ ASSERT_EQ(200, resp->status_code);
+ ASSERT_EQ(1, nlohmann::json::parse(resp->body).size());
+
+ del_conversation_model(req, resp);
+
+ ASSERT_EQ(200, resp->status_code);
+
+ get_conversation_models(req, resp);
+
+ ASSERT_EQ(200, resp->status_code);
+ ASSERT_EQ(0, nlohmann::json::parse(resp->body).size());
+}
+
+TEST_F(CoreAPIUtilsTest, TestInvalidConversationModels) {
+ // test with no model_name
+ nlohmann::json model_config = R"({
+ })"_json;
+
+ if (std::getenv("api_key") == nullptr) {
+ LOG(INFO) << "Skipping test as api_key is not set.";
+ return;
+ }
+
+ model_config["api_key"] = std::string(std::getenv("api_key"));
+
+ auto req = std::make_shared<http_req>();
+ auto resp = std::make_shared<http_res>(nullptr);
+
+ req->body = model_config.dump();
+
+ post_conversation_model(req, resp);
+
+ ASSERT_EQ(400, resp->status_code);
+ ASSERT_EQ("Property `model_name` is not provided or not a string.", nlohmann::json::parse(resp->body)["message"]);
+
+ // test with invalid model_name
+ model_config["model_name"] = "invalid_model_name";
+
+ req->body = model_config.dump();
+
+ post_conversation_model(req, resp);
+
+ ASSERT_EQ(400, resp->status_code);
+ ASSERT_EQ("Model namespace `` is not supported.", nlohmann::json::parse(resp->body)["message"]);
+
+ // test with no api_key
+ model_config["model_name"] = "openai/gpt-3.5-turbo";
+ model_config.erase("api_key");
+
+ req->body = model_config.dump();
+
+ post_conversation_model(req, resp);
+
+ ASSERT_EQ(400, resp->status_code);
+ ASSERT_EQ("API key is not provided", nlohmann::json::parse(resp->body)["message"]);
+
+ // test with api_key as integer
+ model_config["api_key"] = 123;
+
+ req->body = model_config.dump();
+
+ post_conversation_model(req, resp);
+
+ ASSERT_EQ(400, resp->status_code);
+ ASSERT_EQ("API key is not a string", nlohmann::json::parse(resp->body)["message"]);
+
+ // test with model_name as integer
+
+ model_config["api_key"] = std::string(std::getenv("api_key"));
+ model_config["model_name"] = 123;
+
+ req->body = model_config.dump();
+
+ post_conversation_model(req, resp);
+
+ ASSERT_EQ(400, resp->status_code);
+ ASSERT_EQ("Property `model_name` is not provided or not a string.", nlohmann::json::parse(resp->body)["message"]);
+}
\ No newline at end of file