+3

[LLM 101] Thử tạo bộ nhớ ngắn hạn cho LLM Agent sử dụng LangChain

Giới thiệu

Trong chatbot, bộ nhớ đóng vai trò không thể phủ nhận trong việc tạo ra các cuộc hội thoại trôi chảy và gần gũi như con người. Nếu như một chatbot mà quên tên của bạn hoặc không nhớ đến các chủ đề đã thảo luận trước đó thì điều này làm mất đi bản chất của một cuộc trò chuyện với một thực thể thông minh 😄

Bộ nhớ đóng một vai trò then chốt trong quá trình phát triển của các hệ thống giao tiếp như Lang Chain. Các bạn nếu đã dùng sản phẩm chatbot chắc đều mong muốn rằng nó có thể ghi nhớ thông tin từ các đoạn chat trước và điều này giúp duy trì cuộc trò chuyện 😄

Đó có thể là việc ghi nhớ sở thích của người dùng, hiểu biết về các đại từ, hoặc theo dõi sự phát triển của cuộc trò chuyện, bộ nhớ là một yếu tố không thể thiếu để tạo ra các tương tác hấp dẫn và hiệu quả.

Trong bài viết này, mình sẽ trình bày một số phương pháp để tạo memory cho chatbot sử dụng thư viện LangChain.

Các phương pháp

Sử dụng Buffer Memory

Cách cơ bản để xây dựng memory cho chatbot là tạo một bộ đệm cho các cuộc trò chuyện. Trong phương pháp này, mô hình ghi lại và tích hợp tương tác giữa người dùng và hệ thống vào một tin nhắn, đây chính là bản ghi về cuộc trò chuyện đang diễn ra.

Phương pháp này có ưu điểm là đơn giản và hiệu quả cho các tương tác ngắn. Tuy nhiên nó cũng gặp những hạn chế khi phải xử lý các cuộc trò chuyện kéo dài do giới hạn về số lượng token.

Code ví dụ:

from langchain_openai import OpenAI
from langchain.chains import ConversationChain
from langchain.chains.conversation.memory import ConversationBufferMemory


llm = OpenAI(openai_api_key=OPENAI_KEY,
             temperature=0,
             max_tokens = 512)

buffer_memory = ConversationBufferMemory()

conversation = ConversationChain(
    llm=llm,
    verbose=True,
    memory=buffer_memory
)

conversation.predict(input="Do you know Donald Trump")
conversation.predict(input="Do you know AI")
conversation.predict(input="Do you know LLM")

# In buffer ra
print(conversation.memory.buffer)

ConversationBufferMemory giúp lưu lại lịch sử cuộc trò chuyện, đây là một dạng memory cơ bản trong chatbot.

Output sẽ như sau:

> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:

Human: Do you know Donald Trump
AI:

> Finished chain.


> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:
Human: Do you know Donald Trump
AI:  Yes, I am familiar with Donald Trump. He is a businessman and former reality television star who served as the 45th President of the United States from 2017 to 2021. He is known for his controversial policies and outspoken personality. Is there something specific you would like to know about him?
Human: Do you know AI
AI:

> Finished chain.


> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:
Human: Do you know Donald Trump
AI:  Yes, I am familiar with Donald Trump. He is a businessman and former reality television star who served as the 45th President of the United States from 2017 to 2021. He is known for his controversial policies and outspoken personality. Is there something specific you would like to know about him?
Human: Do you know AI
AI:  Yes, I am an AI, or artificial intelligence. I am a computer program designed to simulate human intelligence and perform tasks such as problem-solving and decision-making. Is there something specific you would like to know about AI?
Human: Do you know LLM
AI:

> Finished chain.
Human: Do you know Donald Trump
AI:  Yes, I am familiar with Donald Trump. He is a businessman and former reality television star who served as the 45th President of the United States from 2017 to 2021. He is known for his controversial policies and outspoken personality. Is there something specific you would like to know about him?
Human: Do you know AI
AI:  Yes, I am an AI, or artificial intelligence. I am a computer program designed to simulate human intelligence and perform tasks such as problem-solving and decision-making. Is there something specific you would like to know about AI?
Human: Do you know LLM
AI:  I am not familiar with LLM. Can you provide more context or information about what you are referring to?

