本指南提供了测试历史记录获取功能的完整步骤,包括两个服务的接口:
所有接口都需要在请求头中携带 Authorization token:
Authorization: Bearer {your_token}
# 检查 shudao-chat-py 服务
curl http://localhost:22001/docs
# 检查 shudao-aichat 服务
curl http://localhost:8000/docs
接口地址: GET /apiv1/get_history_record
请求参数:
ai_conversation_id (可选): 指定对话ID,0或不传表示获取所有business_type (可选): 业务类型过滤
测试用例 1:获取所有历史记录
curl -X GET "http://localhost:22001/apiv1/get_history_record?ai_conversation_id=0" \
-H "Authorization: Bearer YOUR_TOKEN_HERE"
测试用例 2:按业务类型过滤(AI问答)
curl -X GET "http://localhost:22001/apiv1/get_history_record?business_type=0" \
-H "Authorization: Bearer YOUR_TOKEN_HERE"
测试用例 3:获取指定对话
curl -X GET "http://localhost:22001/apiv1/get_history_record?ai_conversation_id=123" \
-H "Authorization: Bearer YOUR_TOKEN_HERE"
预期响应格式:
{
"statusCode": 200,
"msg": "success",
"data": [
{
"id": 123,
"content": "用户问题内容摘要...",
"business_type": 0,
"exam_name": "",
"created_at": 1712345678
}
]
}
接口地址: POST /apiv1/stream/chat-with-db
请求示例:
curl -X POST "http://localhost:22001/apiv1/stream/chat-with-db" \
-H "Authorization: Bearer YOUR_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d '{
"message": "什么是桥梁施工安全规范?",
"ai_conversation_id": 0,
"business_type": 0
}'
说明:
ai_conversation_id=0 会创建新对话ai_conversation_id 和 ai_message_id接口地址: POST /apiv1/delete_history_record
请求示例:
curl -X POST "http://localhost:22001/apiv1/delete_history_record" \
-H "Authorization: Bearer YOUR_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d '{
"ai_conversation_id": 123
}'
接口地址: POST /api/v1/report/complete-flow
请求示例:
curl -X POST "http://localhost:8000/api/v1/report/complete-flow" \
-H "Authorization: Bearer YOUR_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d '{
"user_question": "请介绍隧道施工安全管理规范",
"ai_conversation_id": 0,
"window_size": 3,
"n_results": 10,
"is_network_search_enabled": true,
"enable_online_model": false
}'
参数说明:
user_question: 用户问题(必填)user_id: 用户ID(可选,不填会从token解析)ai_conversation_id: 对话ID,0表示创建新对话window_size: 滑动窗口大小,默认3n_results: 检索结果数量,默认10is_network_search_enabled: 是否启用网络搜索,默认trueenable_online_model: 是否启用在线模型,默认falseSSE 事件流说明:
data: {"type": "initial", "user_id": "N7114089", "ai_conversation_id": 456, "ai_message_id": 789}
data: {"type": "status", "message": "正在分析问题意图..."}
data: {"type": "intent", "is_professional_question": true, "keywords": ["隧道", "施工安全"]}
data: {"type": "documents", "total": 5}
data: {"type": "report_start", "file_index": 1, "source_file": "隧道施工规范.pdf"}
data: {"type": "report_chunk", "file_index": 1, "chunk": "报告内容..."}
data: {"type": "completed", "message": "报告生成完成"}
接口地址: POST /api/v1/report/update-ai-message
请求示例:
curl -X POST "http://localhost:8000/api/v1/report/update-ai-message" \
-H "Content-Type: application/json" \
-d '{
"ai_message_id": 789,
"content": "完整的AI回复内容..."
}'
# 发起报告生成请求
curl -X POST "http://localhost:8000/api/v1/report/complete-flow" \
-H "Authorization: Bearer YOUR_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d '{
"user_question": "桥梁施工有哪些安全要求?",
"ai_conversation_id": 0
}'
记录返回的:
ai_conversation_id: 新创建的对话IDai_message_id: AI消息ID# 获取历史记录列表
curl -X GET "http://localhost:22001/apiv1/get_history_record?business_type=0" \
-H "Authorization: Bearer YOUR_TOKEN_HERE"
验证点:
user_id 应为当前用户的 userCode(如 "N7114089")# 使用已有的 ai_conversation_id 继续对话
curl -X POST "http://localhost:8000/api/v1/report/complete-flow" \
-H "Authorization: Bearer YOUR_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d '{
"user_question": "具体有哪些规范文件?",
"ai_conversation_id": 456
}'
# 获取指定对话的详情
curl -X GET "http://localhost:22001/apiv1/get_history_record?ai_conversation_id=456" \
-H "Authorization: Bearer YOUR_TOKEN_HERE"
原因: Token 无效或过期
解决:
# 检查 token 格式
# 正确格式:Authorization: Bearer {token}
# 或直接:Authorization: {token}
排查步骤:
检查数据库中的 user_id 字段:
-- 查看 ai_conversation 表
SELECT id, user_id, content, created_at
FROM ai_conversation
WHERE is_deleted = 0
ORDER BY created_at DESC
LIMIT 10;
-- 查看 ai_message 表
SELECT id, user_id, type, content, ai_conversation_id
FROM ai_message
WHERE is_deleted = 0
ORDER BY created_at DESC
LIMIT 10;
验证 token 解析的 user_id:
# 查看服务日志
tail -f shudao-aichat/logs/app.log | grep "从token解析user_id"
原因: user_id 被写死或解析错误(已修复)
验证修复:
# 检查日志中的 user_id
tail -f shudao-aichat/logs/app.log | grep "Complete Flow"
应该看到类似:
[Complete Flow] 从token解析user_id: N7114089
[对话记录] 创建新conversation: id=456, user_id=N7114089
错误信息:
[vite] http proxy error: /apiv1/get_history_record
Error: connect ECONNREFUSED 127.0.0.1:22001
解决:
确认 shudao-chat-py 服务正在运行:
curl http://localhost:22001/docs
检查 vite.config.js 代理配置:
proxy: {
'/apiv1': {
target: 'http://localhost:22001',
changeOrigin: true
}
}
SELECT
c.id,
c.user_id,
c.content,
c.business_type,
c.created_at,
COUNT(m.id) as message_count
FROM ai_conversation c
LEFT JOIN ai_message m ON m.ai_conversation_id = c.id AND m.is_deleted = 0
WHERE c.user_id = 'N7114089' AND c.is_deleted = 0
GROUP BY c.id
ORDER BY c.created_at DESC;
SELECT
id,
user_id,
type,
LEFT(content, 100) as content_preview,
created_at
FROM ai_message
WHERE ai_conversation_id = 456 AND is_deleted = 0
ORDER BY created_at ASC;
{
"base_url_chat": "http://localhost:22001",
"base_url_aichat": "http://localhost:8000",
"auth_token": "YOUR_TOKEN_HERE"
}
获取历史记录
{{base_url_chat}}/apiv1/get_history_record?business_type=0Authorization: Bearer {{auth_token}}创建新对话
{{base_url_aichat}}/api/v1/report/complete-flowAuthorization: Bearer {{auth_token}}删除对话
{{base_url_chat}}/apiv1/delete_history_recordAuthorization: Bearer {{auth_token}}{"ai_conversation_id": 123}# 使用 Apache Bench 测试
ab -n 100 -c 10 \
-H "Authorization: Bearer YOUR_TOKEN" \
http://localhost:22001/apiv1/get_history_record?business_type=0
# 使用 wrk 测试
wrk -t4 -c100 -d30s \
-H "Authorization: Bearer YOUR_TOKEN" \
http://localhost:22001/apiv1/get_history_record
# shudao-chat-py 日志
tail -f shudao-main/shudao-chat-py/logs/app.log
# shudao-aichat 日志
tail -f shudao-aichat/logs/app.log
[Complete Flow]: 报告生成流程[对话记录]: 对话创建/更新从token解析user_id: user_id 解析get_history_record: 历史记录查询修复后的系统应该满足:
如有问题,请检查日志并参考本指南的排查步骤。