見出し画像

LangChainのエンティティメモリを試す

「LangChain」の新機能「エンティティメモリ」を試したので、まとめました。

前回

1. エンティティメモリ

「エンティティメモリ」は、会話に含まれているエンティティ情報を保存し、必要に応じてその記憶を取り出して利用するメモリです。

処理の流れは、次のとおりです。

(1) ユーザー入力からエンティティ情報を抽出してエンティティストアに保存
(2) エンティティストアからエンティティ情報を取得して応答を生成
(3) エンティティストアのエンティティ情報を更新


2. Colabでの実行

Google Colabでの実行手順は、次のとりです。

(1) パッケージのインストール。

# パッケージのインストール
!pip install langchain
!pip install openai

(2) 環境変数の準備。
以下のコードの <OpenAI_APIのトークン> にはOpenAI APIのトークンを指定します。(有料)

import os
os.environ["OPENAI_API_KEY"] = "<OpenAI_APIのトークン>"

(3) 会話チェーンの準備。
promptにENTITY_MEMORY_CONVERSATION_TEMPLATE、memoryにConversationEntityMemoryを指定します。

from langchain import OpenAI, ConversationChain
from langchain.chains.conversation.memory import ConversationEntityMemory
from langchain.chains.conversation.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE

# 会話チェーンの生成
llm = OpenAI(temperature=0)
conversation = ConversationChain(
    llm=llm, 
    verbose=True,
    prompt=ENTITY_MEMORY_CONVERSATION_TEMPLATE,
    memory=ConversationEntityMemory(llm=llm)
)

(4) 会話1。
エンティティ情報「ギター」「コミュ障」「結束バンド」「メンバー」を取り出しましたが空でした。

conversation.predict(input="ギターがうまいけどコミュ障なぼっちちゃんが、結束バンドのメンバーになりました。")
> Entering new ConversationChain chain...
Prompt after formatting:
You are an assistant to a human, powered by a large language model trained by OpenAI.

You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.

You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.

Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist.

Context:
{'ギター': '', 'コミュ障': '', '結束バンド': '', 'メンバー': ''}

Current conversation:

Last line:
Human: ギターがうまいけどコミュ障なぼっちちゃんが、結束バンドのメンバーになりました。
You:

> Finished chain.
' それは素晴らしいですね!コミュ障な人が結束バンドのメンバーになるというのは、とても素晴らしいことです。そのメンバーの仲間として、彼をサポートしてあげることができるのではないでしょうか?'

(5) 会話2
エンティティ情報「結束バンド」を取り出してます

conversation.predict(input='結束バンドの他のメンバーは、ギターボーカルの喜多ちゃん、ドラムの虹夏ちゃん、ベースのリョウさんがいます。')
> Entering new ConversationChain chain...
Prompt after formatting:
You are an assistant to a human, powered by a large language model trained by OpenAI.

You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.

You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.

Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist.

Context:
{'結束バンド': '結束バンドはギターがうまいコミュ障なぼっちちゃんがメンバーであるバンドです。', '喜多ちゃん': '', '虹夏ちゃん': '', 'リョウさん': ''}

Current conversation:
Human: ギターがうまいけどコミュ障なぼっちちゃんが、結束バンドのメンバーになりました。
AI:  それは素晴らしいですね!コミュ障な人が結束バンドのメンバーになるというのは、とても素晴らしいことです。そのメンバーの仲間として、彼をサポートしてあげることができるのではないでしょうか?
Last line:
Human: 結束バンドの他のメンバーは、ギターボーカルの喜多ちゃん、ドラムの虹夏ちゃん、ベースのリョウさんがいます。
You:

> Finished chain.
' すごいですね!結束バンドのメンバーは、ギター、ボーカル、ドラム、ベースと、バンドを構成するために必要な全てのパートを揃えているようですね!それぞれのメンバーが持つ特質を活かして、素晴らしい演奏を聴かせてくれることでしょう!'

(6) 会話3
エンティティ情報「結束バンド」「喜多ちゃん」「虹夏ちゃん」「リョウさん」を取り出してます

conversation.predict(input='結束バンドのメンバーを教えて下さい。')
> Entering new ConversationChain chain...
Prompt after formatting:
You are an assistant to a human, powered by a large language model trained by OpenAI.

You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.

You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.

Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist.

Context:
{'結束バンド': '結束バンドはギターがうまいコミュ障なぼっちちゃんがメンバーであるバンドで、他のメンバーはギターボーカルの喜多ちゃん、ドラムの虹夏ちゃん、ベースのリョウさんがいます。', '喜多ちゃん': '喜多ちゃんはギターボーカルで、結束バンドの他のメンバーとして虹夏ちゃん(ドラム)とリョウさん(ベース)がいる。', '虹夏ちゃん': '虹夏ちゃんは結束バンドのドラム担当である。', 'リョウさん': 'リョウさんは結束バンドのベースプレイヤーである。'}

Current conversation:
Human: ギターがうまいけどコミュ障なぼっちちゃんが、結束バンドのメンバーになりました。
AI:  それは素晴らしいですね!コミュ障な人が結束バンドのメンバーになるというのは、とても素晴らしいことです。そのメンバーの仲間として、彼をサポートしてあげることができるのではないでしょうか?
Human: 結束バンドの他のメンバーは、ギターボーカルの喜多ちゃん、ドラムの虹夏ちゃん、ベースのリョウさんがいます。
AI:  すごいですね!結束バンドのメンバーは、ギター、ボーカル、ドラム、ベースと、バンドを構成するために必要な全てのパートを揃えているようですね!それぞれのメンバーが持つ特質を活かして、素晴らしい演奏を聴かせてくれることでしょう!
Last line:
Human: 結束バンドのメンバーを教えて下さい。
You:

> Finished chain.
' 結束バンドのメンバーは、ギターがうまいコミュ障なぼっちちゃん、ギターボーカルの喜多ちゃん、ドラムの虹夏ちゃん、ベースのリョウさんです。'

3. エンティティ情報の確認

エンティティストアで保持しているエンティティ情報は、ConversationChainmemory.storeで確認できます。

# エンティティ情報の確認
from pprint import pprint
pprint(conversation.memory.store)
{'ギター': 'ギターを演奏することが得意で、コミュ障なぼっちちゃんが結束バンドのメンバーになった。',
 'コミュ障': 'コミュ障なぼっちちゃんがギターをうまく演奏し、結束バンドのメンバーになった。',
 'メンバー': 'メンバーにはギターがうまいでコミュ障なぼっちちゃんがいる。',
 'リョウさん': 'リョウさんは結束バンドのベースプレイヤーで、他のメンバーはギターボーカルの喜多ちゃん、ドラムの虹夏ちゃん、そしてリョウさんである。',
 '喜多ちゃん': '喜多ちゃんはギターボーカルで、結束バンドの他のメンバーとして虹夏ちゃん(ドラム)とリョウさん(ベース)がいる。',
 '結束バンド': '結束バンドはギターがうまいコミュ障なぼっちちゃんがメンバーであるバンドで、他のメンバーはギターボーカルの喜多ちゃん、ドラムの虹夏ちゃん、ベースのリョウさんがいます。メンバーはギター、ボーカル、ドラム、ベースと、バンドを構成するために必要な全てのパートを揃えている。',
 '虹夏ちゃん': '虹夏ちゃんは結束バンドのドラム担当であり、他のメンバーとしてギターボーカルの喜多ちゃん、ドラムの虹夏ちゃん、ベースのリョウさんがいる。'}

次回



この記事が気に入ったらサポートをしてみませんか?