-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: Support Multiple System Messages with Better API #3167
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7a73100
init
OmCheeLin 5a3b945
fix none
OmCheeLin c2d5ab7
style
OmCheeLin 4e25e36
fix
OmCheeLin bab389f
add arg
OmCheeLin f8907ac
Merge branch 'master' into better-api
OmCheeLin 8260bae
enhance: Support Multiple System Messages with Better API PR3167 (#3330)
Wendong-Fan d1bed57
update test
Wendong-Fan 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
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 |
|---|---|---|
|
|
@@ -491,8 +491,8 @@ def __init__( | |
|
|
||
| # Set up system message and initialize messages | ||
| self._original_system_message = ( | ||
| BaseMessage.make_assistant_message( | ||
| role_name="Assistant", content=system_message | ||
| BaseMessage.make_system_message( | ||
| role_name="System", content=system_message | ||
| ) | ||
| if isinstance(system_message, str) | ||
| else system_message | ||
|
|
@@ -1629,8 +1629,8 @@ def _generate_system_message_for_output_language( | |
| content = self._original_system_message.content + language_prompt | ||
| return self._original_system_message.create_new_instance(content) | ||
| else: | ||
| return BaseMessage.make_assistant_message( | ||
| role_name="Assistant", | ||
| return BaseMessage.make_system_message( | ||
| role_name="System", | ||
| content=language_prompt, | ||
| ) | ||
|
|
||
|
|
@@ -1650,6 +1650,45 @@ def init_messages(self) -> None: | |
| ) | ||
| ) | ||
|
|
||
| def update_system_message( | ||
| self, system_message: Optional[Union[BaseMessage, str]] = None | ||
| ) -> None: | ||
| r"""Update the system message. | ||
| It will reset conversation with new system message. | ||
|
|
||
| Args: | ||
| system_message (Optional[Union[BaseMessage, str]]): | ||
| Can be either a BaseMessage object or a string. | ||
| If a string is provided, it will be converted | ||
| into a BaseMessage object. | ||
| """ | ||
| self._original_system_message = ( | ||
| BaseMessage.make_system_message( | ||
| role_name="System", content=system_message | ||
| ) | ||
| if isinstance(system_message, str) | ||
| else system_message | ||
| ) | ||
| self._system_message = ( | ||
| self._generate_system_message_for_output_language() | ||
| ) | ||
| self.init_messages() | ||
|
|
||
| def append_to_system_message(self, content: str) -> None: | ||
| original_content = ( | ||
hesamsheikh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self._original_system_message.content | ||
| if self._original_system_message | ||
| else "" | ||
| ) | ||
| new_system_message = original_content + content | ||
|
||
| self._original_system_message = BaseMessage.make_system_message( | ||
| role_name="System", content=new_system_message | ||
| ) | ||
| self._system_message = ( | ||
| self._generate_system_message_for_output_language() | ||
| ) | ||
| self.init_messages() | ||
|
|
||
| def reset_to_original_system_message(self) -> None: | ||
| r"""Reset system message to original, removing any appended context. | ||
|
|
||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's add a validation for case that None is passed (like return)