Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions app/api/v1/endpoints/aichat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
19 changes: 15 additions & 4 deletions app/api/v1/endpoints/articleDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,32 @@ 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.
"""
# 根据 ID 查询文章信息
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:
Expand Down