Multi-Turn Dialogue
This section describes how to use LangChain to enable multi-turn dialog.
Prerequisites
- You have completed operations in Installing RAG SDK.
- You have completed containerization deployment based on "Installing MindIE" > "Mode 3: Container Installation" in MindIE Installation Guide.
- The Llama3-8B-Chinese-Chat model is running.
Procedure
- Run the vim command in any directory of the container to create a demo.py file. The file content is as follows:
from langchain.memory import ConversationBufferWindowMemory from langchain.chains import LLMChain from langchain_core.prompts import PromptTemplate from mx_rag.llm import Text2TextLLM from mx_rag.utils import ClientParam if __name__ == '__main__': template = """You are a chatbot having a conversation with a human. Please answer as briefly as possible. {chat_history} Human: {human_input}""" dev = 1 prompt = PromptTemplate( input_variables=["chat_history", "human_input"], template=template ) # k can be the saved number of dialog rounds. ConversationBufferMemory and ConversationTokenBufferMemory are also supported. For details, see the official LangChain documents. memory = ConversationBufferWindowMemory(memory_key="chat_history", k=3) client_param = ClientParam(ca_file="/path/to/ca.crt") chat = Text2TextLLM(base_url="https://ip:port/v1/chat/completions", model_name="Llama3-8B-Chinese-Chat", client_param=client_param) llm_chain = LLMChain(llm=chat, prompt=prompt, memory=memory, verbose=True) questions = ["Remember that Xiao Ming's father is Xiao Gang," "What are the first four of the seven continents?" "What about the last three?"] for question in questions: llm_chain.predict(human_input=question) completion = llm_chain.predict(human_input="Who is Xiao Ming's father?") print(completion) - Run the sample code to request the historical information in the model. The prompt combination result is as follows:
You are a chatbot having a conversation with a human. Please answer as briefly as possible. Human: Remember that Xiao Ming's father is Xiao Gang. AI: Okay, Xiao Ming's father is Xiao Gang. Human: What are the first four of the seven continents? AI: Asia, Africa, Europe, and North America. Human: What about the last three? AI: South America, Australia, and Antarctica. Human: Who is Xiao Ming's father? AI: Xiao Ming's father is Xiao Gang.
Parent topic: Application Development