-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: add temporary extra user content parts #7976
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,7 +1,7 @@ | ||||||||||||||||||||||||||||||||||||||||||||
| # Inspired by MoonshotAI/kosong, credits to MoonshotAI/kosong authors for the original implementation. | ||||||||||||||||||||||||||||||||||||||||||||
| # License: Apache License 2.0 | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| from typing import Any, ClassVar, Literal, cast | ||||||||||||||||||||||||||||||||||||||||||||
| from typing import Any, ClassVar, Literal, TypeVar, cast | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| from pydantic import ( | ||||||||||||||||||||||||||||||||||||||||||||
| BaseModel, | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -13,13 +13,16 @@ | |||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
| from pydantic_core import core_schema | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| ContentPartT = TypeVar("ContentPartT", bound="ContentPart") | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| class ContentPart(BaseModel): | ||||||||||||||||||||||||||||||||||||||||||||
| """A part of the content in a message.""" | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| __content_part_registry: ClassVar[dict[str, type["ContentPart"]]] = {} | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| type: Literal["text", "think", "image_url", "audio_url"] | ||||||||||||||||||||||||||||||||||||||||||||
| _no_save: bool = PrivateAttr(default=False) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| def __init_subclass__(cls, **kwargs: Any) -> None: | ||||||||||||||||||||||||||||||||||||||||||||
| super().__init_subclass__(**kwargs) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -50,7 +53,10 @@ def validate_content_part(value: Any) -> Any: | |||||||||||||||||||||||||||||||||||||||||||
| if not isinstance(type_value, str): | ||||||||||||||||||||||||||||||||||||||||||||
| raise ValueError(f"Cannot validate {value} as ContentPart") | ||||||||||||||||||||||||||||||||||||||||||||
| target_class = cls.__content_part_registry[type_value] | ||||||||||||||||||||||||||||||||||||||||||||
| return target_class.model_validate(value) | ||||||||||||||||||||||||||||||||||||||||||||
| part = target_class.model_validate(value) | ||||||||||||||||||||||||||||||||||||||||||||
| if cast(dict[str, Any], value).get("_no_save"): | ||||||||||||||||||||||||||||||||||||||||||||
| part._no_save = True | ||||||||||||||||||||||||||||||||||||||||||||
| return part | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| raise ValueError(f"Cannot validate {value} as ContentPart") | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -59,6 +65,17 @@ def validate_content_part(value: Any) -> Any: | |||||||||||||||||||||||||||||||||||||||||||
| # for subclasses, use the default schema | ||||||||||||||||||||||||||||||||||||||||||||
| return handler(source_type) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| def mark_as_temp(self: ContentPartT) -> ContentPartT: | ||||||||||||||||||||||||||||||||||||||||||||
| """Mark this content part as provider-facing only, not persisted.""" | ||||||||||||||||||||||||||||||||||||||||||||
| self._no_save = True | ||||||||||||||||||||||||||||||||||||||||||||
| return self | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| def model_dump_for_context(self) -> dict[str, Any]: | ||||||||||||||||||||||||||||||||||||||||||||
| data = self.model_dump() | ||||||||||||||||||||||||||||||||||||||||||||
| if self._no_save: | ||||||||||||||||||||||||||||||||||||||||||||
| data["_no_save"] = True | ||||||||||||||||||||||||||||||||||||||||||||
| return data | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| class TextPart(ContentPart): | ||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -329,7 +346,14 @@ def dump_messages_with_checkpoints(messages: list[Message]) -> list[dict]: | |||||||||||||||||||||||||||||||||||||||||||
| """Dump runtime messages and reinsert bound checkpoint segments.""" | ||||||||||||||||||||||||||||||||||||||||||||
| dumped: list[dict] = [] | ||||||||||||||||||||||||||||||||||||||||||||
| for message in messages: | ||||||||||||||||||||||||||||||||||||||||||||
| dumped.append(message.model_dump()) | ||||||||||||||||||||||||||||||||||||||||||||
| message_data = message.model_dump() | ||||||||||||||||||||||||||||||||||||||||||||
| if isinstance(message.content, list): | ||||||||||||||||||||||||||||||||||||||||||||
| message_data["content"] = [ | ||||||||||||||||||||||||||||||||||||||||||||
| part.model_dump() | ||||||||||||||||||||||||||||||||||||||||||||
| for part in message.content | ||||||||||||||||||||||||||||||||||||||||||||
| if not getattr(part, "_no_save", False) | ||||||||||||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||||||||||||
| dumped.append(message_data) | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+349
to
+356
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. 在 dump_messages_with_checkpoints 函数中,目前的实现存在两个可以改进的地方:
建议在消息被标记为 _no_save 或过滤后内容为空且无工具调用时跳过该消息。此外,根据项目规则,新功能的实现应伴随相应的单元测试。
Suggested change
References
|
||||||||||||||||||||||||||||||||||||||||||||
| if message._checkpoint_after is not None: | ||||||||||||||||||||||||||||||||||||||||||||
| dumped.append( | ||||||||||||||||||||||||||||||||||||||||||||
| CheckpointMessageSegment(content=message._checkpoint_after).model_dump() | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
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
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.
suggestion (bug_risk): The
_no_saveflag is treated as truthy rather than explicitly boolean, which can lead to surprising behavior.cast(dict[str, Any], value).get("_no_save")will treat any truthy value (e.g.'false',1, non-empty strings) as enabling_no_save. If this field is meant to be strictly boolean, consider checking explicitly forTrue(or validating/coercing the type) to avoid accidental activation from loosely typed inputs.