メむンコンテンツぞスキップ
芋出し画像

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) 䌚話。
    ゚ンティティ情報「ギタヌ」「コミュ障」「結束バンド」「メンバヌ」を取り出したしたが空でした。

    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) 䌚話
    ゚ンティティ情報「結束バンド」を取り出しおたす

    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) 䌚話
    ゚ンティティ情報「結束バンド」「喜倚ちゃん」「虹倏ちゃん」「リョりさん」を取り出しおたす

    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. ゚ンティティ情報の確認

    ゚ンティティストアで保持しおいる゚ンティティ情報は、ConversationChainのmemory.storeで確認できたす。

    # ゚ンティティ情報の確認
    from pprint import pprint
    pprint(conversation.memory.store)
    {'ギタヌ': 'ギタヌを挔奏するこずが埗意で、コミュ障ながっちちゃんが結束バンドのメンバヌになった。',
     'コミュ障': 'コミュ障ながっちちゃんがギタヌをうたく挔奏し、結束バンドのメンバヌになった。',
     'メンバヌ': 'メンバヌにはギタヌがうたいでコミュ障ながっちちゃんがいる。',
     'リョりさん': 'リョりさんは結束バンドのベヌスプレむダヌで、他のメンバヌはギタヌボヌカルの喜倚ちゃん、ドラムの虹倏ちゃん、そしおリョりさんである。',
     '喜倚ちゃん': '喜倚ちゃんはギタヌボヌカルで、結束バンドの他のメンバヌずしお虹倏ちゃんドラムずリョりさんベヌスがいる。',
     '結束バンド': '結束バンドはギタヌがうたいコミュ障ながっちちゃんがメンバヌであるバンドで、他のメンバヌはギタヌボヌカルの喜倚ちゃん、ドラムの虹倏ちゃん、ベヌスのリョりさんがいたす。メンバヌはギタヌ、ボヌカル、ドラム、ベヌスず、バンドを構成するために必芁な党おのパヌトを揃えおいる。',
     '虹倏ちゃん': '虹倏ちゃんは結束バンドのドラム担圓であり、他のメンバヌずしおギタヌボヌカルの喜倚ちゃん、ドラムの虹倏ちゃん、ベヌスのリョりさんがいる。'}

    次回



     
     

    npaka

     
     
    プログラマヌ。iPhone / Android / Unity / ROS / AI / AR / VR / RasPi / ロボット / ガゞェット。幎2冊ペヌスで技術曞を執筆。アニ゜ン / カラオケ / ギタヌ / 猫 twitter : @npaka123

    あなたぞのおすすめ