From e5ac076fc4a9a65c360cebdb1ec4a07f281a765e Mon Sep 17 00:00:00 2001 From: sabaimran Date: Sat, 9 Nov 2024 18:27:46 -0800 Subject: [PATCH] Move construct_chat_history method back to conversation.utils.py --- src/khoj/processor/conversation/utils.py | 29 +++++++++++++++++++++++ src/khoj/processor/tools/run_code.py | 9 +++++-- src/khoj/routers/helpers.py | 30 +----------------------- src/khoj/routers/research.py | 2 +- 4 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/khoj/processor/conversation/utils.py b/src/khoj/processor/conversation/utils.py index 248afa81..6dee4378 100644 --- a/src/khoj/processor/conversation/utils.py +++ b/src/khoj/processor/conversation/utils.py @@ -524,6 +524,35 @@ def get_image_from_url(image_url: str, type="pil"): return ImageWithType(content=None, type=None) +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", "summarize"]: + chat_history += f"User: {chat['intent']['query']}\n" + + if chat["intent"].get("inferred-queries"): + chat_history += f'{agent_name}: {{"queries": {chat["intent"].get("inferred-queries")}}}\n' + + chat_history += f"{agent_name}: {chat['message']}\n\n" + elif chat["by"] == "khoj" and ("text-to-image" in chat["intent"].get("type")): + chat_history += f"User: {chat['intent']['query']}\n" + chat_history += f"{agent_name}: [generated image redacted for space]\n" + elif chat["by"] == "khoj" and ("excalidraw" in chat["intent"].get("type")): + chat_history += f"User: {chat['intent']['query']}\n" + chat_history += f"{agent_name}: {chat['intent']['inferred-queries'][0]}\n" + elif chat["by"] == "you": + raw_attached_files = chat.get("attachedFiles") + if raw_attached_files: + attached_files: Dict[str, str] = {} + for file in raw_attached_files: + attached_files[file["name"]] = file["content"] + + attached_file_context = gather_raw_attached_files(attached_files) + chat_history += f"User: {attached_file_context}\n" + + return chat_history + + def commit_conversation_trace( session: list[ChatMessage], response: str | list[dict], diff --git a/src/khoj/processor/tools/run_code.py b/src/khoj/processor/tools/run_code.py index d5770ca0..c4e2ace1 100644 --- a/src/khoj/processor/tools/run_code.py +++ b/src/khoj/processor/tools/run_code.py @@ -10,8 +10,13 @@ import aiohttp from khoj.database.adapters import ais_user_subscribed from khoj.database.models import Agent, KhojUser from khoj.processor.conversation import prompts -from khoj.processor.conversation.utils import ChatEvent, clean_code_python, clean_json -from khoj.routers.helpers import construct_chat_history, send_message_to_model_wrapper +from khoj.processor.conversation.utils import ( + ChatEvent, + clean_code_python, + clean_json, + construct_chat_history, +) +from khoj.routers.helpers import send_message_to_model_wrapper from khoj.utils.helpers import timer from khoj.utils.rawconfig import LocationData diff --git a/src/khoj/routers/helpers.py b/src/khoj/routers/helpers.py index aed76ad1..93ec6828 100644 --- a/src/khoj/routers/helpers.py +++ b/src/khoj/routers/helpers.py @@ -91,6 +91,7 @@ from khoj.processor.conversation.utils import ( ChatEvent, ThreadedGenerator, clean_json, + construct_chat_history, generate_chatml_messages_with_context, save_to_conversation_log, ) @@ -270,35 +271,6 @@ def gather_raw_attached_files( return f"I have attached the following files:\n\n{contextual_data}" -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", "summarize"]: - chat_history += f"User: {chat['intent']['query']}\n" - - if chat["intent"].get("inferred-queries"): - chat_history += f'{agent_name}: {{"queries": {chat["intent"].get("inferred-queries")}}}\n' - - chat_history += f"{agent_name}: {chat['message']}\n\n" - elif chat["by"] == "khoj" and ("text-to-image" in chat["intent"].get("type")): - chat_history += f"User: {chat['intent']['query']}\n" - chat_history += f"{agent_name}: [generated image redacted for space]\n" - elif chat["by"] == "khoj" and ("excalidraw" in chat["intent"].get("type")): - chat_history += f"User: {chat['intent']['query']}\n" - chat_history += f"{agent_name}: {chat['intent']['inferred-queries'][0]}\n" - elif chat["by"] == "you": - raw_attached_files = chat.get("attachedFiles") - if raw_attached_files: - attached_files: Dict[str, str] = {} - for file in raw_attached_files: - attached_files[file["name"]] = file["content"] - - attached_file_context = gather_raw_attached_files(attached_files) - chat_history += f"User: {attached_file_context}\n" - - return chat_history - - async def acreate_title_from_history( user: KhojUser, conversation: Conversation, diff --git a/src/khoj/routers/research.py b/src/khoj/routers/research.py index c7755b0a..76203989 100644 --- a/src/khoj/routers/research.py +++ b/src/khoj/routers/research.py @@ -11,6 +11,7 @@ from khoj.processor.conversation import prompts from khoj.processor.conversation.utils import ( InformationCollectionIteration, clean_json, + construct_chat_history, construct_iteration_history, construct_tool_chat_history, ) @@ -19,7 +20,6 @@ from khoj.processor.tools.run_code import run_code from khoj.routers.api import extract_references_and_questions from khoj.routers.helpers import ( ChatEvent, - construct_chat_history, generate_summary_from_files, send_message_to_model_wrapper, )