mirror of
https://github.com/open-webui/open-webui
synced 2025-05-19 20:57:54 +00:00
refac: more reasoning tags support
This commit is contained in:
parent
d42f811a8d
commit
10ffbca34b
@ -1074,6 +1074,9 @@ async def process_chat_response(
|
|||||||
|
|
||||||
# We might want to disable this by default
|
# We might want to disable this by default
|
||||||
detect_reasoning = True
|
detect_reasoning = True
|
||||||
|
reasoning_tags = ["think", "reason", "reasoning", "thought"]
|
||||||
|
current_tag = None
|
||||||
|
|
||||||
reasoning_start_time = None
|
reasoning_start_time = None
|
||||||
|
|
||||||
reasoning_content = ""
|
reasoning_content = ""
|
||||||
@ -1116,13 +1119,20 @@ async def process_chat_response(
|
|||||||
content = f"{content}{value}"
|
content = f"{content}{value}"
|
||||||
|
|
||||||
if detect_reasoning:
|
if detect_reasoning:
|
||||||
if "<think>\n" in content:
|
for tag in reasoning_tags:
|
||||||
# Remove the <think> tag
|
start_tag = f"<{tag}>\n"
|
||||||
content = content.replace("<think>\n", "")
|
end_tag = f"</{tag}>\n"
|
||||||
ongoing_content = content
|
|
||||||
|
|
||||||
reasoning_start_time = time.time()
|
if start_tag in content:
|
||||||
reasoning_content = ""
|
# Remove the start tag
|
||||||
|
content = content.replace(start_tag, "")
|
||||||
|
ongoing_content = content
|
||||||
|
|
||||||
|
reasoning_start_time = time.time()
|
||||||
|
reasoning_content = ""
|
||||||
|
|
||||||
|
current_tag = tag
|
||||||
|
break
|
||||||
|
|
||||||
if reasoning_start_time is not None:
|
if reasoning_start_time is not None:
|
||||||
# Remove the last value from the content
|
# Remove the last value from the content
|
||||||
@ -1130,16 +1140,18 @@ async def process_chat_response(
|
|||||||
|
|
||||||
reasoning_content += value
|
reasoning_content += value
|
||||||
|
|
||||||
if "</think>\n" in reasoning_content:
|
end_tag = f"</{current_tag}>\n"
|
||||||
|
if end_tag in reasoning_content:
|
||||||
reasoning_end_time = time.time()
|
reasoning_end_time = time.time()
|
||||||
reasoning_duration = int(
|
reasoning_duration = int(
|
||||||
reasoning_end_time
|
reasoning_end_time
|
||||||
- reasoning_start_time
|
- reasoning_start_time
|
||||||
)
|
)
|
||||||
|
|
||||||
reasoning_content = (
|
reasoning_content = (
|
||||||
reasoning_content.strip("<think>\n")
|
reasoning_content.strip(
|
||||||
.strip("</think>\n")
|
f"<{current_tag}>\n"
|
||||||
|
)
|
||||||
|
.strip(end_tag)
|
||||||
.strip()
|
.strip()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user