-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
171 lines (130 loc) · 5.03 KB
/
app.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: utf-8 -*-
"""RAG-on-PDF-Q-A-using-LLM.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/12VYDwQtvKhaXYjDJ93Dnh3opXkKT1JOW
"""
"""## 2.2. Import các gói thư viện cần thiết"""
import chainlit as cl
import torch
from chainlit.types import AskFileResponse
from transformers import BitsAndBytesConfig
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
from langchain_huggingface.llms import HuggingFacePipeline
from langchain.memory import ConversationBufferMemory
from langchain_community.chat_message_histories import ChatMessageHistory
from langchain.chains import ConversationalRetrievalChain
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_chroma import Chroma
from langchain_community.document_loaders import PyPDFLoader, TextLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
from langchain import hub
"""## 2.3. Cài đặt lại các hàm và instance ở file trước"""
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
embedding = HuggingFaceEmbeddings()
"""## 2.4. Xây dựng hàm xử lý file input đầu vào"""
def process_file(file: AskFileResponse):
if file.type == "text/plain":
Loader = TextLoader
elif file.type == "application/pdf":
Loader = PyPDFLoader
loader = Loader(file.path)
documents = loader.load()
docs = text_splitter.split_documents(documents)
for i, doc in enumerate(docs):
doc.metadata["source"] = f"source_{i}"
return docs
"""## 2.5. Xây dựng hàm khởi tạo Chroma database"""
def get_vector_db(file: AskFileResponse):
docs = process_file(file)
cl.user_session.set("docs", docs)
vector_db = Chroma.from_documents(documents=docs, embedding=embedding)
return vector_db
"""## 2.6. Khởi tạo mô hình ngôn ngữ lớn"""
def get_huggingface_llm(model_name: str = "lmsys/vicuna-7b-v1.5", max_new_token: int = 512):
nf4_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_use_double_quant=True,
bnb_4bit_compute_dtype=torch.bfloat16
)
model = AutoModelForCausalLM.from_pretrained(
model_name,
quantization_config=nf4_config,
low_cpu_mem_usage=True
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
model_pipeline = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
max_new_tokens=max_new_token,
pad_token_id=tokenizer.eos_token_id,
device_map="auto"
)
llm = HuggingFacePipeline(pipeline=model_pipeline)
return llm
LLM = get_huggingface_llm()
"""## 2.7. Khởi tạo welcome message"""
welcome_message = """Welcome to the PDF QA! To get started:
1. Upload a PDF or text file
2. Ask a question about the file
"""
"""## 2.8. Khởi tạo hàm on_chat_start"""
@cl.on_chat_start
async def on_chat_start():
files = None
while files is None:
files = await cl.AskFileMessage(
content=welcome_message,
accept=["text/plain", "application/pdf"],
max_size_mb=20,
timeout=180
).send()
file = files[0]
msg = cl.Message(content=f"Processing '{file.name}'...",
disable_feedback=True)
await msg.send()
vector_db = await cl.make_async(get_vector_db)(file)
message_history = ChatMessageHistory()
memory = ConversationBufferMemory(
memory_key="chat_history",
output_key="answer",
chat_memory=message_history,
return_messages=True
)
retriever = vector_db.as_retriever(search_type="mmr",
search_kwargs={'k': 3})
chain = ConversationalRetrievalChain.from_llm(
llm=LLM,
chain_type="stuff",
retriever=retriever,
memory=memory,
return_source_documents=True
)
msg.content = f"'{file.name}' processed. You can now ask questions!"
await msg.update()
cl.user_session.set("chain", chain)
"""## 2.9. Khởi tạo hàm on_message"""
@cl.on_message
async def on_message(message: cl.Message):
chain = cl.user_session.get("chain")
cb = cl.AsyncLangchainCallbackHandler()
res = await chain.ainvoke(message.content, callbacks=[cb])
answer = res["answer"]
source_documents = res["source_documents"]
text_elements = []
if source_documents:
for source_idx, source_doc in enumerate(source_documents):
source_name = f"source_{source_idx}"
text_elements.append(
cl.Text(content=source_doc.page_content, name=source_name)
)
source_names = [text_el.name for text_el in text_elements]
if source_names:
answer += f"\nSources: {', '.join(source_names)}"
else:
answer += "\nNo sources found"
await cl.Message(content=answer, elements=text_elements).send()