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