diff --git "a/alembic/versions/0ff7f968ec3b_\345\242\236\345\212\240\346\226\207\347\214\256\345\272\223\347\256\200\344\273\213.py" "b/alembic/versions/0ff7f968ec3b_\345\242\236\345\212\240\346\226\207\347\214\256\345\272\223\347\256\200\344\273\213.py" new file mode 100644 index 0000000..3464753 --- /dev/null +++ "b/alembic/versions/0ff7f968ec3b_\345\242\236\345\212\240\346\226\207\347\214\256\345\272\223\347\256\200\344\273\213.py" @@ -0,0 +1,32 @@ +"""增加文献库简介 + +Revision ID: 0ff7f968ec3b +Revises: 9256d579f585 +Create Date: 2025-06-01 19:36:53.514027 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '0ff7f968ec3b' +down_revision: Union[str, None] = '9256d579f585' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + """Upgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('articleDB', sa.Column('intro', sa.Text(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade() -> None: + """Downgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('articleDB', 'intro') + # ### end Alembic commands ### diff --git a/app/api/v1/endpoints/aichat.py b/app/api/v1/endpoints/aichat.py index 6d3770d..bdfb579 100644 --- a/app/api/v1/endpoints/aichat.py +++ b/app/api/v1/endpoints/aichat.py @@ -10,7 +10,8 @@ from app.utils.get_db import get_db from app.utils.readPDF import read_pdf from fastapi import HTTPException - +from app.curd.articleDB import update_article_intro +from app.models.model import ArticleDB router = APIRouter() redis_client = get_redis_client() @@ -154,4 +155,26 @@ async def generate_graph( detail=f"AI服务异常: {str(e)}" ) return {"mermaid_code": ans.strip().replace("```mermaid", "").replace("```", "").strip()} - \ No newline at end of file + +@router.get("/intro", response_model=dict) +async def review_notes( + article_id: int, + db: AsyncSession = Depends(get_db) +): + from sqlalchemy.future import select + # 查找文献path + result = await db.execute(select(ArticleDB).where(ArticleDB.id == article_id)) + article = result.scalars().first() + + 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: + raise HTTPException( + status_code=500, + detail=f"AI服务异常: {str(e)}" + ) + # 更新文章简介到数据库 + articleDB = await update_article_intro(db, article_id, ans.strip()) + return {"articleDB": articleDB.model_dump()} \ No newline at end of file diff --git a/app/curd/articleDB.py b/app/curd/articleDB.py index b2e6ae7..1670657 100644 --- a/app/curd/articleDB.py +++ b/app/curd/articleDB.py @@ -90,4 +90,20 @@ async def recommend_article_in_db(db: AsyncSession, recommend_article: Recommend ) articles = result.scalars().all() - return [GetResponse.model_validate(article) for article in articles] \ No newline at end of file + return [GetResponse.model_validate(article) for article in articles] + +async def update_article_intro(db: AsyncSession, article_id: int, intro: str): + """ + Update the introduction of an article. + """ + result = await db.execute(select(ArticleDB).where(ArticleDB.id == article_id)) + article = result.scalars().first() + + if not article: + return None + + article.intro = intro + await db.commit() + await db.refresh(article) + + return GetResponse.model_validate(article) \ No newline at end of file diff --git a/app/models/model.py b/app/models/model.py index 77c3421..b85ef9e 100644 --- a/app/models/model.py +++ b/app/models/model.py @@ -184,6 +184,7 @@ class ArticleDB(Base): author = Column(String(300), nullable=False) file_path = Column(String(200), nullable=False) clicks = Column(Integer, default=0, nullable=False) # 点击量 + intro = Column(Text, nullable=True) # 文章简介 create_time = Column(DateTime, default=func.now(), nullable=False) # 创建时间 update_time = Column(DateTime, default=func.now(), onupdate=func.now(), nullable=False) # 更新时间 \ No newline at end of file diff --git a/app/schemas/articleDB.py b/app/schemas/articleDB.py index 53b5b6d..f103cc2 100644 --- a/app/schemas/articleDB.py +++ b/app/schemas/articleDB.py @@ -29,6 +29,8 @@ class GetResponse(BaseModel): update_time: datetime author: str file_path: str + clicks: int + intro: str | None = None class Config: from_attributes = True