# Policy: Only apply_to_* True fields are anonymized; others reach the LLM unchanged.
from upsonic import Agent, Task
from upsonic.safety_engine.base import Policy
from upsonic.safety_engine.policies.pii_policies import PIIRule, PIIAnonymizeAction
policy = Policy(
name="PII Anonymize - Description Only",
description="Only anonymize PII in task description",
rule=PIIRule(),
action=PIIAnonymizeAction(),
apply_to_description=True,
apply_to_context=False,
apply_to_system_prompt=False,
apply_to_chat_history=False,
apply_to_tool_outputs=False,
)
agent = Agent(
"anthropic/claude-sonnet-4-6",
system_prompt="User's email is john.doe@example.com",
user_policy=policy,
debug=True
)
task = Task(
description="My email is john.doe@example.com. What is my email?"
)
# Original description: "My email is john.doe@example.com. What is my email?"
# Anonymized description to provider: "My email is k8w.xqz@lmno.pqrs. What is my email?"
# System prompt to provider (NOT anonymized): still "User's email is john.doe@example.com"
# De-anonymized assistant text (user sees from print_do): real email in the answer, e.g. "Your email is john.doe@example.com."
result = agent.print_do(task)
print(result)