Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core[patch]: Runnable with message history to use add_messages #17958

Merged
merged 1 commit into from
Feb 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 5 additions & 7 deletions libs/core/langchain_core/runnables/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ class InMemoryHistory(BaseChatMessageHistory, BaseModel):

messages: List[BaseMessage] = Field(default_factory=list)

def add_message(self, message: BaseMessage) -> None:
\"\"\"Add a self-created message to the store\"\"\"
self.messages.append(message)
def add_messages(self, messages: List[BaseMessage]) -> None:
\"\"\"Add a list of messages to the store\"\"\"
self.messages.extend(messages)

def clear(self) -> None:
self.messages = []
Expand Down Expand Up @@ -420,7 +420,7 @@ async def _aenter_history(
return await run_in_executor(config, self._enter_history, input, config)

def _exit_history(self, run: Run, config: RunnableConfig) -> None:
hist = config["configurable"]["message_history"]
hist: BaseChatMessageHistory = config["configurable"]["message_history"]

# Get the input messages
inputs = load(run.inputs)
Expand All @@ -436,9 +436,7 @@ def _exit_history(self, run: Run, config: RunnableConfig) -> None:
# Get the output messages
output_val = load(run.outputs)
output_messages = self._get_output_messages(output_val)

for m in input_messages + output_messages:
hist.add_message(m)
hist.add_messages(input_messages + output_messages)

def _merge_configs(self, *configs: Optional[RunnableConfig]) -> RunnableConfig:
config = super()._merge_configs(*configs)
Expand Down