mirror of
https://github.com/hwchase17/langchain.git
synced 2026-01-22 20:59:05 +00:00
partners: (langchain-deepseek) fix deepseek-r1 always returns an empty reasoning_content when reasoning (#31065)
## Description
deepseek-r1 always returns an empty string `reasoning_content` to the
first chunk when thinking, and sets `reasoning_content` to None when
thinking is over, to determine when to switch to normal output.
Therefore, whether the reasoning_content field exists should be judged
as None.
## Demo
deepseek-r1 reasoning output:
```
{'delta': {'content': None, 'function_call': None, 'refusal': None, 'role': 'assistant', 'tool_calls': None, 'reasoning_content': ''}, 'finish_reason': None, 'index': 0, 'logprobs': None}
{'delta': {'content': None, 'function_call': None, 'refusal': None, 'role': None, 'tool_calls': None, 'reasoning_content': '好的'}, 'finish_reason': None, 'index': 0, 'logprobs': None}
{'delta': {'content': None, 'function_call': None, 'refusal': None, 'role': None, 'tool_calls': None, 'reasoning_content': ','}, 'finish_reason': None, 'index': 0, 'logprobs': None}
{'delta': {'content': None, 'function_call': None, 'refusal': None, 'role': None, 'tool_calls': None, 'reasoning_content': '用户'}, 'finish_reason': None, 'index': 0, 'logprobs': None}
...
```
deepseek-r1 first normal output
```
...
{'delta': {'content': ' main', 'function_call': None, 'refusal': None, 'role': None, 'tool_calls': None, 'reasoning_content': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}
{'delta': {'content': '\n\nimport', 'function_call': None, 'refusal': None, 'role': None, 'tool_calls': None, 'reasoning_content': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}
...
```
---------
Co-authored-by: ccurme <chester.curme@gmail.com>
This commit is contained in:
@@ -255,12 +255,14 @@ class ChatDeepSeek(BaseChatOpenAI):
|
||||
if (choices := chunk.get("choices")) and generation_chunk:
|
||||
top = choices[0]
|
||||
if isinstance(generation_chunk.message, AIMessageChunk):
|
||||
if reasoning_content := top.get("delta", {}).get("reasoning_content"):
|
||||
if (
|
||||
reasoning_content := top.get("delta", {}).get("reasoning_content")
|
||||
) is not None:
|
||||
generation_chunk.message.additional_kwargs["reasoning_content"] = (
|
||||
reasoning_content
|
||||
)
|
||||
# Handle use via OpenRouter
|
||||
elif reasoning := top.get("delta", {}).get("reasoning"):
|
||||
elif (reasoning := top.get("delta", {}).get("reasoning")) is not None:
|
||||
generation_chunk.message.additional_kwargs["reasoning_content"] = (
|
||||
reasoning
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user