Utils
__all__ = ['Conversation', 'ManifestValidator', 'PythonRunner', 'CommandRunner', 'minify_json', 'J2', 'Chat', 'str_to_hash', 'import_optional_dependency', 'execute_futures_dict', 'TokenCounter', 'PromptStack', 'remove_null_values_in_dict_recursively', 'Stream', 'constants']
module-attribute
Chat
Source code in griptape/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 : print), kw_only=True)
class-attribute
instance-attribute
processing_text: str = field(default='processing...', kw_only=True)
class-attribute
instance-attribute
prompt_prefix: str = field(default='Q: ', kw_only=True)
class-attribute
instance-attribute
response_prefix: str = field(default='A: ', kw_only=True)
class-attribute
instance-attribute
structure: Structure = field()
class-attribute
instance-attribute
start()
Source code in griptape/griptape/utils/chat.py
CommandRunner
Source code in griptape/griptape/utils/command_runner.py
run(command)
Source code in griptape/griptape/utils/command_runner.py
Conversation
Source code in griptape/griptape/utils/conversation.py
memory: ConversationMemory = field()
class-attribute
instance-attribute
__str__()
lines()
J2
Source code in griptape/griptape/utils/j2.py
environment: Environment = field(default=Factory(lambda : 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/griptape/utils/manifest_validator.py
schema()
PromptStack
Source code in griptape/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)
class-attribute
instance-attribute
Input
dataclass
Source code in griptape/griptape/utils/prompt_stack.py
content: str | list[dict]
instance-attribute
role: str
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 |
ConversationMemory
|
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/griptape/utils/prompt_stack.py
add_generic_input(content)
add_input(content, role)
add_system_input(content)
PythonRunner
Source code in griptape/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/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/griptape/utils/stream.py
structure: Structure = field()
class-attribute
instance-attribute
run(*args)
Source code in griptape/griptape/utils/stream.py
validate_structure(_, structure)
TokenCounter
Source code in griptape/griptape/utils/token_counter.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.