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
workflows:
# Workflow to decide where to route the user
decide_on_help_desk:
- name: "decide_help_desk_prompt"
type: "jinja"
config:
data_transformations:
messages:
jinja_template_str: "
{% for message in value %}
{% if message['role'] == 'user' %}
User: {{ message['content'] }}\n
{% else %}
Agent: {{ message['content'] }}\n
{% endif %}
{% endfor %}"
output_template:
jinja_template_str: "You must decide if the user conversation below should get routed to IT, HR, or Generic Chatbot, based on the *current state of the user* (e.g. their last message)
Conversation:
{{messages}}
Based on the above conversation please output 'IT', 'HR', or 'Other'.
"
inputs:
messages: messages
- name: "help_desk_decision_gen"
type: "generation"
config:
llm_model: "gpt-3.5-turbo"
max_tokens: 3
temperature: 0.2
inputs:
input_prompt: "decide_help_desk_prompt.output"
# HR Workflow
hr_workflow:
- name: "hr_instructions"
type: "jinja"
config:
output_template:
jinja_template_str: "
To figure out how many days off you have you should:
1. Calculate how many years you've been at the company
2. Multiply the number of years by 2
3. Add 10 to the result
"
inputs: {}
# IT Workflow
it_workflow:
- name: "it_instructions"
type: "jinja"
config:
output_template:
jinja_template_str: "
To reset your password you should:
1. Go to the company's password reset page
2. Enter your username
3. Follow the instructions on the page
"
inputs: {}
# Generic Chatbot Workflow
generic_chatbot_workflow:
- name: "generic_chatbot_instructions"
type: "jinja"
config:
output_template:
jinja_template_str: "No extra information"
inputs: {}
# Respond to the user with retrieved info
respond_to_user_given_context:
- name: "respond_to_user_template"
type: "jinja"
config:
data_transformations:
messages:
jinja_template_str: "
{% for message in value %}
{% if message['role'] == 'user' %}
User: {{ message['content'] }}\n
{% else %}
Agent: {{ message['content'] }}\n
{% endif %}
{% endfor %}"
output_template:
jinja_template_str: "
You are a helpful internal chatbot! You sometimes get access to information from HR, IT, etc. Sometimes you just have to think on the fly!
Here is some helpful information based on the conversation thus far:
{{retrieved_info}}
Based on the conversation, you should figure out what best to respond to the user with.
Conversation:
{{messages}}
Now, respond! The most important rule is that you *only* give one step at a time, the user will let you know once they've finished that step.
"
inputs:
messages: messages
retrieved_info: retrieved_info
- name: "respond_to_user"
type: "generation"
config:
llm_model: "gpt-3.5-turbo"
max_tokens: 100
temperature: 0.2
inputs:
input_prompt: "respond_to_user_template.output"
plan:
# Step 1: Decide where to route the help desk request
- workflow_name: "decide_on_help_desk"
workflow_inputs:
messages: messages
# Step 2: Run the appropriate workflow based on the decision
- branch: "get_help_desk_info"
conditional_workflows:
- condition: if
condition_input_var: "decide_on_help_desk.help_desk_decision_gen.output"
operator: "contains"
reference_var: "IT"
workflow_name: "it_workflow"
workflow_inputs: {}
- condition: elif
condition_input_var: "decide_on_help_desk.help_desk_decision_gen.output"
operator: "equals"
reference_var: "HR"
workflow_name: "hr_workflow"
workflow_inputs: {}
- condition: else
workflow_name: "generic_chatbot_workflow"
workflow_inputs: {}
merge_outputs:
retrieved_info:
- "it_workflow.it_instructions.output"
- "hr_workflow.hr_instructions.output"
- "generic_chatbot_workflow.generic_chatbot_instructions.output"
# Step 3: Generate the final response using retrieved information
- workflow_name: "respond_to_user_given_context"
workflow_inputs:
messages: messages
retrieved_info: "get_help_desk_info.retrieved_info.output"
final_output_nodes: ["respond_to_user_given_context.respond_to_user"]
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.