kangda-robot-backend/ruoyi-fastapi-backend/test/test_search_llm_standalone.py

140 lines
4.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
独立测试脚本:搜索服务 + LLM处理
不需要启动完整的FastAPI服务直接测试搜索和LLM集成功能
"""
import asyncio
import sys
import os
# 添加项目路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# 加载环境变量
from dotenv import load_dotenv
load_dotenv('.env.dev')
async def test_search_with_llm():
"""测试搜索服务 + LLM处理"""
print("=" * 70)
print("搜索服务 + LLM 独立测试")
print("=" * 70)
# 导入服务
from module_admin.service.search_service import SearchService, SearchServiceError
# 测试问题
test_question = "今天上海的天气怎么样"
print(f"\n测试问题: {test_question}")
print("-" * 70)
# 步骤1: 检查关键词
print("\n步骤1: 检查是否触发搜索服务...")
should_handle = SearchService.should_handle(test_question)
print(f"✓ 关键词检测: {'通过 (将使用搜索服务)' if should_handle else '未通过'}")
if not should_handle:
print("\n⚠️ 问题不包含搜索关键词,不会触发搜索服务")
return
# 步骤2: 搜索服务流式处理 (Bypass RAGFlow)
print("\n步骤2: 测试搜索服务流式响应...")
# 构造请求参数,启用流式
from module_admin.entity.vo.ragflow_vo import ConverseWithChatAssistantModel
converse_params = ConverseWithChatAssistantModel(
chat_id="test-chat-id",
question=test_question,
stream=True
)
try:
# 调用 handle_search_chat
print("\n正在获取流式搜索结果...")
response = await SearchService.handle_search_chat(converse_params, redis=None)
# 验证是否为 StreamingResponse
from fastapi.responses import StreamingResponse
if isinstance(response, StreamingResponse):
print("\n【搜索智能回答 (流式)】")
search_context = ""
async for line in response.body_iterator:
line = line.strip()
if not line: continue
if line.startswith('data:'):
data_str = line[5:].strip()
try:
data_json = json.loads(data_str)
content = data_json.get('data', '')
if content:
print(content, end='', flush=True)
search_context += content
except:
pass
print("\n\n✓ 搜索流式响应完成!")
# 注意:由于 SearchService 现在 bypass 了 RAGFlow所以这里的 search_context 就是最终答案
# 如果要测试 DeepSeek我们需要手动构造另一段测试逻辑或者模拟 RAG 流程
else:
print(f"\n✗ 预期返回 StreamingResponse实际返回: {type(response)}")
except Exception as e:
print(f"\n✗ 搜索流式测试失败: {e}")
import traceback
traceback.print_exc()
# 步骤3: 单独测试 DeepSeek 流式
print("\n\n步骤3: 单独测试 DeepSeek LLM 流式...")
try:
from utils.deepseek_client import DeepSeekAPIClient
client = DeepSeekAPIClient()
print("\n正在调用 DeepSeek 流式生成...")
print(f"问题: {test_question}")
print("\n【DeepSeek 回答 (流式)】")
messages = [{"role": "user", "content": test_question}]
# 使用 chat_completion 开启 stream=True
response = await client.chat_completion(
messages=messages,
stream=True
)
# 处理 AsyncStream
async for chunk in response:
content = chunk.choices[0].delta.content or ""
if content:
print(content, end='', flush=True)
print("\n\n✓ DeepSeek 流式测试成功!")
except Exception as e:
print(f"\n✗ DeepSeek 流式测试失败: {e}")
# import traceback
# traceback.print_exc()
print("\n" + "=" * 70)
print("测试完成")
print("=" * 70)
if __name__ == "__main__":
print("\n提示:此测试需要以下配置:")
print("1. ✓ SEARCH_API_KEY (搜索服务)")
print("2. ✓ DEEPSEEK_API_KEY (LLM服务)")
print()
try:
asyncio.run(test_search_with_llm())
except KeyboardInterrupt:
print("\n\n测试被用户中断")
except Exception as e:
print(f"\n\n测试失败: {e}")
import traceback
traceback.print_exc()