Skip to content

AsyncMongoClient support for MongoDBChatMessageHistory #215

@BParvatkar

Description

@BParvatkar

I am trying to use AsyncMongoClient for MongoDBChatMessageHistory.

def get_chat_history(client: AsyncMongoClient, session_id: str) -> MongoDBChatMessageHistory:
    return MongoDBChatMessageHistory(
        connection_string=None,
        session_id=session_id,
        client=client,
        database_name="db",
        collection_name="conversation",
    )


prompt = ChatPromptTemplate.from_messages(
        [
            ("system", "You're an assistant"),
            MessagesPlaceholder("history"),
            ("human", "{input}"),
        ]
    )

    llm = ChatBedrockConverse(
        model_id="model_id", region_name="region_name", provider="provider"
    )
    chain = prompt | llm
    chain_with_history = RunnableWithMessageHistory(
        chain,
        get_session_history=lambda session_id: get_chat_history(client, session_id),
        input_messages_key="input",
        history_messages_key="history",
    )

Console message

/Users/mongodb-setup/.venv/lib/python3.12/site-packages/langchain_mongodb/chat_message_histories.py:169: RuntimeWarning: coroutine 'AsyncCollection.insert_one' was never awaited
  self.collection.insert_one(
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

I saw that MongoDBChatMessageHistory expects MongoClient and not AsyncMongoClient. Is there a way to use it? Or the support isnt available yet?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions