Skip to content

Commit

Permalink
core[minor]: Use BaseChatMessageHistory async methods in RunnableWith…
Browse files Browse the repository at this point in the history
…MessageHistory (langchain-ai#19565)

Co-authored-by: Eugene Yurtsev <[email protected]>
  • Loading branch information
2 people authored and gkorland committed Mar 30, 2024
1 parent d71bac5 commit e162298
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
26 changes: 17 additions & 9 deletions libs/core/langchain_core/runnables/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from langchain_core.load.load import load
from langchain_core.pydantic_v1 import BaseModel
from langchain_core.runnables.base import Runnable, RunnableBindingBase, RunnableLambda
from langchain_core.runnables.config import run_in_executor
from langchain_core.runnables.passthrough import RunnablePassthrough
from langchain_core.runnables.utils import (
ConfigurableFieldSpec,
Expand Down Expand Up @@ -403,21 +402,30 @@ def _get_output_messages(
raise ValueError()

def _enter_history(self, input: Any, config: RunnableConfig) -> List[BaseMessage]:
hist = config["configurable"]["message_history"]
# return only historic messages
if self.history_messages_key:
return hist.messages.copy()
# return all messages
else:
hist: BaseChatMessageHistory = config["configurable"]["message_history"]
messages = hist.messages.copy()

if not self.history_messages_key:
# return all messages
input_val = (
input if not self.input_messages_key else input[self.input_messages_key]
)
return hist.messages.copy() + self._get_input_messages(input_val)
messages += self._get_input_messages(input_val)
return messages

async def _aenter_history(
self, input: Dict[str, Any], config: RunnableConfig
) -> List[BaseMessage]:
return await run_in_executor(config, self._enter_history, input, config)
hist: BaseChatMessageHistory = config["configurable"]["message_history"]
messages = (await hist.aget_messages()).copy()

if not self.history_messages_key:
# return all messages
input_val = (
input if not self.input_messages_key else input[self.input_messages_key]
)
messages += self._get_input_messages(input_val)
return messages

def _exit_history(self, run: Run, config: RunnableConfig) -> None:
hist: BaseChatMessageHistory = config["configurable"]["message_history"]
Expand Down
4 changes: 2 additions & 2 deletions libs/core/tests/unit_tests/runnables/test_runnable_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1254,9 +1254,9 @@ def get_by_session_id(session_id: str) -> BaseChatMessageHistory:
input_messages_key="question",
history_messages_key="history",
)
with_message_history.with_config(
await with_message_history.with_config(
{"configurable": {"session_id": "session-123"}}
).invoke({"question": "hello"})
).ainvoke({"question": "hello"})

assert store == {
"session-123": [HumanMessage(content="hello"), AIMessage(content="hello")]
Expand Down

0 comments on commit e162298

Please sign in to comment.