This example demonstrates how to use ControlFlow to create a task that anonymizes sensitive information in text. It showcases the use of custom types and context passing for data privacy tasks.

Code

The following code creates a function that takes a text string containing sensitive information and returns an anonymized version along with the replacements made:

import controlflow as cf
from pydantic import BaseModel

class AnonymizationResult(BaseModel):
    original: str
    anonymized: str
    replacements: dict[str, str]

def anonymize_text(text: str) -> AnonymizationResult:
    return cf.run(
        "Anonymize the given text by replacing personal information with generic placeholders",
        result_type=AnonymizationResult,
        context={"text": text}
    )

Now we can use this function to anonymize text containing sensitive information:

Key concepts

This implementation showcases several important ControlFlow features:

  1. Pydantic models: We use a Pydantic model (AnonymizationResult) to define the structure of our anonymization result. This ensures that the task returns well-structured, consistent results including the original text, anonymized text, and replacements made.

    class AnonymizationResult(BaseModel):
        original: str
        anonymized: str
        replacements: dict[str, str]
    
  2. Context passing: We pass the original text as context to the task, providing all necessary information for the anonymization process.

    context={"text": text}
    

By leveraging these ControlFlow features, we create an efficient and flexible data anonymization tool. This example demonstrates how ControlFlow can be used to build AI-powered privacy-enhancing workflows that can handle sensitive information with care.