feat: jev 接入,判断创建、修改、问答、不明确四类意图
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import logging
|
||||
import inspect
|
||||
import time
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import TYPE_CHECKING, TypeVar
|
||||
@@ -30,6 +31,8 @@ class BaseNode(ABC):
|
||||
|
||||
def __call__(self, state: T) -> T:
|
||||
"""执行节点,统一记录日志、耗时并保留业务异常类型"""
|
||||
if inspect.iscoroutinefunction(self.process):
|
||||
return self._acall(state)
|
||||
start_time = time.perf_counter()
|
||||
self.logger.info("--- %s 开始 ---", self.name)
|
||||
try:
|
||||
@@ -52,6 +55,24 @@ class BaseNode(ABC):
|
||||
def process(self, state: T) -> T:
|
||||
"""子类实现节点核心逻辑,返回状态更新"""
|
||||
|
||||
async def _acall(self, state: T) -> T:
|
||||
"""异步节点等待执行完成后再记录结果和耗时"""
|
||||
start_time = time.perf_counter()
|
||||
self.logger.info("--- %s 开始 ---", self.name)
|
||||
try:
|
||||
result = await self.process(state)
|
||||
except (AppError, ValidationError):
|
||||
self.logger.exception("%s 执行失败", self.name)
|
||||
raise
|
||||
except Exception as error:
|
||||
self.logger.exception("%s 执行失败", self.name)
|
||||
raise ImportProcessError(str(error), node_name=self.name) from error
|
||||
else:
|
||||
self.logger.info("--- %s 完成 ---", self.name)
|
||||
return result
|
||||
finally:
|
||||
self.logger.info("%s 耗时 %.3f 秒", self.name, time.perf_counter() - start_time)
|
||||
|
||||
def log_step(self, step_name: str, message: str = ""):
|
||||
"""记录步骤日志。"""
|
||||
log_msg = f"[{step_name}]"
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
"""需求理解节点"""
|
||||
|
||||
from app.clients.decision import DecisionEngine, get_decision_engine
|
||||
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
|
||||
|
||||
|
||||
class RequirementNode(BaseNode):
|
||||
name = "requirement_node"
|
||||
|
||||
def __init__(self, *, decision_engine: DecisionEngine | None = None):
|
||||
super().__init__()
|
||||
self._decision_engine = decision_engine
|
||||
|
||||
@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:
|
||||
# 构建jev请求
|
||||
request = DecisionRequest(
|
||||
# 获取 InputNode 整理好的完整上下文
|
||||
state=self.build_decision_state(state["input_context"]),
|
||||
decisions={"intent": build_intent_question()},
|
||||
)
|
||||
|
||||
# 调用 jev
|
||||
engine = self._decision_engine or get_decision_engine()
|
||||
decision = await engine.run(request)
|
||||
|
||||
updated_state: QueryState = {**state, "intent_decision": decision}
|
||||
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,退出时关闭连接
|
||||
state = InputNode().process(state)
|
||||
async with DecisionEngine() as engine:
|
||||
node = RequirementNode(decision_engine=engine)
|
||||
final_state = await node.process(state)
|
||||
# 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())
|
||||
|
||||
@@ -2,8 +2,10 @@ from typing import NotRequired, TypedDict
|
||||
|
||||
from app.schemas.base import JsonObject
|
||||
from app.schemas.input_context import InputContext
|
||||
from app.schemas.decision import DecisionResponse
|
||||
|
||||
|
||||
class QueryState(TypedDict):
|
||||
request: JsonObject
|
||||
input_context: NotRequired[InputContext]
|
||||
intent_decision: NotRequired[DecisionResponse]
|
||||
|
||||
Reference in New Issue
Block a user