trulens.core.experimental¶
trulens.core.experimental
¶
Attributes¶
_FEATURE_SETUPS
module-attribute
¶
_FEATURE_SETUPS: Dict[Feature, str] = {
OTEL_TRACING: "trulens.experimental.otel_tracing._feature"
}
Mapping from experimental flags to their setup class module by name (module containing _FeatureSetup class).
Using name here as we don't want to import them until they are needed and also importing them here would result in a circular import.
This is used to check if the optional imports are available before enabling the feature flags.
Classes¶
Feature
¶
Experimental feature flags.
Use TruSession.experimental_enable_feature to enable these features:
Examples:
from trulens.core.session import TruSession
from trulens.core.experimental import Feature
session = TruSession()
session.experimental_enable_feature(Feature.OTEL_TRACING)
_FeatureSetup
¶
Bases: BaseModel
Abstract class for utilities that manage experimental features.
Attributes¶
REQUIREMENT
class-attribute
¶
REQUIREMENT: ImportErrorMessages
The optional imports required to use the feature.
Functions¶
assert_optionals_installed
abstractmethod
staticmethod
¶
assert_optionals_installed() -> None
Assert that the optional requirements for the feature are installed.
are_optionals_installed
abstractmethod
staticmethod
¶
are_optionals_installed() -> bool
Check if the optional requirements for the feature are installed.
assert_can_enable
staticmethod
¶
assert_can_enable(feature: Feature) -> None
Asserts that the given feature can be enabled.
This is used to check if the optional imports are available before enabling the feature flags.
can_enable
staticmethod
¶
Check if the given feature can be enabled.
This is used to check if the optional imports are available before enabling the feature flags.
load_setup
staticmethod
¶
load_setup(modname: str) -> Type[_FeatureSetup]
Load the setup class for the given module.
_Setting
¶
Bases: Generic[T]
A setting that attains some value and can be prevented from further changes ("frozen").
Attributes¶
frozen_by
instance-attribute
¶
Set of representations of frames (not in trulens) that have frozen this value.
If empty, it has not been frozen and can be changed.
Functions¶
set
¶
Set/Get the value.
Set the value first if a value is provided. Make it unchangeable if freeze is set.
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the setting has already been frozen and the value is different from the current value. |
get
¶
get(freeze: bool = False) -> T
Get the value of this setting.
If freeze is True, freeze the setting so it cannot be changed.
freeze
¶
freeze(value: Optional[T] = None) -> T
Lock the value of this setting.
If a value is provided, attempt to set the setting first to that value.
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the setting has already been frozen and the value is different from the current value. |
_Settings
¶
A collection of settings to enable/disable experimental features.
A feature can be enabled/disabled and/or frozen so that once it is set, it cannot be changed again. Locking is necessary for some features like OTEL tracing as once components have been instrumented with old or new tracing, the instrumentation cannot be changed.
Attributes¶
settings
instance-attribute
¶
The settings for the experimental features.
Functions¶
set
¶
Get/Set the given feature flag to the given value.
Sets the flag to the given value if the value parameter is set. Freezes the flag if the freeze parameter is set to True.
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the flag was already frozen to a different value. |
freeze
¶
Lock the given feature flag to the given value.
If the value is not provided, freeze the flag to its current value.
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the flag has already been frozen to a different value. |
get
¶
Determine the value of the given feature flag.
enable
¶
Enable the given feature flag.
Freeze the flag if the freeze parameter is set to True.
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the flag was already frozen to disabled. |
disable
¶
Disable the given feature flag.
Freezes the flag if the freeze parameter is set to True.
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the flag was already frozen to enabled. |
set_multiple
¶
set_multiple(
flags: Union[
Iterable[Union[str, Feature]],
Mapping[Union[str, Feature], bool],
],
freeze: bool = False,
)
Set multiple feature flags.
If freeze is set, freeze the flags. If a dictionary is passed, the keys are the feature flags and the values are the values to set them to. If a list is passed, the flags are set to True.
RAISES | DESCRIPTION |
---|---|
ValueError
|
If any of the flags are already frozen to a different value than specified. |
_WithExperimentalSettings
¶
Bases: BaseModel
, WithIdentString
Mixin to add experimental flags and control methods.
Prints out messages when features are enabled/disabled, frozen, and when a setting fails to take up due to earlier freeze.
Attributes¶
_experimental_feature_flags
class-attribute
instance-attribute
¶
_experimental_feature_flags: _Settings = PrivateAttr(
default_factory=_Settings
)
EXPERIMENTAL: Flags to control experimental features.
Functions¶
_ident_str
¶
_ident_str() -> str
Get a string to identify this instance in some way without overburdening the output with details.
_experimental_feature
¶
_experimental_feature(
flag: Union[str, Feature],
*,
value: Optional[bool] = None,
freeze: bool = False
) -> bool
Get and/or set the value of the given feature flag.
Set it first if value is given. Freeze it if freeze
is set.
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the flag is frozen to a different value. |
_experimental_freeze_feature
¶
Get and freeze the given feature flag.
experimental_enable_feature
¶
Enable the given feature flag.
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the flag is already frozen to disabled. |
experimental_disable_feature
¶
Disable the given feature flag.
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the flag is already frozen to enabled. |
experimental_feature
¶
Determine the value of the given feature flag.
If freeze
is set, the flag will be frozen to the value returned.
experimental_set_features
¶
experimental_set_features(
flags: Optional[
Union[
Iterable[Union[str, Feature]],
Mapping[Union[str, Feature], bool],
]
],
freeze: bool = False,
)
Set multiple feature flags.
If freeze
is set, the flags will be frozen to the values given.
RAISES | DESCRIPTION |
---|---|
ValueError
|
If any flag is already frozen to a different value than |
_experimental_assert_feature
¶
Raise a ValueError if the given feature flag is not enabled.
Gives instructions on how to enable the feature flag if error gets raised.
_experimental_method_override
staticmethod
¶
Decorator to replace the decorated method with the given one if the specified feature is enabled.
Freezes the flag if the frozen
parameter is set.
Example
class MyClass(_WithExperimentalSettings, ...):
def my_method_experimental(self, ...): ...
@MyClass._experimental_method_override(
flag=Feature.OTEL_TRACING,
enabled=my_method_experimental
)
def my_method(self, ...): ...
_experimental_method
staticmethod
¶
_experimental_method(
flag: Feature,
enabled: Callable,
disabled: Callable,
freeze: bool = False,
) -> Callable
Select between two methods based on the status of a feature flag.
The selection happens after the method is called. Freezes the flag if
the freeze
parameter is set.
Example
python
class MyClass(_WithExperimentalSettings, ...):
...
def my_method_default(self, ...): ...
def my_method_experimental(self, ...): ...
...
my_method = MyClass._experimental_method(
flag=Feature.OTEL_TRACING,
enabled=my_method_experimental,
disabled=my_method_default
)