Closed ali-faiz-brainx closed 7 months ago
Hi, I have the same problem as you. Put mongo_history in the chat_memory parameter of conversational_memory.
mongo_history = MongoDBChatMessageHistory(
connection_string="mongodb+srv://alifaiz:database@cluster0.eq70b.mongodb.net",
session_id="new_session"
)
conversational_memory = ConversationBufferMemory(
chat_memory=mongo_history
memory_key='chat_history',
return_messages=True,
# output_key="answer"
)
It worked for me.
@wellCh4n How can I store additional fields in a session, such as user ID, to track user conversation history using session IDs?
@wellCh4n How can I store additional fields in a session, such as user ID, to track user conversation history using session IDs?
Hi! @iiitmahesh, A little late in answer, but your requirement is difficult to implement, in my mind.
Cuz save history in db, used langchain.memory.chat_memory.BaseChatMemory.save_context
, it will add a user_message and ai_message, and we don't have more parameters to control what's inside the message
object.
def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None:
input_str, output_str = self._get_input_output(inputs, outputs)
self.chat_memory.add_user_message(input_str)
self.chat_memory.add_ai_message(output_str)
def add_user_message(self, message: str) -> None:
self.add_message(HumanMessage(content=message))
def add_ai_message(self, message: str) -> None:
self.add_message(AIMessage(content=message))
def add_message(self, message: BaseMessage) -> None:
from pymongo import errors
try:
self.collection.insert_one(
{
"SessionId": self.session_id,
"History": json.dumps(_message_to_dict(message), ensure_ascii=False),
}
)
except errors.WriteError as err:
logger.error(err)
So, you need override the method that named save_context
to a add additional fields.
Keep the discussion going if you have any ideas.
I want to develop a chatbot that answer questions to user based on the previous chat history. The chatbot should answer the questions according to langchain promptTemplate and I want to save the chat history in MongoDB with unique session id and database name.can anyone help on this??
Hi, @ali-faiz-brainx! I'm Dosu, and I'm helping the LangChain team manage their backlog. I'm marking this issue as stale.
It looks like you're encountering a "value is not a valid dict" error when using MongoDBChatMessageHistory with an agent and RetrievalQA. There have been suggestions from other users regarding potential solutions and desires to develop a chatbot that answers questions based on previous chat history and saves the chat history in MongoDB with a unique session ID and database name.
Could you please confirm if this issue is still relevant to the latest version of the LangChain repository? If it is, please let the LangChain team know by commenting on this issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days. Thank you!
@induumez did you find a solution ?
System Info
I want to develop a chatbot that answer questions to user based on the Pinecone vectors. And I want to save the chat history in MongoDB. The history part works well with buffer memory but gives value is not a valid dict error with MongoDB memory.
Here is the Code I'm using
Error:
Who can help?
No response
Information
Related Components
Reproduction
Steps to reproduce the issue:
Expected behavior
Chat history should be saved in my database.