Skip to main content

Mongodb 聊天消息历史记录 (Mongodb Chat Message History)

本文档介绍如何使用 Mongodb 存储聊天消息历史记录。(This notebook goes over how to use Mongodb to store chat message history.)

MongoDB 是一种源可用的跨平台文档导向数据库程序。MongoDB 被归类为 NoSQL 数据库程序,它使用类似 JSON 的文档和可选的模式。(MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas.)

MongoDB 由 MongoDB Inc. 开发,并在 Server Side Public License (SSPL) 下获得许可。- 维基百科 (MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License (SSPL). - Wikipedia)

# 提供连接字符串以连接到 MongoDB 数据库
connection_string = "mongodb://mongo_user:password123@mongo:27017"
from langchain.memory import MongoDBChatMessageHistory

message_history = MongoDBChatMessageHistory(
connection_string=connection_string, session_id="test-session"
)

message_history.add_user_message("hi!")

message_history.add_ai_message("whats up?")
message_history.messages
    [HumanMessage(content='hi!', additional_kwargs={}, example=False),
AIMessage(content='whats up?', additional_kwargs={}, example=False)]