feat: 优化部分代码及提示词
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
"""将简单修改或新增规划转换为动作列表"""
|
||||
"""将简单修改或包含新增、调整的规划转换为动作列表"""
|
||||
|
||||
import json
|
||||
|
||||
@@ -10,7 +10,7 @@ from app.actions.plan import assemble_plan_actions, plan_block_reason, plan_mate
|
||||
from app.clients.llm import get_llm
|
||||
from app.processor.base import BaseNode
|
||||
from app.processor.state import QueryState
|
||||
from app.prompt.action import ACTION_SYSTEM_PROMPT
|
||||
from app.prompt.action import build_action_prompt
|
||||
from app.schemas.action_validation import ActionValidationError, schema_errors
|
||||
from app.schemas.actions import ActionBatch, AddAction
|
||||
|
||||
@@ -93,7 +93,13 @@ class ActionNode(BaseNode):
|
||||
try:
|
||||
result = await llm_client.ainvoke(
|
||||
[
|
||||
SystemMessage(content=ACTION_SYSTEM_PROMPT),
|
||||
SystemMessage(
|
||||
content=build_action_prompt(
|
||||
state["requirement"],
|
||||
state.get("screen_plan"),
|
||||
repairing=state.get("repair_plan") is not None,
|
||||
)
|
||||
),
|
||||
HumanMessage(content=json.dumps(payload, ensure_ascii=False)),
|
||||
]
|
||||
)
|
||||
@@ -215,9 +221,29 @@ if __name__ == "__main__":
|
||||
}
|
||||
],
|
||||
)
|
||||
# 加 --modify 测试规划到已有组件调整,无需新增物料
|
||||
if "--modify" in sys.argv:
|
||||
state["change_scope"].needs_planning = True
|
||||
state["screen_plan"] = ScreenPlan(
|
||||
goal="向右移动 50px",
|
||||
design_summary="调整图表位置",
|
||||
layout_description="保留尺寸,向右移动",
|
||||
components=[
|
||||
{
|
||||
"plan_id": "sales-position",
|
||||
"disposition": "modify",
|
||||
"target_id": "sales-chart",
|
||||
"purpose": "调整位置",
|
||||
"layout": {"x": 150, "y": 200, "width": 400, "height": 300},
|
||||
}
|
||||
],
|
||||
)
|
||||
node = ActionNode()
|
||||
final_state = await node.process(state)
|
||||
if "--add" in sys.argv and final_state.get("actions") is not None:
|
||||
if (
|
||||
state.get("screen_plan") is not None
|
||||
and final_state.get("actions") is not None
|
||||
):
|
||||
final_state = ValidatorNode().process(final_state)
|
||||
print(
|
||||
json.dumps(
|
||||
|
||||
@@ -7,7 +7,7 @@ from langchain_core.messages import HumanMessage, SystemMessage
|
||||
from app.clients.llm import get_llm
|
||||
from app.processor.base import BaseNode
|
||||
from app.processor.state import QueryState
|
||||
from app.prompt.requirement import REQUIREMENT_SYSTEM_PROMPT
|
||||
from app.prompt.requirement import REQUIREMENT_SYSTEM_PROMPT, build_requirement_input
|
||||
from app.schemas.requirement import Requirement
|
||||
|
||||
|
||||
@@ -18,16 +18,12 @@ class RequirementNode(BaseNode):
|
||||
updated_state: QueryState = state.copy()
|
||||
context = state["input_context"]
|
||||
intent = state["intent"]
|
||||
payload = {
|
||||
"intent": intent,
|
||||
"input_context": context.model_dump(mode="json", exclude_unset=True),
|
||||
}
|
||||
|
||||
llm_client = get_llm(Requirement)
|
||||
llm_res = await llm_client.ainvoke(
|
||||
[
|
||||
SystemMessage(content=REQUIREMENT_SYSTEM_PROMPT),
|
||||
HumanMessage(content=json.dumps(payload, ensure_ascii=False)),
|
||||
HumanMessage(content=build_requirement_input(context, intent)),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
"""按顺序在副本上模拟执行动作"""
|
||||
"""校验动作范围,再按顺序修改大屏 JSON 副本,检查整批动作能否执行"""
|
||||
|
||||
from pydantic import BaseModel, ValidationError
|
||||
|
||||
from app.actions.plan import validate_plan_actions
|
||||
from app.actions.plan import validate_plan_actions, validate_plan_result
|
||||
from app.actions.registry import apply_action
|
||||
from app.processor.base import BaseNode
|
||||
from app.processor.state import QueryState
|
||||
@@ -45,7 +45,7 @@ class ValidatorNode(BaseNode):
|
||||
updated_state["simulated_screen"] = None
|
||||
return updated_state
|
||||
elif requirement is not None:
|
||||
# 规划到动作的范围校验尚未接入,不能将新增混入简单修改
|
||||
# 此分支没有规划,只校验简单修改,新增动作需要走上面的规划分支
|
||||
if any(isinstance(action, AddAction) for action in batch.actions):
|
||||
updated_state["validation_passed"] = False
|
||||
updated_state["validation_errors"] = [
|
||||
@@ -87,6 +87,7 @@ class ValidatorNode(BaseNode):
|
||||
updated_state["simulated_screen"] = None
|
||||
return updated_state
|
||||
|
||||
# 在副本上试执行,不改变用户传入的大屏,也不操作前端画布
|
||||
screen = state["input_context"].screen.model_copy(deep=True)
|
||||
for index, action in enumerate(batch.actions):
|
||||
try:
|
||||
@@ -102,9 +103,18 @@ class ValidatorNode(BaseNode):
|
||||
updated_state["simulated_screen"] = None
|
||||
return updated_state
|
||||
|
||||
if state.get("screen_plan") is not None:
|
||||
errors = validate_plan_result(state, screen)
|
||||
if errors:
|
||||
updated_state["validation_passed"] = False
|
||||
updated_state["validation_errors"] = errors
|
||||
updated_state["simulated_screen"] = None
|
||||
return updated_state
|
||||
|
||||
updated_state["actions"] = batch.actions
|
||||
updated_state["validation_passed"] = True
|
||||
updated_state["validation_errors"] = []
|
||||
# 保存整批动作执行后的 JSON,供后续检查,不代表前端已经完成更新
|
||||
updated_state["simulated_screen"] = screen
|
||||
return updated_state
|
||||
|
||||
|
||||
Reference in New Issue
Block a user