Maplemx / Agently

[AI Agent Application Development Framework] - 🚀 Build AI agent native application in very few code 💬 Easy to interact with AI agent in code using structure data and chained-calls syntax 🧩 Enhance AI Agent using plugins instead of rebuild a whole new agent
http://agently.tech
Apache License 2.0
908 stars 100 forks source link

如何在workflow中使用2个agent #121

Open zifeiyu-tan opened 5 days ago

zifeiyu-tan commented 5 days ago

`"""创建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

Maplemx commented 2 days ago

需要考虑两个问题:

  1. 每个agent是独立拥有自己的对话记录,还是共享对话记录?
  2. 第二个agent需要拿到的只是第一个agent的reply还是之前完整的对话记录信息?

思路上:

  1. 你可以通过创建多个agent对象实例来在工作流中使用不同的agent,每个agent对象实例都独立拥有自己的属性设定(通过.set_agent_prompt()或是.set_role()等方法设置),你可以通过.set_agent_prompt("chat_history", [完整的对话记录])对每个agent进行独立的对话记录管理;
  2. 你可以在工作流中创建两个工作块,分别命名为get_first_agent_reply和get_second_agent_reply,将get_first_agent_reply块的agent请求结果通过storage或是inputs传递给get_second_agent_reply块即可
zifeiyu-tan commented 2 days ago

对于“将get_first_agent_reply块的agent请求结果通过storage或是inputs传递给get_second_agent_reply块即可”有没有什么例子?