|
|
@@ -4,12 +4,12 @@ from datetime import datetime
|
|
|
from ..core.ark_client import get_client
|
|
|
from ..config.config import Config
|
|
|
from ..schemas.chat import ChatMessage, ChatResponse, CommentRequest, CirclePromptConfig, HistoricalFigure, \
|
|
|
- RephraseRequest, FigureUpsert, GroupChatRequest
|
|
|
+ RephraseRequest, FigureUpsert, GroupChatRequest, PostCommentBotRequest
|
|
|
from ..db.souyue_mongo import get_mblog_by_id
|
|
|
from ..db.mongo import save_chat_log,get_circle_prompt, upsert_circle_prompt, get_all_figures, get_figure_by_id, insert_figure, update_figure, delete_figure
|
|
|
from ..utils.chat_utils import get_web_search_tools, get_knowledge_search_tools
|
|
|
from ..db.ai_config import get_config_by_app_name
|
|
|
-from ..db.mongo import save_chat_history, get_chat_history, delete_chat_history, get_sessions
|
|
|
+from ..db.mongo import save_chat_history, get_chat_history, delete_chat_history, get_sessions, get_last_response_id
|
|
|
router = APIRouter()
|
|
|
|
|
|
import re
|
|
|
@@ -399,3 +399,66 @@ async def group_chat(
|
|
|
return {
|
|
|
"data": result,
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+_POST_COMMENT_BOT_BASE_PROMPT = "你是一个评论区智能助手,帮助用户理解帖子内容并回答问题。回复简洁自然,使用纯文本。"
|
|
|
+
|
|
|
+
|
|
|
+@router.post("/postCommentBot")
|
|
|
+async def post_comment_bot(request: PostCommentBotRequest):
|
|
|
+ session_id = f"post_{request.post_id}_{request.user_id}"
|
|
|
+ previous_response_id = get_last_response_id(request.user_id, session_id)
|
|
|
+ is_first_turn = (previous_response_id is None)
|
|
|
+
|
|
|
+ system_text = _POST_COMMENT_BOT_BASE_PROMPT
|
|
|
+ if is_first_turn:
|
|
|
+ doc = get_mblog_by_id(request.post_id)
|
|
|
+ if not doc:
|
|
|
+ raise HTTPException(status_code=404, detail="帖子不存在")
|
|
|
+ system_text += f"\n\n【当前帖子信息】\n标题:{doc.get('title', '')}\n摘要:{doc.get('brief', '')}"
|
|
|
+ if request.parent_comment:
|
|
|
+ system_text += f"\n\n【用户正在回复的评论】\n{request.parent_comment}"
|
|
|
+
|
|
|
+ system_msg = {"role": "system", "content": [{"type": "input_text", "text": system_text}]}
|
|
|
+
|
|
|
+ save_chat_history(
|
|
|
+ user_id=request.user_id, session_id=session_id,
|
|
|
+ role="user", content=request.message, timestamp=datetime.now(),
|
|
|
+ )
|
|
|
+
|
|
|
+ ai_config = get_config_by_app_name(request.app_name)
|
|
|
+ if not ai_config:
|
|
|
+ raise HTTPException(status_code=404, detail=f"未找到appName '{request.app_name}' 的配置")
|
|
|
+ client = get_client(request.app_name)
|
|
|
+
|
|
|
+ response = client.responses.create(
|
|
|
+ model=Config.MODEL_NAME,
|
|
|
+ input=[system_msg, {"role": "user", "content": request.message}],
|
|
|
+ stream=False,
|
|
|
+ store=True,
|
|
|
+ previous_response_id=previous_response_id,
|
|
|
+ text={"format": {"type": "text"}},
|
|
|
+ )
|
|
|
+
|
|
|
+ result = ""
|
|
|
+ for item in response.output:
|
|
|
+ if hasattr(item, 'type') and item.type == 'message' and hasattr(item, 'content'):
|
|
|
+ if isinstance(item.content, list):
|
|
|
+ for c in item.content:
|
|
|
+ if hasattr(c, 'text'):
|
|
|
+ result += c.text
|
|
|
+ else:
|
|
|
+ result += str(item.content)
|
|
|
+
|
|
|
+ result = _strip_markdown(result)
|
|
|
+
|
|
|
+ if not result:
|
|
|
+ raise HTTPException(status_code=500, detail="AI未能生成回复")
|
|
|
+
|
|
|
+ save_chat_history(
|
|
|
+ user_id=request.user_id, session_id=session_id,
|
|
|
+ role="assistant", content=result, timestamp=datetime.now(),
|
|
|
+ response_id=response.id,
|
|
|
+ )
|
|
|
+
|
|
|
+ return {"data": result}
|