refac
This commit is contained in:
1 parent
a1579a01ff
commit
c755ef60c6
1 file changed
+15
-11
@@ -4490,6 +4490,14 @@ async def streaming_chat_response_handler(response, ctx):
|
|||||||
delta_data.get('summary_index'),
|
delta_data.get('summary_index'),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def get_response_data_with_full_output_index(response_data: dict):
|
||||||
|
if prior_output and isinstance(response_data.get('output_index'), int):
|
||||||
|
return {
|
||||||
|
**response_data,
|
||||||
|
'output_index': response_data['output_index'] + len(prior_output),
|
||||||
|
}
|
||||||
|
return response_data
|
||||||
|
|
||||||
async def flush_pending_delta_data(threshold: int = 0):
|
async def flush_pending_delta_data(threshold: int = 0):
|
||||||
nonlocal delta_count
|
nonlocal delta_count
|
||||||
nonlocal last_delta_data
|
nonlocal last_delta_data
|
||||||
@@ -4515,6 +4523,7 @@ async def streaming_chat_response_handler(response, ctx):
|
|||||||
nonlocal last_delta_type
|
nonlocal last_delta_type
|
||||||
nonlocal last_delta_key
|
nonlocal last_delta_key
|
||||||
|
|
||||||
|
delta_data = get_response_data_with_full_output_index(delta_data)
|
||||||
delta_key = get_response_delta_key(delta_data)
|
delta_key = get_response_delta_key(delta_data)
|
||||||
if (
|
if (
|
||||||
last_delta_data
|
last_delta_data
|
||||||
@@ -4536,25 +4545,20 @@ async def streaming_chat_response_handler(response, ctx):
|
|||||||
if delta_count >= delta_chunk_size:
|
if delta_count >= delta_chunk_size:
|
||||||
await flush_pending_delta_data(delta_chunk_size)
|
await flush_pending_delta_data(delta_chunk_size)
|
||||||
|
|
||||||
async def emit_response_completion_event(response_event: dict, stream_output: list | None = None):
|
async def emit_response_completion_event(response_data: dict, stream_output: list | None = None):
|
||||||
if prior_output and isinstance(response_event.get('output_index'), int):
|
if response_data.get('type', '').endswith('.delta'):
|
||||||
response_event = {
|
|
||||||
**response_event,
|
|
||||||
'output_index': response_event['output_index'] + len(prior_output),
|
|
||||||
}
|
|
||||||
|
|
||||||
if response_event.get('type', '').endswith('.delta'):
|
|
||||||
await queue_pending_delta_data(
|
await queue_pending_delta_data(
|
||||||
response_event,
|
response_data,
|
||||||
response_event.get('type', 'response.delta'),
|
response_data.get('type', 'response.delta'),
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
response_data = get_response_data_with_full_output_index(response_data)
|
||||||
await flush_pending_delta_data()
|
await flush_pending_delta_data()
|
||||||
await event_emitter(
|
await event_emitter(
|
||||||
{
|
{
|
||||||
'type': 'response:completion',
|
'type': 'response:completion',
|
||||||
'data': response_event,
|
'data': response_data,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
await save_current_response_stream(stream_output)
|
await save_current_response_stream(stream_output)
|
||||||
|
|||||||
Reference in new issue
Block a user