mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-01 19:12:42 +00:00
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:
0
libs/core/tests/unit_tests/chat_history/__init__.py
Normal file
0
libs/core/tests/unit_tests/chat_history/__init__.py
Normal file
68
libs/core/tests/unit_tests/chat_history/test_chat_history.py
Normal file
68
libs/core/tests/unit_tests/chat_history/test_chat_history.py
Normal 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")
|
Reference in New Issue
Block a user