From 8af9dc3ee1167f897d40bf1a9eca2683d290f5d1 Mon Sep 17 00:00:00 2001 From: Debanjum Date: Sat, 26 Oct 2024 10:45:42 -0700 Subject: [PATCH 1/2] Unescape special characters in prompt traces for better readability --- src/khoj/processor/conversation/utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/khoj/processor/conversation/utils.py b/src/khoj/processor/conversation/utils.py index 710de9ff..a1c8d0a4 100644 --- a/src/khoj/processor/conversation/utils.py +++ b/src/khoj/processor/conversation/utils.py @@ -431,6 +431,8 @@ def commit_conversation_trace( # Write files and stage them for filename, content in files_to_commit.items(): file_path = os.path.join(repo_path, filename) + # Unescape special characters in content for better readability + content = content.strip().replace("\\n", "\n").replace("\\t", "\t") with open(file_path, "w", encoding="utf-8") as f: f.write(content) repo.index.add([filename]) From fd71a4b0865f11dae44cc3b87d760917551790e4 Mon Sep 17 00:00:00 2001 From: sabaimran Date: Sat, 26 Oct 2024 14:08:00 -0700 Subject: [PATCH 2/2] 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