-
Notifications
You must be signed in to change notification settings - Fork 114
feat: add as_generic_chat_history function to convert any Context to … #1007
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
akihikokuroda
wants to merge
3
commits into
generative-computing:main
Choose a base branch
from
akihikokuroda:issue932
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.
+290
−6
Open
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # pytest: unit | ||
| """Convert a heterogeneous context to a generic chat history. | ||
|
|
||
| The as_generic_chat_history() function converts any Context into a list of | ||
| Messages, gracefully handling unknown component types by converting them to | ||
| strings. This is useful for working with mixed-type contexts or when you need | ||
| a more flexible interface than as_chat_history(). | ||
| """ | ||
|
|
||
| from mellea.core import CBlock, ModelOutputThunk | ||
| from mellea.stdlib.components import Message, as_generic_chat_history | ||
| from mellea.stdlib.context import ChatContext | ||
|
|
||
|
|
||
| def basic_example() -> list[Message]: | ||
| """Convert a standard Message-based context to chat history.""" | ||
| ctx = ChatContext() | ||
| ctx = ctx.add(Message("user", "What is 2+2?")) | ||
| ctx = ctx.add(Message("assistant", "2+2 equals 4.")) | ||
|
|
||
| history = as_generic_chat_history(ctx) | ||
| assert len(history) == 2 | ||
| assert history[0].content == "What is 2+2?" | ||
| assert history[1].content == "2+2 equals 4." | ||
| return history | ||
|
|
||
|
|
||
| def with_heterogeneous_components() -> list[Message]: | ||
| """Handle mixed component types gracefully. | ||
|
|
||
| Unlike as_chat_history(), as_generic_chat_history() can handle any | ||
| component type by converting unknown types to strings. | ||
| """ | ||
| ctx = ChatContext() | ||
| ctx = ctx.add(Message("user", "Summarize this")) | ||
| ctx = ctx.add(CBlock("Some inline content to process")) | ||
| mot = ModelOutputThunk(value="The summary is...") | ||
| ctx = ctx.add(mot) | ||
|
|
||
| history = as_generic_chat_history(ctx) | ||
| assert len(history) == 3 | ||
| assert history[0].role == "user" | ||
| assert history[1].role == "user" # CBlock defaults to 'user' | ||
| assert history[2].role == "assistant" # MOT defaults to 'assistant' | ||
| return history | ||
|
|
||
|
|
||
| def with_custom_formatter() -> list[Message]: | ||
| """Use a custom formatter for ModelOutputThunk with unparsed content. | ||
|
|
||
| You can provide a formatter function to customize how unparsed outputs | ||
| or other unknown types are converted to strings. | ||
| """ | ||
|
|
||
| def my_formatter(obj: object) -> str: | ||
| return f"[Formatted: {type(obj).__name__}]" | ||
|
|
||
| ctx = ChatContext() | ||
| ctx = ctx.add(Message("user", "Process this")) | ||
| # Add a ModelOutputThunk with a non-Message parsed_repr | ||
| mot = ModelOutputThunk(value="raw data") | ||
| mot.parsed_repr = {"type": "dict", "data": "structured"} | ||
| ctx = ctx.add(mot) | ||
|
|
||
| history = as_generic_chat_history(ctx, formatter=my_formatter) | ||
| assert len(history) == 2 | ||
| assert "[Formatted:" in history[1].content | ||
| return history | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| basic = basic_example() | ||
| print("Basic example:") | ||
| for msg in basic: | ||
| print(f" {msg.role}: {msg.content}") | ||
|
|
||
| heterogeneous = with_heterogeneous_components() | ||
| print("\nHeterogeneous example:") | ||
| for msg in heterogeneous: | ||
| print(f" {msg.role}: {msg.content}") | ||
|
|
||
| custom = with_custom_formatter() | ||
| print("\nCustom formatter example:") | ||
| for msg in custom: | ||
| print(f" {msg.role}: {msg.content}") |
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
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.
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.
I think we also need to consider the case if
c.parsed_repris a string (I believe that's what a MOT will return after a completed CBlock action (?) )quick way to test:
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.
Yes, it was an issue. I fix it. Thanks!
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.
should we add a test case for it too?