AlauStone commited on
Commit
f866cb1
·
verified ·
1 Parent(s): 247ddd3

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -40
app.py CHANGED
@@ -868,16 +868,17 @@ with st.sidebar:
868
  # --- 模型设置 ---
869
  with st.expander("⚙️ 模型设置"):
870
  selected_display_name = st.selectbox(
871
- "模型", list(model_mapping.keys()), index=0, label_visibility="collapsed"
 
872
  )
873
 
874
- web_on = st.toggle("🌐 联网增强", value=False)
875
 
876
  c1, c2 = st.columns(2)
877
  with c1:
878
- ui_top_k = st.number_input("Top-K", 1, 15, 5)
879
  with c2:
880
- ui_threshold = st.number_input("阈值", 0.0, 1.0, 0.25, step=0.05)
881
 
882
  # --- 修改密码 ---
883
  with st.expander("🔐 修改密码"):
@@ -1125,7 +1126,7 @@ def llm_answer(query, context_docs, selected_display_name, web_enabled):
1125
 
1126
 
1127
  # =========================
1128
- # 10. 聊天渲染
1129
  # =========================
1130
  if "messages" not in st.session_state:
1131
  # 首次加载时从数据库恢复最近对话
@@ -1136,40 +1137,53 @@ if "messages" not in st.session_state:
1136
  for m in saved
1137
  ]
1138
 
1139
- for m in st.session_state.messages:
1140
- with st.chat_message(m["role"]):
1141
- st.markdown(m["content"])
1142
- if "meta" in m:
1143
- st.caption(m["meta"])
1144
-
1145
- if q := st.chat_input("输入问题...", key="chat_input_v3"):
1146
- st.session_state.messages.append({"role": "user", "content": q})
1147
- _save_chat_message(CURRENT_USER, "user", q)
1148
- with st.chat_message("user"):
1149
- st.markdown(q)
1150
-
1151
- with st.chat_message("assistant"):
1152
- container = st.empty()
1153
- container.markdown("*🔍 正在搜索知识库...*")
1154
- relevant_docs = search_local(q, ui_top_k, ui_threshold)
1155
- container.markdown("*🤔 正在组织语言...*")
1156
-
1157
- if web_on:
1158
- with st.status("🌐 正在抓取实时网络数据...", expanded=False) as s:
1159
- time.sleep(0.1)
1160
- s.update(label="✅ 网络资料已就绪", state="complete")
1161
 
1162
- try:
1163
- full_response = container.write_stream(
1164
- llm_answer(q, relevant_docs, selected_display_name, web_on)
1165
- )
1166
- meta_info = st.session_state.get("last_meta", "")
1167
- st.caption(meta_info)
1168
- st.session_state.messages.append(
1169
- {"role": "assistant", "content": full_response, "meta": meta_info}
1170
- )
1171
- _save_chat_message(CURRENT_USER, "assistant", full_response, meta_info)
1172
- except Exception as e:
1173
- logger.error(f"模型调用异常: {e}")
1174
- container.error(f" 抱歉,连接模型时出错了: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1175
 
 
868
  # --- 模型设置 ---
869
  with st.expander("⚙️ 模型设置"):
870
  selected_display_name = st.selectbox(
871
+ "模型", list(model_mapping.keys()), index=0, label_visibility="collapsed",
872
+ key="sel_model"
873
  )
874
 
875
+ web_on = st.toggle("🌐 联网增强", value=False, key="sel_web")
876
 
877
  c1, c2 = st.columns(2)
878
  with c1:
879
+ ui_top_k = st.number_input("Top-K", 1, 15, 5, key="sel_topk")
880
  with c2:
881
+ ui_threshold = st.number_input("阈值", 0.0, 1.0, 0.25, step=0.05, key="sel_threshold")
882
 
883
  # --- 修改密码 ---
884
  with st.expander("🔐 修改密码"):
 
1126
 
1127
 
1128
  # =========================
1129
+ # 10. 聊天渲染(使用 @st.fragment 避免整页刷新)
1130
  # =========================
1131
  if "messages" not in st.session_state:
1132
  # 首次加载时从数据库恢复最近对话
 
1137
  for m in saved
1138
  ]
1139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1140
 
1141
+ @st.fragment
1142
+ def _chat_fragment():
1143
+ """聊天区域独立 fragment,提交消息时只刷新此区域,不刷新整个页面。"""
1144
+ # 从 session_state 读取侧边栏设置(fragment 内部无法直接访问外部局部变量)
1145
+ _model_name = st.session_state.get("sel_model", list(model_mapping.keys())[0])
1146
+ _web_on = st.session_state.get("sel_web", False)
1147
+ _top_k = st.session_state.get("sel_topk", 5)
1148
+ _threshold = st.session_state.get("sel_threshold", 0.25)
1149
+
1150
+ for m in st.session_state.messages:
1151
+ with st.chat_message(m["role"]):
1152
+ st.markdown(m["content"])
1153
+ if "meta" in m:
1154
+ st.caption(m["meta"])
1155
+
1156
+ if q := st.chat_input("输入问题...", key="chat_input_v3"):
1157
+ st.session_state.messages.append({"role": "user", "content": q})
1158
+ _save_chat_message(CURRENT_USER, "user", q)
1159
+ with st.chat_message("user"):
1160
+ st.markdown(q)
1161
+
1162
+ with st.chat_message("assistant"):
1163
+ container = st.empty()
1164
+ container.markdown("*🔍 正在搜索知识库...*")
1165
+ relevant_docs = search_local(q, _top_k, _threshold)
1166
+ container.markdown("*🤔 正在组织语言...*")
1167
+
1168
+ if _web_on:
1169
+ with st.status("🌐 正在抓取实时网络数据...", expanded=False) as s:
1170
+ time.sleep(0.1)
1171
+ s.update(label="✅ 网络资料已就绪", state="complete")
1172
+
1173
+ try:
1174
+ full_response = container.write_stream(
1175
+ llm_answer(q, relevant_docs, _model_name, _web_on)
1176
+ )
1177
+ meta_info = st.session_state.get("last_meta", "")
1178
+ st.caption(meta_info)
1179
+ st.session_state.messages.append(
1180
+ {"role": "assistant", "content": full_response, "meta": meta_info}
1181
+ )
1182
+ _save_chat_message(CURRENT_USER, "assistant", full_response, meta_info)
1183
+ except Exception as e:
1184
+ logger.error(f"模型调用异常: {e}")
1185
+ container.error(f"❌ 抱歉,连接模型时��错了: {str(e)}")
1186
+
1187
+
1188
+ _chat_fragment()
1189