80 lines
2.3 KiB
Python
80 lines
2.3 KiB
Python
"""整理澄清问题,等待用户补充"""
|
|
|
|
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.clarify import CLARIFY_SYSTEM_PROMPT
|
|
from app.schemas.clarification import Clarification
|
|
|
|
|
|
class ClarifyNode(BaseNode):
|
|
name = "clarify_node"
|
|
|
|
async def process(self, state: QueryState) -> QueryState:
|
|
updated_state: QueryState = state.copy()
|
|
intent = state["intent"]
|
|
|
|
payload = {
|
|
"intent": intent,
|
|
"input_context": state["input_context"].model_dump(mode="json"),
|
|
}
|
|
|
|
if intent != "unclear":
|
|
# 以需求节点的问题为依据补齐选项,不重新判断意图
|
|
payload["requirement"] = state["requirement"].model_dump(mode="json")
|
|
|
|
llm_client = get_llm(Clarification)
|
|
clarification = await llm_client.ainvoke(
|
|
[
|
|
SystemMessage(content=CLARIFY_SYSTEM_PROMPT),
|
|
HumanMessage(content=json.dumps(payload, ensure_ascii=False)),
|
|
]
|
|
)
|
|
|
|
updated_state["clarification"] = clarification
|
|
return updated_state
|
|
|
|
|
|
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, config={"configurable": {"thread_id": "clarify-test"}}
|
|
)
|
|
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,
|
|
default=str,
|
|
)
|
|
)
|
|
|
|
asyncio.run(main())
|