-
Notifications
You must be signed in to change notification settings - Fork 1
/
Langchain-zhipu.py
77 lines (57 loc) · 2.41 KB
/
Langchain-zhipu.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
71
72
73
74
75
76
77
from langchain.text_splitter import CharacterTextSplitter
from ChatGLM3 import ChatGLM3
from langchain.document_loaders import UnstructuredFileLoader,DirectoryLoader
from langchain.vectorstores import FAISS
from langchain_community.document_loaders import TextLoader
from transformers import AutoTokenizer,AutoModel
from langchain.embeddings import HuggingFaceBgeEmbeddings
from langchain.chains import RetrievalQA
# def Pipei(query):
# # 根据提问匹配上下文
#
# docs = store.similarity_search(query)
#
# context = [doc.page_content for doc in docs]
# # print(type(context))
# context = str(context)
# print(context)
# response, history = model.chat(tokenizer, context, history=[])
# print(response)
model_path = "E:\ChatGLM3-6B\model"
llm = ChatGLM3()
llm.load_model(model_name_or_path=model_path)
# tokenizer = AutoTokenizer.from_pretrained("E:\ChatGLM3-6B\model", trust_remote_code=True)
# model = AutoModel.from_pretrained("E:\ChatGLM3-6B\model",trust_remote_code=True).cuda()
# model=model.eval()
# llm=HuggingFacePipeline(model=model,tokenizer=tokenizer)
filepath="E:\Langchain-CHAT\Langchain-Learning\data\\test.txt"
# 加载文件
#loader=UnstructuredFileLoader(filepath)
loader=TextLoader(filepath,encoding="utf-8")
docs=loader.load()
print(docs)
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>文本分割<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
#文件分割
text_splitter=CharacterTextSplitter(chunk_size=500,chunk_overlap=200)
docs=text_splitter.split_documents(docs)
print(docs)
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>文本embedding<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
#构建向量数据库
model_name = "E:\ChatGLM3-6B\embedding\\bge-large-zh"
model_kwargs = {'device': 'cuda'}
encode_kwargs = {'normalize_embeddings': True} # set True to compute cosine similarity
embedding = HuggingFaceBgeEmbeddings(
model_name=model_name,
model_kwargs=model_kwargs,
encode_kwargs=encode_kwargs,
)
store=FAISS.from_documents(docs,embedding)
print(store)
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>开始对话<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
query = "梅西会来中国吗?"
#Pipei(query)
qa=RetrievalQA.from_chain_type(llm,chain_type="stuff",retriever=store.as_retriever())
response=qa.run(query)
# demo=gr.ChatInterface(chat)
#
# demo.launch(inbrowser=True)