From fd71a4b0865f11dae44cc3b87d760917551790e4 Mon Sep 17 00:00:00 2001 From: sabaimran Date: Sat, 26 Oct 2024 14:08:00 -0700 Subject: [PATCH] Add better exception handling in the prompt trace logic, use default value from parameters --- src/khoj/processor/conversation/utils.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/khoj/processor/conversation/utils.py b/src/khoj/processor/conversation/utils.py index a1c8d0a4..7c46b3b3 100644 --- a/src/khoj/processor/conversation/utils.py +++ b/src/khoj/processor/conversation/utils.py @@ -358,7 +358,7 @@ def commit_conversation_trace( response: str | list[dict], tracer: dict, system_message: str | list[dict] = "", - repo_path: str = "/tmp/khoj_promptrace", + repo_path: str = "/tmp/promptrace", ) -> str: """ Save trace of conversation step using git. Useful to visualize, compare and debug traces. @@ -380,7 +380,7 @@ def commit_conversation_trace( uid, cid, mid = tracer.get("uid", "main"), tracer.get("cid", "main"), tracer.get("mid") # Infer repository path from environment variable or provided path - repo_path = os.getenv("PROMPTRACE_DIR", repo_path) or "/tmp/promptrace" + repo_path = os.getenv("PROMPTRACE_DIR", repo_path) try: # Prepare git repository @@ -456,11 +456,11 @@ Metadata logger.debug(f"Saved conversation trace to repo at {repo_path}") return repo_path except Exception as e: - logger.error(f"Failed to add conversation trace to repo: {str(e)}") + logger.error(f"Failed to add conversation trace to repo: {str(e)}", exc_info=True) return None -def merge_message_into_conversation_trace(query: str, response: str, tracer: dict, repo_path=None) -> bool: +def merge_message_into_conversation_trace(query: str, response: str, tracer: dict, repo_path="/tmp/promptrace") -> bool: """ Merge the message branch into its parent conversation branch. @@ -474,14 +474,14 @@ def merge_message_into_conversation_trace(query: str, response: str, tracer: dic bool: True if merge was successful, False otherwise """ try: - # Infer repository path from environment variable or provided path - repo_path = os.getenv("PROMPTRACE_DIR", repo_path) or "/tmp/promptrace" - repo = Repo(repo_path) - # Extract branch names msg_branch = f"m_{tracer['mid']}" conv_branch = f"c_{tracer['cid']}" + # Infer repository path from environment variable or provided path + repo_path = os.getenv("PROMPTRACE_DIR", repo_path) + repo = Repo(repo_path) + # Checkout conversation branch repo.heads[conv_branch].checkout() @@ -508,5 +508,5 @@ Metadata logger.debug(f"Successfully merged {msg_branch} into {conv_branch}") return True except Exception as e: - logger.error(f"Failed to merge message {msg_branch} into conversation {conv_branch}: {str(e)}") + logger.error(f"Failed to merge message {msg_branch} into conversation {conv_branch}: {str(e)}", exc_info=True) return False