From c5af48c45b729747571e877a13adec789ca0b57c Mon Sep 17 00:00:00 2001 From: Takahiro Kikumoto Date: Tue, 18 Mar 2025 10:34:17 +0900 Subject: [PATCH] Add support for outputting reasoning process in DeepSeek - Modified `stream_response` method to handle reasoning content blocks. - Added logic to yield "" and "" tags around reasoning content. --- .../providers/aws_bedrock_deepseek_pipeline.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/examples/pipelines/providers/aws_bedrock_deepseek_pipeline.py b/examples/pipelines/providers/aws_bedrock_deepseek_pipeline.py index 310958a..8f6512e 100644 --- a/examples/pipelines/providers/aws_bedrock_deepseek_pipeline.py +++ b/examples/pipelines/providers/aws_bedrock_deepseek_pipeline.py @@ -165,9 +165,22 @@ class Pipeline: def stream_response(self, model_id: str, payload: dict) -> Generator: streaming_response = self.bedrock_runtime.converse_stream(**payload) + + in_resasoning_context = False for chunk in streaming_response["stream"]: - if "contentBlockDelta" in chunk and "text" in chunk["contentBlockDelta"]["delta"]: - yield chunk["contentBlockDelta"]["delta"]["text"] + if in_resasoning_context and "contentBlockStop" in chunk: + in_resasoning_context = False + yield "\n \n\n" + elif "contentBlockDelta" in chunk and "delta" in chunk["contentBlockDelta"]: + if "reasoningContent" in chunk["contentBlockDelta"]["delta"]: + if not in_resasoning_context: + yield "" + + in_resasoning_context = True + if "text" in chunk["contentBlockDelta"]["delta"]["reasoningContent"]: + yield chunk["contentBlockDelta"]["delta"]["reasoningContent"]["text"] + elif "text" in chunk["contentBlockDelta"]["delta"]: + yield chunk["contentBlockDelta"]["delta"]["text"] def get_completion(self, model_id: str, payload: dict) -> str: response = self.bedrock_runtime.converse(**payload)