From 4ec7623c47c91b1bcc25918288a1e4139d73a501 Mon Sep 17 00:00:00 2001 From: jknapp Date: Sun, 16 Mar 2025 18:10:57 -0700 Subject: [PATCH] testing thinking --- claude_sonnet-3-7-bedrock.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/claude_sonnet-3-7-bedrock.py b/claude_sonnet-3-7-bedrock.py index 4098b1b..3c12a56 100644 --- a/claude_sonnet-3-7-bedrock.py +++ b/claude_sonnet-3-7-bedrock.py @@ -151,10 +151,10 @@ class Pipeline: "inferenceConfig": {"temperature": 1}, "additionalModelRequestFields": reasoning_config } - # if body.get("stream", False): - # return self.stream_response(model_id, payload) - #else: - return self.get_completion(model_id, payload) + if body.get("stream", False): + return self.stream_response(model_id, payload) + else: + return self.get_completion(model_id, payload) except Exception as e: return f"Error: {e}" @@ -175,14 +175,18 @@ class Pipeline: def stream_response(self, model_id: str, payload: dict) -> Generator: # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/bedrock-runtime/client/converse_stream.html - if "system" in payload: - del payload["system"] - if "additionalModelRequestFields" in payload: - del payload["additionalModelRequestFields"] streaming_response = self.bedrock_runtime.converse_stream(**payload) for chunk in streaming_response["stream"]: if "contentBlockDelta" in chunk: - yield chunk["contentBlockDelta"]["delta"]["text"] + delta = chunk["contentBlockDelta"]["delta"] + + # Handle reasoning content (Chain of Thought) + if "reasoningContent" in delta and "text" in delta["reasoningContent"]: + yield {"type": "reasoning", "content": delta["reasoningContent"]["text"]} + + # Handle regular response text + if "text" in delta: + yield {"type": "response", "content": delta["text"]} def get_completion(self, model_id: str, payload: dict) -> str: # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/bedrock-runtime/client/converse.html