From 75eec7e1b0371303db38d3001df5b91b5518050b Mon Sep 17 00:00:00 2001 From: imClumsyPanda Date: Wed, 12 Apr 2023 22:41:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20Prompt=20=E6=A8=A1?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- knowledge_based_chatglm.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/knowledge_based_chatglm.py b/knowledge_based_chatglm.py index b073873..6a7601b 100644 --- a/knowledge_based_chatglm.py +++ b/knowledge_based_chatglm.py @@ -1,9 +1,5 @@ from langchain.chains import RetrievalQA -from langchain.prompts.chat import ( - ChatPromptTemplate, - SystemMessagePromptTemplate, - HumanMessagePromptTemplate, -) +from langchain.prompts import PromptTemplate from langchain.embeddings.huggingface import HuggingFaceEmbeddings from langchain.vectorstores import FAISS from langchain.document_loaders import UnstructuredFileLoader @@ -80,24 +76,28 @@ def init_knowledge_vector_store(filepath:str): def get_knowledge_based_answer(query, vector_store, chat_history=[]): global chatglm, embeddings - system_template = """基于以下内容,简洁和专业的来回答用户的问题。 - 如果无法从中得到答案,请说 "不知道" 或 "没有足够的相关信息",不要试图编造答案。答案请使用中文。 - ---------------- - {context} - ---------------- - """ - messages = [ - SystemMessagePromptTemplate.from_template(system_template), - HumanMessagePromptTemplate.from_template("{question}"), - ] - prompt = ChatPromptTemplate.from_messages(messages) + prompt_template = """基于以下已知信息,简洁和专业的来回答用户的问题。 +如果无法从中得到答案,请说 "根据已知信息无法回答该问题" 或 "没有提供足够的相关信息",不允许在答案中添加编造成分,答案请使用中文。 + +已知内容: +{context} + +问题: +{question}""" + prompt = PromptTemplate( + template=prompt_template, + input_variables=["context", "question"] + ) chatglm.history = chat_history knowledge_chain = RetrievalQA.from_llm( llm=chatglm, retriever=vector_store.as_retriever(search_kwargs={"k": VECTOR_SEARCH_TOP_K}), prompt=prompt ) + knowledge_chain.combine_documents_chain.document_prompt = PromptTemplate( + input_variables=["page_content"], template="{page_content}" + ) knowledge_chain.return_source_documents = True