mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-18 16:16:33 +00:00
core[patch]: allow message utils to work with lcel (#23743)
The functions `convert_to_messages` has had an expansion of the arguments it can take: 1. Previously, it only could take a `Sequence` in order to iterate over it. This has been broadened slightly to an `Iterable` (which should have no other impact). 2. Support for `PromptValue` and `BaseChatPromptTemplate` has been added. These are generated when combining messages using the overloaded `+` operator. Functions which rely on `convert_to_messages` (namely `filter_messages`, `merge_message_runs` and `trim_messages`) have had the type of their arguments similarly expanded. Resolves #23706. <!-- If no one reviews your PR within a few days, please @-mention one of baskaryan, efriis, eyurtsev, ccurme, vbarda, hwchase17. --> --------- Signed-off-by: JP-Ellis <josh@jpellis.me> Co-authored-by: Bagatur <baskaryan@gmail.com>
This commit is contained in:
@@ -63,6 +63,38 @@
|
||||
"Notice that if the contents of one of the messages to merge is a list of content blocks then the merged message will have a list of content blocks. And if both messages to merge have string contents then those are concatenated with a newline character."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "11f7e8d3",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"The `merge_message_runs` utility also works with messages composed together using the overloaded `+` operation:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b51855c5",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"messages = (\n",
|
||||
" SystemMessage(\"you're a good assistant.\")\n",
|
||||
" + SystemMessage(\"you always respond with a joke.\")\n",
|
||||
" + HumanMessage([{\"type\": \"text\", \"text\": \"i wonder why it's called langchain\"}])\n",
|
||||
" + HumanMessage(\"and who is harrison chasing anyways\")\n",
|
||||
" + AIMessage(\n",
|
||||
" 'Well, I guess they thought \"WordRope\" and \"SentenceString\" just didn\\'t have the same ring to it!'\n",
|
||||
" )\n",
|
||||
" + AIMessage(\n",
|
||||
" \"Why, he's probably chasing after the last cup of coffee in the office!\"\n",
|
||||
" )\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"merged = merge_message_runs(messages)\n",
|
||||
"print(\"\\n\\n\".join([repr(x) for x in merged]))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1b2eee74-71c8-4168-b968-bca580c25d18",
|
||||
|
@@ -16,6 +16,7 @@ from typing import (
|
||||
Any,
|
||||
Callable,
|
||||
Dict,
|
||||
Iterable,
|
||||
List,
|
||||
Literal,
|
||||
Optional,
|
||||
@@ -40,6 +41,7 @@ if TYPE_CHECKING:
|
||||
from langchain_text_splitters import TextSplitter
|
||||
|
||||
from langchain_core.language_models import BaseLanguageModel
|
||||
from langchain_core.prompt_values import PromptValue
|
||||
from langchain_core.runnables.base import Runnable
|
||||
|
||||
AnyMessage = Union[
|
||||
@@ -284,7 +286,7 @@ def _convert_to_message(message: MessageLikeRepresentation) -> BaseMessage:
|
||||
|
||||
|
||||
def convert_to_messages(
|
||||
messages: Sequence[MessageLikeRepresentation],
|
||||
messages: Union[Iterable[MessageLikeRepresentation], PromptValue],
|
||||
) -> List[BaseMessage]:
|
||||
"""Convert a sequence of messages to a list of messages.
|
||||
|
||||
@@ -294,6 +296,11 @@ def convert_to_messages(
|
||||
Returns:
|
||||
List of messages (BaseMessages).
|
||||
"""
|
||||
# Import here to avoid circular imports
|
||||
from langchain_core.prompt_values import PromptValue
|
||||
|
||||
if isinstance(messages, PromptValue):
|
||||
return messages.to_messages()
|
||||
return [_convert_to_message(m) for m in messages]
|
||||
|
||||
|
||||
@@ -329,7 +336,7 @@ def _runnable_support(func: Callable) -> Callable:
|
||||
|
||||
@_runnable_support
|
||||
def filter_messages(
|
||||
messages: Sequence[MessageLikeRepresentation],
|
||||
messages: Union[Iterable[MessageLikeRepresentation], PromptValue],
|
||||
*,
|
||||
include_names: Optional[Sequence[str]] = None,
|
||||
exclude_names: Optional[Sequence[str]] = None,
|
||||
@@ -417,7 +424,7 @@ def filter_messages(
|
||||
|
||||
@_runnable_support
|
||||
def merge_message_runs(
|
||||
messages: Sequence[MessageLikeRepresentation],
|
||||
messages: Union[Iterable[MessageLikeRepresentation], PromptValue],
|
||||
) -> List[BaseMessage]:
|
||||
"""Merge consecutive Messages of the same type.
|
||||
|
||||
@@ -506,7 +513,7 @@ def merge_message_runs(
|
||||
|
||||
@_runnable_support
|
||||
def trim_messages(
|
||||
messages: Sequence[MessageLikeRepresentation],
|
||||
messages: Union[Iterable[MessageLikeRepresentation], PromptValue],
|
||||
*,
|
||||
max_tokens: int,
|
||||
token_counter: Union[
|
||||
|
@@ -127,7 +127,6 @@ _MESSAGES_TO_TRIM = [
|
||||
HumanMessage("This is a 4 token text.", id="third"),
|
||||
AIMessage("This is a 4 token text.", id="fourth"),
|
||||
]
|
||||
|
||||
_MESSAGES_TO_TRIM_COPY = [m.copy(deep=True) for m in _MESSAGES_TO_TRIM]
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user