Agently
Agently copied to clipboard
如何在workflow中使用2个agent
`"""创建Agent实例和Workflow实例""" import Agently agent = ( Agently.create_agent() .set_settings("current_model", "OAIClient") .set_settings("model.OAIClient.url", "https://api.moonshot.cn/v1") .set_settings("model.OAIClient.options", { "model": "moonshot-v1-8k" }) .set_settings("model.OAIClient.auth", { "api_key": "******" }) ) workflow = Agently.Workflow() """创建执行块(Chunk)"""
启动块
@workflow.chunk( chunk_id = "Start", type = "Start" )
用户输入块
@workflow.chunk( chunk_id = "User Input", handles = { "outputs": [{ "handle": "user_input" }], } ) def user_input_executor(inputs, storage): return { "user_input": input("[User]: ") }
Agent回复块
@workflow.chunk( chunk_id = "Assistant Reply", handles = { "inputs": [{ "handle": "user_input" }], "outputs": [{ "handle": "assistant_reply" }], } ) def assistant_reply_executor(inputs, storage): chat_history = storage.get("chat_history") or [] reply = ( agent .chat_history(chat_history) .input(inputs["user_input"]) .start() ) print("[Assistant]: ", reply) return { "assistant_reply": reply }
对话记录更新块
@workflow.chunk( chunk_id = "Update Chat History", handles = { "inputs": [ { "handle": "user_input" }, { "handle": "assistant_reply" }, ], }, ) def update_chat_history_executor(inputs, storage): chat_history = storage.get("chat_history") or [] chat_history.append({ "role": "user", "content": inputs["user_input"] }) chat_history.append({ "role": "assistant", "content": inputs["assistant_reply"] }) storage.set("chat_history", chat_history) return
退出块
@workflow.chunk( chunk_id = "Goodbye", ) def goodbye_executor(inputs, storage): print("Bye~👋") return
"""连接执行块""" workflow.chunks["Start"].connect_to(workflow.chunks["User Input"]) ( workflow.chunks["User Input"].handle("user_input") .if_condition(lambda data: data == "#exit").connect_to(workflow.chunks["Goodbye"]) .else_condition().connect_to(workflow.chunks["Assistant Reply"].handle("user_input")) ) workflow.chunks["User Input"].handle("user_input").connect_to(workflow.chunks["Update Chat History"].handle("user_input")) workflow.chunks["Assistant Reply"].handle("assistant_reply").connect_to(workflow.chunks["Update Chat History"].handle("assistant_reply")) workflow.chunks["Update Chat History"].connect_to(workflow.chunks["User Input"])
"""获取工作流Mermaid代码(可绘图)""" print(workflow.draw())
"""启动工作流""" workflow.start()`
这是在官网中拿到的关于workflow使用的例子,如果我在这个例子中使用2个agent,第2个agent需要拿到第一个agent的reply,并且由于是多轮对话,那么我需要如何写chat_history