Skip to content

Commit 0b75445

Browse files
committed
refac
1 parent 139206f commit 0b75445

1 file changed

Lines changed: 55 additions & 7 deletions

File tree

backend/open_webui/utils/middleware.py

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2905,6 +2905,13 @@ def update_assistant_message_from_stream(assistant_message, raw):
29052905
if not isinstance(line, str):
29062906
return
29072907

2908+
def append_output_text(item, text):
2909+
parts = item.setdefault('content', [])
2910+
if parts and parts[-1].get('type') == 'output_text':
2911+
parts[-1]['text'] += text
2912+
else:
2913+
parts.append({'type': 'output_text', 'text': text})
2914+
29082915
for raw_part in line.splitlines():
29092916
part = raw_part.removeprefix('data:').strip()
29102917
if not part or part == '[DONE]':
@@ -2932,8 +2939,50 @@ def update_assistant_message_from_stream(assistant_message, raw):
29322939
assistant_message['usage'] = merge_usage(assistant_message.get('usage'), raw_usage)
29332940

29342941
for choice in data.get('choices', []):
2935-
content = (choice.get('delta', {}) or {}).get('content')
2942+
delta = choice.get('delta', {}) or {}
2943+
content = delta.get('content')
2944+
reasoning_content = delta.get('reasoning_content') or delta.get('reasoning') or delta.get('thinking')
2945+
2946+
if reasoning_content:
2947+
output = assistant_message.setdefault('output', [])
2948+
if not output or output[-1].get('type') != 'reasoning':
2949+
output.append(
2950+
{
2951+
'type': 'reasoning',
2952+
'id': output_id('r'),
2953+
'status': 'in_progress',
2954+
'start_tag': '<think>',
2955+
'end_tag': '</think>',
2956+
'attributes': {'type': 'reasoning_content'},
2957+
'content': [],
2958+
'summary': None,
2959+
'started_at': time.time(),
2960+
}
2961+
)
2962+
2963+
append_output_text(output[-1], reasoning_content)
2964+
29362965
if content:
2966+
output = assistant_message.get('output')
2967+
if output:
2968+
if output[-1].get('type') == 'reasoning':
2969+
output[-1]['status'] = 'completed'
2970+
output[-1]['ended_at'] = time.time()
2971+
output[-1]['duration'] = int(output[-1]['ended_at'] - output[-1]['started_at'])
2972+
2973+
if not output or output[-1].get('type') != 'message':
2974+
output.append(
2975+
{
2976+
'type': 'message',
2977+
'id': output_id('msg'),
2978+
'status': 'in_progress',
2979+
'role': 'assistant',
2980+
'content': [],
2981+
}
2982+
)
2983+
2984+
append_output_text(output[-1], content)
2985+
29372986
assistant_message['content'] = assistant_message.get('content', '') + content
29382987

29392988

@@ -3395,10 +3444,12 @@ async def non_streaming_chat_response_handler(response, ctx):
33953444
)
33963445

33973446
choices = response_data.get('choices', [])
3398-
if choices and choices[0].get('message', {}).get('content'):
3399-
content = response_data['choices'][0]['message']['content']
3447+
response_output = response_data.get('output')
3448+
content = choices[0].get('message', {}).get('content') if choices else ''
34003449

3401-
if content:
3450+
if choices and (content or response_output):
3451+
3452+
if content or response_output:
34023453
await event_emitter(
34033454
{
34043455
'type': 'chat:completion',
@@ -3414,7 +3465,6 @@ async def non_streaming_chat_response_handler(response, ctx):
34143465

34153466
# Use output from backend if provided (OR-compliant backends),
34163467
# otherwise generate from response content
3417-
response_output = response_data.get('output')
34183468
if not response_output:
34193469
choice_message = choices[0].get('message', {})
34203470
reasoning_content = choice_message.get('reasoning_content') or choice_message.get('reasoning')
@@ -3453,7 +3503,6 @@ async def non_streaming_chat_response_handler(response, ctx):
34533503
'type': 'chat:completion',
34543504
'data': {
34553505
'done': True,
3456-
'content': content,
34573506
'output': response_output,
34583507
'title': title,
34593508
},
@@ -3470,7 +3519,6 @@ async def non_streaming_chat_response_handler(response, ctx):
34703519
{
34713520
'done': True,
34723521
'role': 'assistant',
3473-
'content': content,
34743522
'output': response_output,
34753523
**({'usage': usage} if usage else {}),
34763524
},

0 commit comments

Comments
 (0)