|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Example: Configurable Security Policy |
| 4 | +
|
| 5 | +This example demonstrates how to use the security_policy_filename field |
| 6 | +to customize the security policy included in agent system prompts. |
| 7 | +
|
| 8 | +The security policy is a critical component that defines what actions |
| 9 | +the agent is allowed to perform and under what conditions. By making |
| 10 | +it configurable, you can: |
| 11 | +
|
| 12 | +1. Use different security policies for different environments |
| 13 | +2. Customize security rules for specific use cases |
| 14 | +3. Implement organization-specific security guidelines |
| 15 | +4. Test with relaxed or stricter security policies |
| 16 | +
|
| 17 | +Usage: |
| 18 | + python examples/20_configurable_security_policy.py |
| 19 | +""" |
| 20 | + |
| 21 | +from pathlib import Path |
| 22 | + |
| 23 | +from pydantic import SecretStr |
| 24 | + |
| 25 | +from openhands.sdk import LLM |
| 26 | +from openhands.sdk.agent import Agent |
| 27 | + |
| 28 | + |
| 29 | +def setup_example_security_policy(): |
| 30 | + """Set up the example custom security policy template.""" |
| 31 | + # Get the path to the SDK prompts directory |
| 32 | + current_dir = Path(__file__).parent |
| 33 | + sdk_dir = current_dir.parent / "openhands" / "sdk" / "agent" / "prompts" |
| 34 | + |
| 35 | + # Read the example custom security policy |
| 36 | + example_policy_path = current_dir / "example_custom_security_policy.j2" |
| 37 | + target_policy_path = sdk_dir / "example_custom_security_policy.j2" |
| 38 | + |
| 39 | + # Copy the example policy to the prompts directory if it doesn't exist |
| 40 | + if not target_policy_path.exists(): |
| 41 | + with open(example_policy_path, "r") as f: |
| 42 | + content = f.read() |
| 43 | + with open(target_policy_path, "w") as f: |
| 44 | + f.write(content) |
| 45 | + print(f"Created example security policy at: {target_policy_path}") |
| 46 | + |
| 47 | + return "example_custom_security_policy.j2" |
| 48 | + |
| 49 | + |
| 50 | +def main(): |
| 51 | + """Demonstrate configurable security policy functionality.""" |
| 52 | + print("=== Configurable Security Policy Example ===\n") |
| 53 | + |
| 54 | + # Set up the example security policy |
| 55 | + custom_policy_filename = setup_example_security_policy() |
| 56 | + |
| 57 | + # Create LLM instance for testing |
| 58 | + llm = LLM(model="test-model", api_key=SecretStr("test-key"), base_url="http://test") |
| 59 | + |
| 60 | + # 1. Agent with default security policy |
| 61 | + print("1. Agent with default security policy:") |
| 62 | + default_agent = Agent(llm=llm) |
| 63 | + print(f" Security policy filename: {default_agent.security_policy_filename}") |
| 64 | + |
| 65 | + # Extract security section from system message |
| 66 | + system_message = default_agent.system_message |
| 67 | + security_start = system_message.find("<SECURITY>") |
| 68 | + security_end = system_message.find("</SECURITY>") + len("</SECURITY>") |
| 69 | + security_section = system_message[security_start:security_end] |
| 70 | + |
| 71 | + print(" Security section preview:") |
| 72 | + print(f" {security_section[:100]}...") |
| 73 | + print() |
| 74 | + |
| 75 | + # 2. Agent with custom security policy |
| 76 | + print("2. Agent with custom security policy:") |
| 77 | + custom_agent = Agent(llm=llm, security_policy_filename=custom_policy_filename) |
| 78 | + print(f" Security policy filename: {custom_agent.security_policy_filename}") |
| 79 | + |
| 80 | + # Extract security section from system message |
| 81 | + custom_system_message = custom_agent.system_message |
| 82 | + custom_security_start = custom_system_message.find("<SECURITY>") |
| 83 | + custom_security_end = custom_system_message.find("</SECURITY>") + len("</SECURITY>") |
| 84 | + custom_security_section = custom_system_message[ |
| 85 | + custom_security_start:custom_security_end |
| 86 | + ] |
| 87 | + |
| 88 | + print(" Security section preview:") |
| 89 | + print(f" {custom_security_section[:100]}...") |
| 90 | + print() |
| 91 | + |
| 92 | + # 3. Demonstrate model_copy with different security policy |
| 93 | + print("3. Using model_copy to change security policy:") |
| 94 | + copied_agent = default_agent.model_copy( |
| 95 | + update={"security_policy_filename": custom_policy_filename} |
| 96 | + ) |
| 97 | + print(f" Original agent: {default_agent.security_policy_filename}") |
| 98 | + print(f" Copied agent: {copied_agent.security_policy_filename}") |
| 99 | + print() |
| 100 | + |
| 101 | + # 4. Show that different policies produce different system messages |
| 102 | + print("4. Verification that different policies produce different content:") |
| 103 | + default_has_custom = "🔒 Example Custom Security Policy" in system_message |
| 104 | + custom_has_custom = "🔒 Example Custom Security Policy" in custom_system_message |
| 105 | + |
| 106 | + print(f" Default agent has example custom policy content: {default_has_custom}") |
| 107 | + print(f" Custom agent has example custom policy content: {custom_has_custom}") |
| 108 | + |
| 109 | + default_has_default = "🔐 Security Policy" in system_message |
| 110 | + custom_has_default = "🔐 Security Policy" in custom_system_message |
| 111 | + |
| 112 | + print(f" Default agent has default policy content: {default_has_default}") |
| 113 | + print(f" Custom agent has default policy content: {custom_has_default}") |
| 114 | + |
| 115 | + print("\n=== Example Complete ===") |
| 116 | + print("\nKey takeaways:") |
| 117 | + print( |
| 118 | + "- The security_policy_filename field allows customization of security policies" |
| 119 | + ) |
| 120 | + print("- Default value is 'security_policy.j2' for backward compatibility") |
| 121 | + print("- Different security policies produce different system message content") |
| 122 | + print( |
| 123 | + "- You can use model_copy() to create agents with different security policies" |
| 124 | + ) |
| 125 | + print("\nTo create your own security policy:") |
| 126 | + print("1. Create a new .j2 template file in openhands/sdk/agent/prompts/") |
| 127 | + print("2. Define your security rules and guidelines") |
| 128 | + print("3. Pass the filename to the Agent constructor") |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == "__main__": |
| 132 | + main() |
0 commit comments