From 9e5585776c8621545239b29fced83fb88e275ec4 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Mon, 15 Apr 2024 13:59:59 +0530 Subject: [PATCH] Support getting latest N chat messages via chat history API Get latest N if N > 0, else return all messages except latest N from the conversation --- src/khoj/routers/api_chat.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/khoj/routers/api_chat.py b/src/khoj/routers/api_chat.py index 76cf6d12..9af00053 100644 --- a/src/khoj/routers/api_chat.py +++ b/src/khoj/routers/api_chat.py @@ -76,6 +76,7 @@ def chat_history( request: Request, common: CommonQueryParams, conversation_id: Optional[int] = None, + n: Optional[int] = None, ): user = request.user.object validate_conversation_config() @@ -109,6 +110,13 @@ def chat_history( } ) + # Get latest N messages if N > 0 + if n > 0: + meta_log["chat"] = meta_log["chat"][-n:] + # Else return all messages except latest N + else: + meta_log["chat"] = meta_log["chat"][:n] + update_telemetry_state( request=request, telemetry_type="api",