Open in Colab

Memory#

chainとagentはデフォルトでステートレスで独立しています。チャットボットなどでは前の状態を記憶することが重要となり、memoryはこれを実現します。

ChatMessageHistory#

ChatMessageHistory クラスは HumanMessageAIMessage を補完しフェッチするためのメソッドを持っています。

from langchain.memory import ChatMessageHistory

history = ChatMessageHistory()
history.add_user_message("hi!")
history.add_ai_message("whats up?")
history.messages
[HumanMessage(content='hi!', additional_kwargs={}, example=False),
 AIMessage(content='whats up?', additional_kwargs={}, example=False)]

ConversationBufferMemory#

ConversationBufferMemory クラスは ChatMessageHistory のラッパーでメッセージを変数に取り出します。

from langchain.memory import ConversationBufferMemory


memory = ConversationBufferMemory()
memory.chat_memory.add_user_message("こんにちは")
memory.chat_memory.add_ai_message("いい天気ですね")
memory.load_memory_variables({})
{'history': 'Human: こんにちは\nAI: いい天気ですね'}
memory = ConversationBufferMemory(return_messages=True)
memory.chat_memory.add_user_message("こんにちは")
memory.chat_memory.add_ai_message("いい天気ですね")
memory.load_memory_variables({})
{'history': [HumanMessage(content='こんにちは', additional_kwargs={}, example=False),
  AIMessage(content='いい天気ですね', additional_kwargs={}, example=False)]}

Using in a chain#

chainを使って会話をします、引数 verboseTrue を設定することでプロンプトが確認できます。

from langchain.llms import OpenAI
from langchain.chains import ConversationChain


llm = OpenAI(temperature=0)
conversation = ConversationChain(
    llm=llm, 
    verbose=True, 
    memory=ConversationBufferMemory()
)
conversation.predict(input="こんにちは、今日はこれからご飯を炊きます")
> 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: こんにちは、今日はこれからご飯を炊きます
AI:

> Finished chain.
' こんにちは!ご飯を炊くのは楽しそうですね!何を炊きますか?'
conversation.predict(input="枝豆が旬なので、枝豆と生姜の炊き込みご飯を炊きます")
> 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: こんにちは、今日はこれからご飯を炊きます
AI:  こんにちは!ご飯を炊くのは楽しそうですね!何を炊きますか?
Human: 枝豆が旬なので、枝豆と生姜の炊き込みご飯を炊きます
AI:

> Finished chain.
' 枝豆と生姜の炊き込みご飯ですか!おいしそうですね!それに何を一緒に食べますか?'
conversation.predict(input="そうですね、あなたはなにがおかずとしてよいと思いますか?")
> 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: こんにちは、今日はこれからご飯を炊きます
AI:  こんにちは!ご飯を炊くのは楽しそうですね!何を炊きますか?
Human: 枝豆が旬なので、枝豆と生姜の炊き込みご飯を炊きます
AI:  枝豆と生姜の炊き込みご飯ですか!おいしそうですね!それに何を一緒に食べますか?
Human: そうですね、あなたはなにがおかずとしてよいと思いますか?
AI:

> Finished chain.
' 私は、枝豆と生姜の炊き込みご飯には、鶏肉のタレをかけたものがおすすめです!それと、サラダや野菜を一緒に食べると、さらに美味しくなりますよ!'