Skip to content

Commit

Permalink
Adding BaseChatMessageHistory.__str__ (#14311)
Browse files Browse the repository at this point in the history
Adding __str__ to base chat message history to make it easier to debug
  • Loading branch information
jamesbraza authored Dec 5, 2023
1 parent 8b00601 commit 3b75d37
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
10 changes: 9 additions & 1 deletion libs/core/langchain_core/chat_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
from abc import ABC, abstractmethod
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):
Expand Down Expand Up @@ -65,3 +70,6 @@ def add_message(self, message: BaseMessage) -> None:
@abstractmethod
def clear(self) -> None:
"""Remove all messages from the store"""

def __str__(self) -> str:
return get_buffer_string(self.messages)
11 changes: 10 additions & 1 deletion libs/core/tests/unit_tests/runnables/test_history.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
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.runnables.base import RunnableLambda
from langchain_core.runnables.config import RunnableConfig
from langchain_core.runnables.history import RunnableWithMessageHistory
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]:
chat_history_store = {}

Expand Down

0 comments on commit 3b75d37

Please sign in to comment.