本地RAG尝试
This commit is contained in:
@@ -119,6 +119,7 @@ def _handle_ai_response():
|
||||
api_thought = ""
|
||||
display_text = ""
|
||||
display_thought = ""
|
||||
rag_sources = None # 存储 RAG 检索来源信息
|
||||
|
||||
# 调用流式 API
|
||||
stream = api_client.chat_stream(
|
||||
@@ -213,6 +214,25 @@ def _handle_ai_response():
|
||||
last_msg = messages_update[-1] if messages_update else {}
|
||||
if isinstance(last_msg, dict) and last_msg.get("role") == "tool":
|
||||
tool_name = last_msg.get("name", "unknown")
|
||||
tool_content = last_msg.get("content", "")
|
||||
# 存储 RAG 检索结果
|
||||
if tool_name == "search_knowledge_base":
|
||||
# 尝试解析 tool_content,它可能是 JSON 字符串
|
||||
sources = []
|
||||
try:
|
||||
if isinstance(tool_content, str):
|
||||
import json
|
||||
data = json.loads(tool_content)
|
||||
else:
|
||||
data = tool_content
|
||||
# 提取来源列表
|
||||
if isinstance(data, dict) and "sources" in data:
|
||||
sources = data["sources"]
|
||||
else:
|
||||
sources = [str(data)]
|
||||
except Exception:
|
||||
sources = [str(tool_content)]
|
||||
rag_sources = sources
|
||||
tool_status_placeholder.success(f"✅ 工具 {tool_name} 执行完成")
|
||||
# 短暂显示后清除,保持界面清爽
|
||||
import time
|
||||
@@ -270,6 +290,31 @@ def _handle_ai_response():
|
||||
# 移除光标
|
||||
message_placeholder.markdown(display_text)
|
||||
|
||||
# 显示 RAG 检索来源(如果有)
|
||||
if rag_sources:
|
||||
with st.expander("🔍 检索来源", expanded=False):
|
||||
# 格式化来源列表
|
||||
if isinstance(rag_sources, list):
|
||||
for i, source in enumerate(rag_sources, 1):
|
||||
if isinstance(source, dict):
|
||||
content = source.get("page_content", source.get("content", str(source)))
|
||||
metadata = source.get("metadata", {})
|
||||
filename = metadata.get("filename", metadata.get("source", "未知文件"))
|
||||
page = metadata.get("page", metadata.get("page_number", ""))
|
||||
if page:
|
||||
source_info = f"**来源 {i}:** {filename} (第{page}页)"
|
||||
else:
|
||||
source_info = f"**来源 {i}:** {filename}"
|
||||
st.markdown(source_info)
|
||||
# 显示内容预览(前200字符)
|
||||
preview = content[:200] + "..." if len(content) > 200 else content
|
||||
st.markdown(f"> {preview}")
|
||||
st.markdown("---")
|
||||
else:
|
||||
st.markdown(f"**来源 {i}:** {str(source)}")
|
||||
else:
|
||||
st.markdown(str(rag_sources))
|
||||
|
||||
# 拼装包含思考过程的完整内容,以便后续在历史中正确渲染
|
||||
final_content = display_text
|
||||
if display_thought:
|
||||
|
||||
Reference in New Issue
Block a user