Add async methods to BaseChatMessageHistory and BaseMemory (#16728)

Adds:
   * async methods to BaseChatMessageHistory
   * async methods to ChatMessageHistory
   * async methods to BaseMemory
   * async methods to BaseChatMemory
   * async methods to ConversationBufferMemory
   * tests of ConversationBufferMemory's async methods

  **Twitter handle:** cbornet_
This commit is contained in:
Christophe Bornet
2024-02-05 10:20:28 -08:00
committed by GitHub
parent b3c3b58f2c
commit 2ef69fe11b
7 changed files with 197 additions and 15 deletions

View File

@@ -66,3 +66,36 @@ def test_bulk_message_implementation_only() -> None:
assert len(store) == 4
assert store[2] == HumanMessage(content="Hello")
assert store[3] == HumanMessage(content="World")
async def test_async_interface() -> None:
"""Test async interface for BaseChatMessageHistory."""
class BulkAddHistory(BaseChatMessageHistory):
def __init__(self) -> None:
self.messages = []
def add_messages(self, message: Sequence[BaseMessage]) -> None:
"""Add a message to the store."""
self.messages.extend(message)
def clear(self) -> None:
"""Clear the store."""
self.messages.clear()
chat_history = BulkAddHistory()
await chat_history.aadd_messages(
[HumanMessage(content="Hello"), HumanMessage(content="World")]
)
assert await chat_history.aget_messages() == [
HumanMessage(content="Hello"),
HumanMessage(content="World"),
]
await chat_history.aadd_messages([HumanMessage(content="!")])
assert await chat_history.aget_messages() == [
HumanMessage(content="Hello"),
HumanMessage(content="World"),
HumanMessage(content="!"),
]
await chat_history.aclear()
assert await chat_history.aget_messages() == []