refac
This commit is contained in:
@@ -157,6 +157,29 @@ def output_id(prefix: str) -> str:
|
|||||||
return f'{prefix}_{uuid4().hex[:24]}'
|
return f'{prefix}_{uuid4().hex[:24]}'
|
||||||
|
|
||||||
|
|
||||||
|
def merge_streamed_reasoning_details(target: list, details) -> None:
|
||||||
|
items = details if isinstance(details, list) else [details]
|
||||||
|
for item in items:
|
||||||
|
if not isinstance(item, dict):
|
||||||
|
continue
|
||||||
|
|
||||||
|
index = item.get('index')
|
||||||
|
existing = (
|
||||||
|
next((detail for detail in target if detail.get('index') == index), None)
|
||||||
|
if isinstance(index, int)
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
if existing is None:
|
||||||
|
target.append(dict(item))
|
||||||
|
continue
|
||||||
|
|
||||||
|
for key, value in item.items():
|
||||||
|
if key in ('text', 'summary') and isinstance(value, str) and isinstance(existing.get(key), str):
|
||||||
|
existing[key] += value
|
||||||
|
else:
|
||||||
|
existing[key] = value
|
||||||
|
|
||||||
|
|
||||||
def _split_tool_calls(
|
def _split_tool_calls(
|
||||||
tool_calls: list[dict],
|
tool_calls: list[dict],
|
||||||
) -> list[dict]:
|
) -> list[dict]:
|
||||||
@@ -4282,21 +4305,35 @@ async def streaming_chat_response_handler(response, ctx):
|
|||||||
)
|
)
|
||||||
reasoning_details = delta.get('reasoning_details')
|
reasoning_details = delta.get('reasoning_details')
|
||||||
if reasoning_content or reasoning_details:
|
if reasoning_content or reasoning_details:
|
||||||
if not output or output[-1].get('type') != 'reasoning':
|
reasoning_item = (
|
||||||
reasoning_item = {
|
next(
|
||||||
'type': 'reasoning',
|
(
|
||||||
'id': output_id('r'),
|
item
|
||||||
'status': 'in_progress',
|
for item in reversed(output)
|
||||||
'start_tag': '<think>',
|
if item.get('type') == 'reasoning'
|
||||||
'end_tag': '</think>',
|
),
|
||||||
'attributes': {'type': 'reasoning_content'},
|
None,
|
||||||
'content': [],
|
)
|
||||||
'summary': None,
|
if reasoning_details and not reasoning_content
|
||||||
'started_at': time.time(),
|
else None
|
||||||
}
|
)
|
||||||
output.append(reasoning_item)
|
|
||||||
else:
|
if reasoning_item is None:
|
||||||
reasoning_item = output[-1]
|
if not output or output[-1].get('type') != 'reasoning':
|
||||||
|
reasoning_item = {
|
||||||
|
'type': 'reasoning',
|
||||||
|
'id': output_id('r'),
|
||||||
|
'status': 'in_progress',
|
||||||
|
'start_tag': '<think>',
|
||||||
|
'end_tag': '</think>',
|
||||||
|
'attributes': {'type': 'reasoning_content'},
|
||||||
|
'content': [],
|
||||||
|
'summary': None,
|
||||||
|
'started_at': time.time(),
|
||||||
|
}
|
||||||
|
output.append(reasoning_item)
|
||||||
|
else:
|
||||||
|
reasoning_item = output[-1]
|
||||||
|
|
||||||
if reasoning_content:
|
if reasoning_content:
|
||||||
# Append to reasoning content
|
# Append to reasoning content
|
||||||
@@ -4317,11 +4354,14 @@ async def streaming_chat_response_handler(response, ctx):
|
|||||||
delta_type = 'content'
|
delta_type = 'content'
|
||||||
|
|
||||||
if reasoning_details:
|
if reasoning_details:
|
||||||
reasoning_item.setdefault('reasoning_details', []).extend(
|
merge_streamed_reasoning_details(
|
||||||
reasoning_details
|
reasoning_item.setdefault('reasoning_details', []),
|
||||||
if isinstance(reasoning_details, list)
|
reasoning_details,
|
||||||
else [reasoning_details]
|
|
||||||
)
|
)
|
||||||
|
data = {
|
||||||
|
'output': full_output(),
|
||||||
|
}
|
||||||
|
delta_type = 'content'
|
||||||
|
|
||||||
if value:
|
if value:
|
||||||
if (
|
if (
|
||||||
|
|||||||
Reference in New Issue
Block a user