Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix message handling in deepseek-reasoner #45

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,49 @@ python3 -m sglang.launch_server --model deepseek-ai/DeepSeek-R1-Distill-Qwen-32B
3. For mathematical problems, it is advisable to include a directive in your prompt such as: "put your final answer within \boxed{}".
4. When evaluating model performance, it is recommended to conduct multiple tests and average the results.

## 7. License
## 7. Message Handling Capability

DeepSeek-R1 now supports successive user or assistant messages without causing an `invalid_request_error`. The message handling logic has been modified to allow successive messages of the same role. Here are some examples of how to format messages with successive user or assistant messages:

### Example 1: Interleaved Messages

```json
[
{"role": "user", "content": "Hello!"},
{"role": "assistant", "content": "Hi! How can I help?"},
{"role": "user", "content": "Tell me about DeepSeek R1."}
]
```

### Example 2: Successive User Messages

```json
[
{"role": "user", "content": "Hello!"},
{"role": "user", "content": "Tell me about DeepSeek R1."}
]
```

### Example 3: Successive Assistant Messages

```json
[
{"role": "assistant", "content": "Hello!"},
{"role": "assistant", "content": "How can I assist you today?"}
]
```

## 8. License
This code repository and the model weights are licensed under the [MIT License](https://github.com/deepseek-ai/DeepSeek-R1/blob/main/LICENSE).
DeepSeek-R1 series support commercial use, allow for any modifications and derivative works, including, but not limited to, distillation for training other LLMs. Please note that:
- DeepSeek-R1-Distill-Qwen-1.5B, DeepSeek-R1-Distill-Qwen-7B, DeepSeek-R1-Distill-Qwen-14B and DeepSeek-R1-Distill-Qwen-32B are derived from [Qwen-2.5 series](https://github.com/QwenLM/Qwen2.5), which are originally licensed under [Apache 2.0 License](https://huggingface.co/Qwen/Qwen2.5-1.5B/blob/main/LICENSE), and now finetuned with 800k samples curated with DeepSeek-R1.
- DeepSeek-R1-Distill-Llama-8B is derived from Llama3.1-8B-Base and is originally licensed under [llama3.1 license](https://huggingface.co/meta-llama/Llama-3.1-8B/blob/main/LICENSE).
- DeepSeek-R1-Distill-Llama-70B is derived from Llama3.3-70B-Instruct and is originally licensed under [llama3.3 license](https://huggingface.co/meta-llama/Llama-3.3-70B-Instruct/blob/main/LICENSE).

## 8. Citation
## 9. Citation
```
```

## 9. Contact
## 10. Contact
If you have any questions, please raise an issue or contact us at [[email protected]]([email protected]).
46 changes: 46 additions & 0 deletions deepseek_reasoner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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)

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))
46 changes: 46 additions & 0 deletions test_deepseek_reasoner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import unittest
from deepseek_reasoner import DeepSeekReasoner

class TestDeepSeekReasoner(unittest.TestCase):

def setUp(self):
self.reasoner = DeepSeekReasoner()

def test_interleaved_messages(self):
request = {
"messages": [
{"role": "user", "content": "Hello!"},
{"role": "assistant", "content": "Hi! How can I help?"},
{"role": "user", "content": "Tell me about DeepSeek R1."}
]
}
response = self.reasoner.handle_request(request)
self.assertEqual(response["status"], "success")
self.assertEqual(len(response["processed_messages"]), 3)

def test_successive_user_messages(self):
request = {
"messages": [
{"role": "user", "content": "Hello!"},
{"role": "user", "content": "Tell me about DeepSeek R1."}
]
}
response = self.reasoner.handle_request(request)
self.assertEqual(response["status"], "success")
self.assertEqual(len(response["processed_messages"]), 1)
self.assertEqual(response["processed_messages"][0]["content"], "Hello! Tell me about DeepSeek R1.")

def test_successive_assistant_messages(self):
request = {
"messages": [
{"role": "assistant", "content": "Hello!"},
{"role": "assistant", "content": "How can I assist you today?"}
]
}
response = self.reasoner.handle_request(request)
self.assertEqual(response["status"], "success")
self.assertEqual(len(response["processed_messages"]), 1)
self.assertEqual(response["processed_messages"][0]["content"], "Hello! How can I assist you today?")

if __name__ == "__main__":
unittest.main()