|
|
|
@ -1,9 +1,11 @@
|
|
|
|
|
import os
|
|
|
|
|
import tempfile
|
|
|
|
|
import urllib.parse
|
|
|
|
|
import uuid
|
|
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
|
from io import BytesIO
|
|
|
|
|
from logging.handlers import RotatingFileHandler
|
|
|
|
|
|
|
|
|
|
import html2text
|
|
|
|
|
import jieba # 导入 jieba 分词库
|
|
|
|
|
import uvicorn
|
|
|
|
|
from docx import Document
|
|
|
|
@ -20,6 +22,13 @@ from Config.Config import MS_MODEL_PATH, MS_MODEL_LIMIT, MS_HOST, MS_PORT, MS_MA
|
|
|
|
|
from Milvus.Utils.MilvusCollectionManager import MilvusCollectionManager
|
|
|
|
|
from Milvus.Utils.MilvusConnectionPool import *
|
|
|
|
|
from Milvus.Utils.MilvusConnectionPool import MilvusConnectionPool
|
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 将HTML文件转换为Word文件
|
|
|
|
|
def html_to_word_pandoc(html_file, output_file):
|
|
|
|
|
subprocess.run(['pandoc', html_file, '-o', output_file])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 初始化日志
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
@ -75,10 +84,8 @@ def text_to_embedding(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def generate_stream(client, milvus_pool, collection_manager, query):
|
|
|
|
|
"""生成SSE流"""
|
|
|
|
|
# 从连接池获取连接
|
|
|
|
|
connection = milvus_pool.get_connection()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# 1. 将查询文本转换为向量
|
|
|
|
|
current_embedding = text_to_embedding(query)
|
|
|
|
@ -89,7 +96,7 @@ async def generate_stream(client, milvus_pool, collection_manager, query):
|
|
|
|
|
"params": {"nprobe": MS_NPROBE} # 设置 IVF_FLAT 的 nprobe 参数
|
|
|
|
|
}
|
|
|
|
|
# 7. 将文本转换为嵌入向量
|
|
|
|
|
results = collection_manager.search(current_embedding, search_params, limit=5) # 返回 2 条结果
|
|
|
|
|
results = collection_manager.search(current_embedding, search_params, limit=5) # 返回 5 条结果
|
|
|
|
|
|
|
|
|
|
# 3. 处理搜索结果
|
|
|
|
|
logger.info("最相关的知识库内容:")
|
|
|
|
@ -100,7 +107,7 @@ async def generate_stream(client, milvus_pool, collection_manager, query):
|
|
|
|
|
try:
|
|
|
|
|
# 查询非向量字段
|
|
|
|
|
record = collection_manager.query_by_id(hit.id)
|
|
|
|
|
if hit.distance < 0.88: # 设置距离阈值
|
|
|
|
|
if hit.distance < 0.88: # 设置距离阈值
|
|
|
|
|
logger.info(f"ID: {hit.id}")
|
|
|
|
|
logger.info(f"标签: {record['tags']}")
|
|
|
|
|
logger.info(f"用户问题: {record['user_input']}")
|
|
|
|
@ -144,8 +151,11 @@ async def generate_stream(client, milvus_pool, collection_manager, query):
|
|
|
|
|
temperature=0.3,
|
|
|
|
|
stream=False
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
yield {"data": response.choices[0].message.content}
|
|
|
|
|
# 将返回的html代码保存成文件
|
|
|
|
|
htmlStr = response.choices[0].message.content
|
|
|
|
|
with open("Static/1.html", "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(htmlStr)
|
|
|
|
|
yield {"data": htmlStr}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
yield {"data": f"生成报告时出错: {str(e)}"}
|
|
|
|
|
finally:
|
|
|
|
@ -168,11 +178,15 @@ http://10.10.21.22:8000/static/ai.html
|
|
|
|
|
class QueryRequest(BaseModel):
|
|
|
|
|
query: str = Field(..., description="用户查询的问题")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SaveWordRequest(BaseModel):
|
|
|
|
|
html: str = Field(..., description="要保存为Word的HTML内容")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/save-word")
|
|
|
|
|
async def save_to_word(request: Request):
|
|
|
|
|
temp_html = None
|
|
|
|
|
output_file = None
|
|
|
|
|
try:
|
|
|
|
|
# Parse request data
|
|
|
|
|
try:
|
|
|
|
@ -183,52 +197,42 @@ async def save_to_word(request: Request):
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Request parsing failed: {str(e)}")
|
|
|
|
|
raise HTTPException(status_code=400, detail=f"Invalid request: {str(e)}")
|
|
|
|
|
|
|
|
|
|
# Convert HTML to text
|
|
|
|
|
try:
|
|
|
|
|
text_maker = html2text.HTML2Text()
|
|
|
|
|
text_maker.ignore_links = True
|
|
|
|
|
text_maker.ignore_images = True
|
|
|
|
|
text_content = text_maker.handle(html_content)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"HTML conversion failed: {str(e)}")
|
|
|
|
|
raise HTTPException(status_code=400, detail=f"HTML processing error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
# Create Word document
|
|
|
|
|
try:
|
|
|
|
|
doc = Document()
|
|
|
|
|
doc.add_heading('小学数学问答', 0)
|
|
|
|
|
|
|
|
|
|
for para in text_content.split('\n\n'):
|
|
|
|
|
if para.strip():
|
|
|
|
|
doc.add_paragraph(para.strip())
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Document creation failed: {str(e)}")
|
|
|
|
|
raise HTTPException(status_code=500, detail=f"Document creation error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
# Save to stream
|
|
|
|
|
try:
|
|
|
|
|
stream = BytesIO()
|
|
|
|
|
doc.save(stream)
|
|
|
|
|
stream.seek(0)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Document saving failed: {str(e)}")
|
|
|
|
|
raise HTTPException(status_code=500, detail=f"Document saving error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
# Return response
|
|
|
|
|
filename = "小学数学问答.docx"
|
|
|
|
|
encoded_filename = urllib.parse.quote(filename)
|
|
|
|
|
|
|
|
|
|
# 创建临时HTML文件
|
|
|
|
|
temp_html = os.path.join(tempfile.gettempdir(), uuid.uuid4().hex + ".html")
|
|
|
|
|
with open(temp_html, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(html_content)
|
|
|
|
|
|
|
|
|
|
# 使用pandoc转换
|
|
|
|
|
output_file = os.path.join(tempfile.gettempdir(), "小学数学问答.docx")
|
|
|
|
|
subprocess.run(['pandoc', temp_html, '-o', output_file], check=True)
|
|
|
|
|
|
|
|
|
|
# 读取生成的Word文件
|
|
|
|
|
with open(output_file, "rb") as f:
|
|
|
|
|
stream = BytesIO(f.read())
|
|
|
|
|
|
|
|
|
|
# 返回响应
|
|
|
|
|
encoded_filename = urllib.parse.quote("小学数学问答.docx")
|
|
|
|
|
return StreamingResponse(
|
|
|
|
|
stream,
|
|
|
|
|
media_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
|
|
|
headers={"Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}"}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
headers={"Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}"})
|
|
|
|
|
|
|
|
|
|
except HTTPException:
|
|
|
|
|
raise
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Unexpected error: {str(e)}")
|
|
|
|
|
raise HTTPException(status_code=500, detail="Internal server error")
|
|
|
|
|
finally:
|
|
|
|
|
# 清理临时文件
|
|
|
|
|
try:
|
|
|
|
|
if temp_html and os.path.exists(temp_html):
|
|
|
|
|
os.remove(temp_html)
|
|
|
|
|
if output_file and os.path.exists(output_file):
|
|
|
|
|
os.remove(output_file)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.warning(f"Failed to clean up temp files: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/rag")
|
|
|
|
|
async def rag_stream(request: Request):
|
|
|
|
@ -243,10 +247,10 @@ async def rag_stream(request: Request):
|
|
|
|
|
raise HTTPException(status_code=400, detail="无效的请求格式")
|
|
|
|
|
"""RAG+DeepSeek接口"""
|
|
|
|
|
async for chunk in generate_stream(
|
|
|
|
|
request.app.state.deepseek_client,
|
|
|
|
|
request.app.state.milvus_pool,
|
|
|
|
|
request.app.state.collection_manager,
|
|
|
|
|
query_request.query
|
|
|
|
|
request.app.state.deepseek_client,
|
|
|
|
|
request.app.state.milvus_pool,
|
|
|
|
|
request.app.state.collection_manager,
|
|
|
|
|
query_request.query
|
|
|
|
|
):
|
|
|
|
|
return chunk
|
|
|
|
|
|
|
|
|
|