Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor prompt stack #861

Merged
merged 38 commits into from
Jul 2, 2024
Merged

Refactor prompt stack #861

merged 38 commits into from
Jul 2, 2024

Conversation

collindutter
Copy link
Member

@collindutter collindutter commented Jun 13, 2024

Added

  • Message for storing messages in a MessageStack. Messages consist of a role, content, and usage.
  • DeltaMessage for storing partial messages in a MessageStack. Multiple DeltaMessage can be combined to form a Message.
  • TextMessageContent for storing textual content in a Message.
  • ImageMessageContent for storing image content in a Message.
  • Support for adding TextArtifacts, ImageArtifacts, and ListArtifacts to MessageStack.
  • Support for image inputs to OpenAiChatPromptDriver, AzureOpenAiChatPromptDriver, AmazonBedrockPromptDriver, AnthropicPromptDriver, and GooglePromptDriver.
  • Input/output token usage metrics to all Prompt Drivers.
  • FinishPromptEvent.input_token_count and FinishPromptEvent.output_token_count.
  • Support for storing Artifacts as inputs/outputs in Conversation Memory Runs.
  • Agent.input for passing Artifacts as input.
  • Support for PromptTasks to take TextArtifacts, ImageArtifacts, and ListArtifacts as input.

Changed

  • BREAKING: Moved/renamed griptape.utils.PromptStack to griptape.common.MessageStack.
  • BREAKING: Renamed PromptStack.inputs to PromptStack.messages.
  • BREAKING: Moved PromptStack.USER_ROLE, PromptStack.ASSISTANT_ROLE, and PromptStack.SYSTEM_ROLE to Message.
  • BREAKING: Updated return type of PromptDriver.try_run from TextArtifact to Message.
  • BREAKING: Updated return type of PromptDriver.try_stream from Iterator[TextArtifact] to Iterator[DeltaMessage].
  • BREAKING: Removed BasePromptEvent.token_count in favor of FinishPromptEvent.input_token_count and FinishPromptEvent.output_token_count.
  • BREAKING: Removed StartPromptEvent.prompt. Use StartPromptEvent.message_stack instead.
  • BREAKING: Removed Agent.input_template in favor of Agent.input.
  • Default Prompt Driver model in GoogleStructureConfig to gemini-1.5-pro.

Closes #712


📚 Documentation preview 📚: https://griptape--861.org.readthedocs.build//861/

@collindutter collindutter force-pushed the refactor/prompt-stack-elements2 branch 11 times, most recently from 1767a83 to a53762d Compare June 19, 2024 21:29
@collindutter collindutter marked this pull request as ready for review June 19, 2024 22:36

@classmethod
def from_deltas(cls, deltas: Sequence[BaseDeltaPromptStackContent]) -> TextPromptStackContent:
text_deltas = [delta for delta in deltas if isinstance(delta, TextDeltaPromptStackContent)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this silently fail if its not the right type?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I follow -- this will not silently fail as it currently is.

Comment on lines 16 to 18
if isinstance(content, str):
content = [TextPromptStackContent(TextArtifact(value=content))]
self.__attrs_init__(content, **kwargs) # pyright: ignore[reportAttributeAccessIssue]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could use an attrs converter here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's a good way to use a converter and get proper type hints. With a converter, you'll get yelled at for trying to pass in a string. And updating the field's type hints leads down a path of Unions everywhere.

Copy link
Contributor

@dylanholmes dylanholmes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it

content = [TextPromptStackContent(TextArtifact(value=content))]
self.__attrs_init__(content, **kwargs) # pyright: ignore[reportAttributeAccessIssue]

content: list[BasePromptStackContent] = field(metadata={"serializable": True})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's stopping us from replacing this with artifacts: list[BaseArtifacts]?

Is BasePromptStackContent more than a wrapper for BaseArtifact?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went back and forth on this a million times. In this PR, yeah it's not much more. But the upcoming function calling PR, for instance, has an ActionResultPromptStackContent that contains an artifact and some metadata about the action.

Additionally, to quote something @vasinov said:

I think the reason we wanted to keep it separate is because artifacts are a pretty clean abstraction for data passing. In the PromptStack inputs really are elements that currently somewhat match artifacts but we don't know if that's going to hold true in the future. It would also be much easier to add API-specific elements if an API requires it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to avoid creating a PromptStackContent for each type of Artifact? Could there be like a Non-ActionResultPromptStackContent that just contains any artifact?

docs/griptape-framework/misc/events.md Outdated Show resolved Hide resolved
Copy link
Contributor

@dylanholmes dylanholmes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments and a couple of topics for discussion

content = [TextPromptStackContent(TextArtifact(value=content))]
self.__attrs_init__(content, **kwargs) # pyright: ignore[reportAttributeAccessIssue]

content: list[BasePromptStackContent] = field(metadata={"serializable": True})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to avoid creating a PromptStackContent for each type of Artifact? Could there be like a Non-ActionResultPromptStackContent that just contains any artifact?

griptape/drivers/prompt/amazon_bedrock_prompt_driver.py Outdated Show resolved Hide resolved
griptape/drivers/prompt/amazon_bedrock_prompt_driver.py Outdated Show resolved Hide resolved
griptape/drivers/prompt/anthropic_prompt_driver.py Outdated Show resolved Hide resolved
griptape/drivers/prompt/anthropic_prompt_driver.py Outdated Show resolved Hide resolved
griptape/drivers/prompt/anthropic_prompt_driver.py Outdated Show resolved Hide resolved
griptape/drivers/prompt/base_prompt_driver.py Outdated Show resolved Hide resolved
griptape/drivers/prompt/cohere_prompt_driver.py Outdated Show resolved Hide resolved
griptape/drivers/prompt/cohere_prompt_driver.py Outdated Show resolved Hide resolved
griptape/common/prompt_stack/prompt_stack.py Show resolved Hide resolved
@collindutter collindutter force-pushed the refactor/prompt-stack-elements2 branch from f3033bd to 52ea784 Compare June 21, 2024 18:01
@dylanholmes dylanholmes mentioned this pull request Jun 25, 2024
@collindutter collindutter force-pushed the refactor/prompt-stack-elements2 branch from 17b59cc to 29dfb0f Compare July 1, 2024 21:57
docs/griptape-framework/misc/events.md Outdated Show resolved Hide resolved
docs/griptape-framework/misc/events.md Outdated Show resolved Hide resolved
@collindutter collindutter force-pushed the refactor/prompt-stack-elements2 branch from 29dfb0f to 7802c11 Compare July 1, 2024 22:38
dylanholmes
dylanholmes previously approved these changes Jul 1, 2024
Copy link
Contributor

@dylanholmes dylanholmes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@collindutter collindutter force-pushed the refactor/prompt-stack-elements2 branch from 81afadb to 44d54dc Compare July 1, 2024 23:54
Copy link
Contributor

@dylanholmes dylanholmes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥮

@collindutter collindutter merged commit 2dcfe2d into dev Jul 2, 2024
11 checks passed
@collindutter collindutter deleted the refactor/prompt-stack-elements2 branch July 2, 2024 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Calculating Max Output Tokens Is Inconsistent Across Prompt Drivers
3 participants