diff --git a/src/khoj/processor/conversation/anthropic/anthropic_chat.py b/src/khoj/processor/conversation/anthropic/anthropic_chat.py index f3171b88..f0568ccc 100644 --- a/src/khoj/processor/conversation/anthropic/anthropic_chat.py +++ b/src/khoj/processor/conversation/anthropic/anthropic_chat.py @@ -19,6 +19,7 @@ from khoj.processor.conversation.utils import ( ) from khoj.utils.helpers import ConversationCommand, is_none_or_empty from khoj.utils.rawconfig import LocationData +from khoj.utils.yaml import yaml_dump logger = logging.getLogger(__name__) @@ -152,7 +153,6 @@ def converse_anthropic( """ # Initialize Variables current_date = datetime.now() - compiled_references = "\n\n".join({f"# File: {item['file']}\n## {item['compiled']}\n" for item in references}) if agent and agent.personality: system_prompt = prompts.custom_personality.format( @@ -176,7 +176,7 @@ def converse_anthropic( system_prompt = f"{system_prompt}\n{user_name_prompt}" # Get Conversation Primer appropriate to Conversation Type - if conversation_commands == [ConversationCommand.Notes] and is_none_or_empty(compiled_references): + if conversation_commands == [ConversationCommand.Notes] and is_none_or_empty(references): completion_func(chat_response=prompts.no_notes_found.format()) return iter([prompts.no_notes_found.format()]) elif conversation_commands == [ConversationCommand.Online] and is_none_or_empty(online_results): @@ -184,12 +184,12 @@ def converse_anthropic( return iter([prompts.no_online_results_found.format()]) context_message = "" - if not is_none_or_empty(compiled_references): - context_message = f"{prompts.notes_conversation.format(query=user_query, references=compiled_references)}\n\n" + if not is_none_or_empty(references): + context_message = f"{prompts.notes_conversation.format(query=user_query, references=yaml_dump(references))}\n\n" if ConversationCommand.Online in conversation_commands or ConversationCommand.Webpage in conversation_commands: - context_message += f"{prompts.online_search_conversation.format(online_results=str(online_results))}\n\n" + context_message += f"{prompts.online_search_conversation.format(online_results=yaml_dump(online_results))}\n\n" if ConversationCommand.Code in conversation_commands and not is_none_or_empty(code_results): - context_message += f"{prompts.code_executed_context.format(code_results=str(code_results))}\n\n" + context_message += f"{prompts.code_executed_context.format(code_results=yaml_dump(code_results))}\n\n" context_message = context_message.strip() # Setup Prompt with Primer or Conversation History diff --git a/src/khoj/processor/conversation/google/gemini_chat.py b/src/khoj/processor/conversation/google/gemini_chat.py index 29a2a935..44f24d4b 100644 --- a/src/khoj/processor/conversation/google/gemini_chat.py +++ b/src/khoj/processor/conversation/google/gemini_chat.py @@ -19,6 +19,7 @@ from khoj.processor.conversation.utils import ( ) from khoj.utils.helpers import ConversationCommand, is_none_or_empty from khoj.utils.rawconfig import LocationData +from khoj.utils.yaml import yaml_dump logger = logging.getLogger(__name__) @@ -157,7 +158,6 @@ def converse_gemini( """ # Initialize Variables current_date = datetime.now() - compiled_references = "\n\n".join({f"# File: {item['file']}\n## {item['compiled']}\n" for item in references}) if agent and agent.personality: system_prompt = prompts.custom_personality.format( @@ -182,7 +182,7 @@ def converse_gemini( system_prompt = f"{system_prompt}\n{user_name_prompt}" # Get Conversation Primer appropriate to Conversation Type - if conversation_commands == [ConversationCommand.Notes] and is_none_or_empty(compiled_references): + if conversation_commands == [ConversationCommand.Notes] and is_none_or_empty(references): completion_func(chat_response=prompts.no_notes_found.format()) return iter([prompts.no_notes_found.format()]) elif conversation_commands == [ConversationCommand.Online] and is_none_or_empty(online_results): @@ -190,12 +190,12 @@ def converse_gemini( return iter([prompts.no_online_results_found.format()]) context_message = "" - if not is_none_or_empty(compiled_references): - context_message = f"{prompts.notes_conversation.format(query=user_query, references=compiled_references)}\n\n" + if not is_none_or_empty(references): + context_message = f"{prompts.notes_conversation.format(query=user_query, references=yaml_dump(references))}\n\n" if ConversationCommand.Online in conversation_commands or ConversationCommand.Webpage in conversation_commands: - context_message += f"{prompts.online_search_conversation.format(online_results=str(online_results))}\n\n" + context_message += f"{prompts.online_search_conversation.format(online_results=yaml_dump(online_results))}\n\n" if ConversationCommand.Code in conversation_commands and not is_none_or_empty(code_results): - context_message += f"{prompts.code_executed_context.format(code_results=str(code_results))}\n\n" + context_message += f"{prompts.code_executed_context.format(code_results=yaml_dump(code_results))}\n\n" context_message = context_message.strip() # Setup Prompt with Primer or Conversation History diff --git a/src/khoj/processor/conversation/offline/chat_model.py b/src/khoj/processor/conversation/offline/chat_model.py index 96c9cb40..fbde7a98 100644 --- a/src/khoj/processor/conversation/offline/chat_model.py +++ b/src/khoj/processor/conversation/offline/chat_model.py @@ -19,6 +19,7 @@ from khoj.utils import state from khoj.utils.constants import empty_escape_sequences from khoj.utils.helpers import ConversationCommand, in_debug_mode, is_none_or_empty from khoj.utils.rawconfig import LocationData +from khoj.utils.yaml import yaml_dump logger = logging.getLogger(__name__) @@ -185,26 +186,24 @@ def converse_offline( system_prompt = f"{system_prompt}\n{user_name_prompt}" # Get Conversation Primer appropriate to Conversation Type - if conversation_commands == [ConversationCommand.Notes] and is_none_or_empty(compiled_references): + if conversation_commands == [ConversationCommand.Notes] and is_none_or_empty(references): return iter([prompts.no_notes_found.format()]) elif conversation_commands == [ConversationCommand.Online] and is_none_or_empty(online_results): completion_func(chat_response=prompts.no_online_results_found.format()) return iter([prompts.no_online_results_found.format()]) context_message = "" - if not is_none_or_empty(compiled_references): - context_message = f"{prompts.notes_conversation_offline.format(references=compiled_references)}\n\n" + if not is_none_or_empty(references): + context_message = f"{prompts.notes_conversation_offline.format(references=yaml_dump(references))}\n\n" if ConversationCommand.Online in conversation_commands or ConversationCommand.Webpage in conversation_commands: simplified_online_results = online_results.copy() for result in online_results: if online_results[result].get("webpages"): simplified_online_results[result] = online_results[result]["webpages"] - context_message += ( - f"{prompts.online_search_conversation_offline.format(online_results=str(simplified_online_results))}\n\n" - ) + context_message += f"{prompts.online_search_conversation_offline.format(online_results=yaml_dump(simplified_online_results))}\n\n" if ConversationCommand.Code in conversation_commands and not is_none_or_empty(code_results): - context_message += f"{prompts.code_executed_context.format(code_results=str(code_results))}\n\n" + context_message += f"{prompts.code_executed_context.format(code_results=yaml_dump(code_results))}\n\n" context_message = context_message.strip() # Setup Prompt with Primer or Conversation History diff --git a/src/khoj/processor/conversation/openai/gpt.py b/src/khoj/processor/conversation/openai/gpt.py index 89534394..a0edb422 100644 --- a/src/khoj/processor/conversation/openai/gpt.py +++ b/src/khoj/processor/conversation/openai/gpt.py @@ -18,6 +18,7 @@ from khoj.processor.conversation.utils import ( ) from khoj.utils.helpers import ConversationCommand, is_none_or_empty from khoj.utils.rawconfig import LocationData +from khoj.utils.yaml import yaml_dump logger = logging.getLogger(__name__) @@ -155,7 +156,6 @@ def converse( """ # Initialize Variables current_date = datetime.now() - compiled_references = "\n\n".join({f"# File: {item['file']}\n## {item['compiled']}\n" for item in references}) if agent and agent.personality: system_prompt = prompts.custom_personality.format( @@ -179,7 +179,7 @@ def converse( system_prompt = f"{system_prompt}\n{user_name_prompt}" # Get Conversation Primer appropriate to Conversation Type - if conversation_commands == [ConversationCommand.Notes] and is_none_or_empty(compiled_references): + if conversation_commands == [ConversationCommand.Notes] and is_none_or_empty(references): completion_func(chat_response=prompts.no_notes_found.format()) return iter([prompts.no_notes_found.format()]) elif conversation_commands == [ConversationCommand.Online] and is_none_or_empty(online_results): @@ -187,12 +187,12 @@ def converse( return iter([prompts.no_online_results_found.format()]) context_message = "" - if not is_none_or_empty(compiled_references): - context_message = f"{prompts.notes_conversation.format(references=compiled_references)}\n\n" + if not is_none_or_empty(references): + context_message = f"{prompts.notes_conversation.format(references=yaml_dump(references))}\n\n" if not is_none_or_empty(online_results): - context_message += f"{prompts.online_search_conversation.format(online_results=str(online_results))}\n\n" + context_message += f"{prompts.online_search_conversation.format(online_results=yaml_dump(online_results))}\n\n" if not is_none_or_empty(code_results): - context_message += f"{prompts.code_executed_context.format(code_results=str(code_results))}\n\n" + context_message += f"{prompts.code_executed_context.format(code_results=yaml_dump(code_results))}\n\n" context_message = context_message.strip() # Setup Prompt with Primer or Conversation History diff --git a/src/khoj/utils/yaml.py b/src/khoj/utils/yaml.py index e4bf17f3..f658e1eb 100644 --- a/src/khoj/utils/yaml.py +++ b/src/khoj/utils/yaml.py @@ -41,3 +41,7 @@ def parse_config_from_string(yaml_config: dict) -> FullConfig: def parse_config_from_file(yaml_config_file): "Parse and validate config in YML file" return parse_config_from_string(load_config_from_file(yaml_config_file)) + + +def yaml_dump(data): + return yaml.dump(data, allow_unicode=True, sort_keys=False, default_flow_style=False)