From ae4e352b42cdd94050ab81fcd64cc9a7c1c5c1f7 Mon Sep 17 00:00:00 2001 From: Debanjum Date: Fri, 2 May 2025 18:38:41 -0600 Subject: [PATCH] Fix formatting to use Deepseek reasoner for completion via OpenAI API Previously Deepseek reasoner couldn't be used via API for completion because of the additional formatting constrains it required was being applied in this function. The formatting fix was being applied in the chat completion endpoint. --- src/khoj/processor/conversation/openai/utils.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/khoj/processor/conversation/openai/utils.py b/src/khoj/processor/conversation/openai/utils.py index cb337362..7d4a49c0 100644 --- a/src/khoj/processor/conversation/openai/utils.py +++ b/src/khoj/processor/conversation/openai/utils.py @@ -83,6 +83,18 @@ def completion_with_backoff( elif is_twitter_reasoning_model(model_name, api_base_url): reasoning_effort = "high" if deepthought else "low" model_kwargs["reasoning_effort"] = reasoning_effort + 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). + updated_messages: List[dict] = [] + for i, message in enumerate(formatted_messages): + if i > 0 and message["role"] == formatted_messages[i - 1]["role"]: + updated_messages[-1]["content"] += " " + message["content"] + elif i == 1 and formatted_messages[i - 1]["role"] == "system" and message["role"] == "assistant": + updated_messages[-1]["content"] += " " + message["content"] + else: + updated_messages.append(message) + formatted_messages = updated_messages elif is_qwen_reasoning_model(model_name, api_base_url): stream_processor = partial(in_stream_thought_processor, thought_tag="think") # Reasoning is enabled by default. Disable when deepthought is False.