aida agent (基础版)搭建完成
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import asyncio
|
||||
from typing import Annotated, Required, TypedDict
|
||||
|
||||
from langchain_core.messages import AIMessage, AnyMessage, HumanMessage
|
||||
from langgraph.graph import END, START, StateGraph
|
||||
from langgraph.graph.message import add_messages
|
||||
|
||||
"""定义状态"""
|
||||
|
||||
|
||||
class TrendingState(TypedDict):
|
||||
messages: Required[Annotated[list[AnyMessage], add_messages]]
|
||||
input_text: str
|
||||
|
||||
|
||||
"""节点"""
|
||||
|
||||
|
||||
def extract_input_node(state: TrendingState) -> dict:
|
||||
"""从 messages 中提取用户输入"""
|
||||
input_text = state["messages"][0].content if state.get("messages") else ""
|
||||
return {"input_text": input_text}
|
||||
|
||||
|
||||
async def trending_node(state: TrendingState) -> dict:
|
||||
"""趋势分析节点(占位)"""
|
||||
input_text = state.get("input_text", "")
|
||||
|
||||
# TODO: 接入真实的趋势分析逻辑
|
||||
result_text = (
|
||||
f"【趋势分析】\n基于您的输入「{input_text}」,以下是当前服装设计趋势:\n\n"
|
||||
"1. 极简主义持续流行,黑白灰为主色调\n"
|
||||
"2. 可持续时尚成为主流,环保面料受青睐\n"
|
||||
"3. 复古风格回潮,90年代元素重新流行\n"
|
||||
"4. 功能性与美学结合,运动休闲风持续升温"
|
||||
)
|
||||
|
||||
return {"messages": [AIMessage(content=result_text)]}
|
||||
|
||||
|
||||
"""构建图"""
|
||||
|
||||
|
||||
def build_trending_graph():
|
||||
"""构建趋势分析图"""
|
||||
workflow = StateGraph(TrendingState)
|
||||
|
||||
workflow.add_node("extract_input", extract_input_node)
|
||||
workflow.add_node("trending", trending_node)
|
||||
|
||||
workflow.add_edge(START, "extract_input")
|
||||
workflow.add_edge("extract_input", "trending")
|
||||
workflow.add_edge("trending", END)
|
||||
|
||||
return workflow.compile()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
async def test():
|
||||
graph = build_trending_graph()
|
||||
result = await graph.ainvoke(
|
||||
{
|
||||
"messages": [HumanMessage(content="女装连衣裙")],
|
||||
}
|
||||
)
|
||||
print(result["messages"][-1].content)
|
||||
|
||||
asyncio.run(test())
|
||||
Reference in New Issue
Block a user