chenge commited on
Commit
59bc8b3
·
1 Parent(s): fb8117a

think 不折叠

Browse files
Files changed (1) hide show
  1. app.py +29 -9
app.py CHANGED
@@ -451,6 +451,7 @@ class Gradio_Events:
451
  in_thinking = False
452
  history[-1]["meta"]["is_thinking"] = False
453
  history[-1]["meta"]["thinking_done"] = True
 
454
 
455
  # 分离thinking内容和后续内容
456
  think_parts = accumulated_content.split("</think>", 1)
@@ -466,10 +467,15 @@ class Gradio_Events:
466
  history[-1]["content"] = current_content + after_think_content
467
 
468
  accumulated_content = "" # 重置累积内容
 
469
  yield {
470
  chatbot: gr.update(items=history),
471
  state: gr.update(value=state_value)
472
  }
 
 
 
 
473
  continue
474
 
475
  # 如果在thinking模式中,只更新thinking内容,不修改content
@@ -484,6 +490,10 @@ class Gradio_Events:
484
  if not history[-1]["content"]: # 如果content为空才初始化
485
  history[-1]["content"] = ""
486
  history[-1]["content"] += content
 
 
 
 
487
 
488
  yield {
489
  chatbot: gr.update(items=history),
@@ -1346,23 +1356,33 @@ with gr.Blocks(css=css, fill_width=True) as demo:
1346
  const thinking_content = bubble?.meta?.thinking_content || ""
1347
  const is_thinking = bubble?.meta?.is_thinking || false
1348
  const thinking_done = bubble?.meta?.thinking_done || false
 
1349
 
1350
- // 判断是否应该折叠thinking区域
1351
- // 当thinking完成且有正式回答内容时,自动折叠
1352
- const shouldCollapse = thinking_done && content && content.trim().length > 0
1353
 
1354
- // 使用key来重新渲染组件,触发新的默认状态
1355
- const collapseKey = thinking_done ? 'collapsed' : 'expanded'
 
 
 
 
 
 
 
 
 
 
 
1356
 
1357
  return {
1358
- thinking_collapse_props: {
1359
- key: collapseKey, // 重新渲染组件以应用新的默认状态
1360
- default_active_key: shouldCollapse ? [] : ['1'],
1361
  style: {
1362
  display: (thinking_content || is_thinking) ? 'block' : 'none',
1363
  marginBottom: thinking_content || is_thinking ? '12px' : '0'
1364
  }
1365
- },
1366
  thinking_label: is_thinking ? '🤔 正在思考...' : '🤔 思考过程',
1367
  thinking_markdown: {
1368
  value: thinking_content || '思考中...'
 
451
  in_thinking = False
452
  history[-1]["meta"]["is_thinking"] = False
453
  history[-1]["meta"]["thinking_done"] = True
454
+ history[-1]["meta"]["just_finished_thinking"] = True # 标记刚完成thinking
455
 
456
  # 分离thinking内容和后续内容
457
  think_parts = accumulated_content.split("</think>", 1)
 
467
  history[-1]["content"] = current_content + after_think_content
468
 
469
  accumulated_content = "" # 重置累积内容
470
+ # 先yield一次,让前端检测到just_finished_thinking状态
471
  yield {
472
  chatbot: gr.update(items=history),
473
  state: gr.update(value=state_value)
474
  }
475
+
476
+ # 然后清除just_finished_thinking标记(如果有内容的话)
477
+ if history[-1]["content"] and history[-1]["content"].strip():
478
+ history[-1]["meta"]["just_finished_thinking"] = False
479
  continue
480
 
481
  # 如果在thinking模式中,只更新thinking内容,不修改content
 
490
  if not history[-1]["content"]: # 如果content为空才初始化
491
  history[-1]["content"] = ""
492
  history[-1]["content"] += content
493
+ # 清除"刚完成thinking"标记,因为现在在正常输出内容
494
+ # 只在第一次输出内容时清除标记
495
+ if history[-1]["meta"].get("just_finished_thinking") and history[-1]["content"].strip():
496
+ history[-1]["meta"]["just_finished_thinking"] = False
497
 
498
  yield {
499
  chatbot: gr.update(items=history),
 
1356
  const thinking_content = bubble?.meta?.thinking_content || ""
1357
  const is_thinking = bubble?.meta?.is_thinking || false
1358
  const thinking_done = bubble?.meta?.thinking_done || false
1359
+ const just_finished_thinking = bubble?.meta?.just_finished_thinking || false
1360
 
1361
+ // 只在刚完成thinking且有回答内容时自动折叠
1362
+ const shouldAutoCollapse = just_finished_thinking && content && content.trim().length > 0
 
1363
 
1364
+ // 使用组合的key和状态策略
1365
+ let collapseKey = 'thinking'
1366
+ let collapseProps = {}
1367
+
1368
+ if (shouldAutoCollapse) {
1369
+ // 刚完成thinking且有内容时,强制折叠一次
1370
+ collapseKey = 'thinking-auto-collapsed'
1371
+ collapseProps.active_key = []
1372
+ } else {
1373
+ // 其他情况使用默认行为,允许用户控制
1374
+ collapseKey = thinking_done ? 'thinking-done' : 'thinking-active'
1375
+ collapseProps.default_active_key = ['1']
1376
+ }
1377
 
1378
  return {
1379
+ thinking_collapse_props: Object.assign({
1380
+ key: collapseKey,
 
1381
  style: {
1382
  display: (thinking_content || is_thinking) ? 'block' : 'none',
1383
  marginBottom: thinking_content || is_thinking ? '12px' : '0'
1384
  }
1385
+ }, collapseProps), // 动态属性:active_key 或 default_active_key
1386
  thinking_label: is_thinking ? '🤔 正在思考...' : '🤔 思考过程',
1387
  thinking_markdown: {
1388
  value: thinking_content || '思考中...'