Dynamic Step
This guide will walk you through adding a toggle to your existing RAG pipeline to allow users to dynamically choose whether to use a component or not.
Toggle functionality gives users control over which components to execute in your pipeline, providing flexibility and optimization opportunities. For example, you can toggle knowledge base retrieval, specific processing steps, or any conditional logic based on runtime conditions.
Installation
# you can use a Conda environment
pip install --extra-index-url "https://oauth2accesstoken:$(gcloud auth print-access-token)@glsdk.gdplabs.id/gen-ai-internal/simple/" gllm-rag gllm-core gllm-generation gllm-inference gllm-pipeline gllm-retrieval gllm-misc gllm-datastore# you can use a Conda environment
pip install --extra-index-url "https://oauth2accesstoken:$(gcloud auth print-access-token)@glsdk.gdplabs.id/gen-ai-internal/simple/" gllm-rag gllm-core gllm-generation gllm-inference gllm-pipeline gllm-retrieval gllm-misc gllm-datastore# you can use a Conda environment
FOR /F "tokens=*" %T IN ('gcloud auth print-access-token') DO pip install --extra-index-url "https://oauth2accesstoken:%T@glsdk.gdplabs.id/gen-ai-internal/simple/" gllm-rag gllm-core gllm-generation gllm-inference gllm-pipeline gllm-retrieval gllm-misc gllm-datastoreYou can either:
You can refer to the guide whenever you need explanation or want to clarify how each part works.
Follow along with each step to recreate the files yourself while learning about the components and how to integrate them.
Both options will work—choose based on whether you prefer speed or learning by doing!
Project Setup
Extend Your RAG Pipeline Project
Start with your completed RAG pipeline project from the Your First RAG Pipeline tutorial. We don't need to add any new file for this tutorial. Therefore, the structure should stay as is:
<project-name>/
├── data/
│ ├── <index>/...
│ ├── chroma.sqlite3
│ ├── imaginary_animals.csv
├── modules/
│ ├── retriever.py
│ └── response_synthesizer.py
├── .env
├── indexer.py
└── pipeline.py # 👈 Will be updated with toggle1) Build the Toggle Pipeline
Understanding Toggle Steps
A toggle step allows your pipeline to conditionally execute any components based on a boolean condition. This pattern can be applied to any pipeline step - retrieval, processing, validation, or custom logic. Here's how it works:
Toggle Condition: Evaluates a boolean condition from the pipeline state
Conditional Execution: If
True, executes the specified steps; ifFalse, skips themPipeline Continuation: The pipeline continues with subsequent steps regardless
In this example, we'll demonstrate toggling knowledge base retrieval, but the same pattern applies to any pipeline component.
Create the Enhanced Pipeline
Define the toggle state
Extend the RAG state to include the toggle condition:
This adds a use_knowledge_base field that controls whether retrieval is performed.
Create the toggle step
This is the core toggle logic that conditionally executes retrieval:
How it works:
condition: Lambda function that checks the
use_knowledge_basebooleanif_branch: Steps to execute if condition is
True(document retrieval)else_branch: Not specified, so nothing happens if
False
Compose the final pipeline
Chain the toggle step with the synthesis steps:
This creates a pipeline that:
Conditionally retrieves context from knowledge base based on the toggle
Synthesizes a response using either retrieved context or just the query
🧠 The response synthesizer can adapt its behavior based on whether context is available or not.
2) Run the Pipeline
Configure the pipeline state for testing
Set up test cases with different toggle values to see conditional execution in action:
When you run the pipeline with knowledge base enabled, you should see retrieval happening in the debug logs. Otherwise, you should see no retrieval in the debug logs, and the response should be generated purely from the model's knowledge.
Troubleshooting
Pipeline always executes despite toggle being False:
Check that you're using the correct state field name in your condition
Verify the lambda condition syntax:
lambda x: x["your_toggle_field"]Ensure you're passing the state correctly to
invoke()Use debug mode to see which condition is being evaluated
Empty or unexpected responses when toggle is False:
Check that downstream components can handle missing data from toggled steps
Consider updating your prompts to handle scenarios when certain data is unavailable
Toggle condition not working:
Verify your state class includes the boolean field:
your_toggle_field: boolCheck that the boolean value is properly set in your state
Use debug mode to inspect the pipeline execution flow
Test your lambda condition separately to ensure it evaluates correctly
General toggle pattern issues:
Consider the impact of skipped steps on subsequent pipeline components
Test both True and False conditions to verify expected behavior
Congratulations! You've successfully enhanced your RAG pipeline with toggle functionality. You can now conditionally execute any pipeline components based on runtime conditions, providing flexibility and optimization opportunities. This pattern can be applied to knowledge base retrieval, processing steps, validation, or any custom logic in your pipeline.
Last updated