From 6e6d68c5eb067ada3bfe670ce09440440362be5d Mon Sep 17 00:00:00 2001 From: Fantasy lee <129943055+Fantasylee21@users.noreply.github.com> Date: Mon, 2 Jun 2025 12:41:26 +0800 Subject: [PATCH] =?UTF-8?q?[fix]:=20AI=E7=AB=AF=E4=BF=AE=E5=A4=8D=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E7=BB=84=E7=BB=87=E6=96=87=E7=8C=AE=E8=BD=AC=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/v1/endpoints/aichat.py | 25 +++++++++++++++++++++---- app/api/v1/endpoints/articleDB.py | 19 +++++++++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/app/api/v1/endpoints/aichat.py b/app/api/v1/endpoints/aichat.py index bdfb579..95c969f 100644 --- a/app/api/v1/endpoints/aichat.py +++ b/app/api/v1/endpoints/aichat.py @@ -168,12 +168,29 @@ async def review_notes( text = await read_pdf(article.file_path) text += "\n\n请根据以上内容生成文章简介。请尽量控制在200字以内。" - try: - ans = await kimi_chat([{"role": "user", "content": text}], model="moonshot-v1-32k") - except Exception as e: + original_text = await read_pdf(article.file_path) + max_retries = 5 # 最大重试次数 + retry_count = 0 + current_text = original_text + "\n\n请根据以上内容生成文章简介。请尽量控制在200字以内。" + has_error = True + while retry_count < max_retries: + try: + ans = await kimi_chat([{"role": "user", "content": current_text}], model="moonshot-v1-32k") + # 成功获取到答案,跳出循环 + has_error = False + break + except Exception as e: + retry_count += 1 + + text_length = len(original_text) + half_length = text_length // 2 + original_text = original_text[:half_length] + current_text = original_text + "\n\n请根据以上内容生成文章简介。请尽量控制在200字以内。" + continue # 继续下一次循环尝试 + if has_error: raise HTTPException( status_code=500, - detail=f"AI服务异常: {str(e)}" + detail="AI服务异常,无法生成文章简介,请稍后重试。" ) # 更新文章简介到数据库 articleDB = await update_article_intro(db, article_id, ans.strip()) diff --git a/app/api/v1/endpoints/articleDB.py b/app/api/v1/endpoints/articleDB.py index dbd9bc9..715ddbd 100644 --- a/app/api/v1/endpoints/articleDB.py +++ b/app/api/v1/endpoints/articleDB.py @@ -96,9 +96,9 @@ async def download_article(article_id: int, db: AsyncSession = Depends(get_db)): filename=quote(download_filename), media_type="application/pdf" ) - +from app.utils.auth import get_current_user @router.put("/copy", response_model=dict) -async def copy_article(folder_id: int, article_id: int, db: AsyncSession = Depends(get_db)): +async def copy_article(folder_id: int, article_id: int, is_group: bool | None = None, db: AsyncSession = Depends(get_db), user: dict = Depends(get_current_user)): """ Copy an article file by its ID to a specified directory. """ @@ -106,11 +106,22 @@ async def copy_article(folder_id: int, article_id: int, db: AsyncSession = Depen file_path, title = await get_article_info_in_db_by_id(db=db, article_id=article_id) if not file_path: raise HTTPException(status_code=404, detail="File not found") + old_file_path = file_path + + if is_group != None and is_group is True: + url = f"/lhcos-data/{uuid.uuid4()}.pdf" + with open(old_file_path, "rb") as source_file: + with open(url, "wb") as dest_file: + dest_file.write(source_file.read()) + # 用文件名(不带扩展名)作为 Article 名称 + user_id = user.get("id") + from app.curd.group import crud_new_article + article_id = await crud_new_article(user_id, folder_id, title, url, db) + return {"msg": "Article copied successfully", "new_article_id": article_id} - new_article_id = await crud_upload_to_self_folder(name=title, folder_id=folder_id, db=db) + new_article_id = await crud_upload_to_self_folder(name=title, folder_id=folder_id, url=old_file_path ,db=db) # 复制文件到新的目录 - old_file_path = file_path new_file_path = os.path.join("/lhcos-data", f"{new_article_id}.pdf") try: with open(old_file_path, "rb") as source_file: