-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core(minor): Add bulk add messages to BaseChatMessageHistory interface (
#15709) * Add bulk add_messages method to the interface. * Update documentation for add_ai_message and add_human_message to denote them as being marked for deprecation. We should stop using them as they create more incorrect (inefficient) ways of doing things
- Loading branch information
Showing
3 changed files
with
121 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
Empty file.
68 changes: 68 additions & 0 deletions
68
libs/core/tests/unit_tests/chat_history/test_chat_history.py
This file contains 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,68 @@ | ||
from typing import List, Sequence | ||
|
||
from langchain_core.chat_history import BaseChatMessageHistory | ||
from langchain_core.messages import BaseMessage, HumanMessage | ||
|
||
|
||
def test_add_message_implementation_only() -> None: | ||
"""Test implementation of add_message only.""" | ||
|
||
class SampleChatHistory(BaseChatMessageHistory): | ||
def __init__(self, *, store: List[BaseMessage]) -> None: | ||
self.store = store | ||
|
||
def add_message(self, message: BaseMessage) -> None: | ||
"""Add a message to the store.""" | ||
self.store.append(message) | ||
|
||
def clear(self) -> None: | ||
"""Clear the store.""" | ||
raise NotImplementedError() | ||
|
||
store: List[BaseMessage] = [] | ||
chat_history = SampleChatHistory(store=store) | ||
chat_history.add_message(HumanMessage(content="Hello")) | ||
assert len(store) == 1 | ||
assert store[0] == HumanMessage(content="Hello") | ||
chat_history.add_message(HumanMessage(content="World")) | ||
assert len(store) == 2 | ||
assert store[1] == HumanMessage(content="World") | ||
|
||
chat_history.add_messages( | ||
[HumanMessage(content="Hello"), HumanMessage(content="World")] | ||
) | ||
assert len(store) == 4 | ||
assert store[2] == HumanMessage(content="Hello") | ||
assert store[3] == HumanMessage(content="World") | ||
|
||
|
||
def test_bulk_message_implementation_only() -> None: | ||
"""Test that SampleChatHistory works as expected.""" | ||
store: List[BaseMessage] = [] | ||
|
||
class BulkAddHistory(BaseChatMessageHistory): | ||
def __init__(self, *, store: List[BaseMessage]) -> None: | ||
self.store = store | ||
|
||
def add_messages(self, message: Sequence[BaseMessage]) -> None: | ||
"""Add a message to the store.""" | ||
self.store.extend(message) | ||
|
||
def clear(self) -> None: | ||
"""Clear the store.""" | ||
raise NotImplementedError() | ||
|
||
chat_history = BulkAddHistory(store=store) | ||
chat_history.add_message(HumanMessage(content="Hello")) | ||
assert len(store) == 1 | ||
assert store[0] == HumanMessage(content="Hello") | ||
chat_history.add_message(HumanMessage(content="World")) | ||
assert len(store) == 2 | ||
assert store[1] == HumanMessage(content="World") | ||
|
||
chat_history.add_messages( | ||
[HumanMessage(content="Hello"), HumanMessage(content="World")] | ||
) | ||
assert len(store) == 4 | ||
assert store[2] == HumanMessage(content="Hello") | ||
assert store[3] == HumanMessage(content="World") |