นี่คือส่วนประกอบหน่วยความจำที่ใช้กันอย่างแพร่หลายสำหรับ LangChain:

หมายเหตุ: หลังจากกำหนดส่วนประกอบหน่วยความจำแล้ว คุณสามารถส่งมันเป็นพารามิเตอร์ไปยังหน่วยความจำของเชน และใช้มันได้

ConversationBufferMemory

ส่วนประกอบหน่วยความจำที่ขึ้นอยู่กับหน่วยความจำ ซึ่งเก็บข้อมูลในหน่วยความจำ

from langchain.memory import ConversationBufferMemory

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

RedisChatMessageHistory

ส่วนประกอบหน่วยความจำที่ขึ้นอยู่กับ 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

ส่วนประกอบหน่วยความจำที่ขึ้นอยู่กับฐานข้อมูล 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

ส่วนประกอบหน่วยความจำที่ขึ้นอยู่กับ 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?")