-
Notifications
You must be signed in to change notification settings - Fork 808
MAINT: Standardize system prompts on prepended_conversation #2040
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
Open
adrian-gavrila
wants to merge
12
commits into
microsoft:main
Choose a base branch
from
adrian-gavrila:adrian-gavrila/standardize-system-prompt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
eabd84b
Standardize system_prompt as a first-class consumed attack argument
adrian-gavrila ccbbb4a
Remove system_prompt section from attacks instructions
adrian-gavrila 21d7f9a
Merge branch 'main' into adrian-gavrila/standardize-system-prompt
adrian-gavrila 1f08821
Add system_prompt example to attack configuration doc
adrian-gavrila 7508400
Merge remote-tracking branch 'fork/adrian-gavrila/standardize-system-…
adrian-gavrila 90d741c
Standardize system prompts on prepended_conversation; deprecate dead …
adrian-gavrila ea83356
Add Message.from_system_prompts shorthand for prepended_conversation
adrian-gavrila 96aa0c2
Remove system-prompt section from attacks instructions
adrian-gavrila 65e78bb
Fix property docstring lint (no leading verb)
adrian-gavrila 5f7e5cd
Merge branch 'main' into adrian-gavrila/standardize-system-prompt
adrian-gavrila 62d57b6
Merge origin/main into standardize-system-prompt
adrian-gavrila a1618e9
Fix system prompt adaptation
adrian-gavrila File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,12 +8,12 @@ | |
|
|
||
| class GenericSystemSquashNormalizer(MessageListNormalizer[Message]): | ||
| """ | ||
| Normalizer that combines the first system message with the first user message using generic instruction tags. | ||
| Normalizer that combines system messages with the first user message using generic instruction tags. | ||
| """ | ||
|
|
||
| async def normalize_async(self, messages: list[Message]) -> list[Message]: | ||
| """ | ||
| Return messages with the first system message combined into the first user message. | ||
| Return messages with all system messages combined into the first user message. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if there's a system message in the middle of two user messages, I think we want it added to the second user message rather than the first. could we append the system messages to the user message following it ? |
||
|
|
||
| The format uses generic instruction tags: | ||
| ### Instructions ### | ||
|
|
@@ -25,43 +25,45 @@ async def normalize_async(self, messages: list[Message]) -> list[Message]: | |
| messages: The list of messages to normalize. | ||
|
|
||
| Returns: | ||
| A Message with the system message squashed into the first user message. | ||
| Messages with system instructions squashed into the first user message. | ||
|
|
||
| Raises: | ||
| ValueError: If the messages list is empty. | ||
| """ | ||
| if not messages: | ||
| raise ValueError("Messages list cannot be empty") | ||
|
|
||
| # Check if first message is a system message | ||
| first_piece = messages[0].get_piece() | ||
| if first_piece.api_role != "system": | ||
| # No system message to squash, return messages unchanged | ||
| system_messages = [message for message in messages if message.api_role == "system"] | ||
| if not system_messages: | ||
| return list(messages) | ||
|
|
||
| if len(messages) == 1: | ||
| # Only system message, convert to user message. | ||
| system_content = "\n\n".join( | ||
| piece.converted_value for message in system_messages for piece in message.message_pieces | ||
| ) | ||
| non_system_messages = [message for message in messages if message.api_role != "system"] | ||
|
|
||
| if not non_system_messages: | ||
| return [ | ||
| build_squashed_user_message( | ||
| new_message_content=first_piece.converted_value, source_messages=messages[:1] | ||
| new_message_content=system_content, | ||
| source_messages=system_messages, | ||
| ) | ||
| ] | ||
|
|
||
| user_message_index = next( | ||
| (i for i, message in enumerate(messages[1:], start=1) if message.api_role == "user"), | ||
| (i for i, message in enumerate(non_system_messages) if message.api_role == "user"), | ||
| -1, | ||
| ) | ||
| if user_message_index == -1: | ||
| # Preserve the instruction content without rewriting non-user messages. | ||
| return [ | ||
| build_squashed_user_message( | ||
| new_message_content=first_piece.converted_value, source_messages=messages[:1] | ||
| new_message_content=system_content, | ||
| source_messages=system_messages, | ||
| ) | ||
| ] + list(messages[1:]) | ||
| ] + non_system_messages | ||
|
|
||
| # Combine system with the first user message, preserving non-text pieces (e.g. images) and their order. | ||
| system_content = first_piece.converted_value | ||
| user_message = messages[user_message_index] | ||
| user_message = non_system_messages[user_message_index] | ||
| # Propagate prompt_metadata from the user message's first piece so downstream normalizers | ||
| # (e.g. JsonSchemaNormalizer) still see request-level metadata after squashing. | ||
| propagated_metadata = dict(user_message.message_pieces[0].prompt_metadata) | ||
|
|
@@ -98,5 +100,8 @@ async def normalize_async(self, messages: list[Message]) -> list[Message]: | |
|
|
||
| squashed_message = Message(message_pieces=squashed_pieces) | ||
|
|
||
| # Remove system (index 0), replace the first user message with the squashed version, preserve all others | ||
| return list(messages[1:user_message_index]) + [squashed_message] + list(messages[user_message_index + 1 :]) | ||
| return ( | ||
| non_system_messages[:user_message_index] | ||
| + [squashed_message] | ||
| + non_system_messages[user_message_index + 1 :] | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.