Skip to content

Commit

Permalink
Fix adding ErrorArtifacts to Prompt Stack (#1073)
Browse files Browse the repository at this point in the history
  • Loading branch information
collindutter authored Aug 16, 2024
1 parent 14a0f0d commit a931176
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- `JsonExtractionEngine` failing to parse json when the LLM outputs more than just the json.
- Exception when adding `ErrorArtifact`'s to the Prompt Stack.

## [0.29.2] - 2024-08-16

### Fixed
Expand Down
13 changes: 11 additions & 2 deletions griptape/common/prompt_stack/prompt_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@

from attrs import define, field

from griptape.artifacts import ActionArtifact, BaseArtifact, GenericArtifact, ImageArtifact, ListArtifact, TextArtifact
from griptape.artifacts import (
ActionArtifact,
BaseArtifact,
ErrorArtifact,
GenericArtifact,
ImageArtifact,
ListArtifact,
TextArtifact,
)
from griptape.common import (
ActionCallMessageContent,
ActionResultMessageContent,
Expand Down Expand Up @@ -62,6 +70,8 @@ def __to_message_content(self, artifact: str | BaseArtifact) -> list[BaseMessage
return [ImageMessageContent(artifact)]
elif isinstance(artifact, GenericArtifact):
return [GenericMessageContent(artifact)]
elif isinstance(artifact, ErrorArtifact):
return [TextMessageContent(TextArtifact(artifact.to_text()))]
elif isinstance(artifact, ActionArtifact):
action = artifact.value
output = action.output
Expand All @@ -72,6 +82,5 @@ def __to_message_content(self, artifact: str | BaseArtifact) -> list[BaseMessage
elif isinstance(artifact, ListArtifact):
processed_contents = [self.__to_message_content(artifact) for artifact in artifact.value]
return [sub_content for processed_content in processed_contents for sub_content in processed_content]

else:
raise ValueError(f"Unsupported artifact type: {type(artifact)}")
6 changes: 6 additions & 0 deletions tests/unit/common/test_prompt_stack.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from griptape.artifacts import ActionArtifact, GenericArtifact, ImageArtifact, ListArtifact, TextArtifact
from griptape.artifacts.error_artifact import ErrorArtifact
from griptape.common import (
ActionCallMessageContent,
ActionResultMessageContent,
Expand Down Expand Up @@ -45,6 +46,8 @@ def test_add_message(self, prompt_stack):
"role",
)

prompt_stack.add_message(ErrorArtifact("foo"), "role")

assert prompt_stack.messages[0].role == "role"
assert isinstance(prompt_stack.messages[0].content[0], TextMessageContent)
assert prompt_stack.messages[0].content[0].artifact.value == "foo"
Expand Down Expand Up @@ -85,6 +88,9 @@ def test_add_message(self, prompt_stack):
assert isinstance(prompt_stack.messages[6].content[0], GenericMessageContent)
assert prompt_stack.messages[6].content[0].artifact.value == "foo"

assert prompt_stack.messages[7].role == "role"
assert isinstance(prompt_stack.messages[7].content[0], TextMessageContent)

def test_add_system_message(self, prompt_stack):
prompt_stack.add_system_message("foo")

Expand Down

0 comments on commit a931176

Please sign in to comment.