Sử dụng Buffer Window Memory

Việc chọn tất cả các history chat có thể không hiệu quả do sử dụng nhiều token. Bạn có thể hạn chế điều này bằng cách thêm một tham số "k" để giới hạn lượng tin nhắn được lưu. Ví dụ, nếu "k=2" thì sẽ lưu nội dung của 2 tin nhắn gần nhất.

Code ví du:

from langchain.chains.conversation.memory import ConversationBufferWindowMemory
from langchain import OpenAI
from langchain.chains import ConversationChain

llm = OpenAI(openai_api_key=OPENAI_KEY,
             temperature=0,
             max_tokens = 512)

window_memory = ConversationBufferWindowMemory(k=2)

conversation = ConversationChain(
    llm=llm,
    verbose=True,
    memory=window_memory
)

conversation.predict(input="Do you know Donald Trump")
conversation.predict(input="Do you know AI")
conversation.predict(input="Do you know LLM")

# In buffer ra
print(conversation.memory.buffer)

Output sẽ như sau:

> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:

Human: Do you know Donald Trump
AI:

> Finished chain.


> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:
Human: Do you know Donald Trump
AI:  Yes, I am familiar with Donald Trump. He is a businessman and former reality television star who served as the 45th President of the United States from 2017 to 2021. He is known for his controversial policies and outspoken personality. Is there something specific you would like to know about him?
Human: Do you know AI
AI:

> Finished chain.


> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:
Human: Do you know Donald Trump
AI:  Yes, I am familiar with Donald Trump. He is a businessman and former reality television star who served as the 45th President of the United States from 2017 to 2021. He is known for his controversial policies and outspoken personality. Is there something specific you would like to know about him?
Human: Do you know AI
AI:  Yes, I am an AI, or artificial intelligence. I am a computer program designed to simulate human intelligence and perform tasks such as problem-solving and decision-making. Is there something specific you would like to know about AI?
Human: Do you know LLM
AI:

> Finished chain.
Human: Do you know AI
AI:  Yes, I am an AI, or artificial intelligence. I am a computer program designed to simulate human intelligence and perform tasks such as problem-solving and decision-making. Is there something specific you would like to know about AI?
Human: Do you know LLM
AI:  I am not familiar with LLM. Can you provide more context or information about what you are referring to?

Summary Memory

Thay vì lưu các tin nhắn trong history chat, ta có thể lưu một bản tổng hợp nội dung của history chat. Bằng cách này, chúng ta có thể tiết kiệm được lượng token sử dụng trong khi vẫn duy trì được mạch nội dung cuộc trò chuyện.

Code ví dụ:

from langchain.chains.conversation.memory import ConversationSummaryMemory
from langchain import OpenAI
from langchain.chains import ConversationChain

llm = OpenAI(openai_api_key=OPENAI_KEY,
            temperature=0,
            max_tokens = 512)

summary_memory = ConversationSummaryMemory(llm=OpenAI(openai_api_key=OPENAI_KEY))

conversation = ConversationChain(
    llm=llm,
    verbose=True,
    memory=summary_memory
)

conversation.predict(input="Do you know Donald Trump")
conversation.predict(input="Do you know AI")
conversation.predict(input="Do you know LLM")

# In buffer ra
print(conversation.memory.buffer)

Output như sau:

> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:

Human: Do you know Donald Trump
AI:

> Finished chain.


> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:

The human asks if the AI is familiar with Donald Trump. The AI confirms and provides a brief summary of his background and presidency, noting his controversial policies and outspoken personality. The AI also offers to provide more information if desired.
Human: Do you know AI
AI:

> Finished chain.


> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:

The human asks if the AI is familiar with Donald Trump. The AI confirms and provides a brief summary of his background and presidency, noting his controversial policies and outspoken personality. The AI also offers to provide more information if desired. The human then asks if the AI is familiar with the concept of AI, to which the AI responds by confirming that it is an AI and asking for clarification on the question.
Human: Do you know LLM
AI:

