Dưới đây là những thành phần bộ nhớ thông thường được sử dụng cho LangChain:

Lưu ý: Sau khi xác định thành phần bộ nhớ, bạn có thể truyền nó như một tham số vào bộ nhớ của chuỗi và sử dụng nó.

ConversationBufferMemory

Một thành phần bộ nhớ dựa trên bộ nhớ, lưu trữ dữ liệu trong bộ nhớ

from langchain.memory import ConversationBufferMemory

memory = ConversationBufferMemory()
memory.save_context({"input": "hi"}, {"output": "whats up"})

RedisChatMessageHistory

Một thành phần bộ nhớ dựa trên Redis

from langchain.memory import RedisChatMessageHistory

history = RedisChatMessageHistory(
	session_id="abc123",
	url="redis://192.168.0.100:6379/0",
	key_prefix="demo_prefix:"
)

history.add_user_message("hi!")

history.add_ai_message("whats up?")

PostgresChatMessageHistory

Một thành phần bộ nhớ dựa trên cơ sở dữ liệu Postgres

from langchain.memory import PostgresChatMessageHistory

history = PostgresChatMessageHistory(
    connection_string="postgresql://postgres:mypassword@localhost/chat_history",
    session_id="foo",
)

history.add_user_message("hi!")

history.add_ai_message("whats up?")

MongoDBChatMessageHistory

Một thành phần bộ nhớ dựa trên MongoDB

from langchain.memory import MongoDBChatMessageHistory

connection_string = "mongodb://mongo_user:password123@mongo:27017"

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

message_history.add_user_message("hi!")

message_history.add_ai_message("whats up?")