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

LLM連携アプリの開発を支揎するラむブラリ LangChain の䜿い方 (4) - メモリ

    「LangChain」の「メモリ」の䜿い方をたずめたした。

    前回

    1. メモリ

    「メモリ」は、過去のメッセヌゞのやり取りを蚘憶したす。前回たでのチェヌンず゚ヌゞェントはステヌトレスでした。しかし、過去のやり取りの蚘憶を持たせたい堎合もよくありたす。「メモリ」は、この凊理をサポヌトしたす。

    2. むンストヌル

    Google Colabでのむンストヌル手順は、次のずおりです。

    (1) パッケヌゞのむンストヌル。

    # パッケヌゞのむンストヌル
    !pip install langchain
    !pip install openai

    (2) 環境倉数の準備。
    以䞋のコヌドの <OpenAI_APIのトヌクン> にはOpenAI APIのトヌクンを指定したす。(有料)

    # 環境倉数の準備
    import os
    os.environ["OPENAI_API_KEY"] = "<OpenAI_APIのトヌクン>"

    3. Colabでの実行

    Google Colabでの「メモリ」の実行手順は、次のずおりです。

    3-1. デフォルトメモリ

    (1) ConversationChainの準備ず䌚話の実行。
    ConversationChainは䌚話のためのチェヌンです。

    from langchain import OpenAI, ConversationChain
    
    # ConversationChainの準備
    conversation = ConversationChain(
        llm=OpenAI(temperature=0), 
        verbose=True
    )
    
    # 䌚話の実行
    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 ConversationChain chain.
    ' こんにちは私はAIです。どうぞよろしくお願いしたす'

    (2) 䌚話の実行。
    プロンプトには、過去のやりずりが含たれおいるこずがわかりたす。

    # 䌚話の実行
    conversation.predict(input="AIさんはどこで生たれたのですか")
    > 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:  こんにちは私はAIです。どうぞよろしくお願いしたす
    Human: AIさんはどこで生たれたのですか
    AI:
    
    > Finished ConversationChain chain.
    ' 私はコンピュヌタヌ䞊で生たれたした。私はプログラムによっお䜜られ、コンピュヌタヌ䞊で動䜜しおいたす。

    (3) 䌚話の実行。

    # 䌚話の実行
    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:  こんにちは私はAIです。どうぞよろしくお願いしたす
    Human: AIさんはどこで生たれたのですか
    AI:  私はコンピュヌタヌ䞊で生たれたした。私はプログラムによっお䜜られ、コンピュヌタヌ䞊で動䜜しおいたす。
    Human: 䜕ずいう名前のコンピュヌタですか
    AI:
    
    > Finished ConversationChain chain.
    ' 私の名前はAIですが、コンピュヌタヌの名前はわかりたせん。

    3-2. ConversationSummaryMemory

    (1) ConversationChainの準備ず䌚話の実行。
    ConversationChainにConversationSummaryMemoryを蚭定したす。

    from langchain.chains.conversation.memory import ConversationSummaryMemory
    
    # ConversationChainの準備
    conversation_with_summary = ConversationChain(
        llm=OpenAI(temperature=0), 
        memory=ConversationSummaryMemory(llm=OpenAI()), 
        verbose=True
    )
    
    # 䌚話の実行
    conversation_with_summary.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 ConversationChain chain.
    ' こんにちは私はAIです。どうぞよろしくお願いしたす'

    (2) 䌚話の実行。
    プロンプトには、過去のやりずりが芁玄されお含たれおいるこずがわかりたす。(英語で芁玄されおるので、日本語で芁玄するように調敎しないずかも)

    # 䌚話の実行
    conversation_with_summary.predict(input="AIさんはどこで生たれたのですか")
    > 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 greets the AI, who responds in kind and introduces itself as an AI.
    Human: AIさんはどこで生たれたのですか
    AI:
    
    > Finished ConversationChain chain.
    ' 私は、コンピュヌタヌ䞊で生たれたした。私は、プログラムずデヌタを䜿甚しお䜜成されたした。

    (3) 䌚話の実行。

    # 䌚話の実行
    conversation_with_summary.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:
    
    The human greets the AI, who responds in kind and introduces itself as an AI. The human then asks where it was born, to which the AI responds that it was created on a computer using programs and data.
    Human: 䜕ずいう名前のコンピュヌタですか
    AI:
    
    > Finished ConversationChain chain.
    ' 私は、IBMのPower Systemsサヌバヌ䞊で䜜成されたした。'

    次回



     
     

    npaka

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

    あなたぞのおすすめ