How to Create a Tool Using a Flow#

Prerequisites

This guide assumes familiarity with:

Equipping assistants with Tools enhances their capabilities. In WayFlow, tools can be defined in various ways. One approach is to define a flow as the basis for the tool. In this guide, you will see a basic example of how a flow is used to define a tool.

Defining the tool#

In this guide, you will use an LLM.

WayFlow supports several LLM API providers. Select an LLM from the options below:

from wayflowcore.models import OCIGenAIModel

if __name__ == "__main__":

    llm = OCIGenAIModel(
        model_id="provider.model-id",
        service_endpoint="https://url-to-service-endpoint.com",
        compartment_id="compartment-id",
        auth_type="API_KEY",
    )

Now define a flow and pass additional information to describe the tool, including a name, description, and the output choice.

import os

from wayflowcore.controlconnection import ControlFlowEdge
from wayflowcore.dataconnection import DataFlowEdge
from wayflowcore.flow import Flow
from wayflowcore.models.llmmodelfactory import LlmModelFactory
from wayflowcore.property import StringProperty
from wayflowcore.steps import PromptExecutionStep, StartStep
from wayflowcore.tools.servertools import ServerTool

model_config = {
    "model_type": "vllm",
    "host_port": os.environ["MY_LLM_HOST_PORT"],
    "model_id": "meta-llama/Meta-Llama-3.1-8B-Instruct",
}
llm = LlmModelFactory.from_config(model_config)

start_step = StartStep(input_descriptors=[StringProperty("topic")])
poem_generation_step = PromptExecutionStep(
    llm=llm, prompt_template="Write a 12 lines poem about the following topic: {{ topic }}"
)
poem_generation_flow = Flow(
    begin_step_name="start_step",
    steps={"start_step": start_step, "poem_generation_step": poem_generation_step},
    control_flow_edges=[
        ControlFlowEdge(source_step=start_step, destination_step=poem_generation_step),
        ControlFlowEdge(source_step=poem_generation_step, destination_step=None),
    ],
    data_flow_edges=[DataFlowEdge(start_step, "topic", poem_generation_step, "topic")],
)

poem_generation_tool = ServerTool.from_flow(
    flow=poem_generation_flow,
    flow_name="generate_poem",
    flow_description="A simple flow to generate a 12 lines poem based on a topic.",
    # The below line is needed to specify which output of the flow should be used as the output of
    # the tool
    flow_output=PromptExecutionStep.OUTPUT,
)

Note

The above example also works with more complex flows. However, only flows that do not yield are supported — meaning the flow must run to completion without pausing to request additional input from the user.

Tip

You can now use this tool like any other server tool, and pass it either to an Agent or to a ToolExecutionStep.

Recap#

In this guide, you learned how to create server tools from Flows by using the ServerTool.from_flow method.

Below is the complete code from this guide.
import os

from wayflowcore.controlconnection import ControlFlowEdge
from wayflowcore.dataconnection import DataFlowEdge
from wayflowcore.flow import Flow
from wayflowcore.models.llmmodelfactory import LlmModelFactory
from wayflowcore.property import StringProperty
from wayflowcore.steps import PromptExecutionStep, StartStep
from wayflowcore.tools.servertools import ServerTool

model_config = {
    "model_type": "vllm",
    "host_port": os.environ["MY_LLM_HOST_PORT"],
    "model_id": "meta-llama/Meta-Llama-3.1-8B-Instruct",
}
llm = LlmModelFactory.from_config(model_config)

start_step = StartStep(input_descriptors=[StringProperty("topic")])
poem_generation_step = PromptExecutionStep(
    llm=llm, prompt_template="Write a 12 lines poem about the following topic: {{ topic }}"
)
poem_generation_flow = Flow(
    begin_step_name="start_step",
    steps={"start_step": start_step, "poem_generation_step": poem_generation_step},
    control_flow_edges=[
        ControlFlowEdge(source_step=start_step, destination_step=poem_generation_step),
        ControlFlowEdge(source_step=poem_generation_step, destination_step=None),
    ],
    data_flow_edges=[DataFlowEdge(start_step, "topic", poem_generation_step, "topic")],
)

poem_generation_tool = ServerTool.from_flow(
    flow=poem_generation_flow,
    flow_name="generate_poem",
    flow_description="A simple flow to generate a 12 lines poem based on a topic.",
    # The below line is needed to specify which output of the flow should be used as the output of
    # the tool
    flow_output=PromptExecutionStep.OUTPUT,
)

Next steps#

Having learned how to use tools in WayFlow, you may now proceed to How to Build Assistants with Tools.