mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-11 13:55:03 +00:00
Adding BaseChatMessageHistory.__str__
(#14311)
Adding __str__ to base chat message history to make it easier to debug
This commit is contained in:
parent
8b0060184d
commit
3b75d37cee
@ -3,7 +3,12 @@ from __future__ import annotations
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage
|
from langchain_core.messages import (
|
||||||
|
AIMessage,
|
||||||
|
BaseMessage,
|
||||||
|
HumanMessage,
|
||||||
|
get_buffer_string,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class BaseChatMessageHistory(ABC):
|
class BaseChatMessageHistory(ABC):
|
||||||
@ -65,3 +70,6 @@ class BaseChatMessageHistory(ABC):
|
|||||||
@abstractmethod
|
@abstractmethod
|
||||||
def clear(self) -> None:
|
def clear(self) -> None:
|
||||||
"""Remove all messages from the store"""
|
"""Remove all messages from the store"""
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return get_buffer_string(self.messages)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
from typing import Any, Callable, Sequence, Union
|
from typing import Any, Callable, Sequence, Union
|
||||||
|
|
||||||
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage
|
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, SystemMessage
|
||||||
from langchain_core.pydantic_v1 import BaseModel
|
from langchain_core.pydantic_v1 import BaseModel
|
||||||
from langchain_core.runnables.base import RunnableLambda
|
from langchain_core.runnables.base import RunnableLambda
|
||||||
from langchain_core.runnables.config import RunnableConfig
|
from langchain_core.runnables.config import RunnableConfig
|
||||||
@ -8,6 +8,15 @@ from langchain_core.runnables.history import RunnableWithMessageHistory
|
|||||||
from tests.unit_tests.fake.memory import ChatMessageHistory
|
from tests.unit_tests.fake.memory import ChatMessageHistory
|
||||||
|
|
||||||
|
|
||||||
|
def test_interfaces() -> None:
|
||||||
|
history = ChatMessageHistory()
|
||||||
|
history.add_message(SystemMessage(content="system"))
|
||||||
|
history.add_user_message("human 1")
|
||||||
|
history.add_ai_message("ai")
|
||||||
|
history.add_message(HumanMessage(content="human 2"))
|
||||||
|
assert str(history) == "System: system\nHuman: human 1\nAI: ai\nHuman: human 2"
|
||||||
|
|
||||||
|
|
||||||
def _get_get_session_history() -> Callable[..., ChatMessageHistory]:
|
def _get_get_session_history() -> Callable[..., ChatMessageHistory]:
|
||||||
chat_history_store = {}
|
chat_history_store = {}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user