BigDL-LLM实践(4):使用chain和内存缓冲区来记忆聊天记录

在构建聊天应用程序时,我们可以使用更复杂的 chain 和内存缓冲区来记忆聊天记录。这对于实现多轮聊天体验非常有用。

from langchain import PromptTemplate
from langchain.chains import ConversationChain
from langchain.chains.conversation.memory import ConversationBufferMemory
from bigdl.llm.langchain.llms import TransformersLLM

llm = TransformersLLM.from_model_id(
        model_id="lmsys/vicuna-7b-v1.5",
        model_kwargs={"temperature": 0, "max_length": 1024, "trust_remote_code": True},
    )

template = "The following is a friendly conversation between a human and an AI.\
    \nCurrent conversation:\n{history}\nHuman: {input}\nAI Asistant:"

prompt = PromptTemplate(template=template, input_variables=["history", "input"])

conversation_chain = ConversationChain(
    verbose=True,
    prompt=prompt,
    llm=llm,
    memory=ConversationBufferMemory(),
    llm_kwargs={"max_new_tokens": 256},
)

怎么运行?

query ="Good morning AI!" 
result = conversation_chain.run(query)

下一次对话时,会加载之前的聊天记录

query ="Tell me about Intel." 
result = conversation_chain.run(query)
> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI.    
Current conversation:
Human: Good morning AI!
AI: The following is a friendly conversation between a human and an AI.    
Current conversation:

Human: Good morning AI!
AI Asistant: Good morning! How can I assist you today?
Human: Tell me about Intel.
AI Asistant:
Intel is a multinational technology company that specializes in the development and manufacturing of computer hardware and technology solutions. It was founded in 1976 by Robert Noyce and Gordon Moore, and is headquartered in Santa Clara, California. Intel is best known for its microprocessors, which are the "brains" of most computers and devices. They also produce other hardware such as motherboard chipsets, flash memory, and other components. Intel is one of the largest and most well-known technology companies in the world, and is a leader in the development of new technologies and innovations in the field of computing.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值