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: Deepseek r1 reasoning content #1523

Merged
merged 2 commits into from
Jan 30, 2025
Merged
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
40 changes: 29 additions & 11 deletions camel/models/deepseek_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========

import os
import warnings
from typing import Any, Dict, List, Optional, Union

from openai import OpenAI, Stream

from camel.configs import DEEPSEEK_API_PARAMS, DeepSeekConfig
from camel.logger import get_logger
from camel.messages import OpenAIMessage
from camel.models.base_model import BaseModelBackend
from camel.types import (
Expand All @@ -28,6 +28,8 @@
)
from camel.utils import BaseTokenCounter, OpenAITokenCounter, api_keys_required

logger = get_logger(__name__)


class DeepSeekModel(BaseModelBackend):
r"""DeepSeek API in a unified BaseModelBackend interface.
Expand Down Expand Up @@ -116,11 +118,12 @@ def run(
if self.model_type in [
ModelType.DEEPSEEK_REASONER,
]:
warnings.warn(
"Warning: You are using an DeepSeek Reasoner model, "
import re

logger.warning(
"You are using a DeepSeek Reasoner model, "
"which has certain limitations, reference: "
"`https://api-docs.deepseek.com/guides/reasoning_model#api-parameters`.",
UserWarning,
"`https://api-docs.deepseek.com/guides/reasoning_model#api-parameters`"
)

# Check and remove unsupported parameters and reset the fixed
Expand All @@ -138,14 +141,29 @@ def run(
if key in self.model_config_dict:
del self.model_config_dict[key]

# Remove thinking content from messages before sending to API
# This ensures only the final response is sent, excluding
# intermediate thought processes
messages = [
{ # type: ignore[misc]
**msg,
'content': re.sub(
r'<think>.*?</think>',
'',
msg['content'], # type: ignore[arg-type]
flags=re.DOTALL,
).strip(),
}
for msg in messages
]

response = self._client.chat.completions.create(
messages=messages,
model=self.model_type,
**self.model_config_dict,
)

# Temporary solution to handle the case where
# deepseek returns a reasoning_content
# Handle reasoning content with <think> tags at the beginning
if (
self.model_type
in [
Expand All @@ -156,10 +174,10 @@ def run(
):
reasoning_content = response.choices[0].message.reasoning_content
combined_content = (
response.choices[0].message.content
+ "\n\nBELOW IS THE REASONING CONTENT:\n\n"
+ (reasoning_content if reasoning_content else "")
)
f"<think>\n{reasoning_content}\n</think>\n"
if reasoning_content
else ""
) + response.choices[0].message.content

response = ChatCompletion.construct(
id=response.id,
Expand Down
152 changes: 99 additions & 53 deletions examples/models/deepseek_reasoner_model_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"""
please set the below os environment:
export DEEPSEEK_API_KEY=""
export GET_REASONING_CONTENT="true"
"""

model = ModelFactory.create(
Expand Down Expand Up @@ -60,80 +61,125 @@

# If we set the os environment GET_REASONING_CONTENT to "true", we get the reasoning content.

# ruff: noqa: E501
# ruff: noqa: E501,RUF001
'''
===============================================================================
The word 'strawberry' contains **3 Rs**. Here's the breakdown:

1. **S**
2. **T**
3. **R** (1st R)
4. **A**
5. **W**
6. **B**
7. **E**
8. **R** (2nd R)
9. **R** (3rd R)
10. **Y**

The Rs appear in the 3rd, 8th, and 9th positions. 🍓

BELOW IS THE REASONING CONTENT:

Okay, let's see. The user is asking how many times the letter 'R' appears in the word 'strawberry'. Hmm, first, I need to make sure I spell the word correctly. Strawberry is S-T-R-A-W-B-E-R-R-Y. Wait, let me break it down letter by letter.

S, T, R, A, W, B, E, R, R, Y. So let's count the Rs. The third letter is R. Then after that, the next letters are A, W, B, E, so the 8th and 9th letters are R and R again. So that's three Rs? Wait, no. Let me check again. S-T-R (1), then later after E, there are two Rs: R and R. So that would be the third letter R and then the two Rs towards the end. Wait, no. Let's list them out step by step:
<think>
Okay, let's see. The user is asking how many times the letter 'R' appears in the word 'strawberry'. Hmm, first, I need to make sure I spell the word correctly. Strawberry... S-T-R-A-W-B-E-R-R-Y. Let me write that out letter by letter to check.

S, T, R, A, W, B, E, R, R, Y. Wait, let me count again. Starting from the beginning: S (1), T (2), R (3), A (4), W (5), B (6), E (7), R (8), R (9), Y (10). So the letters are S-T-R-A-W-B-E-R-R-Y. That's 10 letters total. Now, looking for the letter 'R'. Let's go through each letter one by one.

1. S – no
2. T – no
3. R – yes, that's the first R.
4. A – no
5. W – no
6. B – no
7. E – no
8. R – second R here.
9. R – third R? Wait, no. Let me check again. After E, the letters are R, R, Y. So positions 8 and 9 are both R. So that's two Rs after the E. So in total, positions 3, 8, 9. Wait, no. Wait, let's break it down again.

Wait, maybe I miscounted the letters. Let me write the word out: strawberry. Breaking it down:

S - 1
T - 2
R - 3
A - 4
W - 5
B - 6
E - 7
R - 8
R - 9
Y - 10

So positions 3, 8, and 9. That would be three Rs? But wait, the standard spelling of strawberry is S-T-R-A-W-B-E-R-R-Y. So after the B, it's E, then double R, then Y. So yes, two Rs after the E. So that's the R at position 3 (from the start) and then two more at positions 8 and 9. So total three Rs? Wait, no. Wait, let me check the spelling again. Strawberry. The correct spelling is S-T-R-A-W-B-E-R-R-Y. So the letters are S, T, R, A, W, B, E, R, R, Y. So that's three Rs? No, wait, R is at position 3, then again at 8 and 9. So that's three Rs? But that can't be right. Wait, maybe I'm miscounting. Let's count the letters again:

1. S
2. T
3. R (1st R)
3. R
4. A
5. W
6. B
7. E
8. R (2nd R)
9. R (3rd R)
8. R
9. R
10. Y

Wait, so that's three Rs? But wait, when I spell strawberry, I think it's S-T-R-A-W-B-E-R-R-Y. So after the B, it's E, then R, R, Y. So that's two Rs in a row. So positions 8 and 9. So total Rs would be the one at position 3 and then the two at positions 8 and 9. That makes three Rs. But wait, is that correct? Let me write out the letters with their positions:

1: S
2: T
3: R
4: A
5: W
6: B
7: E
8: R
9: R
10: Y

Yes, that's correct. So there's an R at position 3, and two Rs at positions 8 and 9. So total three Rs. But wait, when I actually think about the spelling of strawberry, I might be making a mistake here. Let me confirm the correct spelling. Strawberry is spelled S-T-R-A-W-B-E-R-R-Y. Yes, that's right. The double R comes after the E. So that's two Rs there. Plus the initial R after T. So total three Rs. But wait, is the first R really there? Let me think. S-T-R-A-W... Yes, the third letter is R. So that's one. Then after the W, B, E, then two Rs. So three total. Hmm, but I might be miscounting. Let me check again. Maybe I added an extra R by mistake. Let's spell it out slowly: S, T, R, A, W, B, E, R, R, Y. That's 10 letters. So the Rs are at positions 3, 8, and 9. So three Rs. But wait, when I say the word out loud, "strawberry," I don't hear three Rs. Maybe I'm overcomplicating. Let me write it down: strawberry. Breaking it down: S T R A W B E R R Y. So R is the third letter, then after B and E, there are two Rs. So total three Rs. But maybe I'm wrong. Let me look it up mentally. The correct spelling of strawberry is S-T-R-A-W-B-E-R-R-Y. Yes, so two Rs at the end before Y. So that's two Rs there. Plus the R in the beginning. So three. But wait, I think I might have confused the spelling with another word. Let me confirm again. Strawberry: S-T-R-A-W-B-E-R-R-Y. So R appears once after T, then again twice after E. So that's three Rs. Wait, but maybe the first R isn't there. Let me check again: S-T-R... yes, that's correct. The word starts with S-T-R, like 'straw', which is S-T-R-A-W. So the third letter is R. Then after that, B-E-R-R-Y. So the Rs are in positions 3, 8, and 9. So three Rs. Hmm, but I'm now doubting because when I think of the pronunciation, "strawberry" has two Rs: the one in 'straw' and the double R in 'berry'. But 'straw' is S-T-R-A-W, so that's one R. Then 'berry' is B-E-R-R-Y, which is two Rs. So total three Rs. But maybe I'm overcomplicating. Alternatively, maybe the correct spelling only has two Rs. Wait, no. The correct spelling of strawberry does have two Rs in the 'berry' part. Let me confirm. Yes, 'berry' is spelled B-E-R-R-Y. So strawberry is S-T-R-A-W-B-E-R-R-Y. Therefore, three Rs: one in 'straw' and two in 'berry'. So the answer should be three. But I want to be absolutely sure. Let me count each letter one by one:
So yes, positions 3, 8, and 9. That's three Rs. Wait, but when I think about the pronunciation, strawberry is pronounced with two Rs: /ˈstrɔːbəri/ or sometimes /ˈstrɔːbɛri/, but the spelling has two Rs after the E. So in the spelling, it's E-R-R-Y. So two Rs there. So the total Rs are the one at the beginning (position 3) and then two more at positions 8 and 9. That makes three Rs in total. But wait, no, the standard spelling of 'strawberry' is S-T-R-A-W-B-E-R-R-Y. Let me confirm. Yes, strawberry has two Rs towards the end. So the letters are S, T, R, A, W, B, E, R, R, Y. So that's three Rs: one in the third position and two towards the end. Wait, that can't be. Let me check again. The word 'strawberry' is spelled S-T-R-A-W-B-E-R-R-Y. So letters:

1. S
2. T
3. R (1)
3. R
4. A
5. W
6. B
7. E
8. R (2)
9. R (3)
8. R
9. R
10. Y

Yes, three Rs. So the answer is 3. But wait, I just thought of something. Maybe the user is tricking me. Let me check another way. If I write the word out: strawberry. Let's see: s t r a w b e r r y. So under each letter:
So yes, three Rs: positions 3, 8, and 9. But that's not correct. Wait, no. Wait, the correct spelling is S-T-R-A-W-B-E-R-R-Y. So after E, there are two Rs, then Y. So the Rs are at positions 3, 8, and 9. So three Rs. But I think I'm making a mistake here because the standard spelling should have two Rs. Wait, let me check an external source. Hmm, I can't actually look it up, but from my knowledge, 'strawberry' is spelled with two Rs. Let me think: the word comes from 'straw' + 'berry', but the spelling combines them as 'strawberry' with two Rs. Wait, no, the correct spelling is S-T-R-A-W-B-E-R-R-Y. So yes, two Rs after the E. So in total, that would be one R in the third position (S-T-R...) and two Rs after the E. Wait, but that would be three Rs. But that's not possible because the word is pronounced with two Rs. Wait, maybe I'm confusing the pronunciation with the spelling. Let me write it out again:

S T R A W B E R R Y

Breaking it down:

S (1)
T (2)
R (3)
A (4)
W (5)
B (6)
E (7)
R (8)
R (9)
Y (10)

So positions 3, 8, and 9: three Rs. But that's not correct. Wait, no. Wait, maybe I'm splitting the letters wrong. Let me see: S-T-R-A-W-B-E-R-R-Y. So after the B, it's E, then two Rs, then Y. So that's two Rs after the E. But the initial R is in the third position. So total three Rs. But when I think about the word 'strawberry', I always thought it had two Rs. Wait, maybe I'm wrong here. Let me think of other words. For example, 'berry' is B-E-R-R-Y, which has two Rs. So 'strawberry' is 'straw' + 'berry', so combining them, it should have the two Rs from 'berry'. So yes, the Rs in 'strawberry' come from the 'berry' part. Therefore, the R in the third position (from 'straw') and the two Rs from 'berry'. Wait, no. Wait, 'straw' is S-T-R-A-W, so the R is in the third position. Then 'berry' is B-E-R-R-Y. So combining them, it's S-T-R-A-W-B-E-R-R-Y. So that's three Rs: one from 'straw' and two from 'berry'. Therefore, the total number of Rs is three. But that seems off because when I write 'strawberry', I usually write it with two Rs. Wait, maybe I'm making a mistake here. Let me physically write it out on paper. S-T-R-A-W-B-E-R-R-Y. Yes, that's three Rs. Wait, but when I look up the spelling, 'strawberry' is spelled with two Rs. Hmm, perhaps I'm overcomplicating. Let me count again: S, T, R, A, W, B, E, R, R, Y. So yes, three Rs. But that contradicts my previous thought. Wait, maybe the confusion comes from the fact that in 'berry', there are two Rs, and in 'strawberry', since it's a compound of 'straw' and 'berry', the Rs are preserved. So 'straw' has one R, 'berry' has two, so total three. But when you combine them, the spelling is 'strawberry' with two Rs? No, that can't be. Let me check again. If I write 'straw' + 'berry' = 'strawberry', it's S-T-R-A-W-B-E-R-R-Y. So yes, three Rs. Wait, but I think the correct spelling is actually with two Rs. Wait, maybe I'm miscounting. Let me count the letters in 'strawberry' again. Let's see:

S (1)
T (2)
R (3)
A (4)
W (5)
B (6)
E (7)
R (8)
R (9)
Y (10)

So that's 10 letters. The Rs are at positions 3, 8, and 9. So three Rs. But now I'm confused because I thought it was two. Wait, maybe I've been spelling it wrong all along. Let me think of other similar words. For example, 'blueberry' is B-L-U-E-B-E-R-R-Y. That's two Rs. Similarly, 'raspberry' is R-A-S-P-B-E-R-R-Y. So two Rs. But 'strawberry' would follow the same pattern. Wait, but according to my breakdown, it's three. Hmm. Wait, maybe I'm splitting the word incorrectly. Let me check the etymology. 'Strawberry' comes from Old English 'strēawberġe', where 'strēaw' means straw and 'berġe' is berry. So combining them, it's 'strawberry'. The spelling evolved, but the modern spelling is indeed S-T-R-A-W-B-E-R-R-Y. So that's two Rs after the E. But where is the third R? Wait, no. Wait, in the spelling S-T-R-A-W-B-E-R-R-Y, the letters are S, T, R, A, W, B, E, R, R, Y. So that's three Rs: one from the 'straw' part (the R in S-T-R) and two from the 'berry' part (the R-R). So total three Rs. But that seems incorrect. Wait, maybe the R in 'straw' is not present. Wait, 'straw' is S-T-R-A-W. So yes, it has an R. Then 'berry' is B-E-R-R-Y. So when combined, it's S-T-R-A-W-B-E-R-R-Y. So three Rs. But I'm now doubting myself because I always thought it was two. Let me check the correct spelling. [Pauses] Well, since I can't actually look it up, I have to rely on my knowledge. But I think the correct answer is two Rs. Wait, but according to the spelling breakdown, it's three. Wait, maybe I'm making a mistake in the spelling. Let me try typing 'strawberry' out: S-T-R-A-W-B-E-R-R-Y. Yes, that's how it's spelled. So three Rs. But when I say it, it's pronounced with two Rs. Maybe that's why I'm confused. So the answer should be three Rs. But I'm not sure. Let me try another approach. Let's write the word and underline each R:

S T R A W B E R R Y

Underlining the Rs: R at position 3, then R at 8, and R at 9. So three Rs. Therefore, the answer should be three. But I need to be sure. Wait, maybe I'm counting the letters incorrectly. Let's count again:

S (1) – no
T (2) – no
R (3) – yes
A (4) – no
W (5) – no
B (6) – no
E (7) – no
R (8) – yes
R (9) – yes
Y (10) – no

So that's three Rs. Therefore, the answer is three. But I'm still unsure because I thought it was two. Maybe I need to confirm with another method. Think of the word 'strawberry' and how it's spelled. If I write it out, it's S-T-R-A-W-B-E-R-R-Y. So yes, three Rs. I think I was mistaken before, thinking it was two, but actually, it's three. So the correct answer is three Rs.
</think>
The word 'strawberry' is spelled S-T-R-A-W-B-E-R-R-Y. Breaking it down:

s (1)
t (2)
r (3)
a (4)
w (5)
b (6)
e (7)
r (8)
r (9)
y (10)
1. **S**
2. **T**
3. **R** (first R)
4. **A**
5. **W**
6. **B**
7. **E**
8. **R** (second R)
9. **R** (third R)
10. **Y**

Yes, three Rs at positions 3, 8, and 9. So the answer is 3. But wait, sometimes people might misspell it as 'strawbery' with one R. But the correct spelling has two Rs in the 'berry' part. So strawberry has three Rs. Therefore, the answer should be three.
There are **3 Rs** in 'strawberry'.
===============================================================================
'''
Loading