InMemory Cache
메모리에 데이터 저장
가장 빠른 접근 속도
애플리케이션 재시작 시 데이터 소멸
SQLite Cache
파일 기반 데이터베이스에 저장
영구적인 데이터 보존
시스템 재시작 후에도 데이터 유지
from langchain.globals import set_llm_cache
from langchain.cache import InMemoryCache, SQLiteCache
set_llm_cache(InMemoryCache())
set_llm_cache(SQLiteCache("cache.db"))
챗봇 메모리의 필요성
메모리 타입별 특징
메모리 구현 방식/패턴
# 메모리 구현체 사용
memory = ConversationBufferMemory(
memory_key="chat_history",
return_messages=True
)
# LLMChain에서 메모리 사용
chain = LLMChain(
llm=llm,
prompt=prompt,
memory=memory
)
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
# 메모리 구현체 사용
memory = ConversationBufferMemory(
return_messages=True,
memory_key="chat_history"
)
# LCEL 방식으로 메모리 통합
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful AI"),
MessagesPlaceholder(variable_name="chat_history"),
("human", "{input}")
])
chain = RunnablePassthrough.assign(
chat_history=memory.load_memory_variables | itemgetter("chat_history")
) | prompt | llm