Constrained Generation Controller

U

@

·

Build a constrained generation controller enforcing length, format, vocabulary, and structural constraints during and after LLM generation.

45 copies0 forks
Implement a constrained generation controller for enforcing output rules.

## Generation Constraints
{{constraints}}

## Output Format
{{output_format}}

## Enforcement Level
{{enforcement_level}}

Build the controller:

```python
class ConstrainedGenerator:
    def __init__(self, constraints: List[Constraint]):
        pass
    
    def validate_partial(self, partial_output: str) -> ValidationResult:
        """Check constraints during generation"""
        pass
    
    def enforce_at_generation(self, logits: np.ndarray, partial: str) -> np.ndarray:
        """Mask invalid tokens"""
        pass
    
    def post_process(self, output: str) -> str:
        """Fix constraint violations"""
        pass
```

Constraint types:
- Length limits (min/max)
- Format patterns (regex)
- Vocabulary restrictions
- Structural requirements
- Content policies

Include:
- Soft vs hard constraints
- Constraint conflict resolution
- Performance optimization

Details

Category

Coding

Use Cases

Constrained generationOutput controlFormat enforcement

Works Best With

claude-sonnet-4-20250514gpt-4o
Created Shared

Create your own prompt vault and start sharing

Constrained Generation Controller | Promptsy