Variables
RuntimeVariable¶
RuntimeVariable is a wrapper
around a variable that offers the following niceties:
- Automatic type-hinting of the variable. (Note that you can pass a
type_hintto the constructor to override the type hint.) - The variable can be updated over time through the
updatemethod. (Note that you need to specify thestepargument , as it is not tracked automatically.) - The variable history is tracked in the
value_repr_historyattribute, and you can inspect the history of the variable over time through therepr_at_stepmethod. Note that this property returns a tuple of text and image representations. (See Storing variable representations for more details.)
Storing variable representations¶
Variables can be represented in two ways: as text and as images.
By default, we use Python's built-in repr function to store the
text representations of the variables. This is done both for historical
tracking, and because we send text data to the LLM. In order to avoid
prompts that are too long, we truncate the text representations of the
variables to a maximum length of 300 characters
by default. (This can be changed by setting the max_var_repr_len
argument in the
constructor.)
Customizing the repr of variables¶
Classes that want to provide a custom repr method for the LLM
should follow the LLMReprProvider
protocol by implementing the
llm_repr method.
Classes that want to provide a custom image and text repr for the LLM
should follow the LLMImageReprProvider
protocol by implementing
the llm_image_repr
method.
Handling new variable values¶
The update method
allows you to update the value of the variable at a given step.
If you are not sure about whether the variable has actually been updated,
you can use the skip_if_equal
argument.
Examples¶
Handling a variable¶
from conatus.runtime.variable import RuntimeVariable
# Equivalent to 'my_var = 1'
var = RuntimeVariable("my_var", 1)
var.update(2, step=10)
# And now you can inspect the history of the variable over time
assert var.value_repr_history == [
(0, ("1", None)),
(10, ("2", None)),
]
assert var.repr_at_step(0) == ("1", None)
assert var.repr_at_step(10) == ("2", None)
assert var.repr_at_step(4) == ("1", None)
Providing a custom repr for the LLM¶
from conatus.runtime.variable import RuntimeVariable
class AnonymizedAPIKey:
api_key: str
def __init__(self, api_key: str) -> None:
self.api_key = api_key
def llm_repr(self) -> str:
return self.api_key[:3] + "... (truncated)"
var = RuntimeVariable("my_var", AnonymizedAPIKey("1234567890"))
assert var.value_repr == ("123... (truncated)", None)
conatus.runtime.variable.RuntimeVariable
¶
RuntimeVariable(
name: str,
value: ParamType,
type_hint: TypeOfType | None = None,
*,
initial_step: int = 0,
imported: bool = False,
max_var_repr_len: int | None = None
)
Bases: ArbitraryBaseModel
Variable wrapper for the Runtime.
| PARAMETER | DESCRIPTION |
|---|---|
|
The name of the variable.
TYPE:
|
|
The value of the variable.
TYPE:
|
|
The type hint of the variable.
TYPE:
|
|
The step number at which the variable was initialized.
TYPE:
|
|
Whether the variable is imported.
TYPE:
|
|
The maximum length of the repr of the variable. If not provided, will eventually default to
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
update |
Update the value of the variable. |
repr_at_step |
Get the repr (text, image) of the variable at a given step. |
__repr__ |
Override of the |
| ATTRIBUTE | DESCRIPTION |
|---|---|
name |
The name of the variable.
TYPE:
|
type_hint |
The type hint of the variable.
TYPE:
|
value |
The value of the variable.
TYPE:
|
imported |
Whether the variable was imported.
TYPE:
|
value_repr_history |
List of all of the values the variable has taken on. |
value_repr |
The latest repr (text, image) of the variable. |
Source code in conatus/runtime/variable.py
imported
instance-attribute
¶
imported: bool
Whether the variable was imported.
"imported" here means that it was initialized at the start of the runtime, and that it was not initialized by an operation in the task run.
value_repr_history
instance-attribute
¶
List of all of the values the variable has taken on.
The list is of the following form:
To avoid memory issues, we only track the changes in the value of the
variable. To find the value of the variable at a
given step, we can use the repr_at_step
method.
value_repr
property
¶
The latest repr (text, image) of the variable.
update
¶
update(
value: ParamType,
step: int,
*,
skip_if_equal: bool = False,
max_var_repr_len: int | None = None
) -> bool
Update the value of the variable.
If you want to ensure that the history is not polluted by duplicates,
you can use the skip_if_equal
argument.
We return a boolean indicating whether the variable was updated.
from conatus.runtime.variable import RuntimeVariable
var0 = RuntimeVariable("var0", 0)
var0.update(1, step=1)
assert var0.value_repr_history == [(0, ("0", None)), (1, ("1", None))]
# You can also ensure that the history is not polluted by duplicates
var0.update(1, step=2, skip_if_equal=True)
assert var0.value_repr_history == [(0, ("0", None)), (1, ("1", None))]
Warning
The skip_if_equal
argument does not compare the values themselves, but rather the
representations of the values as returned by repr or
llm_repr
. We do this
so that mutable variables can be tracked over time.
This means, however, that using this argument will incur a performance cost, as we need to compute the repr of the value at each step.
| PARAMETER | DESCRIPTION |
|---|---|
|
The new value of the variable.
TYPE:
|
|
The step number.
TYPE:
|
|
If
TYPE:
|
|
The maximum length of the repr of the variable. Note that since we only update if the repr has changed, changing this parameter over the lifetime of the variable might cause updates to happen when they otherwise wouldn't. If not provided, will eventually default to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Whether the variable was updated.
TYPE:
|
Source code in conatus/runtime/variable.py
repr_at_step
¶
Get the repr (text, image) of the variable at a given step.
| PARAMETER | DESCRIPTION |
|---|---|
|
The step number. Use -1 to get the latest value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The text repr of the value of the variable at the given step.
TYPE:
|
str | None
|
str | None: The image repr of the value of the variable at the given step, if applicable. |
| RAISES | DESCRIPTION |
|---|---|
InvalidStepNumberError
|
If the step number is invalid, either
because it is negative or because it is lower than the first
step number in the
|
Source code in conatus/runtime/variable.py
__repr__
¶
__repr__() -> str
Override of the repr method for printing outside of LLM calls.
Note that we hide the image repr for readability.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The repr of the variable.
TYPE:
|
Source code in conatus/runtime/variable.py
Customized repr protocols¶
conatus.runtime.variable.LLMReprProvider
¶
Bases: Protocol
Protocol for objects that can provide a custom repr for the LLM.
conatus.runtime.variable.LLMImageReprProvider
¶
Bases: Protocol
Protocol for objects whose custom repr is an image and a text.
llm_image_repr
abstractmethod
¶
Get the repr of the object for the LLM.
Only JPEG in base64 are allowed
We only support JPEG images in base64, but this might change in the future.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The text caption. |
str
|
The image data, encoded in JPEG and in base64. |
Source code in conatus/runtime/variable.py
conatus.runtime.variable.llm_repr
¶
Get the repr of the object for the LLM.
If the variable has a llm_repr
or a llm_image_repr
method,
we use that. Otherwise, we fallback to the default repr function.
Note that max_var_repr_len is honored only for the text value of the
variable.
| PARAMETER | DESCRIPTION |
|---|---|
var
|
The variable to get the repr of.
TYPE:
|
max_var_repr_len
|
The maximum length of the repr of the variable.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The repr of the variable. |
Source code in conatus/runtime/variable.py
Default maximum lengths¶
conatus.utils.common_constants.DEFAULT_MAX_STR_REPR_LEN_PER_VARIABLE
module-attribute
¶
The default maximum length of the repr of a variable.
conatus.utils.common_constants.DEFAULT_MAX_RESULT_REPR_LEN
module-attribute
¶
The default maximum length of the repr of a result.