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

rfc core: basemessage.text #29078

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
21 changes: 21 additions & 0 deletions libs/core/langchain_core/messages/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ def get_lc_namespace(cls) -> list[str]:
"""
return ["langchain", "schema", "messages"]

def text(self) -> str:
Copy link
Collaborator

Choose a reason for hiding this comment

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

can this be a property?

Copy link
Member Author

Choose a reason for hiding this comment

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

originally had it as an @property but thought it might be more forward-compatible with config options we might want to add in the future like custom_separator: str = "" or image_placeholder: str = ""

"""Get the text content of the message.

Returns:
The text content of the message.
"""
if isinstance(self.content, str):
return self.content

# must be a list
blocks = [
block
for block in self.content
if isinstance(block, str)
or block.get("type") == "text"
and isinstance(block.get("text"), str)
]
return "".join(
block if isinstance(block, str) else block["text"] for block in blocks
)

def __add__(self, other: Any) -> ChatPromptTemplate:
"""Concatenate this message with another message."""
from langchain_core.prompts.chat import ChatPromptTemplate
Expand Down
Loading