|
|
@@ -2,9 +2,9 @@ from fastapi import APIRouter, HTTPException
|
|
|
from datetime import datetime
|
|
|
|
|
|
from ..core.ark_client import config, client
|
|
|
-from ..schemas.chat import ChatMessage, ChatResponse, CircleRequest, CirclePromptConfig
|
|
|
+from ..schemas.chat import ChatMessage, ChatResponse, CircleRequest, CirclePromptConfig, HistoricalFigure, RephraseRequest, FigureUpsert
|
|
|
from ..db.souyue_mongo import get_mblog_by_id
|
|
|
-from ..db.mongo import get_circle_prompt, upsert_circle_prompt
|
|
|
+from ..db.mongo import get_circle_prompt, upsert_circle_prompt, get_all_figures, get_figure_by_id, insert_figure, update_figure, delete_figure
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@@ -96,3 +96,85 @@ async def generate_circle_comment(request: CircleRequest):
|
|
|
model=response.model,
|
|
|
usage=response.usage.model_dump() if response.usage else None,
|
|
|
)
|
|
|
+
|
|
|
+
|
|
|
+# ===================== 历史人物管理 =====================
|
|
|
+# 获取历史人物列表
|
|
|
+@router.get("/figures", response_model=list[HistoricalFigure])
|
|
|
+async def list_figures():
|
|
|
+ return get_all_figures()
|
|
|
+
|
|
|
+# 获取单个历史人物
|
|
|
+@router.get("/figures/{id}", response_model=HistoricalFigure)
|
|
|
+async def get_figure(id: str):
|
|
|
+ doc = get_figure_by_id(id)
|
|
|
+ if not doc:
|
|
|
+ raise HTTPException(status_code=404, detail="历史人物不存在")
|
|
|
+ return doc
|
|
|
+
|
|
|
+# 新增历史人物
|
|
|
+@router.post("/figures", response_model=HistoricalFigure)
|
|
|
+async def create_figure(figure: FigureUpsert):
|
|
|
+ inserted_id = insert_figure(figure.model_dump())
|
|
|
+ if not inserted_id:
|
|
|
+ raise HTTPException(status_code=500, detail="新增失败")
|
|
|
+ return {**figure.model_dump(), "_id": inserted_id}
|
|
|
+
|
|
|
+# 修改历史人物
|
|
|
+@router.put("/figures/{id}", response_model=HistoricalFigure)
|
|
|
+async def modify_figure(id: str, figure: FigureUpsert):
|
|
|
+ matched = update_figure(id, figure.model_dump())
|
|
|
+ if not matched:
|
|
|
+ raise HTTPException(status_code=404, detail="历史人物不存在")
|
|
|
+ return {**figure.model_dump(), "_id": id}
|
|
|
+
|
|
|
+# 删除历史人物
|
|
|
+@router.delete("/figures/{id}")
|
|
|
+async def remove_figure(id: str):
|
|
|
+ deleted = delete_figure(id)
|
|
|
+ if not deleted:
|
|
|
+ raise HTTPException(status_code=404, detail="历史人物不存在")
|
|
|
+ return {"message": "删除成功", "id": id}
|
|
|
+
|
|
|
+
|
|
|
+# ===================== 润色接口 =====================
|
|
|
+# 润色接口
|
|
|
+@router.post("/rephrase")
|
|
|
+async def rephrase_as_figure(request: RephraseRequest):
|
|
|
+ figure = get_figure_by_id(request.figureId)
|
|
|
+ if not figure:
|
|
|
+ raise HTTPException(status_code=404, detail="历史人物不存在")
|
|
|
+
|
|
|
+ prompt = (
|
|
|
+ f"你是{figure['name']},{figure['description']},生活在{figure['era']}。\n"
|
|
|
+ f"请将以下话语改写成{figure['name']}的说话风格,保留原意,体现其性格特点({figure['prompt']})。\n"
|
|
|
+ f"只输出改写后的内容,不要解释、不要加引号。\n"
|
|
|
+ f"原文:{request.text}"
|
|
|
+ )
|
|
|
+
|
|
|
+ response = client.responses.create(
|
|
|
+ model=config.MODEL_NAME,
|
|
|
+ input=[{"role": "user", "content": prompt}],
|
|
|
+ stream=False,
|
|
|
+ store=False,
|
|
|
+ # thinking={"type":"auto"},
|
|
|
+ )
|
|
|
+
|
|
|
+ rephrased = ""
|
|
|
+ for item in response.output:
|
|
|
+ if hasattr(item, 'type') and item.type == 'message' and hasattr(item, 'content'):
|
|
|
+ if isinstance(item.content, list):
|
|
|
+ for content_item in item.content:
|
|
|
+ if hasattr(content_item, 'text'):
|
|
|
+ rephrased += content_item.text
|
|
|
+ else:
|
|
|
+ rephrased += str(item.content)
|
|
|
+
|
|
|
+ if not rephrased:
|
|
|
+ raise HTTPException(status_code=500, detail="AI未能生成润色结果")
|
|
|
+
|
|
|
+ return {
|
|
|
+ "original": request.text,
|
|
|
+ "rephrased": rephrased,
|
|
|
+ "figure": figure["name"],
|
|
|
+ }
|