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?")