Spaces:
Running
A newer version of the Gradio SDK is available:
5.27.0
Workflows Subpackage
This subpackage provides a framework for defining, validating, and executing workflows composed of interconnected model steps with dependency management.
Overview
The workflows subpackage enables the creation and execution of workflows where multiple model steps can be combined, with outputs from earlier steps feeding into inputs of later steps. The package handles dependency resolution, execution order, and error handling.
Components
structs.py
Contains the core data structures used throughout the workflow system:
InputField
: Represents an input field with name, description, and variable referenceOutputField
: Represents an output field with name, type, and descriptionModelStep
: Represents a single step in a workflow with input fields, output fields, and model detailsWorkflow
: A collection of ModelSteps with their identifiersTossupWorkflow
: Specialized workflow for quizbowl tossup questions with buzzing capability
configs.py
Provides configuration settings and constants:
AVAILABLE_MODELS
: Supported model configurations from various providersTYPE_MAP
: Mapping of supported field types to Python typesFUNCTION_MAP
: Built-in transformation functions for input/output processing
utils.py
Provides utility functions for workflow operations:
create_dependency_graph
: Builds a dependency graph representing the execution order constraintstopological_sort
: Sorts steps in execution order based on their dependenciesdetect_cycles
: Identifies cyclic dependencies in workflow definitions
executors.py
Handles the execution of workflows:
execute_model_step
: Executes a single model step with input processing and output collectionexecute_simple_workflow
: Handles single-step workflowsexecute_multi_step_workflow
: Manages multi-step workflows with dependency resolutionexecute_workflow
: Main entry point that routes to appropriate executor based on workflow complexity
validators.py
Provides workflow validation functionality:
ValidationErrorType
: Enumeration of possible validation error typesWorkflowValidationError
: Base class for validation errors- Validation functions for steps, DAGs, variables, and types
errors.py
Defines custom exceptions for workflow-related errors:
WorkflowError
: Base class for workflow errorsCyclicDependencyError
: Raised when detecting cycles in the workflow graphUnknownVariableError
: Raised when a step requires a variable that's not provided or produced
Usage Example
from workflows.structs import InputField, ModelStep, OutputField, Workflow
# Define a workflow with two steps
step1 = ModelStep(
id="step1",
model="gpt-4o-mini",
provider="OpenAI",
call_type="llm",
system_prompt="Step1 processing",
input_fields=[InputField(name="value", description="Input value", variable="input.value")],
output_fields=[OutputField(name="result", description="Processed result", type="str", func="upper")],
)
step2 = ModelStep(
id="step2",
model="gpt-4o-mini",
provider="OpenAI",
call_type="llm",
system_prompt="Step2 processing",
input_fields=[InputField(name="result", description="Result from step1", variable="step1.result")],
output_fields=[OutputField(name="final", description="Final output", type="str", func="lower")],
)
workflow = Workflow(
steps={"step1": step1, "step2": step2},
inputs=["input.value"],
outputs={"final": "step2.final"}
)
# Execute the workflow
from workflows.executors import execute_workflow
result = execute_workflow(
workflow=workflow,
input_values={"input.value": "Hello, World!"},
return_full_content=True,
logprob_step="step2"
)
# Access results
final_output = result["final_outputs"]["final"]
intermediate_results = result["intermediate_outputs"]
step_contents = result["step_contents"]
logprob = result["logprob"]
Error Handling
The workflows system provides robust error handling:
- Detects cyclic dependencies in workflow definitions
- Validates input/output variable references
- Ensures all required inputs are provided
- Supports custom validation rules through the validation system
- Provides detailed error messages for debugging
Extending the Workflows System
To extend the workflows system:
- Add new model step types by extending the
ModelStep
class - Create custom field types by extending validation in the execution logic
- Implement additional error types in
errors.py
for specialized error handling - Add new transformation functions to
FUNCTION_MAP
inconfigs.py
- Create specialized workflow types by extending the
Workflow
class