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
This commit is contained in:
Eugene Yurtsev
2024-01-31 11:59:39 -08:00
committed by GitHub
parent af8c5c185b
commit 2e5949b6f8
3 changed files with 121 additions and 7 deletions

View File

@@ -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")