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 support to update TTL for conversations

ozanarmagan38a02e4bbfcc
diff --git a/include/conversation_manager.h b/include/conversation_manager.h
index 8b5618d9..b278eab7 100644
--- a/include/conversation_manager.h
+++ b/include/conversation_manager.h
@@ -16,6 +16,7 @@ class ConversationManager {
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 Option<nlohmann::json> update_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();
diff --git a/include/core_api.h b/include/core_api.h
index a44940a1..96d1d567 100644
--- a/include/core_api.h
+++ b/include/core_api.h
@@ -192,6 +192,8 @@ bool get_conversation(const std::shared_ptr<http_req>& req, const std::shared_pt
bool del_conversation(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);
+bool put_conversation(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);
+
bool post_conversation_model(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);
diff --git a/src/collection.cpp b/src/collection.cpp
index 75c2ffa9..38b53a01 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1501,7 +1501,12 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
return Option<nlohmann::json>(400, conversation_history_op.error());
}
- auto truncate_conversation_history = ConversationManager::truncate_conversation(conversation_history_op.get());
+ auto conversation_history = conversation_history_op.get();
+
+ auto truncate_conversation_history = ConversationManager::truncate_conversation(conversation_history_op.get()["conversation"]);
+
+ conversation_history["conversation"] = truncate_conversation_history.get();
+
if(!truncate_conversation_history.ok()) {
return Option<nlohmann::json>(400, truncate_conversation_history.error());
@@ -1509,7 +1514,7 @@ Option<nlohmann::json> Collection::search(std::string raw_query,
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());
+ auto standalone_question_op = ConversationModel::get_standalone_question(conversation_history, raw_query, conversation_model_op.get());
if(!standalone_question_op.ok()) {
return Option<nlohmann::json>(400, standalone_question_op.error());
}
diff --git a/src/conversation_manager.cpp b/src/conversation_manager.cpp
index c4734977..0d70ab69 100644
--- a/src/conversation_manager.cpp
+++ b/src/conversation_manager.cpp
@@ -199,4 +199,41 @@ void ConversationManager::clear_expired_conversations() {
}
LOG(INFO) << "Cleared " << cleared_conversations << " expired conversations";
+}
+
+Option<nlohmann::json> ConversationManager::update_conversation(nlohmann::json conversation) {
+ std::unique_lock lock(conversations_mutex);
+ if(!conversation.is_object()) {
+ return Option<nlohmann::json>(400, "Conversation is not an object");
+ }
+
+ if(conversation.count("id") == 0) {
+ return Option<nlohmann::json>(400, "Conversation is missing id");
+ }
+
+ int conversation_id = conversation["id"];
+
+ auto conversation_it = conversations.find(conversation_id);
+ if (conversation_it == conversations.end()) {
+ return Option<nlohmann::json>(404, "Conversation not found");
+ }
+
+ auto conversation_key = get_conversation_key(conversation_id);
+
+ auto actual_conversation = conversation_it->second;
+
+ if(conversation.count("ttl") == 0) {
+ return Option<nlohmann::json>(400, "Only `ttl` can be updated");
+ }
+
+ actual_conversation["ttl"] = conversation["ttl"];
+
+ bool insert_op = store->insert(conversation_key, actual_conversation.dump(0));
+ if(!insert_op) {
+ return Option<nlohmann::json>(500, "Error while inserting conversation into the store");
+ }
+
+ conversations[conversation_id] = actual_conversation;
+
+ return Option<nlohmann::json>(actual_conversation);
}
\ No newline at end of file
diff --git a/src/core_api.cpp b/src/core_api.cpp
index be5cf7c1..a1f90a1e 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -2583,6 +2583,36 @@ bool get_conversations(const std::shared_ptr<http_req>& req, const std::shared_p
return true;
}
+bool put_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"]);
+
+ nlohmann::json req_json;
+
+ try {
+ req_json = nlohmann::json::parse(req->body);
+ } catch(const nlohmann::json::parse_error& e) {
+ LOG(ERROR) << "JSON error: " << e.what();
+ res->set_400("Bad JSON.");
+ return false;
+ }
+
+ req_json["id"] = conversation_id;
+
+ auto conversation_op = ConversationManager::update_conversation(req_json);
+
+ if(!conversation_op.ok()) {
+ res->set(conversation_op.code(), conversation_op.error());
+ return false;
+ }
+
+ res->set_200(conversation_op.get().dump());
+ return true;
+}
+
bool post_conversation_model(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {
nlohmann::json req_json;
diff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cpp
index 0f4e7bc2..e495b58c 100644
--- a/src/main/typesense_server.cpp
+++ b/src/main/typesense_server.cpp
@@ -104,6 +104,7 @@ void master_server_routes() {
server->get("/conversations", get_conversations);
server->get("/conversations/:id", get_conversation);
server->del("/conversations/:id", del_conversation);
+ server->put("/conversations/:id", put_conversation);
server->get("/limits", get_rate_limits);
server->get("/limits/active", get_active_throttles);