refactor: extract prompt bundle builder from agent

This commit is contained in:
2026-03-01 15:21:49 +08:00
parent b323a64b0b
commit 55e6fd51ec
2 changed files with 55 additions and 32 deletions

50
core/prompt_flow.py Normal file
View File

@@ -0,0 +1,50 @@
from __future__ import annotations
from dataclasses import dataclass
from typing import TYPE_CHECKING, List
if TYPE_CHECKING:
from core.pydantic_ai_agent import AgentDeps, ConversationState, CustomerMessage, CustomerServiceAgent
@dataclass
class PromptBundle:
user_prompt: str
deps: "AgentDeps"
history: List
def build_prompt_bundle(
agent: "CustomerServiceAgent",
*,
message: "CustomerMessage",
state: "ConversationState",
) -> PromptBundle:
from core.pydantic_ai_agent import AgentDeps
user_prompt = agent._build_prompt(message, state)
profile_context = agent._get_customer_profile_context(message.from_id)
if profile_context:
user_prompt = profile_context + "\n\n" + user_prompt
refusal_hint = agent._get_refusal_context_hint(message.from_id, message.msg, profile_context or "")
if refusal_hint:
user_prompt = refusal_hint + "\n\n" + user_prompt
conv_context = agent._get_conversation_context(message.from_id, acc_id=message.acc_id or "")
if conv_context:
user_prompt = conv_context + user_prompt
intent_hint = agent._get_intent_emotion_hint(message.msg)
if intent_hint:
user_prompt = intent_hint + "\n\n" + user_prompt
deps = AgentDeps(
msg_id=message.msg_id,
acc_id=message.acc_id,
from_id=message.from_id,
platform=message.acc_type,
)
history = agent.message_histories.get(message.from_id, [])
return PromptBundle(user_prompt=user_prompt, deps=deps, history=history)

View File

@@ -30,6 +30,7 @@ from core.find_image_flow import handle_find_image_batch_flow
from core.order_flow import handle_order_notification
from core.ai_reply_flow import execute_ai_turn
from core.reply_finalize_flow import finalize_ai_reply
from core.prompt_flow import build_prompt_bundle
load_dotenv()
@@ -1809,38 +1810,10 @@ class CustomerServiceAgent:
if isinstance(flow_response, AgentResponse):
return flow_response
# 构建提示词(包含对话状态 + 客户画像)
user_prompt = self._build_prompt(message, state)
# 注入客户历史画像(个性化语气、报价策略、主动预测)
profile_context = self._get_customer_profile_context(message.from_id)
if profile_context:
user_prompt = profile_context + "\n\n" + user_prompt
# 前后一致提示:刚拒绝某张图后,客户问「能找到吗」等时,必须区分能做/不能做
refusal_hint = self._get_refusal_context_hint(message.from_id, message.msg, profile_context or "")
if refusal_hint:
user_prompt = refusal_hint + "\n\n" + user_prompt
# 每一次对话都注入近期上下文,确保模型看到完整对话
conv_context = self._get_conversation_context(message.from_id, acc_id=message.acc_id or "")
if conv_context:
user_prompt = conv_context + user_prompt
# 语义匹配:意图/情绪识别(配置 EMBEDDING_MODEL 时用 embedding否则关键词
intent_hint = self._get_intent_emotion_hint(message.msg)
if intent_hint:
user_prompt = intent_hint + "\n\n" + user_prompt
deps = AgentDeps(
msg_id=message.msg_id,
acc_id=message.acc_id,
from_id=message.from_id,
platform=message.acc_type
)
# 取出该客户的历史对话,传给 AI 保持上下文
history = self.message_histories.get(message.from_id, [])
prompt_bundle = build_prompt_bundle(self, message=message, state=state)
user_prompt = prompt_bundle.user_prompt
deps = prompt_bundle.deps
history = prompt_bundle.history
self._log_block("PROMPT->AI 前置提示词", user_prompt)