Conditions and Branches
Build workflows and state machines with conditional paths.
Overview
Agent Service supports branches and conditions, allowing workflows to adapt dynamically based on input data. This enables conditional execution, where different workflows run depending on decisions made within the workflow itself.
This guide explains how to implement branching logic using an AI-powered help desk routing system, which determines whether a user inquiry should go to IT, HR, or a Generic Chatbot based on the conversation’s content.
How Branching Works in Agent Service
Branching in Agent Service follows a structured process:
- Decision-Making Workflow:
- Uses a language model (LLM) to determine which branch to take.
- The LLM outputs a decision based on structured rules.
- Conditional Workflows:
- Each possible decision is mapped to a specific workflow.
- If the decision matches IT, the IT workflow runs.
- If the decision matches HR, the HR workflow runs.
- If neither condition is met, a generic chatbot workflow runs.
- Merging Outputs:
- After execution, the outputs of all possible workflows are merged into a single variable.
- The final response is generated based on retrieved information.
Example: Help Desk Routing with Conditional Workflows
This example routes user conversations based on their content, using branches and conditions to determine the appropriate response path.
YAML Configuration
Breaking Down the Implementation
1. Decision-Making Workflow
The decide_on_help_desk
workflow processes user messages and determines whether to route them to IT, HR, or a generic chatbot.
This decision is made using an LLM (help_desk_decision_gen
) based on the content of the conversation.
2. Conditional Workflows
The branch
named get_help_desk_info
executes a different workflow depending on the decision:
- If IT is detected, the
it_workflow
runs. - If HR is detected, the
hr_workflow
runs. Otherwise, the generic_chatbot_workflow runs.
3. Merging Outputs
- After execution, all possible outputs are combined into
retrieved_info
. - This ensures that only the correct workflow output is used while discarding others.
4. Generating a Response
- The
respond_to_user_given_context
workflow generates a response using the retrieved information. - The LLM is instructed to provide only one step at a time to maintain an interactive experience.
Why Use Branches and Conditions?
- Adaptive Workflows: Dynamically routes requests based on real-time inputs.
- Efficiency: Ensures only the most relevant workflow runs, reducing unnecessary processing.
- Scalability: Easily extendable to handle more branches (e.g., Finance, Legal, etc.). This approach allows Agent Service to intelligently route user requests and provide tailored responses while maintaining flexibility for future expansions.