Langchain 的 Conversation buffer memory

Langchain 的 Conversation buffer memory

本笔记本展示了如何使用 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={})]}

Using in a chain

最后,让我们看一下在链中使用它(设置 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."

完结!

你可能感兴趣的:(LINUX,langchain,linux,人工智能,buffer,memory)