mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-05 09:25:14 +03:00
chore(core): deprecate chat message history (#40711)
Co-authored-by: open-swe[bot] <open-swe@users.noreply.github.com>
This commit is contained in:
1 parent
d5fbcdbb54
commit
3290757339
2 files changed
+50
-2
No files matched your search
@@ -3,10 +3,11 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from langchain_core._api import deprecated
|
||||
from langchain_core.messages import (
|
||||
AIMessage,
|
||||
BaseMessage,
|
||||
@@ -19,6 +20,14 @@ if TYPE_CHECKING:
|
||||
from collections.abc import Sequence
|
||||
|
||||
|
||||
@deprecated(
|
||||
since="1.6.4",
|
||||
removal="2.0.0",
|
||||
addendum=(
|
||||
"See the short-term memory documentation for recommended alternatives: "
|
||||
"https://docs.langchain.com/oss/python/langchain/short-term-memory"
|
||||
),
|
||||
)
|
||||
class BaseChatMessageHistory(ABC):
|
||||
"""Abstract base class for storing chat message history.
|
||||
|
||||
@@ -96,6 +105,10 @@ class BaseChatMessageHistory(ABC):
|
||||
layer, so this operation is expected to incur some latency.
|
||||
"""
|
||||
|
||||
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||||
"""Initialize cooperatively to preserve multiple inheritance."""
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
async def aget_messages(self) -> list[BaseMessage]:
|
||||
"""Async version of getting messages.
|
||||
|
||||
@@ -199,6 +212,14 @@ class BaseChatMessageHistory(ABC):
|
||||
return get_buffer_string(self.messages)
|
||||
|
||||
|
||||
@deprecated(
|
||||
since="1.6.4",
|
||||
removal="2.0.0",
|
||||
addendum=(
|
||||
"See the short-term memory documentation for recommended alternatives: "
|
||||
"https://docs.langchain.com/oss/python/langchain/short-term-memory"
|
||||
),
|
||||
)
|
||||
class InMemoryChatMessageHistory(BaseChatMessageHistory, BaseModel):
|
||||
"""In memory implementation of chat message history.
|
||||
|
||||
|
||||
@@ -1,9 +1,36 @@
|
||||
from collections.abc import Sequence
|
||||
|
||||
from langchain_core.chat_history import BaseChatMessageHistory
|
||||
import pytest
|
||||
|
||||
from langchain_core.chat_history import (
|
||||
BaseChatMessageHistory,
|
||||
InMemoryChatMessageHistory,
|
||||
)
|
||||
from langchain_core.messages import BaseMessage, HumanMessage
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"history_class", [BaseChatMessageHistory, InMemoryChatMessageHistory]
|
||||
)
|
||||
def test_chat_history_deprecated(history_class: type[BaseChatMessageHistory]) -> None:
|
||||
assert "deprecated" in (history_class.__doc__ or "")
|
||||
assert "https://docs.langchain.com/oss/python/langchain/short-term-memory" in (
|
||||
history_class.__doc__ or ""
|
||||
)
|
||||
|
||||
|
||||
async def test_in_memory_history_initialization() -> None:
|
||||
messages = [HumanMessage(content="Hello")]
|
||||
history = InMemoryChatMessageHistory(messages=messages)
|
||||
assert await history.aget_messages() == messages
|
||||
other_history = InMemoryChatMessageHistory()
|
||||
await other_history.aadd_messages([HumanMessage(content="World")])
|
||||
assert history.messages == messages
|
||||
await history.aclear()
|
||||
assert history.messages == []
|
||||
assert other_history.messages == [HumanMessage(content="World")]
|
||||
|
||||
|
||||
def test_add_message_implementation_only() -> None:
|
||||
"""Test implementation of add_message only."""
|
||||
|
||||
|
||||
Reference in new issue
Block a user