feat: 准备loop
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
"""输入解析、意图分流和需求理解流程"""
|
||||
|
||||
import inspect
|
||||
|
||||
from langgraph.graph import END, StateGraph
|
||||
|
||||
from app.processor.nodes.clarify_node import ClarifyNode
|
||||
from app.processor.nodes.input_node import InputNode
|
||||
from app.processor.nodes.intent_node import IntentNode
|
||||
from app.processor.nodes.requirement_node import RequirementNode
|
||||
from app.processor.state import QueryState
|
||||
|
||||
|
||||
def route_after_intent(state: QueryState) -> str:
|
||||
return state["intent"]
|
||||
|
||||
|
||||
def route_after_requirement(state: QueryState) -> bool:
|
||||
return state["requirement"].needs_clarification
|
||||
|
||||
|
||||
def build_graph():
|
||||
# 定义工作流
|
||||
workflow = StateGraph(QueryState)
|
||||
|
||||
# 实例化节点
|
||||
nodes = {
|
||||
"input_node": InputNode(),
|
||||
"intent_node": IntentNode(),
|
||||
"requirement_node": RequirementNode(),
|
||||
"clarify_node": ClarifyNode(),
|
||||
}
|
||||
|
||||
# 注册节点,异步节点使用 BaseNode 的异步入口
|
||||
for node_name, node in nodes.items():
|
||||
action = node._acall if inspect.iscoroutinefunction(node.process) else node
|
||||
workflow.add_node(node_name, action)
|
||||
|
||||
# 定义入口
|
||||
workflow.set_entry_point("input_node")
|
||||
|
||||
# 添加业务边
|
||||
workflow.add_edge("input_node", "intent_node")
|
||||
# 新建和修改进入需求理解,意图不明进入澄清
|
||||
workflow.add_conditional_edges(
|
||||
source="intent_node",
|
||||
path=route_after_intent,
|
||||
path_map={
|
||||
"create_screen": "requirement_node",
|
||||
"modify_screen": "requirement_node",
|
||||
"qa": END,
|
||||
"unclear": "clarify_node",
|
||||
},
|
||||
)
|
||||
workflow.add_conditional_edges(
|
||||
source="requirement_node",
|
||||
path=route_after_requirement,
|
||||
path_map={True: "clarify_node", False: END},
|
||||
)
|
||||
# 本轮只整理问题,用户补充后的恢复流程后续接入
|
||||
workflow.add_edge("clarify_node", END)
|
||||
return workflow.compile()
|
||||
|
||||
|
||||
# 创建全局图实例
|
||||
query_app = build_graph()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from app.clients.decision import close_decision_engine
|
||||
|
||||
async def main():
|
||||
# 1. 修改请求,测试完整流程
|
||||
state: QueryState = {
|
||||
"request": {
|
||||
"prompt": "做个大屏",
|
||||
"screen": {
|
||||
"canvas": {"width": 1920, "height": 1080},
|
||||
"nodes": [
|
||||
{
|
||||
"id": "sales-chart",
|
||||
"type": "bar-chart",
|
||||
"name": "销售额统计",
|
||||
"layout": {"x": 26, "y": 34.8, "width": 400, "height": 260},
|
||||
"props": {"option": {"title": {"text": "销售额统计"}}},
|
||||
}
|
||||
],
|
||||
"dataSource": [],
|
||||
},
|
||||
"context": {"selected_node_ids": ["sales-chart"]},
|
||||
}
|
||||
}
|
||||
# 2. 执行图,程序退出时关闭共享连接
|
||||
try:
|
||||
final_state = await query_app.ainvoke(state)
|
||||
finally:
|
||||
await close_decision_engine()
|
||||
|
||||
# 3. 打印完整 state
|
||||
print(
|
||||
json.dumps(
|
||||
{
|
||||
key: value.model_dump(mode="json", exclude_unset=True)
|
||||
if isinstance(value, BaseModel)
|
||||
else value
|
||||
for key, value in final_state.items()
|
||||
},
|
||||
ensure_ascii=False,
|
||||
indent=2,
|
||||
)
|
||||
)
|
||||
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,73 @@
|
||||
"""整理澄清问题,等待用户补充"""
|
||||
|
||||
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.clarify import CLARIFY_SYSTEM_PROMPT
|
||||
from app.schemas.clarification import Clarification
|
||||
|
||||
|
||||
class ClarifyNode(BaseNode):
|
||||
name = "clarify_node"
|
||||
|
||||
async def process(self, state: QueryState) -> QueryState:
|
||||
intent = state["intent"]
|
||||
print(intent)
|
||||
print("=================")
|
||||
if intent == "unclear":
|
||||
llm = get_llm(Clarification)
|
||||
clarification = await llm.ainvoke(
|
||||
[
|
||||
SystemMessage(content=CLARIFY_SYSTEM_PROMPT),
|
||||
HumanMessage(
|
||||
content=state["input_context"].model_dump_json(
|
||||
exclude_unset=True
|
||||
)
|
||||
),
|
||||
]
|
||||
)
|
||||
else:
|
||||
# 需求理解已经给出问题,直接复用
|
||||
clarification = Clarification(
|
||||
question=state["requirement"].clarification_question
|
||||
)
|
||||
return {**state, "clarification": clarification}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from app.clients.decision import close_decision_engine
|
||||
from app.processor.graph import query_app
|
||||
|
||||
async def main():
|
||||
# 修改请求,通过完整流程测试澄清分支
|
||||
state: QueryState = {
|
||||
"request": {
|
||||
"prompt": "这个图不太对",
|
||||
"screen": {"canvas": {"width": 1920, "height": 1080}, "nodes": []},
|
||||
}
|
||||
}
|
||||
try:
|
||||
final_state = await query_app.ainvoke(state)
|
||||
finally:
|
||||
await close_decision_engine()
|
||||
print(
|
||||
json.dumps(
|
||||
{
|
||||
key: value.model_dump(mode="json")
|
||||
if isinstance(value, BaseModel)
|
||||
else value
|
||||
for key, value in final_state.items()
|
||||
},
|
||||
ensure_ascii=False,
|
||||
indent=2,
|
||||
)
|
||||
)
|
||||
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,149 @@
|
||||
"""意图识别节点"""
|
||||
|
||||
from typing import cast
|
||||
|
||||
from app.clients.decision import close_decision_engine, get_decision_engine
|
||||
from app.processor.base import BaseNode
|
||||
from app.processor.state import QueryState
|
||||
from app.prompt.intent import build_intent_question
|
||||
from app.schemas.base import JsonObject
|
||||
from app.schemas.decision import DecisionRequest
|
||||
from app.schemas.input_context import InputContext
|
||||
from app.schemas.requirement import Intent
|
||||
|
||||
|
||||
class IntentNode(BaseNode):
|
||||
name = "intent_node"
|
||||
|
||||
@staticmethod
|
||||
def build_decision_state(context: InputContext) -> JsonObject:
|
||||
"""只整理判断意图需要的摘要,完整 Screen 仍保留在输入上下文"""
|
||||
nodes: list[JsonObject] = []
|
||||
for node in context.screen.nodes:
|
||||
option = node.props.get("option")
|
||||
title = option.get("title") if isinstance(option, dict) else None
|
||||
titles = title if isinstance(title, list) else [title]
|
||||
nodes.append(
|
||||
{
|
||||
"id": node.id,
|
||||
"type": node.type,
|
||||
"name": node.name,
|
||||
"titles": [
|
||||
item["text"]
|
||||
for item in titles
|
||||
if isinstance(item, dict) and isinstance(item.get("text"), str)
|
||||
],
|
||||
"layout": node.layout.model_dump(mode="json", exclude_unset=True),
|
||||
}
|
||||
)
|
||||
return {
|
||||
"prompt": context.prompt,
|
||||
"context": context.context.model_dump(mode="json", exclude_unset=True),
|
||||
"screen_summary": {
|
||||
"canvas": {
|
||||
"width": context.screen.canvas.width,
|
||||
"height": context.screen.canvas.height,
|
||||
},
|
||||
"nodes": nodes,
|
||||
"data_sources": [
|
||||
{
|
||||
key: source[key]
|
||||
for key in ("id", "name", "type")
|
||||
if key in source
|
||||
}
|
||||
for source in context.screen.dataSource
|
||||
],
|
||||
},
|
||||
"attachments": [
|
||||
{
|
||||
key: attachment[key]
|
||||
for key in ("id", "kind", "name")
|
||||
if key in attachment
|
||||
}
|
||||
for attachment in context.attachments
|
||||
],
|
||||
}
|
||||
|
||||
async def process(self, state: QueryState) -> QueryState:
|
||||
context = state["input_context"]
|
||||
# 构建jev请求
|
||||
request = DecisionRequest(
|
||||
# 将 InputNode 的上下文整理为意图判断摘要
|
||||
state=self.build_decision_state(context),
|
||||
decisions={"intent": build_intent_question()},
|
||||
)
|
||||
|
||||
# 调用 jev
|
||||
decision_engine = get_decision_engine()
|
||||
# 获取 jev 结果
|
||||
decision_res = await decision_engine.run(request)
|
||||
|
||||
# 原生 unclear 或低可信时澄清,原始结果完整保留
|
||||
decision = decision_res.results["intent"]
|
||||
if (
|
||||
decision.choice == "unclear"
|
||||
or decision.confidence < self.config.intent_min_confidence
|
||||
):
|
||||
intent: Intent = "unclear"
|
||||
else:
|
||||
intent = cast(Intent, decision.choice)
|
||||
|
||||
updated_state: QueryState = {
|
||||
**state,
|
||||
"intent_decision": decision_res,
|
||||
"intent": intent,
|
||||
}
|
||||
return updated_state
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from app.processor.nodes.input_node import InputNode
|
||||
|
||||
async def main():
|
||||
# 1. 修改这里的请求,测试不同意图
|
||||
state: QueryState = {
|
||||
"request": {
|
||||
"prompt": "这个图太乱了,帮我重新排一下",
|
||||
"screen": {
|
||||
"canvas": {"width": 1920, "height": 1080},
|
||||
"nodes": [
|
||||
{
|
||||
"id": "sales-chart",
|
||||
"type": "bar-chart",
|
||||
"name": "销售额统计",
|
||||
"layout": {"x": 26, "y": 34.8, "width": 400, "height": 260},
|
||||
"props": {"option": {"title": {"text": "销售额统计"}}},
|
||||
}
|
||||
],
|
||||
"dataSource": [],
|
||||
},
|
||||
"context": {"selected_node_ids": ["sales-chart"]},
|
||||
}
|
||||
}
|
||||
# 2. 先校验输入,再真实调用 Jev,退出时关闭 Jev 连接
|
||||
state = InputNode().process(state)
|
||||
node = IntentNode()
|
||||
try:
|
||||
final_state = await node.process(state)
|
||||
finally:
|
||||
await close_decision_engine()
|
||||
# 3. 打印完整 state,包含选项、概率、置信度和调用信息
|
||||
print(
|
||||
json.dumps(
|
||||
{
|
||||
key: value.model_dump(mode="json", exclude_unset=True)
|
||||
if isinstance(value, BaseModel)
|
||||
else value
|
||||
for key, value in final_state.items()
|
||||
},
|
||||
ensure_ascii=False,
|
||||
indent=2,
|
||||
)
|
||||
)
|
||||
|
||||
asyncio.run(main())
|
||||
@@ -1,100 +1,63 @@
|
||||
"""意图识别节点"""
|
||||
"""沿用意图判断,提取具体需求"""
|
||||
|
||||
from app.clients.decision import close_decision_engine, get_decision_engine
|
||||
import json
|
||||
|
||||
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 build_intent_question
|
||||
from app.schemas.base import JsonObject
|
||||
from app.schemas.decision import DecisionRequest
|
||||
from app.schemas.input_context import InputContext
|
||||
from app.prompt.requirement import REQUIREMENT_SYSTEM_PROMPT
|
||||
from app.schemas.requirement import Requirement
|
||||
|
||||
|
||||
class RequirementNode(BaseNode):
|
||||
name = "requirement_node"
|
||||
|
||||
@staticmethod
|
||||
def build_decision_state(context: InputContext) -> JsonObject:
|
||||
"""只整理判断意图需要的摘要,完整 Screen 仍保留在输入上下文"""
|
||||
nodes: list[JsonObject] = []
|
||||
for node in context.screen.nodes:
|
||||
option = node.props.get("option")
|
||||
title = option.get("title") if isinstance(option, dict) else None
|
||||
titles = title if isinstance(title, list) else [title]
|
||||
nodes.append(
|
||||
{
|
||||
"id": node.id,
|
||||
"type": node.type,
|
||||
"name": node.name,
|
||||
"titles": [
|
||||
item["text"]
|
||||
for item in titles
|
||||
if isinstance(item, dict) and isinstance(item.get("text"), str)
|
||||
],
|
||||
"layout": node.layout.model_dump(mode="json", exclude_unset=True),
|
||||
}
|
||||
)
|
||||
return {
|
||||
"prompt": context.prompt,
|
||||
"context": context.context.model_dump(mode="json", exclude_unset=True),
|
||||
"screen_summary": {
|
||||
"canvas": {
|
||||
"width": context.screen.canvas.width,
|
||||
"height": context.screen.canvas.height,
|
||||
},
|
||||
"nodes": nodes,
|
||||
"data_sources": [
|
||||
{
|
||||
key: source[key]
|
||||
for key in ("id", "name", "type")
|
||||
if key in source
|
||||
}
|
||||
for source in context.screen.dataSource
|
||||
],
|
||||
},
|
||||
"attachments": [
|
||||
{
|
||||
key: attachment[key]
|
||||
for key in ("id", "kind", "name")
|
||||
if key in attachment
|
||||
}
|
||||
for attachment in context.attachments
|
||||
],
|
||||
}
|
||||
|
||||
async def process(self, state: QueryState) -> QueryState:
|
||||
context = state["input_context"]
|
||||
# 构建jev请求
|
||||
request = DecisionRequest(
|
||||
# 将 InputNode 的上下文整理为意图判断摘要
|
||||
state=self.build_decision_state(context),
|
||||
decisions={"intent": build_intent_question()},
|
||||
intent = state["intent"]
|
||||
payload = {
|
||||
"intent": intent,
|
||||
"input_context": context.model_dump(mode="json", exclude_unset=True),
|
||||
}
|
||||
llm = get_llm(Requirement)
|
||||
result = await llm.ainvoke(
|
||||
[
|
||||
SystemMessage(content=REQUIREMENT_SYSTEM_PROMPT),
|
||||
HumanMessage(content=json.dumps(payload, ensure_ascii=False)),
|
||||
]
|
||||
)
|
||||
|
||||
# 调用 jev
|
||||
decision_engine = get_decision_engine()
|
||||
# 获取 jev 结果
|
||||
decision_res = await decision_engine.run(request)
|
||||
print(f"LLM 返回的需求: {result}")
|
||||
print("=" * 66)
|
||||
|
||||
updated_state: QueryState = {
|
||||
**state,
|
||||
"intent_decision": decision_res,
|
||||
}
|
||||
return updated_state
|
||||
# 沿用门控后的意图,合并后重新校验字段一致性
|
||||
requirement = Requirement.model_validate(
|
||||
{**result.model_dump(), "intent": intent}
|
||||
)
|
||||
node_ids = {node.id for node in context.screen.nodes}
|
||||
for change in requirement.changes:
|
||||
if unknown_ids := set(change.target_ids) - node_ids:
|
||||
raise ValueError(f"需求引用了不存在的节点: {sorted(unknown_ids)}")
|
||||
|
||||
return {**state, "requirement": requirement}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from app.clients.decision import close_decision_engine
|
||||
from app.processor.nodes.input_node import InputNode
|
||||
from app.processor.nodes.intent_node import IntentNode
|
||||
|
||||
async def main():
|
||||
# 1. 修改这里的请求,测试不同意图
|
||||
# 1. 修改这里的请求,测试具体需求提取
|
||||
state: QueryState = {
|
||||
"request": {
|
||||
"prompt": "这个图太乱了,帮我重新排一下",
|
||||
"prompt": "把销售额统计向右移动 50px,其他不变",
|
||||
"screen": {
|
||||
"canvas": {"width": 1920, "height": 1080},
|
||||
"nodes": [
|
||||
@@ -111,14 +74,16 @@ if __name__ == "__main__":
|
||||
"context": {"selected_node_ids": ["sales-chart"]},
|
||||
}
|
||||
}
|
||||
# 2. 先校验输入,再真实调用 Jev,退出时关闭 Jev 连接
|
||||
state = InputNode().process(state)
|
||||
node = RequirementNode()
|
||||
# 2. 依次执行输入、意图判断和需求理解
|
||||
try:
|
||||
state = InputNode().process(state)
|
||||
state = await IntentNode().process(state)
|
||||
node = RequirementNode()
|
||||
final_state = await node.process(state)
|
||||
finally:
|
||||
await close_decision_engine()
|
||||
# 3. 打印完整 state,包含选项、概率、置信度和调用信息
|
||||
|
||||
# 3. 打印完整 state
|
||||
print(
|
||||
json.dumps(
|
||||
{
|
||||
|
||||
@@ -3,9 +3,14 @@ from typing import NotRequired, TypedDict
|
||||
from app.schemas.base import JsonObject
|
||||
from app.schemas.input_context import InputContext
|
||||
from app.schemas.decision import DecisionResponse
|
||||
from app.schemas.requirement import Intent, Requirement
|
||||
from app.schemas.clarification import Clarification
|
||||
|
||||
|
||||
class QueryState(TypedDict):
|
||||
request: JsonObject
|
||||
input_context: NotRequired[InputContext]
|
||||
intent_decision: NotRequired[DecisionResponse]
|
||||
intent: NotRequired[Intent]
|
||||
requirement: NotRequired[Requirement]
|
||||
clarification: NotRequired[Clarification]
|
||||
|
||||
Reference in New Issue
Block a user