langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
92.54k stars 14.82k forks source link

using MongoDBChatMessageHistory with agent and RetrievalQA throws value is not a valid dict error #5834

Closed ali-faiz-brainx closed 7 months ago

ali-faiz-brainx commented 1 year ago

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

def run_openai_llm_chain(vectorstore, query):
  # chat completion llm
  llm = ChatOpenAI()

  conversational_memory = ConversationBufferMemory(
      memory_key='chat_history',
      return_messages=True,
      # output_key="answer"
  )

  mongo_history = MongoDBChatMessageHistory(
        connection_string="mongodb+srv://alifaiz:database@cluster0.eq70b.mongodb.net", 
        session_id="new_session"
  )

  qa = RetrievalQA.from_chain_type(
    llm=llm, 
    chain_type='stuff',
    retriever=vectorstore.as_retriever(),
    # memory=mongo_history
  )

  tools = [
      Tool.from_function(
          func=qa.run,
          name="Reader",
          description="useful for when we need to answer question from context"
      )
  ]

  agent = initialize_agent(
      tools,
      llm,
      agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION,
      verbose=True,
      memory=mongo_history
  )

  answer = agent.run(input=query)

  return answer

Error:

ValidationError: 1 validation error for AgentExecutor
memory
  value is not a valid dict (type=type_error.dict)

Who can help?

No response

Information

Related Components

Reproduction

Steps to reproduce the issue:

  1. Use MongoDB as a memory in agents.

Expected behavior

Chat history should be saved in my database.

wellCh4n commented 1 year 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.

iiitmahesh commented 1 year ago

@wellCh4n How can I store additional fields in a session, such as user ID, to track user conversation history using session IDs?

wellCh4n commented 1 year ago

@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.

induumez commented 11 months ago

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??

dosubot[bot] commented 7 months ago

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!

ya-smin20 commented 5 months ago

@induumez did you find a solution ?