utils
__all__ = ['J2', 'Chat', 'CommandRunner', 'Conversation', 'GriptapeCloudStructure', 'ManifestValidator', 'PythonRunner', 'Stream', 'StructureVisualizer', 'TokenCounter', 'add_key_in_dict_recursively', 'build_strict_schema', 'deprecation_warn', 'dict_merge', 'execute_futures_dict', 'execute_futures_list', 'execute_futures_list_dict', 'get_mime_type', 'import_optional_dependency', 'is_dependency_installed', 'load_artifact_from_memory', 'minify_json', 'references_from_artifacts', 'remove_key_in_dict_recursively', 'remove_null_values_in_dict_recursively', 'resolve_refs', 'str_to_hash', 'with_contextvars']
module-attribute
Chat
Utility for running a chat with a Structure.
Attributes:
Name | Type | Description |
---|---|---|
structure |
Structure
|
The Structure to run. |
exit_keywords |
list[str]
|
Keywords that will exit the chat. |
exiting_text |
str
|
Text to display when exiting the chat. |
processing_text |
str
|
Text to display while processing the user's input. |
intro_text |
Optional[str]
|
Text to display when the chat starts. |
prompt_prefix |
str
|
Prefix for the user's input. |
response_prefix |
str
|
Prefix for the assistant's response. |
handle_input |
Callable[[str], str]
|
Function to get the user's input. |
handle_output |
Callable[..., None]
|
Function to output text. Takes a |
Source code in griptape/utils/chat.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
exit_keywords = field(default=['exit'], kw_only=True)
class-attribute
instance-attribute
exiting_text = field(default='Exiting...', kw_only=True)
class-attribute
instance-attribute
handle_input = field(default=Factory(lambda self: self.default_handle_input, takes_self=True), kw_only=True)
class-attribute
instance-attribute
handle_output = field(default=Factory(lambda self: self.default_handle_output, takes_self=True), kw_only=True)
class-attribute
instance-attribute
intro_text = field(default=None, kw_only=True)
class-attribute
instance-attribute
logger_level = field(default=logging.ERROR, kw_only=True)
class-attribute
instance-attribute
processing_text = field(default='Thinking...', kw_only=True)
class-attribute
instance-attribute
prompt_prefix = field(default='User: ', kw_only=True)
class-attribute
instance-attribute
response_prefix = field(default='Assistant: ', kw_only=True)
class-attribute
instance-attribute
structure = field()
class-attribute
instance-attribute
ChatPrompt
_call_handle_output(text, *, stream=False)
Source code in griptape/utils/chat.py
_has_streaming_tasks()
default_handle_input(prompt_prefix)
default_handle_output(text, *, stream=False)
start()
Source code in griptape/utils/chat.py
CommandRunner
Source code in griptape/utils/command_runner.py
run(command)
Source code in griptape/utils/command_runner.py
Conversation
Source code in griptape/utils/conversation.py
memory = field()
class-attribute
instance-attribute
__str__()
lines()
Source code in griptape/utils/conversation.py
prompt_stack()
Source code in griptape/utils/conversation.py
validate_memory(attribute, value)
Source code in griptape/utils/conversation.py
GriptapeCloudStructure
Utility for working with Griptape Cloud Structures.
Attributes:
Name | Type | Description |
---|---|---|
_event_listener |
Optional[EventListener]
|
Event Listener to use. Defaults to an EventListener with a GriptapeCloudEventListenerDriver. |
_observability |
Optional[Observability]
|
Observability to use. Defaults to an Observability with a GriptapeCloudObservabilityDriver. |
observe |
bool
|
Whether to enable observability. Enabling requires the |
Source code in griptape/utils/griptape_cloud.py
_event_listener = field(default=None, kw_only=True, alias='event_listener')
class-attribute
instance-attribute
_observability = field(default=None, kw_only=True, alias='observability')
class-attribute
instance-attribute
_output = field(default=None, init=False)
class-attribute
instance-attribute
in_managed_environment
property
observe = field(default=False, kw_only=True)
class-attribute
instance-attribute
output
property
writable
structure_run_id
property
__enter__()
Source code in griptape/utils/griptape_cloud.py
__exit__(exc_type, exc_value, exc_traceback)
Source code in griptape/utils/griptape_cloud.py
_to_artifact(value)
Source code in griptape/utils/griptape_cloud.py
event_listener()
observability()
Source code in griptape/utils/griptape_cloud.py
J2
Source code in griptape/utils/j2.py
environment = field(default=Factory(lambda self: Environment(loader=FileSystemLoader(self.templates_dir), trim_blocks=True, lstrip_blocks=True), takes_self=True), kw_only=True)
class-attribute
instance-attribute
template_name = field(default=None)
class-attribute
instance-attribute
templates_dir = field(default=abs_path('templates'), kw_only=True)
class-attribute
instance-attribute
render(**kwargs)
ManifestValidator
Source code in griptape/utils/manifest_validator.py
schema()
PythonRunner
Source code in griptape/utils/python_runner.py
libs = field(factory=dict, kw_only=True)
class-attribute
instance-attribute
run(code)
Source code in griptape/utils/python_runner.py
Stream
A wrapper for Structures filters Events relevant to text output and converts them to TextArtifacts.
Attributes:
Name | Type | Description |
---|---|---|
structure |
Structure
|
The Structure to wrap. |
Source code in griptape/utils/stream.py
event_types = field(default=Factory(lambda: [TextChunkEvent, ActionChunkEvent, FinishPromptEvent, FinishStructureRunEvent]))
class-attribute
instance-attribute
structure = field()
class-attribute
instance-attribute
run(*args)
Source code in griptape/utils/stream.py
StructureVisualizer
Utility class to visualize a Structure structure.
Source code in griptape/utils/structure_visualizer.py
base_url = field(default='https://mermaid.ink', kw_only=True)
class-attribute
instance-attribute
build_node_id = field(default=lambda task: task.id.title(), kw_only=True)
class-attribute
instance-attribute
header = field(default='graph TD;', kw_only=True)
class-attribute
instance-attribute
query_params = field(factory=dict, kw_only=True)
class-attribute
instance-attribute
structure = field()
class-attribute
instance-attribute
__render_task(task)
Source code in griptape/utils/structure_visualizer.py
__render_tasks(tasks)
to_url()
Generates a url that renders the Workflow structure as a Mermaid flowchart.
Reference: https://mermaid.js.org/ecosystem/tutorials#jupyter-integration-with-mermaid-js.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
URL to the rendered image |
Source code in griptape/utils/structure_visualizer.py
TokenCounter
Source code in griptape/utils/token_counter.py
add_key_in_dict_recursively(d, key, value, criteria=None)
Add a key in a dictionary recursively.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d
|
Any
|
The dictionary to add the key to. |
required |
key
|
str
|
The key to add. |
required |
value
|
Any
|
The value to add. |
required |
criteria
|
Optional[Callable[[dict], bool]]
|
An optional function to determine if the key should be added. |
None
|
Source code in griptape/utils/dict_utils.py
build_strict_schema(json_schema, schema_id)
Performs a series of post-processing steps to ensure a JSON schema is compatible with LLMs.
- Adds the
$id
and$schema
keys. - Sets
additionalProperties
toFalse
for objects without this key. - Resolves
$ref
s and removes$defs
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_schema
|
dict
|
The JSON schema to ensure is strict. |
required |
schema_id
|
str
|
The ID of the schema. |
required |
Returns: The strict JSON schema.
Source code in griptape/utils/json_schema_utils.py
deprecation_warn(message, stacklevel=2)
Source code in griptape/utils/deprecation.py
dict_merge(dct, merge_dct, *, add_keys=True)
Recursive dict merge.
Inspired by :meth:dict.update()
, instead of
updating only top-level keys, dict_merge recurses down into dicts nested
to an arbitrary depth, updating keys. The merge_dct
is merged into
dct
.
This version will return a copy of the dictionary and leave the original arguments untouched.
The optional argument add_keys
, determines whether keys which are
present in merge_dict
but not dct
should be included in the
new dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dct
|
Optional[dict]
|
onto which the merge is executed |
required |
merge_dct
|
Optional[dict]
|
dct merged into dct |
required |
add_keys
|
bool
|
whether to add new keys |
True
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
updated dict |
Source code in griptape/utils/dict_utils.py
execute_futures_dict(fs_dict)
execute_futures_list(fs_list)
execute_futures_list_dict(fs_dict)
Source code in griptape/utils/futures.py
get_mime_type(file_path_or_bytes)
Attempt to determine the MIME type of a file or bytes.
If the input is a file path, we use the built-in mimetypes
package to guess the MIME type.
If the input is bytes, we use the filetype
library to determine the MIME type.
If the library cannot determine the MIME type (data missing magic bytes), we use a few heuristics to guess the type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path_or_bytes
|
str | bytes
|
The path to the file or the bytes to check. |
required |
Returns: The MIME type of the file or bytes.
Source code in griptape/utils/file_utils.py
import_optional_dependency(name)
Import an optional dependency.
If a dependency is missing, an ImportError with a nice message will be raised.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The module name. |
required |
Returns:
Type | Description |
---|---|
ModuleType
|
The imported module, when found. |
ModuleType
|
None is returned when the package is not found and |
Source code in griptape/utils/import_utils.py
is_dependency_installed(name)
Check if an optional dependency is available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The module name. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the dependency is available. |
bool
|
False if the dependency is not available. |
Source code in griptape/utils/import_utils.py
minify_json(value)
references_from_artifacts(artifacts)
remove_key_in_dict_recursively(d, key)
remove_null_values_in_dict_recursively(d)
resolve_refs(schema)
Recursively resolve all local $refs in the given JSON Schema using $defs as the source.
Required since pydantic does not support nested schemas without $refs. https://github.com/pydantic/pydantic/issues/889
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema
|
dict[str, Any]
|
A JSON Schema as a dictionary, which may contain "$refs" and "$defs". |
required |
Returns: A new dictionary with all local $refs resolved against $defs.