[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m A new release of pip is available: [0m[31;49m24.0[0m[39;49m -> [0m[32;49m24.3.1[0m
[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpip install --upgrade pip[0m
You can alternatively set OPENAI_API_KEY in .env file and load it.
[Note] This is not necessary if you've already set OPENAI_API_KEY in previous steps.
# Load environment variables# Reload any variables that need to be overwritten from the previous cellfrom dotenv import load_dotenvload_dotenv(override=True)
True
Conversation Summary Memory
Let's now explore how to use a more complex type of memory: ConversationSummaryMemory.
This type of memory generates a summary of the conversation over time , which can be useful for compressing conversational information as the conversation progresses.
ConversationSummaryMemory summarizes the conversation as it continues and stores the current summary in memory .
This memory can then be used to insert the summarized conversation history into prompts or chains.
It is particularly useful for longer conversations, where retaining the entire conversation history in the prompt would take up too many tokens.
/var/folders/c4/0f7nfvt16ln8630csjtkk_1w0000gn/T/ipykernel_3687/889678148.py:4: LangChainDeprecationWarning: Please see the migration guide at: https://python.langchain.com/docs/versions/migrating_memory/
memory = ConversationSummaryMemory(
You can store multiple conversations.
memory.save_context( inputs={"human": "What is the price of the Europe travel package?"}, outputs={"ai": "The base price for the 14-night, 15-day Europe package is €3,500. This price includes airfare, hotel accommodations, and admission fees to designated tourist attractions. Additional costs may vary depending on optional tours or personal expenses." },)memory.save_context( inputs={"human": "What are the main tourist attractions included in the trip?"}, outputs={"ai": "This trip includes visits to famous European landmarks such as the Eiffel Tower in Paris, the Colosseum in Rome, the Brandenburg Gate in Berlin, and Rhine Falls in Zurich. You'll comprehensively experience iconic sites in each city." },)memory.save_context( inputs={"human": "Is travel insurance included?"}, outputs={"ai": "Yes, basic travel insurance is provided for all travelers. This insurance includes medical expense coverage and support in emergency situations. Enhanced coverage is available upon request." },)memory.save_context( inputs={"human": "Can I upgrade my flight seat to business class? How much does it cost?" }, outputs={"ai": "Upgrading your flight seat to business class is possible. The upgrade cost is approximately €1,200 round-trip. Business class offers benefits such as wider seats, premium in-flight meals, and additional baggage allowance." },)memory.save_context( inputs={"human": "What is the hotel rating included in the package?"}, outputs={"ai": "The package includes accommodation in 4-star hotels. Each hotel offers comfort and convenience, with central locations providing easy access to tourist sites. All hotels are equipped with excellent services and amenities." },)memory.save_context( inputs={"human": "Can you provide more details about the meal options?"}, outputs={"ai": "This travel package includes daily breakfast served at the hotel. Lunch and dinner are not included, giving travelers the flexibility to explore and enjoy a variety of local cuisines. Additionally, a list of recommended restaurants in each city is provided to help maximize your culinary experience." },)memory.save_context( inputs={"human": "How much is the deposit for booking the package? What is the cancellation policy?" }, outputs={"ai": "A deposit of €500 is required when booking the package. The cancellation policy allows a full refund if canceled at least 30 days before the booking date. After that, the deposit becomes non-refundable. If canceled within 14 days of the travel start date, 50% of the total cost will be charged, and after that, the full cost will be non-refundable." },)
You can check the history of the stored memory.
It provides a concise summary of all previous conversations.
[SystemMessage(content='The human inquires about the price of the Europe travel package. The AI responds that the base price for the 14-night, 15-day Europe package is €3,500, which includes airfare, hotel accommodations, and admission fees to designated tourist attractions. Additional costs may vary based on optional tours or personal expenses. The trip includes visits to famous European landmarks such as the Eiffel Tower in Paris, the Colosseum in Rome, the Brandenburg Gate in Berlin, and Rhine Falls in Zurich, offering a comprehensive experience of iconic sites in each city. Basic travel insurance is included, covering medical expenses and emergency support, with enhanced coverage available upon request. The human asks about upgrading their flight seat to business class, and the AI informs them that it is possible for an additional cost of approximately €1,200 round-trip, which includes benefits like wider seats, premium in-flight meals, and additional baggage allowance. The human then asks about the hotel rating included in the package, and the AI states that the package includes accommodation in 4-star hotels, which offer comfort and convenience with central locations and excellent services and amenities. The human asks for more details about meal options, and the AI explains that the package includes daily breakfast at the hotel, while lunch and dinner are not included, allowing travelers to explore local cuisines. A list of recommended restaurants in each city is provided to enhance the culinary experience. The human asks about the deposit and cancellation policy, and the AI explains that a deposit of €500 is required when booking the package. The cancellation policy allows a full refund if canceled at least 30 days before the booking date. After that, the deposit becomes non-refundable. If canceled within 14 days of the travel start date, 50% of the total cost will be charged, and after that, the full cost will be non-refundable.', additional_kwargs={}, response_metadata={})]
Conversation Summary Buffer Memory
ConversationSummaryBufferMemory combines two key ideas:
It retains a buffer of the recent conversation history in memory while compiling older interactions into a summary without completely flushing them.
It determines when to flush the conversation based on the token length, instead of the number of interactions.
from langchain_openai import ChatOpenAIfrom langchain.memory import ConversationSummaryBufferMemoryllm =ChatOpenAI()memory =ConversationSummaryBufferMemory( llm=llm, max_token_limit=200, # Set the token length threshold for summarization. return_messages=True,)
/var/folders/c4/0f7nfvt16ln8630csjtkk_1w0000gn/T/ipykernel_3687/2100373999.py:6: LangChainDeprecationWarning: Please see the migration guide at: https://python.langchain.com/docs/versions/migrating_memory/
memory = ConversationSummaryBufferMemory(
First, let's save a single conversation, and then check the memory.
memory.save_context( inputs={"human": "What is the price of the Europe travel package?"}, outputs={"ai": "The base price for the 14-night, 15-day Europe package is €3,500. This price includes airfare, hotel accommodations, and admission fees to designated tourist attractions. Additional costs may vary depending on optional tours or personal expenses." },)
Check the conversation stored in memory.
At this point, the conversation is not yet summarized because it hasn't reached the 200-token threshold.
# Check the stored conversation history in memorymemory.load_memory_variables({})["history"]
[HumanMessage(content='What is the price of the Europe travel package?', additional_kwargs={}, response_metadata={}),
AIMessage(content='The base price for the 14-night, 15-day Europe package is €3,500. This price includes airfare, hotel accommodations, and admission fees to designated tourist attractions. Additional costs may vary depending on optional tours or personal expenses.', additional_kwargs={}, response_metadata={})]
Let's add more conversations to exceed the 200-token limit.
memory.save_context( inputs={"human": "What are the main tourist attractions included in the trip?"}, outputs={"ai": "This trip includes visits to famous European landmarks such as the Eiffel Tower in Paris, the Colosseum in Rome, the Brandenburg Gate in Berlin, and Rhine Falls in Zurich. You'll comprehensively experience iconic sites in each city." },)memory.save_context( inputs={"human": "Is travel insurance included?"}, outputs={"ai": "Yes, basic travel insurance is provided for all travelers. This insurance includes medical expense coverage and support in emergency situations. Enhanced coverage is available upon request." },)memory.save_context( inputs={"human": "Can I upgrade my flight seat to business class? How much does it cost?" }, outputs={"ai": "Upgrading your flight seat to business class is possible. The upgrade cost is approximately €1,200 round-trip. Business class offers benefits such as wider seats, premium in-flight meals, and additional baggage allowance." },)memory.save_context( inputs={"human": "What is the hotel rating included in the package?"}, outputs={"ai": "The package includes accommodation in 4-star hotels. Each hotel offers comfort and convenience, with central locations providing easy access to tourist sites. All hotels are equipped with excellent services and amenities." },)
Check the stored conversation history.
The most recent conversation remains unsummarized, while the previous conversations are stored as a summary.
# Check the stored conversation history in memorymemory.load_memory_variables({})["history"]
[SystemMessage(content='The human inquires about the price of the Europe travel package. The AI responds that the base price for the 14-night, 15-day package is €3,500, inclusive of airfare, hotel accommodations, and admission fees. Additional costs may vary depending on optional tours or personal expenses. The trip includes visits to famous European landmarks such as the Eiffel Tower in Paris, the Colosseum in Rome, the Brandenburg Gate in Berlin, and Rhine Falls in Zurich, providing a comprehensive experience of iconic sites in each city.', additional_kwargs={}, response_metadata={}),
HumanMessage(content='Is travel insurance included?', additional_kwargs={}, response_metadata={}),
AIMessage(content='Yes, basic travel insurance is provided for all travelers. This insurance includes medical expense coverage and support in emergency situations. Enhanced coverage is available upon request.', additional_kwargs={}, response_metadata={}),
HumanMessage(content='Can I upgrade my flight seat to business class? How much does it cost?', additional_kwargs={}, response_metadata={}),
AIMessage(content='Upgrading your flight seat to business class is possible. The upgrade cost is approximately €1,200 round-trip. Business class offers benefits such as wider seats, premium in-flight meals, and additional baggage allowance.', additional_kwargs={}, response_metadata={}),
HumanMessage(content='What is the hotel rating included in the package?', additional_kwargs={}, response_metadata={}),
AIMessage(content='The package includes accommodation in 4-star hotels. Each hotel offers comfort and convenience, with central locations providing easy access to tourist sites. All hotels are equipped with excellent services and amenities.', additional_kwargs={}, response_metadata={})]