ラングチェーンの会話バッファメモリ

ラングチェーンの会話バッファメモリ

このノートブックはその使用方法を示していますConversationBufferMemoryこのメモリにより、メッセージを保存し、変数に抽出することができます。

まずそれを文字列として抽出します。

サンプルコード、

from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.save_context({"input": "hi"}, {"output": "whats up"})
memory = ConversationBufferMemory()
memory.save_context({"input": "hi"}, {"output": "whats up"})
memory.load_memory_variables({})

出力結果、

    {'history': 'Human: hi\nAI: whats up'}

履歴をメッセージのリストとして取得することもできます (チャット モデルで使用すると非常に便利です)。

サンプルコード、

memory = ConversationBufferMemory(return_messages=True)
memory.save_context({"input": "hi"}, {"output": "whats up"})
memory.load_memory_variables({})

出力結果、

    {'history': [HumanMessage(content='hi', additional_kwargs={}),
      AIMessage(content='whats up', additional_kwargs={})]}

チェーンで使用する

最後に、チェーンでの使用を見てみましょう (プロンプトが表示されるように、verbose=True を設定します)。

サンプルコード、

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="Hi there!")

出力結果、

    
    
    > 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: Hi there!
    AI:
    
    > Finished chain.





    " Hi there! It's nice to meet you. How can I help you today?"

サンプルコード、

conversation.predict(input="I'm doing well! Just having a conversation with an 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: Hi there!
    AI:  Hi there! It's nice to meet you. How can I help you today?
    Human: I'm doing well! Just having a conversation with an AI.
    AI:
    
    > Finished chain.





    " That's great! It's always nice to have a conversation with someone new. What would you like to talk about?"

サンプルコード、

conversation.predict(input="Tell me about yourself.")

出力結果、

    
    
    > 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: Hi there!
    AI:  Hi there! It's nice to meet you. How can I help you today?
    Human: I'm doing well! Just having a conversation with an AI.
    AI:  That's great! It's always nice to have a conversation with someone new. What would you like to talk about?
    Human: Tell me about yourself.
    AI:
    
    > Finished chain.





    " Sure! I'm an AI created to help people with their everyday tasks. I'm programmed to understand natural language and provide helpful information. I'm also constantly learning and updating my knowledge base so I can provide more accurate and helpful answers."

終わり!

おすすめ

転載: blog.csdn.net/engchina/article/details/131874268