增加了网络搜索前的回答

This commit is contained in:
sladro 2026-01-31 11:14:07 +08:00
parent 8b6c69aa57
commit 80eeae1cdc

View File

@ -1,10 +1,11 @@
import json
import hashlib
import re
import time
import httpx
from datetime import datetime
from typing import Any, Dict, List, Optional
from typing import Any, Dict, Optional
from config.env import SearchConfig
from utils.log_util import logger
from fastapi.responses import StreamingResponse
@ -229,6 +230,9 @@ class SearchService:
async def _stream_baidu_search(cls, question: str):
"""Stream Search + Answer using ZhipuAI (GLM-4)"""
# Note: Method name kept for compatibility, but logic switched to ZhipuAI
# 先回一条提示,避免联网搜索阶段用户感知“卡住”
yield format_sse({'data': '请稍等,我来搜索一下答案…'})
if not SearchConfig.ZHIPUAI_API_KEY:
yield format_sse({'message': 'Configuration Error: Missing ZHIPUAI_API_KEY. Please set valid API key.'}, event='error')
@ -268,9 +272,11 @@ class SearchService:
return
async for line in response.aiter_lines():
if not line.strip(): continue
if not line.strip():
continue
data_str = line[6:].strip() if line.startswith('data: ') else line
if not data_str or data_str == '[DONE]': continue
if not data_str or data_str == '[DONE]':
continue
try:
chunk = json.loads(data_str)
@ -340,7 +346,8 @@ class SearchService:
if choices:
content = choices[0]['message']['content'].strip().upper()
# Simple heuristic cleanup
if 'SEARCH' in content: return 'SEARCH'
if 'SEARCH' in content:
return 'SEARCH'
return 'RAG'
except Exception:
pass # Fail safe to RAG (or logic below)