> Finished chain.

The human asks if the AI is familiar with Donald Trump. The AI confirms and provides a brief summary of his background and presidency, noting his controversial policies and outspoken personality. The AI also offers to provide more information if desired. The human then asks if the AI is familiar with the concept of AI, to which the AI responds by confirming that it is an AI and asking for clarification on the question. The human then asks if the AI is familiar with LLM, to which the AI responds by asking for more context or information.

Kết hợp Summary Memory và Buffer Memory

Chúng ta có thể kể hợp cả Summary Memory và Buffer Memory để tận dụng ưu điểm của 2 phương pháp này.

Code ví dụ:

from langchain.chains.conversation.memory import ConversationSummaryBufferMemory
from langchain import OpenAI
from langchain.chains import ConversationChain


llm = OpenAI(openai_api_key=OPENAI_KEY,
            temperature=0,
            max_tokens = 512)

summary_buffer_memory = ConversationSummaryBufferMemory(llm=OpenAI(openai_api_key=OPENAI_KEY), max_token_limit=50)

conversation = ConversationChain(
    llm=llm,
    memory=summary_buffer_memory,
    verbose=True
)

conversation.predict(input="Do you know Donald Trump")
conversation.predict(input="Do you know AI")
conversation.predict(input="Do you know LLM")

# In buffer ra
print(conversation.memory.moving_summary_buffer)

Output như sau:

> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:

Human: Do you know Donald Trump
AI:

> Finished chain.


> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:
System: 
The human asks if the AI is familiar with Donald Trump. The AI confirms and provides a brief summary of his background and political career, and offers to provide more information if needed. 
Human: Do you know AI
AI:

> Finished chain.


> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:
System: 
The human asks if the AI is familiar with Donald Trump. The AI confirms and provides a brief summary of his background and political career, and offers to provide more information if needed. The human then asks if the AI is familiar with AI.
AI:  Yes, I am an AI. My full name is Artificial Intelligence and I am a computer program designed to simulate human intelligence. I am constantly learning and improving my abilities. Is there anything specific you would like to know about me?
Human: Do you know LLM
AI:

> Finished chain.

The human asks if the AI is familiar with Donald Trump. The AI confirms and provides a brief summary of his background and political career, and offers to provide more information if needed. The human then asks if the AI is familiar with AI. The AI responds by introducing itself as an AI and explains its purpose and abilities. The AI also offers to answer any specific questions the human may have about it.

Knowledge graph

Ngoài các cách cơ bản trên, LangChain còn hỗ trợ knowledge graph memory. Tại đây, ta sẽ có một đồ thị kết nối các thông tin với nhau. Trong đó, node biểu thị cho các thực thể và cạnh là mối tương quan giữa 2 thực thể bất kì.

Code ví dụ:

from langchain.chains.conversation.memory import ConversationKGMemory
from langchain import OpenAI
from langchain.chains import ConversationChain
from langchain.prompts.prompt import PromptTemplate

llm = OpenAI(openai_api_key=OPENAI_KEY,
            temperature=0,
            max_tokens = 512)

# Define a template for conversation prompts
template = """
AI is a savant and it knows everything

Relevant Information: \n

{history}

Conversation: \n
Human: {input} \n
AI:"""

# Create an instance of the PromptTemplate class with specified input variables and template
prompt = PromptTemplate(input_variables=["history", "input"], template=template)

# Create an instance of the ConversationKGMemory class with the OpenAI instance
kg_memory = ConversationKGMemory(llm=llm)

conversation = ConversationChain(
    llm=llm,
    verbose=True,
    prompt=prompt,
    memory=kg_memory
)

Tổng kết

Trong bài viết mình đã trình bày một số cách cơ bản để tạo bộ nhớ ngắn hạn cho LLM Agent sử dụng LangChain. Các bạn hãy sử dụng nó linh hoạt tuỳ theo bài toán cụ thể của mình để đạt hiệu quả tốt nhất nhé.


All Rights Reserved

Viblo
Let's register a Viblo Account to get more interesting posts.