7️⃣Memory

LangChain: Memory

import os
from dotenv import load_dotenv  

load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
# 패키지 설치
#%pip install tiktoken

1. ConversationBufferMemory

from langchain.memory import ConversationBufferMemory

# 메모리 생성
memory = ConversationBufferMemory()
memory.chat_memory.add_user_message("배고프다")
memory.chat_memory.add_ai_message("어디 가서 밥 먹을까?")
memory.chat_memory.add_user_message("라면 먹으러 가자")
memory.chat_memory.add_ai_message("지하철역 앞에 있는 분식집으로 가자")
memory.chat_memory.add_user_message("그럼 출발!")
memory.chat_memory.add_ai_message("OK!")
# 메모리 변수 가져오기
memory.load_memory_variables({})
{'history': 'Human: 배고프다\nAI: 어디 가서 밥 먹을까?\nHuman: 라면 먹으러 가자\nAI: 지하철역 앞에 있는 분식집으로 가자\nHuman: 그럼 출발!\nAI: OK!'}
from langchain.memory import ConversationBufferMemory

# 메모리 생성
memory = ConversationBufferMemory(return_messages=True)
memory.chat_memory.add_user_message("배고프다")
memory.chat_memory.add_ai_message("어디 가서 밥 먹을까?")
memory.chat_memory.add_user_message("라면 먹으러 가자")
memory.chat_memory.add_ai_message("역 앞에 있는 분식집으로 가자")
memory.chat_memory.add_user_message("그럼 출발!")
memory.chat_memory.add_ai_message("OK!")
# 메모리 변수 가져오기
memory.load_memory_variables({})
{'history': [HumanMessage(content='배고프다'),
  AIMessage(content='어디 가서 밥 먹을까?'),
  HumanMessage(content='라면 먹으러 가자'),
  AIMessage(content='역 앞에 있는 분식집으로 가자'),
  HumanMessage(content='그럼 출발!'),
  AIMessage(content='OK!')]}

2. ConversationBufferWindowMemory

from langchain.memory import ConversationBufferWindowMemory

# 메모리 생성
memory = ConversationBufferWindowMemory(k=2, return_messages=True)
memory.save_context({"input": "안녕!"}, {"ouput": "무슨 일이야?"})
memory.save_context({"input": "배고파"}, {"ouput": "나도"})
memory.save_context({"input": "밥 먹자"}, {"ouput": "OK!"})
# 메모리 변수 가져오기
memory.load_memory_variables({})
{'history': [HumanMessage(content='배고파'),
  AIMessage(content='나도'),
  HumanMessage(content='밥 먹자'),
  AIMessage(content='OK!')]}

3. ConversationTokenBufferMemory

from langchain.memory import ConversationTokenBufferMemory
from langchain.chat_models import ChatOpenAI

# 메모리 생성
memory = ConversationTokenBufferMemory(
    llm=ChatOpenAI(
        model="gpt-3.5-turbo",
        temperature=0
    ), 
    max_token_limit=50, 
    return_messages=True
)
memory.save_context({"input": "배고파"}, {"ouput": "어디 가서 밥 먹을까?"})
memory.save_context({"input": "라면 먹으러 가자"}, {"ouput": "역 앞에 있는 분식집으로 가자"})
memory.save_context({"input": "그럼 출발!"}, {"ouput": "OK!"})
# 메모리 변수 가져오기
memory.load_memory_variables({})
{'history': [AIMessage(content='역 앞에 있는 분식집으로 가자'),
  HumanMessage(content='그럼 출발!'),
  AIMessage(content='OK!')]}

4. ConversationSummaryMemory

from langchain.memory import ConversationSummaryMemory
from langchain.chat_models import ChatOpenAI

# 메모리 생성
memory = ConversationSummaryMemory(
    llm=ChatOpenAI(
        model="gpt-3.5-turbo",
        temperature=0
    ),
    return_messages=True
)
memory.save_context({"input": "배고파"}, {"ouput": "어디 가서 밥 먹을까?"})
memory.save_context({"input": "라면 먹으러 가자"}, {"ouput": "역 앞에 있는 분식집으로 가자"})
memory.save_context({"input": "그럼 출발!"}, {"ouput": "OK!"})
# 메모리 변수 가져오기
memory.load_memory_variables({})
{'history': [SystemMessage(content='The human says they are hungry and suggests going to eat ramen. The AI suggests going to the snack bar in front of the station. The human agrees to go and the AI responds with "OK!"')]}

5. ConversationSummaryBufferMemory

from langchain.memory import ConversationSummaryBufferMemory
from langchain.chat_models import ChatOpenAI

# 메모리 생성
memory = ConversationSummaryBufferMemory(
    llm=ChatOpenAI(
        model="gpt-3.5-turbo",
        temperature=0
    ), 
    max_token_limit=50, 
    return_messages=True
)
memory.save_context({"input": "안녕"}, {"ouput": "무슨 일이야?"})
memory.save_context({"input": "배고파"}, {"ouput": "나도"})
memory.save_context({"input": "밥 먹자"}, {"ouput": "OK!"})
# 메모리 변수 가져오기
memory.load_memory_variables({})
{'history': [SystemMessage(content="The human greets the AI in Korean. The AI responds by asking what's going on."),
  HumanMessage(content='배고파'),
  AIMessage(content='나도'),
  HumanMessage(content='밥 먹자'),
  AIMessage(content='OK!')]}

Last updated