diff --git a/src/khoj/processor/conversation/anthropic/anthropic_chat.py b/src/khoj/processor/conversation/anthropic/anthropic_chat.py index 57fc9b234..65e28d210 100644 --- a/src/khoj/processor/conversation/anthropic/anthropic_chat.py +++ b/src/khoj/processor/conversation/anthropic/anthropic_chat.py @@ -19,7 +19,11 @@ generate_chatml_messages_with_context, messages_to_print, ) -from khoj.utils.helpers import ConversationCommand, is_none_or_empty +from khoj.utils.helpers import ( + ConversationCommand, + is_none_or_empty, + truncate_code_context, +) from khoj.utils.rawconfig import LocationData from khoj.utils.yaml import yaml_dump @@ -197,7 +201,9 @@ def converse_anthropic( if ConversationCommand.Online in conversation_commands or ConversationCommand.Webpage in conversation_commands: 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=truncate_code_context(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 5bb74da4e..965d30107 100644 --- a/src/khoj/processor/conversation/google/gemini_chat.py +++ b/src/khoj/processor/conversation/google/gemini_chat.py @@ -19,7 +19,11 @@ generate_chatml_messages_with_context, messages_to_print, ) -from khoj.utils.helpers import ConversationCommand, is_none_or_empty +from khoj.utils.helpers import ( + ConversationCommand, + is_none_or_empty, + truncate_code_context, +) from khoj.utils.rawconfig import LocationData from khoj.utils.yaml import yaml_dump @@ -208,7 +212,9 @@ def converse_gemini( if ConversationCommand.Online in conversation_commands or ConversationCommand.Webpage in conversation_commands: 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=truncate_code_context(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 66660c43e..b1ab77fe5 100644 --- a/src/khoj/processor/conversation/offline/chat_model.py +++ b/src/khoj/processor/conversation/offline/chat_model.py @@ -24,6 +24,7 @@ in_debug_mode, is_none_or_empty, is_promptrace_enabled, + truncate_code_context, ) from khoj.utils.rawconfig import LocationData from khoj.utils.yaml import yaml_dump @@ -211,7 +212,9 @@ def converse_offline( 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=truncate_code_context(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 13b539116..e525fa750 100644 --- a/src/khoj/processor/conversation/openai/gpt.py +++ b/src/khoj/processor/conversation/openai/gpt.py @@ -17,7 +17,11 @@ generate_chatml_messages_with_context, messages_to_print, ) -from khoj.utils.helpers import ConversationCommand, is_none_or_empty +from khoj.utils.helpers import ( + ConversationCommand, + is_none_or_empty, + truncate_code_context, +) from khoj.utils.rawconfig import LocationData from khoj.utils.yaml import yaml_dump @@ -196,7 +200,10 @@ def converse( if not is_none_or_empty(online_results): 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=truncate_code_context(code_results))}\n\n" + ) + context_message = context_message.strip() # Setup Prompt with Primer or Conversation History