DeepSeek-R1/deepseek_reasoner.py
Jason Kneen 837e17f55c Fix message handling in deepseek-reasoner
Fixes #21

Add support for successive user or assistant messages in `deepseek-reasoner`.

* **deepseek_reasoner.py**: 
  - Modify the message handling logic to allow successive messages of the same role.
  - Add a check to merge successive messages of the same role into a single message.
  - Update the `process_messages` function to handle the new message format.

* **README.md**: 
  - Add a section explaining the new message handling capability.
  - Provide examples of how to format messages with successive user or assistant messages.

* **test_deepseek_reasoner.py**: 
  - Add tests to ensure the model correctly processes successive user or assistant messages.
  - Include test cases for both interleaved and successive messages.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/deepseek-ai/DeepSeek-R1/issues/21?shareId=XXXX-XXXX-XXXX-XXXX).
2025-01-22 15:17:35 +00:00

52 lines
1.8 KiB
Python

import json
class DeepSeekReasoner:
def __init__(self):
pass
def process_messages(self, messages):
"""
Process the input messages to ensure they follow the required format.
"""
if not messages:
raise ValueError("Messages cannot be empty")
# Merge successive messages of the same role
merged_messages = []
for message in messages:
if merged_messages and merged_messages[-1]['role'] == message['role']:
merged_messages[-1]['content'] += " " + message['content']
else:
merged_messages.append(message)
# Ensure interleaved user and assistant messages
for i in range(1, len(merged_messages)):
if merged_messages[i]['role'] == merged_messages[i-1]['role']:
raise ValueError("Messages must be interleaved between user and assistant")
return merged_messages
def handle_request(self, request):
"""
Handle the incoming request and process the messages.
"""
try:
messages = request.get('messages', [])
processed_messages = self.process_messages(messages)
return {"status": "success", "processed_messages": processed_messages}
except Exception as e:
return {"status": "error", "message": str(e)}
# Example usage
if __name__ == "__main__":
reasoner = DeepSeekReasoner()
request = {
"messages": [
{"role": "user", "content": "Hello!"},
{"role": "user", "content": "Tell me about DeepSeek R1."},
{"role": "assistant", "content": "DeepSeek R1 is a reasoning model."}
]
}
response = reasoner.handle_request(request)
print(json.dumps(response, indent=2))