在构建聊天应用程序时,我们可以使用更复杂的 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.