diff --git a/src/khoj/processor/conversation/prompts.py b/src/khoj/processor/conversation/prompts.py index 2af72d8c..fa62dbb2 100644 --- a/src/khoj/processor/conversation/prompts.py +++ b/src/khoj/processor/conversation/prompts.py @@ -411,6 +411,10 @@ Tell the user exactly what the document says in response to their query, while a extract_relevant_summary = PromptTemplate.from_template( """ {personality_context} + +Conversation History: +{chat_history} + Target Query: {query} Document Contents: diff --git a/src/khoj/routers/api_chat.py b/src/khoj/routers/api_chat.py index 37a33534..f8f534ae 100644 --- a/src/khoj/routers/api_chat.py +++ b/src/khoj/routers/api_chat.py @@ -758,7 +758,12 @@ async def chat( yield result response = await extract_relevant_summary( - q, contextual_data, subscribed=subscribed, uploaded_image_url=uploaded_image_url, agent=agent + q, + contextual_data, + conversation_history=meta_log, + subscribed=subscribed, + uploaded_image_url=uploaded_image_url, + agent=agent, ) response_log = str(response) async for result in send_llm_response(response_log): @@ -1238,7 +1243,11 @@ async def get_chat( yield result response = await extract_relevant_summary( - q, contextual_data, subscribed=subscribed, uploaded_image_url=uploaded_image_url + q, + contextual_data, + conversation_history=meta_log, + subscribed=subscribed, + uploaded_image_url=uploaded_image_url, ) response_log = str(response) async for result in send_llm_response(response_log): diff --git a/src/khoj/routers/helpers.py b/src/khoj/routers/helpers.py index 752115c1..54e55869 100644 --- a/src/khoj/routers/helpers.py +++ b/src/khoj/routers/helpers.py @@ -208,7 +208,7 @@ def get_next_url(request: Request) -> str: def construct_chat_history(conversation_history: dict, n: int = 4, agent_name="AI") -> str: chat_history = "" for chat in conversation_history.get("chat", [])[-n:]: - if chat["by"] == "khoj" and chat["intent"].get("type") in ["remember", "reminder"]: + if chat["by"] == "khoj" and chat["intent"].get("type") in ["remember", "reminder", "summarize"]: chat_history += f"User: {chat['intent']['query']}\n" chat_history += f"{agent_name}: {chat['message']}\n" elif chat["by"] == "khoj" and ("text-to-image" in chat["intent"].get("type")): @@ -574,7 +574,12 @@ async def extract_relevant_info(q: str, corpus: str, subscribed: bool, agent: Ag async def extract_relevant_summary( - q: str, corpus: str, subscribed: bool = False, uploaded_image_url: str = None, agent: Agent = None + q: str, + corpus: str, + conversation_history: dict, + subscribed: bool = False, + uploaded_image_url: str = None, + agent: Agent = None, ) -> Union[str, None]: """ Extract relevant information for a given query from the target corpus @@ -587,8 +592,11 @@ async def extract_relevant_summary( prompts.personality_context.format(personality=agent.personality) if agent and agent.personality else "" ) + chat_history = construct_chat_history(conversation_history) + extract_relevant_information = prompts.extract_relevant_summary.format( query=q, + chat_history=chat_history, corpus=corpus.strip(), personality_context=personality_context, )