# 01. ConversationBufferMemory

## ConversationBufferMemory <a href="#conversationbuffermemory" id="conversationbuffermemory"></a>

This memory allows you to save the message and then extract the message to the variable.

First you can extract it as a string.

```
from langchain.memory import ConversationBufferMemory
```

```
memory = ConversationBufferMemory()
memory.save_context(
    inputs={
        "human": "Hello, I want to open a bank account remotely. How to start?"
    },
    outputs={
        "ai": "Hello! We are glad that you would like to open an account. First, Please prepare your ID for verification.?"
    },
)
```

memory `load_memory_variables({})` The function returns the message history.

```
# 'history' heck the conversation history saved in the key.
memory.load_memory_variables({})
```

```
{'history':'Human: Hello, I would like to open a bank account in hypertrophy. How should I start?\nAI: Hello! I am glad you want to open an account. First, can you prepare your ID for your certification?'} 
```

`save_context(inputs, outputs)` You can use the method to save the conversation history.

* This method `inputs` Wow `outputs` Receive two factors.
* `inputs` Input of the user, `outputs` Saves the output of AI.
* With this method, the conversation history `history` Stored in the key.
* after `load_memory_variables` You can use the method to check the history of the saved conversation.

```
# inputs: dictionary(key: "human" or "ai", value: question)
# outputs: dictionary(key: "ai" or "human", value: answer)
memory.save_context(
    inputs={"human": "Yes, I have my ID ready. What should I do now?"},
    outputs={
        "ai": "Thank you. Please upload a clear photo of the front and back of your ID. We will then proceed with the identity verification process."
    },
)
```

```
# Save 2 conversations.
memory.save_context(
    inputs={"human": "I uploaded a photo. How does identity verification work?"},
    outputs={
        "ai": "I have checked the photos you uploaded. Now, please proceed with identity verification via mobile phone.Please enter the verification number sent to you via text message."
    },
)
memory.save_context(
    inputs={"human": "I entered the verification number. How do I open an account now?"},
    outputs={
        "ai": " Your identity verification is complete. Now, please select the type of account you want and enter the required information. You can select the type of deposit, currency, etc."
    },
)
```

```
# historyCheck the saved conversation log.
print(memory.load_memory_variables({})["history"])
```

```
Human: Hello, I would like to open a bank account in hypertrophy. How should I start? 
AI: Hello! I am glad you want to open an account. First, would you please prepare your ID for your certification? 
Human: I uploaded a photo. How does my certification work? 
AI: I checked the photos you uploaded. Now proceed with your authentication through your phone. Just enter the authorization number sent by text. 
Human: You have entered the authorization number. How is the account opening now? 
AI: Your authentication is complete. Now select the type of account you want and enter the information you need. You can choose the type of deposit, the type of currency, etc. 
```

```
# Saves 2 additional conversations.
memory.save_context(
    inputs={"human": "I have entered all the information. What is the next step?"},
    outputs={
        "ai": "We have confirmed the information you have entered. The account opening process is almost complete. Finally, please agree to the terms and conditions and confirm the account opening.
."
    },
)
memory.save_context(
    inputs={"human": "I have completed all the procedures. Is the account opene?"},
    outputs={
        "ai": "Yes, your account has been opened. Your account number and related information have been sent to the email address you registered. Please contact us at any time if you need further assistance. Thank you!"
    },
)
```

```
# history Check the saved conversation log
.
print(memory.load_memory_variables({})["history"])
```

```
 Human: Hello, I would like to open a bank account in hypertrophy. How should I start? 
AI: Hello! I am glad you want to open an account. First, would you please prepare your ID for your certification? 
Human: I uploaded a photo. How does my certification work? 
AI: I checked the photos you uploaded. Now proceed with your authentication through your phone. Just enter the authorization number sent by text. 
Human: You have entered the authorization number. How is the account opening now? 
AI: Your authentication is complete. Now select the type of account you want and enter the information you need. You can choose the type of deposit, the type of currency, etc. 
Human: I have entered all the information. What are the next steps? 
AI: I checked the information you entered. The account opening process is almost over. Finally, agree to the terms and conditions, and confirm the opening of your account. 
Human: All procedures are completed. Is your account open? 
AI: Yes, the account is open. Your account number and related information were sent by registered email. Please feel free to contact us for further assistance. Thank you! 
```

`return_messages=True` When set to `HumanMessage` Wow `AIMessage` Returns the object.

```
memory = ConversationBufferMemory(return_messages=True)

memory.save_context(
    inputs={
        "human": "Hello, I would like to open a bank account remotely. How do I get started?"
    },
    outputs={
        "ai": "Hello! I am glad that you would like to open an account. First, please prepare your ID for verification?"
    },
)

memory.save_context(
    inputs={"human": "Yes, I have my ID ready. What should I do now?"},
    outputs={
        "ai": "Thank you. Please upload a clear photo of the front and back of your ID. We will then proceed with the identity verification process."
    },
)

memory.save_context(
    inputs={"human": "I uploaded a photo. How do I verify my identity?"},
    outputs={
        "ai": "We have confirmed the photo you uploaded. Now, please proceed with identity verification via mobile phone. Please enter the verification number sent to you via text message."
    },
)

```

```
# history Check the conversation history saved in.
memory.load_memory_variables({})["history"]
```

```
[HumanMessage (content=' Hello, I would like to open a bank account in hypertrophy. How should I start?'), AIMessage (content='Hello! I am glad you want to open an account. First, would you please prepare your ID for your certification?'), HumanMessage (content=' Yes, I prepared my ID. What should I do now?'), AIMessage (content='Thank you. Shoot and upload your ID card clearly. We will then proceed with the authentication process.'), HumanMessage (content=' uploaded the photo. How does my certification work?'), AIMessage (content=' I checked the uploaded photos. Now proceed with your authentication through your phone. Please enter the authorization number sent by text.')] 
```

### Apply to Chain <a href="#chain" id="chain"></a>

```
# API KEY A configuration file for managing environment variables
from dotenv import load_dotenv

# API KEY load information
load_dotenv()
```

```
from langchain_openai import ChatOpenAI
from langchain.chains import ConversationChain

# LLM create a model.
llm = ChatOpenAI(temperature=0)

# ConversationChain creates.
conversation = ConversationChain(
    # ConversationBufferMemory I use.
    llm=llm,
    memory=ConversationBufferMemory(),
)
```

`ConversationChain` Use to conduct a conversation.

```
# Start a conversation.
response = conversation.predict(
    input="Hello, I would like to open a bank account remotely. How do I get started?"
)
print(response)
```

```
Hello! To open a bank account, you must first access the bank's official website and follow the online opening process. Usually you need to submit documents such as personal information, a copy of your ID, an address card, etc., fill out an online form and have an electronic signature. After that, you can proceed with additional steps according to the instructions provided by the bank. What bank are you considering?
```

```
 One. Access to the bank's official website 
2. Follow the online opening procedure 
3. Submission of personal information, copies of identification cards, address documents, etc. 
4. Online form filling and electronic signature 
5. Additional steps proceed according to bank guidance 
```
