Instruction
conatus.runtime.instruction.RuntimeInstruction
¶
Bases: ABC
Instruction for the Runtime.
There are two types of instructions that are executable by the Runtime
:
RuntimeJSONInstruction, which are structured instructions that are sent through the function calling capabilities of an AI model.RuntimePythonInstruction, which is essentially a piece of Python code.
Each of these classes has a code method, which returns the code of the
instruction. This will be essentially for replaying the run.
No execution capabilities
This class, and its subclasses, do not have any execution capabilities.
The actual execution is done by the Runtime
class. Treat this class as a piece
of code.
We're doing this in large part to avoid circular imports. See the
documentation of RuntimeState
for more information.
import_statements
abstractmethod
¶
Get the import statements for the instruction.
The import statements are returned as a list of tuples, where the first element is the module name and the second element is the name of the object.
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[str, str]]
|
list[tuple[str, str]]: The import statements for the instruction. |
Source code in conatus/runtime/instruction.py
conatus.runtime.instruction.RuntimeJSONInstruction
dataclass
¶
RuntimeJSONInstruction(
action_name: str,
arguments: dict[str, JSONType | RuntimeVariable],
returns: list[tuple[str, TypeOfType]],
stdout_output: str | None = None,
stderr_output: str | None = None,
modified_variables: list[str] = list(),
execution_successful: bool = False,
)
Bases: RuntimeInstruction
An instruction for the agent that is a JSON call.
from conatus.runtime.state import RuntimeJSONInstruction
instruction = RuntimeJSONInstruction(
action_name="search_google",
arguments={"query": "What happens if you eat soap?"},
returns=[("result", str)],
)
assert instruction.code() == (
"result: str = search_google(query='What happens if you eat soap?')"
)
arguments
instance-attribute
¶
arguments: dict[str, JSONType | RuntimeVariable]
The arguments for the action.
The arguments can be either a JSONType
or a RuntimeVariable.
returns
instance-attribute
¶
returns: list[tuple[str, TypeOfType]]
The return variables of the action, in the format (name, type_hint).
stdout_output
class-attribute
instance-attribute
¶
stdout_output: str | None = None
The output of the code snippet to stdout, if any.
stderr_output
class-attribute
instance-attribute
¶
stderr_output: str | None = None
The output of the code snippet to stderr, if any.
modified_variables
class-attribute
instance-attribute
¶
The variables that were modified by the code snippet.
execution_successful
class-attribute
instance-attribute
¶
execution_successful: bool = False
Whether the execution of the instruction was successful.
argument_to_str
staticmethod
¶
argument_to_str(
argument: JSONType | RuntimeVariable,
) -> str
Convert an argument to a string.
This is necessary to print instructions as if it was Python code.
If the argument is a RuntimeVariable
, we return the name of the
variable.
If the argument is a string, we return the string itself, in quotes.
Otherwise, we return the string representation of the argument.
Example¶
from conatus.runtime.state import RuntimeJSONInstruction
from conatus.runtime.variable import RuntimeVariable
variable = RuntimeVariable(name="variable", value="value")
assert RuntimeJSONInstruction.argument_to_str(variable) == "variable"
assert RuntimeJSONInstruction.argument_to_str("string") == "'string'"
assert RuntimeJSONInstruction.argument_to_str(1) == "1"
| RETURNS | DESCRIPTION |
|---|---|
str
|
The string representation of the argument.
TYPE:
|
Source code in conatus/runtime/instruction.py
code
¶
code() -> str
Get the Python code of the instruction.
A few rules:
-
If the action has no return value, the code will be:
-
If the action has one return value, the code will be:
-
If the action has multiple return values, the code will be:
| RETURNS | DESCRIPTION |
|---|---|
str
|
The Python code of the instruction.
TYPE:
|
Source code in conatus/runtime/instruction.py
import_statements
¶
Get the import statements for the instruction.
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[str, str]]
|
list[tuple[str, str]]: The import statements for the instruction. |
Source code in conatus/runtime/instruction.py
conatus.runtime.instruction.RuntimePythonInstruction
dataclass
¶
RuntimePythonInstruction(
snippet: str,
stdout_output: str | None = None,
stderr_output: str | None = None,
modified_variables: list[str] = list(),
execution_successful: bool = False,
)
Bases: RuntimeInstruction
An instruction for the agent that is a Python code snippet.
from conatus.runtime.state import RuntimePythonInstruction
code_snippet = "result = 10093/100"
instruction = RuntimePythonInstruction(code_snippet)
assert instruction.code() == code_snippet
stdout_output
class-attribute
instance-attribute
¶
stdout_output: str | None = None
The output of the code snippet to stdout, if any.
stderr_output
class-attribute
instance-attribute
¶
stderr_output: str | None = None
The output of the code snippet to stderr, if any.
modified_variables
class-attribute
instance-attribute
¶
The variables that were modified by the code snippet.
execution_successful
class-attribute
instance-attribute
¶
execution_successful: bool = False
Whether the execution of the instruction was successful.
code
¶
code() -> str
Get the Python code of the instruction.
It's just the snippet.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The instruction as Python code.
TYPE:
|
import_statements
¶
Get the import statements for the instruction.
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[str, str]]
|
list[tuple[str, str]]: The import statements for the instruction. |