NVIDIA / NeMo-Guardrails

NeMo Guardrails is an open-source toolkit for easily adding programmable guardrails to LLM-based conversational systems.
Other
3.97k stars 362 forks source link

Problem in Integrating Langchain RunnableWithMessageHistory #665

Open nikithakriz opened 1 month ago

nikithakriz commented 1 month ago

I am currently exploring the ways to integrate nemoguardrails to my existing RAG chatbot system.

Guardrail Script

config =  RailsConfig.from_content(
                yaml_content=YAML_CONTENT, colang_content=COLANG_CONTENT)
 guardrails = RunnableRails(config, llm=llm_instance)

RAG chain with message history

conversational_rag_chain = RunnableWithMessageHistory(
                    rag_chain,
                    lambda session_id: chat_history,
                    input_messages_key="input",
                    history_messages_key="chat_history",
                    output_messages_key="answer",
                )

rag_with_guardrails = guardrails | conversational_rag_chain

When i try to invoke this chain by following line of code rag_with_guardrails .invoke( {"input": user_query}, config={"configurable": {"session_id": self.session_id}} )

It raises this error when i ask question which needs retrieval

ValueError: Missing keys ['session_id'] in config['configurable'] Expected keys are ['session_id'].When using via .invoke() or .stream(), pass in a config; e.g., chain.invoke({'input': 'foo'}, {'configurable': {'session_id': '[your-value-here]'}})
Bot: {'output': None}

for normal greeting query i got response like below: {'output': 'Hello there!'}

So, how can i overcome the error and integrate RunnableWithMessageHistory and nemoguardrails together? am i missing something??

drazvan commented 1 month ago

This is a bug. The config parameter is not used properly. It needs to be fixed. Contributions are welcomed here!

kaushikabhishek87 commented 1 month ago

Hi @nikithakriz have you checked this doc from langchain on using session id https://github.com/langchain-ai/langchain/blob/master/docs/docs/integrations/memory/mongodb_chat_message_history.ipynb . chat_history for session_id requires "session_id":

chain_with_history = RunnableWithMessageHistory(
    chain,
    lambda session_id: MongoDBChatMessageHistory(
        session_id=session_id,
        connection_string="mongodb://mongo_user:password123@mongo:27017",
        database_name="my_db",
        collection_name="chat_histories",
    ),
    input_messages_key="question",
    history_messages_key="history",
) 
nikithakriz commented 1 month ago

Hi @nikithakriz have you checked this doc from langchain on using session id https://github.com/langchain-ai/langchain/blob/master/docs/docs/integrations/memory/mongodb_chat_message_history.ipynb . chat_history for session_id requires "session_id":

chain_with_history = RunnableWithMessageHistory(
    chain,
    lambda session_id: MongoDBChatMessageHistory(
        session_id=session_id,
        connection_string="mongodb://mongo_user:password123@mongo:27017",
        database_name="my_db",
        collection_name="chat_histories",
    ),
    input_messages_key="question",
    history_messages_key="history",
) 

I am able to create chain with chat history without nemoguardrails, the error raising when integrating with nemoguardrails

drazvan commented 1 week ago

@nikithakriz : can you test on the latest develop branch and see if the issue is fixed? Thanks!