- 新增对话历史管理:MongoDB新增conversations集合,存储用户与AI的对话上下文,支持多轮对话意图延续
- 新增对话历史API(conversation.py):GET/DELETE conversation历史、列出所有会话
- 意图解析增强:支持基于对话历史的意图识别,上下文理解更准确
- 字段提取优化:支持"提取文档中的医院数量"等自然语言模式,智能去除"文档中的"前缀
- 文档对比优化:从指令中提取文件名并精确匹配source_docs,支持"对比A和B两个文档"
- 文档摘要优化:使用LLM生成真实AI摘要而非返回原始文档预览
【Word模板填表核心功能】
- Word模板字段生成:空白Word上传后,自动从源文档(Excel/Word/TXT/MD)内容AI生成字段名
- Word模板填表(_fill_docx):将提取数据写入Word模板表格,支持精确匹配、模糊匹配、追加新行
- 数据润色(_polish_word_filled_data):LLM对多行Excel数据进行统计归纳(合计/平均/极值),转化为专业自然语言描述
- 段落格式输出:使用📌字段名+值段落+分隔线(灰色横线)格式,提升可读性
- 导出链打通:fill_template返回filled_file_path,export直接返回已填好的Word文件
【其他修复】
- 修复Word导出Windows文件锁问题:NamedTemporaryFile改为mkstemp+close
- 修复Word方框非法字符:扩展clean_text移除\uFFFD、□等Unicode替代符和零宽字符
- 修复文档对比"需要至少2个文档":从指令提取具体文件名优先匹配而非取前2个
- 修复导出format硬编码:自动识别docx/xlsx格式
- Docx解析器增加备用解析方法和更完整的段落/表格/标题提取
- RAG服务新增MySQL数据源支持
98 lines
2.4 KiB
Python
98 lines
2.4 KiB
Python
"""
|
||
对话历史 API 接口
|
||
|
||
提供对话历史的存储和查询功能
|
||
"""
|
||
import logging
|
||
from typing import Optional
|
||
|
||
from fastapi import APIRouter, HTTPException
|
||
from pydantic import BaseModel
|
||
|
||
from app.core.database import mongodb
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
router = APIRouter(prefix="/conversation", tags=["对话历史"])
|
||
|
||
|
||
# ==================== 请求/响应模型 ====================
|
||
|
||
class ConversationMessage(BaseModel):
|
||
role: str
|
||
content: str
|
||
intent: Optional[str] = None
|
||
|
||
|
||
class ConversationHistoryResponse(BaseModel):
|
||
success: bool
|
||
messages: list
|
||
|
||
|
||
class ConversationListResponse(BaseModel):
|
||
success: bool
|
||
conversations: list
|
||
|
||
|
||
# ==================== 接口 ====================
|
||
|
||
@router.get("/{conversation_id}/history", response_model=ConversationHistoryResponse)
|
||
async def get_conversation_history(conversation_id: str, limit: int = 20):
|
||
"""
|
||
获取对话历史
|
||
|
||
Args:
|
||
conversation_id: 对话会话ID
|
||
limit: 返回消息数量(默认20条)
|
||
"""
|
||
try:
|
||
messages = await mongodb.get_conversation_history(conversation_id, limit=limit)
|
||
return ConversationHistoryResponse(
|
||
success=True,
|
||
messages=messages
|
||
)
|
||
except Exception as e:
|
||
logger.error(f"获取对话历史失败: {e}")
|
||
return ConversationHistoryResponse(
|
||
success=False,
|
||
messages=[]
|
||
)
|
||
|
||
|
||
@router.delete("/{conversation_id}")
|
||
async def delete_conversation(conversation_id: str):
|
||
"""
|
||
删除对话会话
|
||
|
||
Args:
|
||
conversation_id: 对话会话ID
|
||
"""
|
||
try:
|
||
success = await mongodb.delete_conversation(conversation_id)
|
||
return {"success": success}
|
||
except Exception as e:
|
||
logger.error(f"删除对话失败: {e}")
|
||
return {"success": False, "error": str(e)}
|
||
|
||
|
||
@router.get("/all", response_model=ConversationListResponse)
|
||
async def list_conversations(limit: int = 50, skip: int = 0):
|
||
"""
|
||
获取会话列表
|
||
|
||
Args:
|
||
limit: 返回数量
|
||
skip: 跳过数量
|
||
"""
|
||
try:
|
||
conversations = await mongodb.list_conversations(limit=limit, skip=skip)
|
||
return ConversationListResponse(
|
||
success=True,
|
||
conversations=conversations
|
||
)
|
||
except Exception as e:
|
||
logger.error(f"获取会话列表失败: {e}")
|
||
return ConversationListResponse(
|
||
success=False,
|
||
conversations=[]
|
||
) |