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

core: add RemoveMessage #23636

Merged
merged 11 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions libs/community/tests/unit_tests/load/test_serializable.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ def test_serializable_mapping() -> None:
"structured",
"StructuredPrompt",
),
# This is not exported from langchain, only langchain_core
("langchain", "schema", "messages", "RemoveMessage"): (
"langchain_core",
"messages",
"modifier",
"RemoveMessage",
),
}
serializable_modules = import_all_modules("langchain")

Expand Down
6 changes: 6 additions & 0 deletions libs/core/langchain_core/load/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@
"tool",
"ToolMessage",
),
("langchain", "schema", "messages", "RemoveMessage"): (
"langchain_core",
"messages",
"modifier",
"RemoveMessage",
),
("langchain", "schema", "agent", "AgentAction"): (
"langchain_core",
"agents",
Expand Down
2 changes: 2 additions & 0 deletions libs/core/langchain_core/messages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from langchain_core.messages.chat import ChatMessage, ChatMessageChunk
from langchain_core.messages.function import FunctionMessage, FunctionMessageChunk
from langchain_core.messages.human import HumanMessage, HumanMessageChunk
from langchain_core.messages.modifier import RemoveMessage
from langchain_core.messages.system import SystemMessage, SystemMessageChunk
from langchain_core.messages.tool import (
InvalidToolCall,
Expand Down Expand Up @@ -70,6 +71,7 @@
"ToolCallChunk",
"ToolMessage",
"ToolMessageChunk",
"RemoveMessage",
"_message_from_dict",
"convert_to_messages",
"get_buffer_string",
Expand Down
23 changes: 23 additions & 0 deletions libs/core/langchain_core/messages/modifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import Any, List, Literal

from langchain_core.messages.base import BaseMessage


class RemoveMessage(BaseMessage):
"""Message responsible for deleting other messages."""

type: Literal["remove"] = "remove"
Copy link
Collaborator

Choose a reason for hiding this comment

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

RemoveMessage better here for 1:1 correspondence


def __init__(self, id: str, **kwargs: Any) -> None:
if kwargs.pop("content", None):
raise ValueError("RemoveMessage does not support 'content' field.")

return super().__init__("", id=id, **kwargs)

@classmethod
def get_lc_namespace(cls) -> List[str]:
"""Get the namespace of the langchain object."""
return ["langchain", "schema", "messages"]


RemoveMessage.update_forward_refs()
1 change: 1 addition & 0 deletions libs/core/tests/unit_tests/messages/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"ToolCallChunk",
"ToolMessage",
"ToolMessageChunk",
"RemoveMessage",
"convert_to_messages",
"get_buffer_string",
"merge_content",
Expand Down
Loading