绘制工作流连接关系图

在开始介绍具体的连接关系前,首先要向您介绍workflow.draw()方法,使用该方法,能够方便地将您已经编排好的工作流运行关系转化为Mermaid代码进行输出,您可以将Mermaid代码贴入Mermaid.live或是其他支持Mermaid代码渲染的编辑器(大部分Markdown编辑器都支持)查看工作流对象实例所对应的工作流图。还记得在介绍工作流的基本构成要素时提到的那张最基础的大模型应用工作流图吗?使用 Agently Workflow 搭建的工作流骨架以及绘制出的工作流图如下:

使用workflow.draw()绘制工作流图
import Agently
workflow = Agently.Workflow()

"""定义工作块,但暂不实现"""
@workflow.chunk()
def user_input(inputs, storage):
    pass

@workflow.chunk()
def assistant_reply(inputs, storage):
    pass

"""表达工作流连接关系"""
(
    workflow
        .connect_to("user_input")
        .if_condition(lambda return_value, storage: return_value=="#exit")
            .connect_to("END")
        .else_condition()
            .connect_to("assistant_reply")
            .connect_to("user_input")
)

"""绘制工作流图"""
print(workflow.draw())
运行结果
%%{ init: { 'flowchart': { 'curve': 'linear' }, 'theme': 'neutral' } }%%
%% Rendered By Agently %%
flowchart LR
classDef chunk_style fill:#fbfcdb,stroke:#666,stroke-width:1px,color:#333;
classDef condition_chunk_style fill:#ECECFF,stroke:#9370DB,stroke-width:1px,color:#333;
classDef loop_style fill:#f5f7fa,stroke:#666,stroke-width:1px,color:#333,stroke-dasharray: 5 5
    1e604417-8504-40d4-b0c8-6f353353d01d("START"):::chunk_style -.-> |"* -->-- default"| 6feb3818-ef0e-4d18-8f3d-b815d91968c0("user_input"):::chunk_style
    6feb3818-ef0e-4d18-8f3d-b815d91968c0("user_input"):::chunk_style -.-> |"* -->-- default"| 58307bc7-57b5-44a9-a2d3-35223257dabe{{"Condition"}}:::condition_chunk_style
    58307bc7-57b5-44a9-a2d3-35223257dabe{{"Condition"}}:::condition_chunk_style -.-> |"* -- ◇ -- default"| b282bcf6-8874-4d01-acb9-915314431ea3("END"):::chunk_style
    58307bc7-57b5-44a9-a2d3-35223257dabe{{"Condition"}}:::condition_chunk_style -.-> |"* -- ◇ -- default"| ec06ed09-0899-487a-9203-32896382e4fe("assistant_reply"):::chunk_style
    ec06ed09-0899-487a-9203-32896382e4fe("assistant_reply"):::chunk_style -.-> |"* -->-- default"| 6feb3818-ef0e-4d18-8f3d-b815d91968c0("user_input"):::chunk_style
%%{ init: { 'flowchart': { 'curve': 'linear' }, 'theme': 'neutral' } }%%
%% Rendered By Agently %%
flowchart LR
classDef chunk_style fill:#fbfcdb,stroke:#666,stroke-width:1px,color:#333;
classDef condition_chunk_style fill:#ECECFF,stroke:#9370DB,stroke-width:1px,color:#333;
classDef loop_style fill:#f5f7fa,stroke:#666,stroke-width:1px,color:#333,stroke-dasharray: 5 5
    1e604417-8504-40d4-b0c8-6f353353d01d("START"):::chunk_style -.-> |"* -->-- default"| 6feb3818-ef0e-4d18-8f3d-b815d91968c0("user_input"):::chunk_style
    6feb3818-ef0e-4d18-8f3d-b815d91968c0("user_input"):::chunk_style -.-> |"* -->-- default"| 58307bc7-57b5-44a9-a2d3-35223257dabe{{"Condition"}}:::condition_chunk_style
    58307bc7-57b5-44a9-a2d3-35223257dabe{{"Condition"}}:::condition_chunk_style -.-> |"* -- ◇ -- default"| b282bcf6-8874-4d01-acb9-915314431ea3("END"):::chunk_style
    58307bc7-57b5-44a9-a2d3-35223257dabe{{"Condition"}}:::condition_chunk_style -.-> |"* -- ◇ -- default"| ec06ed09-0899-487a-9203-32896382e4fe("assistant_reply"):::chunk_style
    ec06ed09-0899-487a-9203-32896382e4fe("assistant_reply"):::chunk_style -.-> |"* -->-- default"| 6feb3818-ef0e-4d18-8f3d-b815d91968c0("user_input"):::chunk_style

在新编写的工作流正式运行前,使用workflow.draw()方法绘制并检查工作流连接关系是否被正确编排,是我们非常鼓励的开发习惯!