refac/enh: reasoning tag handling

This commit is contained in:
Timothy Jaeryang Baek 2025-07-16 15:20:03 +04:00
parent 980b7c55a5
commit 83c09f15ac

View file

@ -1574,8 +1574,18 @@ async def process_chat_response(
if content_blocks[-1]["type"] == "text": if content_blocks[-1]["type"] == "text":
for start_tag, end_tag in tags: for start_tag, end_tag in tags:
# Match start tag e.g., <tag> or <tag attr="value">
start_tag_pattern = rf"<{re.escape(start_tag)}(\s.*?)?>" if start_tag.startswith("<") and end_tag.endswith(">"):
# Match start tag e.g., <tag> or <tag attr="value">
# remove both '<' and '>' from start_tag
start_tag = start_tag[1:-1]
# Match start tag with attributes
start_tag_pattern = rf"<{re.escape(start_tag)}(\s.*?)?>"
else:
# Handle cases where start_tag is just a tag name
start_tag_pattern = rf"{re.escape(start_tag)}"
match = re.search(start_tag_pattern, content) match = re.search(start_tag_pattern, content)
if match: if match:
attr_content = ( attr_content = (
@ -1626,8 +1636,13 @@ async def process_chat_response(
elif content_blocks[-1]["type"] == content_type: elif content_blocks[-1]["type"] == content_type:
start_tag = content_blocks[-1]["start_tag"] start_tag = content_blocks[-1]["start_tag"]
end_tag = content_blocks[-1]["end_tag"] end_tag = content_blocks[-1]["end_tag"]
# Match end tag e.g., </tag>
end_tag_pattern = rf"<{re.escape(end_tag)}>" if end_tag.startswith("<") and end_tag.endswith(">"):
# Match end tag e.g., </tag>
end_tag_pattern = rf"{re.escape(end_tag)}"
else:
# Handle cases where end_tag is just a tag name
end_tag_pattern = rf"{re.escape(end_tag)}"
# Check if the content has the end tag # Check if the content has the end tag
if re.search(end_tag_pattern, content): if re.search(end_tag_pattern, content):
@ -1744,18 +1759,19 @@ async def process_chat_response(
) )
reasoning_tags = [ reasoning_tags = [
("think", "/think"), ("<think>", "</think>"),
("thinking", "/thinking"), ("<thinking>", "</thinking>"),
("reason", "/reason"), ("<reason>", "</reason>"),
("reasoning", "/reasoning"), ("<reasoning>", "</reasoning>"),
("thought", "/thought"), ("<thought>", "</thought>"),
("Thought", "/Thought"), ("<Thought>", "</Thought>"),
("|begin_of_thought|", "|end_of_thought|"), ("<|begin_of_thought|>", "<|end_of_thought|>"),
("◁think▷", "◁/think▷"),
] ]
code_interpreter_tags = [("code_interpreter", "/code_interpreter")] code_interpreter_tags = [("<code_interpreter>", "</code_interpreter>")]
solution_tags = [("|begin_of_solution|", "|end_of_solution|")] solution_tags = [("<|begin_of_solution|>", "<|end_of_solution|>")]
try: try:
for event in events: for event in events: