This commit is contained in:
2025-01-19 06:58:40 +00:00
parent 339af355f9
commit 064f56e4b9
2 changed files with 56 additions and 54 deletions
+2 -2
View File
@@ -372,7 +372,7 @@
<div class="col-auto"> <div class="col-auto">
<div class="sidebar"> <div class="sidebar">
<!-- 文献页面的侧边栏 --> <!-- 文献页面的侧边栏 -->
<div class="sidebar-menu hidden" id="DetailSidebar"> <div class="sidebar-menu" id="DetailSidebar">
<div class="sidebar-item" onclick="showPaperList()"> <div class="sidebar-item" onclick="showPaperList()">
<i class="bi bi-list-ul"></i> <i class="bi bi-list-ul"></i>
<span>Paper List</span> <span>Paper List</span>
@@ -385,7 +385,7 @@
<div class="col"> <div class="col">
<div class="content-wrapper"> <div class="content-wrapper">
<!-- 文献详情页面 --> <!-- 文献详情页面 -->
<div id="DetailPage" class="content-body hidden"> <div id="DetailPage" class="content-body">
<div class="content-header d-flex justify-content-between align-items-center"> <div class="content-header d-flex justify-content-between align-items-center">
<h3 class="content-title" id="paperTitle">Paper List</h3> <h3 class="content-title" id="paperTitle">Paper List</h3>
<button class="btn btn-info" onclick="uploadPaper()" style="padding: 10px 20px;"> <button class="btn btn-info" onclick="uploadPaper()" style="padding: 10px 20px;">
+54 -52
View File
@@ -697,9 +697,9 @@ class TaskStatus:
COMPLETED = "completed" COMPLETED = "completed"
FAILED = "failed" FAILED = "failed"
@app.post("/paper/{paper_id}/qa") @app.post("/paper/{file_hash}/qa")
async def ask_reference_question( async def ask_reference_question(
paper_id: str, file_hash: str,
question: QuestionModel question: QuestionModel
): ):
"""Ask questions about the paper (async)""" """Ask questions about the paper (async)"""
@@ -708,7 +708,7 @@ async def ask_reference_question(
try: try:
# Verify paper exists # Verify paper exists
paper = await db.papers.find_one({"_id": ObjectId(paper_id)}) paper = await db.papers.find_one({"file_hash": file_hash})
if not paper: if not paper:
raise HTTPException(status_code=404, detail="Paper not found") raise HTTPException(status_code=404, detail="Paper not found")
@@ -718,7 +718,7 @@ async def ask_reference_question(
# Create background task # Create background task
asyncio.create_task(paper_question( asyncio.create_task(paper_question(
task_id=task_id, task_id=task_id,
paper_id=paper_id, file_hash=file_hash,
question=question.question, question=question.question,
paper=paper paper=paper
)) ))
@@ -738,7 +738,7 @@ async def ask_reference_question(
except Exception as e: except Exception as e:
print(f"Error closing Redis connection: {e}") print(f"Error closing Redis connection: {e}")
async def paper_question(task_id: str, paper_id: str, question: str, paper: dict): async def paper_question(task_id: str, file_hash: str, question: str, paper: dict):
"""Process paper Q&A background task""" """Process paper Q&A background task"""
redis = await get_redis() redis = await get_redis()
@@ -749,35 +749,37 @@ async def paper_question(task_id: str, paper_id: str, question: str, paper: dict
f"task:{task_id}", f"task:{task_id}",
mapping={ mapping={
"status": TaskStatus.PROCESSING, "status": TaskStatus.PROCESSING,
"paper_id": paper_id, "file_hash": file_hash,
"question": question "question": question
} }
) )
# Get analysis report from Redis # Get analysis report from Redis
await redis.select(190) await redis.select(190)
report_key = f"paper_report:{paper_id}" report_key = f"paper_report:{file_hash}"
report_data = await redis.get(report_key) report_data = await redis.get(report_key)
if not report_data: if not report_data:
raise Exception("Paper analysis report does not exist, please analyze first") raise Exception("Paper analysis report does not exist, please analyze first")
# Read PDF file content # Get PDF content from file storage service
file_path = paper.get("paper_link") async with aiohttp.ClientSession() as session:
if not file_path or not os.path.exists(file_path): async with session.get(f'https://files.aiot.ml/pdf/{file_hash}') as response:
raise Exception("Paper file does not exist") if response.status != 200:
raise Exception(f"Failed to get PDF content: HTTP {response.status}")
# Read PDF content
pdf_content = "" pdf_content = await response.json()
with open(file_path, 'rb') as file: if not pdf_content.get('content'):
pdf_reader = PyPDF2.PdfReader(file) raise Exception("No PDF content returned")
for page in pdf_reader.pages:
content = page.extract_text() content = pdf_content['content']
pdf_content += content # If content is a list, join it into a single string
if isinstance(content, list):
# Limit content length content = '\n'.join(content)
MAX_CHARS = 180000
pdf_content = pdf_content[:MAX_CHARS] # Limit content length
MAX_CHARS = 180000
content = content[:MAX_CHARS]
# Build system prompt and user prompt # Build system prompt and user prompt
system_prompt = """ system_prompt = """
@@ -788,7 +790,7 @@ async def paper_question(task_id: str, paper_id: str, question: str, paper: dict
# Build context # Build context
context = { context = {
"Paper Content": pdf_content, "Paper Content": content,
"Analysis Report": json.loads(report_data) "Analysis Report": json.loads(report_data)
} }
@@ -806,7 +808,7 @@ async def paper_question(task_id: str, paper_id: str, question: str, paper: dict
# Save conversation history to Redis db191 # Save conversation history to Redis db191
await redis.select(191) await redis.select(191)
chat_history_key = f"chat_history:{paper_id}" chat_history_key = f"chat_history:{file_hash}"
# Get existing history # Get existing history
existing_history = await redis.get(chat_history_key) existing_history = await redis.get(chat_history_key)
@@ -850,6 +852,33 @@ async def paper_question(task_id: str, paper_id: str, question: str, paper: dict
except Exception as e: except Exception as e:
print(f"Error closing Redis connection: {e}") print(f"Error closing Redis connection: {e}")
@app.get("/paper/{file_hash}/qa/history")
async def get_paper_qa_history(
file_hash: str
):
"""Get paper Q&A history"""
redis = await get_redis()
try:
# Get conversation history from Redis db191
await redis.select(191)
chat_history_key = f"chat_history:{file_hash}"
history_data = await redis.get(chat_history_key)
if not history_data:
return []
return json.loads(history_data)
except Exception as e:
print(f"Error getting QA history: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
finally:
try:
await redis.aclose()
except Exception as e:
print(f"Error closing Redis connection: {e}")
@app.get("/paper/task/{task_id}") @app.get("/paper/task/{task_id}")
async def get_task_status(task_id: str): async def get_task_status(task_id: str):
"""Get task status and result""" """Get task status and result"""
@@ -890,33 +919,6 @@ async def get_task_status(task_id: str):
except Exception as e: except Exception as e:
print(f"Error closing Redis connection: {e}") print(f"Error closing Redis connection: {e}")
@app.get("/paper/{paper_id}/qa/history")
async def get_paper_qa_history(
paper_id: str
):
"""Get paper Q&A history"""
redis = await get_redis()
try:
# Get conversation history from Redis db191
await redis.select(191)
chat_history_key = f"chat_history:{paper_id}"
history_data = await redis.get(chat_history_key)
if not history_data:
return []
return json.loads(history_data)
except Exception as e:
print(f"Error getting QA history: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
finally:
try:
await redis.aclose()
except Exception as e:
print(f"Error closing Redis connection: {e}")
# 添加新的路由,通过哈希值获取报告 # 添加新的路由,通过哈希值获取报告
@app.get("/paper/check/{file_hash}") @app.get("/paper/check/{file_hash}")
async def check_report( async def check_report(