From 80df3bb8c4219daff196d2d7244fa129a7650983 Mon Sep 17 00:00:00 2001 From: Debanjum Date: Wed, 20 Nov 2024 11:50:55 -0800 Subject: [PATCH] Enable prompt tracing only when PROMPTRACE_DIR env var set Better to decouple prompt tracing from debug mode or verbosity level and require explicit, independent config to enable prompt tracing --- src/khoj/processor/conversation/utils.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/khoj/processor/conversation/utils.py b/src/khoj/processor/conversation/utils.py index 91ba3c72..c02958c9 100644 --- a/src/khoj/processor/conversation/utils.py +++ b/src/khoj/processor/conversation/utils.py @@ -291,7 +291,7 @@ def save_to_conversation_log( user_message=q, ) - if in_debug_mode() or state.verbose > 1: + if os.getenv("PROMPTRACE_DIR"): merge_message_into_conversation_trace(q, chat_response, tracer) logger.info( @@ -578,7 +578,7 @@ def commit_conversation_trace( response: str | list[dict], tracer: dict, system_message: str | list[dict] = "", - repo_path: str = "/tmp/promptrace", + repo_path: str = None, ) -> str: """ Save trace of conversation step using git. Useful to visualize, compare and debug traces. @@ -589,6 +589,11 @@ def commit_conversation_trace( except ImportError: return None + # Infer repository path from environment variable or provided path + repo_path = repo_path or os.getenv("PROMPTRACE_DIR") + if not repo_path: + return None + # Serialize session, system message and response to yaml system_message_yaml = json.dumps(system_message, ensure_ascii=False, sort_keys=False) response_yaml = json.dumps(response, ensure_ascii=False, sort_keys=False) @@ -601,9 +606,6 @@ def commit_conversation_trace( # Extract chat metadata for session 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) - try: # Prepare git repository os.makedirs(repo_path, exist_ok=True)