Utils
__all__ = ['Conversation', 'ManifestValidator', 'PythonRunner', 'CommandRunner', 'minify_json', 'J2', 'Chat', 'str_to_hash', 'import_optional_dependency', 'is_dependency_installed', 'execute_futures_dict', 'TokenCounter', 'PromptStack', 'remove_null_values_in_dict_recursively', 'dict_merge', 'Stream', 'constants', 'load_artifact_from_memory', 'deprecation_warn', 'load_file', 'load_files']
module-attribute
Chat
Source code in griptape/utils/chat.py
exit_keywords: list[str] = field(default=['exit'], kw_only=True)
class-attribute
instance-attribute
exiting_text: str = field(default='Exiting...', kw_only=True)
class-attribute
instance-attribute
intro_text: Optional[str] = field(default=None, kw_only=True)
class-attribute
instance-attribute
output_fn: Callable[[str], None] = field(default=Factory(lambda self: self.default_output_fn, takes_self=True), kw_only=True)
class-attribute
instance-attribute
processing_text: str = field(default='Thinking...', kw_only=True)
class-attribute
instance-attribute
prompt_prefix: str = field(default='User: ', kw_only=True)
class-attribute
instance-attribute
response_prefix: str = field(default='Assistant: ', kw_only=True)
class-attribute
instance-attribute
structure: Structure = field()
class-attribute
instance-attribute
default_output_fn(text)
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: ConversationMemory = field()
class-attribute
instance-attribute
__str__()
lines()
J2
Source code in griptape/utils/j2.py
environment: 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: Optional[str] = field(default=None)
class-attribute
instance-attribute
templates_dir: str = field(default=abs_path('templates'), kw_only=True)
class-attribute
instance-attribute
render(**kwargs)
ManifestValidator
Source code in griptape/utils/manifest_validator.py
schema()
PromptStack
Bases: SerializableMixin
Source code in griptape/utils/prompt_stack.py
ASSISTANT_ROLE = 'assistant'
class-attribute
instance-attribute
GENERIC_ROLE = 'generic'
class-attribute
instance-attribute
SYSTEM_ROLE = 'system'
class-attribute
instance-attribute
USER_ROLE = 'user'
class-attribute
instance-attribute
inputs: list[Input] = field(factory=list, kw_only=True, metadata={'serializable': True})
class-attribute
instance-attribute
Input
Bases: SerializableMixin
Source code in griptape/utils/prompt_stack.py
content: str = field(metadata={'serializable': True})
class-attribute
instance-attribute
role: str = field(metadata={'serializable': True})
class-attribute
instance-attribute
is_assistant()
is_generic()
is_system()
add_assistant_input(content)
add_conversation_memory(memory, index=None)
Add the Conversation Memory runs to the Prompt Stack.
If autoprune is enabled, this will fit as many Conversation Memory runs into the Prompt Stack as possible without exceeding the token limit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
memory |
BaseConversationMemory
|
The Conversation Memory to add the Prompt Stack to. |
required |
index |
Optional[int]
|
Optional index to insert the Conversation Memory runs at. Defaults to appending to the end of the Prompt Stack. |
None
|
Source code in griptape/utils/prompt_stack.py
add_generic_input(content)
add_input(content, role)
add_system_input(content)
PythonRunner
Source code in griptape/utils/python_runner.py
libs: dict[str, str] = 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 that converts CompletionChunkEvent
s into an iterator of TextArtifacts.
It achieves this by running the Structure in a separate thread, listening for events from the Structure, and yielding those events.
See relevant Stack Overflow post: https://stackoverflow.com/questions/9968592/turn-functions-with-a-callback-into-python-generators
Attributes:
Name | Type | Description |
---|---|---|
structure |
Structure
|
The Structure to wrap. |
_event_queue |
Queue[BaseEvent]
|
A queue to hold events from the Structure. |
Source code in griptape/utils/stream.py
structure: Structure = field()
class-attribute
instance-attribute
run(*args)
Source code in griptape/utils/stream.py
validate_structure(_, structure)
Source code in griptape/utils/stream.py
TokenCounter
Source code in griptape/utils/token_counter.py
deprecation_warn(message, stacklevel=2)
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 |
dict
|
onto which the merge is executed |
required |
merge_dct |
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)
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:
The imported module, when found.
None is returned when the package is not found and errors
is False.
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: True if the dependency is available. False if the dependency is not available.
Source code in griptape/utils/import_utils.py
load_file(path)
Load a file from the given path and return its content as bytes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The path to the file to load. |
required |
Returns:
Type | Description |
---|---|
bytes
|
The content of the file. |
Source code in griptape/utils/file_utils.py
load_files(paths, futures_executor=None)
Load multiple files concurrently and return a dictionary of their content.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
paths |
list[str]
|
The paths to the files to load. |
required |
futures_executor |
Optional[ThreadPoolExecutor]
|
The executor to use for concurrent loading. If None, a new ThreadPoolExecutor will be created. |
None
|
Returns:
Type | Description |
---|---|
dict[str, bytes]
|
A dictionary where the keys are a hash of the path and the values are the content of the files. |