Utilities
Guardrails utils.
- class smarter.apps.guardrail.utils.GuardrailExample(filepath, filename)[source]
Bases:
objectA class for loading and working with built-in YAML-based guardrail examples.
This class reads guardrail example files in YAML format, parses their contents, and exposes metadata and serialization methods for inspection and testing.
- Parameters:
See also
GuardrailExamplesfor managing collections of guardrail examples.Example usage:
example = GuardrailExample(filepath="/path/to/examples", filename="my_guardrail.yaml") print(example.name) print(example.to_yaml()) print(example.to_json())
- class smarter.apps.guardrail.utils.GuardrailExamples(*args, **kwargs)[source]
Bases:
objectA class for managing a collection of
GuardrailExampleinstances.This class loads all YAML-based guardrail examples from a specified directory, providing access to the collection and utility methods for counting and retrieving examples.
- Parameters:
Note
Only files ending with
.yamlin the guardrails path are loaded as examples.Tip
Use
count()to get the number of loaded guardrail examples, and theguardrails()property to access the list.See also
GuardrailExamplefor individual example details.Example usage:
examples = GuardrailExamples() print(examples.count()) for example in examples.guardrails: print(example.filename, example.name)
- GUARDRAILS_PATH = '/Users/mcdaniel/Desktop/gh/smarter-sh/smarter/smarter/smarter/apps/guardrail/data/guardrails'
- HERE = '/Users/mcdaniel/Desktop/gh/smarter-sh/smarter/smarter/smarter/apps/guardrail'
- property guardrails: list[GuardrailExample]
Return a list of guardrails in dictionary format.
- smarter.apps.guardrail.utils.add_builtin_guardrails(user_profile, verbose=False)[source]
Apply the built-in Guardrail manifests, in
data/guardrails, for a user.manage.py initialize_platformapplies them for the Smarter admin user, so that every account’s LLMClients may list them in theirspec.guardrails.- Parameters:
user_profile (
Optional[UserProfile]) – The UserProfile instance representing the new user. Must not be None.- Returns:
Returns True if all example guardrails are created and validated successfully.
- Return type:
- Raises:
SmarterValueError – If user_profile is not provided, or if manifest/secret application fails, or if a guardrail does not have a valid YAML representation.
Note
This function applies the manifests with the
apply_manifestmanagement command.This function is called during deployment jobs.
Important
The user_profile parameter must be a valid UserProfile instance. Passing None or an incorrect type will result in an error.
If any manifest or secret update fails, the function raises an exception and does not proceed with guardrail creation.
See also
SmarterValueError
Example usage:
from smarter.apps.account.models import UserProfile from smarter.apps.guardrail.utils import add_example_guardrails user_profile = UserProfile.objects.get(user__username="newuser") success = add_example_guardrails(user_profile) if success: print("Example guardrails created successfully.")