-
Checked other resources
Commit to Help
Example Codefrom langchain_community.chat_message_histories import ChatMessageHistory
from langchain_core.chat_history import BaseChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory
store = {}
def get_session_history(user_id: str, conversation_id: str) -> BaseChatMessageHistory:
if (user_id, conversation_id) not in store:
store[(user_id, conversation_id)] = ChatMessageHistory()
return store[(user_id, conversation_id)]
with_message_history = RunnableWithMessageHistory(
runnable,
get_session_history,
input_messages_key="input",
history_messages_key="history",
history_factory_config=[
ConfigurableFieldSpec(
id="user_id",
annotation=str,
name="User ID",
description="Unique identifier for the user.",
default="",
is_shared=True,
),
ConfigurableFieldSpec(
id="conversation_id",
annotation=str,
name="Conversation ID",
description="Unique identifier for the conversation.",
default="",
is_shared=True,
),
],
) DescriptionI would like to set a title for each conversation in my chat message history and retrieve it from the memory afterwards. i followed the langchain documentation here: https://python.langchain.com/v0.1/docs/expression_language/how_to/message_history/ When I'm printing the ChatMessageHistory().json() i get additional fields: System InfoSystem Information
Package Information
Packages not installed (Not Necessarily a Problem)The following packages were not found:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The easiest way to do this would be to implement class CustomChatMessageHistory(BaseChatMessageHistory):
thread_name: str
def __init__(self, thread_name: str):
super().__init__() # Initialize parent class
self.thread_name = thread_name
return
# Implement abstract methods here And to pass the name when you create the CustomChatMessageHistory def get_session_history(user_id: str, conversation_id: str, thread_name: str) -> BaseChatMessageHistory:
if (user_id, conversation_id) not in store:
store[(user_id, conversation_id)] = CustomChatMessageHistory(thread_name)
return store[(user_id, conversation_id)] Take a look at the documentation for how to implement BaseChatMessageHistory: https://api.python.langchain.com/en/latest/chat_history/langchain_core.chat_history.BaseChatMessageHistory.html |
Beta Was this translation helpful? Give feedback.
The easiest way to do this would be to implement
BaseChatMessageHistory
and to modify the init function:And to pass the name when you create the CustomChatMessageHistory