Skip to content

Commit 73b4e95

Browse files
committed
Issue #116: Support <think> tags
1 parent 4095c2e commit 73b4e95

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/api/models/bedrock.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ def chat_stream(self, chat_request: ChatRequest) -> AsyncIterable[bytes]:
211211
response = self._invoke_bedrock(chat_request, stream=True)
212212
message_id = self.generate_message_id()
213213
stream = response.get("stream")
214+
self.think_emitted = False
214215
for chunk in stream:
215216
stream_response = self._create_response_stream(
216217
model_id=chat_request.model, message_id=message_id, chunk=chunk
@@ -235,6 +236,7 @@ def chat_stream(self, chat_request: ChatRequest) -> AsyncIterable[bytes]:
235236

236237
# return an [DONE] message at the end.
237238
yield self.stream_response_to_bytes()
239+
self.think_emitted = False # Cleanup
238240

239241
def _parse_system_prompts(self, chat_request: ChatRequest) -> list[dict[str, str]]:
240242
"""Create system prompts.
@@ -498,6 +500,9 @@ def _create_response(
498500
message.content = c["text"]
499501
else:
500502
logger.warning("Unknown tag in message content " + ",".join(c.keys()))
503+
if message.reasoning_content:
504+
message.content = f"<think>{message.reasoning_content}</think>{message.content}"
505+
message.reasoning_content = None
501506

502507
response = ChatResponse(
503508
id=message_id,
@@ -566,11 +571,19 @@ def _create_response_stream(
566571
content=delta["text"],
567572
)
568573
elif "reasoningContent" in delta:
569-
# ignore "signature" in the delta.
570574
if "text" in delta["reasoningContent"]:
571-
message = ChatResponseMessage(
572-
reasoning_content=delta["reasoningContent"]["text"],
573-
)
575+
content = delta["reasoningContent"]["text"]
576+
if not self.think_emitted:
577+
# Port of "content_block_start" with "thinking"
578+
content = "<think>" + content
579+
self.think_emitted = True
580+
message = ChatResponseMessage(content=content)
581+
elif "signature" in delta["reasoningContent"]:
582+
# Port of "signature_delta"
583+
if self.think_emitted:
584+
message = ChatResponseMessage(content="\n </think> \n\n")
585+
else:
586+
return None # Ignore signature if no <think> started
574587
else:
575588
# tool use
576589
index = chunk["contentBlockDelta"]["contentBlockIndex"] - 1

0 commit comments

Comments
 (0)