Skip to content

Commit 5ea056f

Browse files
committed
Add custom handling logic when speaking with deepseak reasoner
1 parent d640299 commit 5ea056f

File tree

1 file changed

+13
-0
lines changed
  • src/khoj/processor/conversation/openai

1 file changed

+13
-0
lines changed

src/khoj/processor/conversation/openai/utils.py

+13
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ def llm_thread(
181181
elif model_name.startswith("o1-"):
182182
temperature = 1
183183
model_kwargs.pop("response_format", None)
184+
elif model_name.startswith("deepseek-reasoner"):
185+
# Two successive messages cannot be from the same role. Should merge any back-to-back messages from the same role.
186+
# The first message should always be a user message (except system message).
187+
updated_messages = []
188+
for i, message in enumerate(formatted_messages):
189+
if i > 0 and message["role"] == formatted_messages[i - 1]["role"]:
190+
updated_messages[-1]["content"] += " " + message["content"]
191+
elif i == 1 and formatted_messages[i - 1]["role"] == "system" and message["role"] == "assistant":
192+
updated_messages[-1]["content"] += " " + message["content"]
193+
else:
194+
updated_messages.append(message)
195+
196+
formatted_messages = updated_messages
184197

185198
if os.getenv("KHOJ_LLM_SEED"):
186199
model_kwargs["seed"] = int(os.getenv("KHOJ_LLM_SEED"))

0 commit comments

Comments
 (0)