refac
This commit is contained in:
1 parent
c98d8ecacc
commit
fa2abe4cb6
1 file changed
+44
-4
@@ -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,6 +4305,20 @@ 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:
|
||||||
|
reasoning_item = (
|
||||||
|
next(
|
||||||
|
(
|
||||||
|
item
|
||||||
|
for item in reversed(output)
|
||||||
|
if item.get('type') == 'reasoning'
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
if reasoning_details and not reasoning_content
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
|
||||||
|
if reasoning_item is None:
|
||||||
if not output or output[-1].get('type') != 'reasoning':
|
if not output or output[-1].get('type') != 'reasoning':
|
||||||
reasoning_item = {
|
reasoning_item = {
|
||||||
'type': 'reasoning',
|
'type': 'reasoning',
|
||||||
@@ -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