forked from chatchat-space/Langchain-Chatchat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknowledge_based_chatglm.py
70 lines (60 loc) · 2.66 KB
/
knowledge_based_chatglm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from langchain.prompts.prompt import PromptTemplate
from langchain.chains import ChatVectorDBChain
from langchain.prompts.chat import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
)
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
from langchain.document_loaders import UnstructuredFileLoader
from chatglm_llm import ChatGLM
def init_knowledge_vector_store(filepath):
embeddings = HuggingFaceEmbeddings(model_name="GanymedeNil/text2vec-large-chinese", )
loader = UnstructuredFileLoader(filepath, mode="elements")
docs = loader.load()
vector_store = FAISS.from_documents(docs, embeddings)
return vector_store
def get_wiki_agent_answer(query, vector_store, chat_history=[]):
system_template = """基于以下内容,简洁和专业的来回答用户的问题。
如果无法从中得到答案,请说 "不知道" 或 "没有足够的相关信息",不要试图编造答案。答案请使用中文。
----------------
{context}
----------------
"""
messages = [
SystemMessagePromptTemplate.from_template(system_template),
HumanMessagePromptTemplate.from_template("{question}"),
]
prompt = ChatPromptTemplate.from_messages(messages)
condese_propmt_template = """任务: 给一段对话和一个后续问题,将后续问题改写成一个独立的问题。确保问题是完整的,没有模糊的指代。
----------------
聊天记录:
{chat_history}
----------------
后续问题:{question}
----------------
改写后的独立、完整的问题:"""
new_question_prompt = PromptTemplate.from_template(condese_propmt_template)
chatglm = ChatGLM()
chatglm.history = chat_history
knowledge_chain = ChatVectorDBChain.from_llm(
llm=chatglm,
vectorstore=vector_store,
qa_prompt=prompt,
condense_question_prompt=new_question_prompt,
)
knowledge_chain.return_source_documents = True
knowledge_chain.top_k_docs_for_context = 10
result = knowledge_chain({"question": query, "chat_history": chat_history})
return result, chatglm.history
if __name__ == "__main__":
filepath = input("Input your local knowledge file path 请输入本地知识文件路径:")
vector_store = init_knowledge_vector_store(filepath)
history = []
while True:
query = input("Input your question 请输入问题:")
resp, history = get_wiki_agent_answer(query=query,
vector_store=vector_store,
chat_history=history)
print(resp)