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="sidebar">
<!-- 文献页面的侧边栏 -->
<div class="sidebar-menu hidden" id="DetailSidebar">
<div class="sidebar-menu" id="DetailSidebar">
<div class="sidebar-item" onclick="showPaperList()">
<i class="bi bi-list-ul"></i>
<span>Paper List</span>
@@ -385,7 +385,7 @@
<div class="col">
<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">
<h3 class="content-title" id="paperTitle">Paper List</h3>
<button class="btn btn-info" onclick="uploadPaper()" style="padding: 10px 20px;">
+50 -48
View File
@@ -697,9 +697,9 @@ class TaskStatus:
COMPLETED = "completed"
FAILED = "failed"
@app.post("/paper/{paper_id}/qa")
@app.post("/paper/{file_hash}/qa")
async def ask_reference_question(
paper_id: str,
file_hash: str,
question: QuestionModel
):
"""Ask questions about the paper (async)"""
@@ -708,7 +708,7 @@ async def ask_reference_question(
try:
# 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:
raise HTTPException(status_code=404, detail="Paper not found")
@@ -718,7 +718,7 @@ async def ask_reference_question(
# Create background task
asyncio.create_task(paper_question(
task_id=task_id,
paper_id=paper_id,
file_hash=file_hash,
question=question.question,
paper=paper
))
@@ -738,7 +738,7 @@ async def ask_reference_question(
except Exception as 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"""
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}",
mapping={
"status": TaskStatus.PROCESSING,
"paper_id": paper_id,
"file_hash": file_hash,
"question": question
}
)
# Get analysis report from Redis
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)
if not report_data:
raise Exception("Paper analysis report does not exist, please analyze first")
# Read PDF file content
file_path = paper.get("paper_link")
if not file_path or not os.path.exists(file_path):
raise Exception("Paper file does not exist")
# Get PDF content from file storage service
async with aiohttp.ClientSession() as session:
async with session.get(f'https://files.aiot.ml/pdf/{file_hash}') as response:
if response.status != 200:
raise Exception(f"Failed to get PDF content: HTTP {response.status}")
# Read PDF content
pdf_content = ""
with open(file_path, 'rb') as file:
pdf_reader = PyPDF2.PdfReader(file)
for page in pdf_reader.pages:
content = page.extract_text()
pdf_content += content
pdf_content = await response.json()
if not pdf_content.get('content'):
raise Exception("No PDF content returned")
content = pdf_content['content']
# If content is a list, join it into a single string
if isinstance(content, list):
content = '\n'.join(content)
# Limit content length
MAX_CHARS = 180000
pdf_content = pdf_content[:MAX_CHARS]
content = content[:MAX_CHARS]
# Build system prompt and user prompt
system_prompt = """
@@ -788,7 +790,7 @@ async def paper_question(task_id: str, paper_id: str, question: str, paper: dict
# Build context
context = {
"Paper Content": pdf_content,
"Paper Content": content,
"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
await redis.select(191)
chat_history_key = f"chat_history:{paper_id}"
chat_history_key = f"chat_history:{file_hash}"
# Get existing history
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:
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}")
async def get_task_status(task_id: str):
"""Get task status and result"""
@@ -890,33 +919,6 @@ async def get_task_status(task_id: str):
except Exception as 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}")
async def check_report(