diff --git a/src/khoj/processor/conversation/openai/utils.py b/src/khoj/processor/conversation/openai/utils.py index 7d21662f..444b6541 100644 --- a/src/khoj/processor/conversation/openai/utils.py +++ b/src/khoj/processor/conversation/openai/utils.py @@ -185,6 +185,17 @@ def llm_thread( model_kwargs.pop("response_format", None) elif model_name.startswith("o3-"): temperature = 1 + # Get the first system message and add the string `Formatting re-enabled` to it. See https://platform.openai.com/docs/guides/reasoning-best-practices + if len(formatted_messages) > 0: + system_messages = [ + (i, message) for i, message in enumerate(formatted_messages) if message["role"] == "system" + ] + if len(system_messages) > 0: + first_system_message_index, first_system_message = system_messages[0] + formatted_messages[first_system_message_index][ + "content" + ] = f"{first_system_message} Formatting re-enabled" + elif model_name.startswith("deepseek-reasoner"): # Two successive messages cannot be from the same role. Should merge any back-to-back messages from the same role. # The first message should always be a user message (except system message).