辞图智能数据问答平台提供了完整的基于 Redis 的对话管理功能,支持多用户、多会话的智能问答场景。本文档详细介绍了所有与 Redis 对话管理相关的 API 接口。
http://localhost:8084
application/json
/api/v0/user/{user_id}/conversations
GET
参数名 | 类型 | 位置 | 必填 | 说明 |
---|---|---|---|---|
user_id | string | 路径参数 | 是 | 用户ID |
limit | integer | 查询参数 | 否 | 最大返回数量,默认为 USER_MAX_CONVERSATIONS |
GET /api/v0/user/john_doe/conversations?limit=10
{
"success": true,
"code": 200,
"message": "获取用户对话列表成功",
"data": {
"user_id": "john_doe",
"conversations": [
{
"conversation_id": "conv_20241201_001",
"start_time": "2024-12-01T10:00:00",
"last_activity": "2024-12-01T10:30:00",
"message_count": 6
}
],
"total_count": 1
}
}
/api/v0/conversation/{conversation_id}/messages
GET
参数名 | 类型 | 位置 | 必填 | 说明 |
---|---|---|---|---|
conversation_id | string | 路径参数 | 是 | 对话ID |
limit | integer | 查询参数 | 否 | 最大返回消息数量 |
GET /api/v0/conversation/conv_20241201_001/messages?limit=50
{
"success": true,
"code": 200,
"message": "获取对话消息成功",
"data": {
"conversation_id": "conv_20241201_001",
"conversation_meta": {
"created_at": "2024-12-01T10:00:00",
"message_count": 6
},
"messages": [
{
"role": "user",
"content": "查询销售数据",
"timestamp": "2024-12-01T10:00:00",
"metadata": {}
},
{
"role": "assistant",
"content": "好的,我来帮您查询销售数据...",
"timestamp": "2024-12-01T10:00:05",
"metadata": {
"type": "DATABASE",
"sql": "SELECT * FROM sales"
}
}
],
"message_count": 6
}
}
/api/v0/conversation/{conversation_id}/context
GET
参数名 | 类型 | 位置 | 必填 | 说明 |
---|---|---|---|---|
conversation_id | string | 路径参数 | 是 | 对话ID |
count | integer | 查询参数 | 否 | 上下文消息数量,默认为 CONVERSATION_CONTEXT_COUNT |
GET /api/v0/conversation/conv_20241201_001/context?count=5
{
"success": true,
"code": 200,
"message": "获取对话上下文成功",
"data": {
"conversation_id": "conv_20241201_001",
"context": "User: 查询销售数据\nAssistant: 好的,我来帮您查询销售数据...\nUser: 能否按月份分组?",
"context_message_count": 5
}
}
/api/v0/user/{user_id}/conversations/full
GET
参数名 | 类型 | 位置 | 必填 | 说明 |
---|---|---|---|---|
user_id | string | 路径参数 | 是 | 用户ID |
conversation_limit | integer | 查询参数 | 否 | 对话数量限制,不传则返回所有对话 |
message_limit | integer | 查询参数 | 否 | 每个对话的消息数限制,不传则返回所有消息 |
# 获取所有对话和消息
GET /api/v0/user/john_doe/conversations/full
# 限制返回数据量
GET /api/v0/user/john_doe/conversations/full?conversation_limit=50&message_limit=100
# 只限制对话数量
GET /api/v0/user/john_doe/conversations/full?conversation_limit=20
{
"success": true,
"code": 200,
"message": "获取用户完整对话数据成功",
"data": {
"user_id": "john_doe",
"conversations": [
{
"conversation_id": "conv_20241201_001",
"start_time": "2024-12-01T10:00:00",
"last_activity": "2024-12-01T10:30:00",
"meta": {
"message_count": 6,
"created_at": "2024-12-01T10:00:00"
},
"messages": [
{
"role": "user",
"content": "查询销售数据",
"timestamp": "2024-12-01T10:00:00"
},
{
"role": "assistant",
"content": "好的,我来帮您查询销售数据...",
"timestamp": "2024-12-01T10:00:05"
}
],
"message_count": 6
}
],
"total_conversations": 1,
"total_messages": 6,
"conversation_limit_applied": null,
"message_limit_applied": null,
"query_time": "2024-12-01T15:30:00"
}
}
/api/v0/conversation_stats
GET
无
GET /api/v0/conversation_stats
{
"success": true,
"code": 200,
"message": "获取统计信息成功",
"data": {
"total_users": 125,
"total_conversations": 1250,
"total_messages": 5000,
"active_users_today": 45,
"active_conversations_today": 200,
"cache_hit_rate": 0.75,
"redis_info": {
"connected": true,
"memory_usage": "120MB",
"keys_count": 2500
}
}
}
/api/v0/conversation_cleanup
POST
无
POST /api/v0/conversation_cleanup
{
"success": true,
"code": 200,
"message": "对话清理完成",
"data": {
"cleaned_conversations": 50,
"cleaned_messages": 200,
"cleanup_time": "2024-12-01T15:30:00"
}
}
/api/v0/ask_agent
POST
参数名 | 类型 | 位置 | 必填 | 说明 |
---|---|---|---|---|
question | string | 请求体 | 是 | 用户问题 |
session_id | string | 请求体 | 否 | 浏览器会话ID |
user_id | string | 请求体 | 否 | 用户ID |
conversation_id | string | 请求体 | 否 | 对话ID |
continue_conversation | boolean | 请求体 | 否 | 是否继续现有对话 |
POST /api/v0/ask_agent
Content-Type: application/json
{
"question": "查询最近一个月的销售数据",
"session_id": "session_12345",
"user_id": "john_doe",
"continue_conversation": true
}
{
"success": true,
"code": 200,
"message": "查询执行完成",
"data": {
"type": "DATABASE",
"response": "",
"sql": "SELECT * FROM sales WHERE date >= DATE_SUB(NOW(), INTERVAL 1 MONTH)",
"query_result": {
"rows": [...],
"columns": ["date", "amount", "product"],
"row_count": 150,
"total_row_count": 150,
"is_limited": false
},
"summary": "查询到最近一个月的销售数据共150条记录...",
"session_id": "session_12345",
"execution_path": ["classify_question", "generate_sql", "execute_sql", "generate_summary"],
"classification_info": {
"question_type": "DATABASE",
"confidence": 0.95,
"method": "rule_based_database_keywords"
},
"conversation_id": "conv_20241201_002",
"user_id": "john_doe",
"is_guest_user": false,
"context_used": true,
"from_cache": false,
"conversation_status": "continued",
"conversation_message": "继续现有对话"
}
}
所有 API 都遵循统一的错误响应格式:
{
"success": false,
"code": 500,
"message": "处理失败",
"data": {
"error": "具体错误信息",
"error_type": "error_type_code",
"can_retry": true,
"timestamp": "2024-12-01T15:30:00"
}
}
错误码 | 说明 |
---|---|
400 | 请求参数错误 |
422 | 参数验证失败 |
500 | 系统内部错误 |
503 | 服务暂时不可用 |
/conversations/full
API 获取完整数据相关配置参数在 app_config.py
中定义:
# Redis 对话管理配置
USER_MAX_CONVERSATIONS = 50 # 用户最大对话数
CONVERSATION_CONTEXT_COUNT = 10 # 默认上下文消息数量
REDIS_CONVERSATION_TTL = 86400 * 7 # 对话过期时间(7天)
系统采用上下文感知的缓存策略:
第1次问答(无上下文)→ 缓存存储 ✅
第2次相同问题(有上下文)→ 缓存命中 ✅(优化后)
第3次相同问题(新对话,无上下文)→ 缓存命中 ✅
# 在 app_config.py 中控制缓存行为
ENABLE_QUESTION_ANSWER_CACHE = True # 全局缓存开关
QUESTION_ANSWER_TTL = 24 * 3600 # 缓存过期时间(24小时)
/api/v0/user/{user_id}/conversations/full
API