Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
__import__('pysqlite3')
|
2 |
+
import sys
|
3 |
+
sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
|
4 |
+
|
5 |
+
import streamlit as st
|
6 |
+
from huggingface_hub import InferenceClient
|
7 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, ServiceContext, PromptTemplate
|
8 |
+
from llama_index.vector_stores.chroma import ChromaVectorStore
|
9 |
+
from llama_index.core import StorageContext
|
10 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
11 |
+
from langchain.text_splitter import CharacterTextSplitter
|
12 |
+
from langchain.vectorstores import Chroma
|
13 |
+
import chromadb
|
14 |
+
from langchain.memory import ConversationBufferMemory
|
15 |
+
import pandas as pd
|
16 |
+
from langchain.schema import Document
|
17 |
+
|
18 |
+
|
19 |
+
# Set page config
|
20 |
+
st.set_page_config(page_title="MBAL Chatbot", page_icon="🛡️", layout="wide")
|
21 |
+
|
22 |
+
# Set your Hugging Face token here
|
23 |
+
|
24 |
+
HF_TOKEN = st.secrets["HF_TOKEN"]
|
25 |
+
|
26 |
+
@st.cache_resource
|
27 |
+
def init_chroma():
|
28 |
+
persist_directory = "chroma_db"
|
29 |
+
chroma_client = chromadb.PersistentClient(path=persist_directory)
|
30 |
+
chroma_collection = chroma_client.get_or_create_collection("my_collection")
|
31 |
+
return chroma_client, chroma_collection
|
32 |
+
|
33 |
+
@st.cache_resource
|
34 |
+
def init_vectorstore():
|
35 |
+
persist_directory = "chroma_db"
|
36 |
+
embeddings = HuggingFaceEmbeddings()
|
37 |
+
vectorstore = Chroma(persist_directory=persist_directory, embedding_function=embeddings, collection_name="my_collection")
|
38 |
+
return vectorstore
|
39 |
+
@st.cache_resource
|
40 |
+
def setup_vector():
|
41 |
+
# Đọc dữ liệu từ file Excel
|
42 |
+
df = pd.read_excel("chunk_metadata_template.xlsx")
|
43 |
+
chunks = []
|
44 |
+
|
45 |
+
# Tạo danh sách các Document có metadata
|
46 |
+
for _, row in df.iterrows():
|
47 |
+
chunk_with_metadata = Document(
|
48 |
+
page_content=row['page_content'],
|
49 |
+
metadata={
|
50 |
+
'chunk_id': row['chunk_id'],
|
51 |
+
'document_title': row['document_title'],
|
52 |
+
'topic': row['topic'],
|
53 |
+
'access': row['access']
|
54 |
+
}
|
55 |
+
)
|
56 |
+
chunks.append(chunk_with_metadata)
|
57 |
+
|
58 |
+
# Khởi tạo embedding
|
59 |
+
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
|
60 |
+
|
61 |
+
# Khởi tạo hoặc ghi vào vectorstore đã tồn tại
|
62 |
+
persist_directory = "chroma_db"
|
63 |
+
collection_name = "my_collection"
|
64 |
+
|
65 |
+
# Tạo vectorstore từ dữ liệu và ghi vào Chroma
|
66 |
+
vectorstore = Chroma.from_documents(
|
67 |
+
documents=chunks,
|
68 |
+
embedding=embeddings,
|
69 |
+
persist_directory=persist_directory,
|
70 |
+
collection_name=collection_name
|
71 |
+
)
|
72 |
+
|
73 |
+
# Ghi xuống đĩa để đảm bảo dữ liệu được lưu
|
74 |
+
vectorstore.persist()
|
75 |
+
|
76 |
+
return vectorstore
|
77 |
+
|
78 |
+
# Initialize components
|
79 |
+
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3", token=HF_TOKEN)
|
80 |
+
chroma_client, chroma_collection = init_chroma()
|
81 |
+
init_vectorstore()
|
82 |
+
vectorstore = setup_vector()
|
83 |
+
|
84 |
+
# Initialize memory buffer
|
85 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
86 |
+
|
87 |
+
def rag_query(query):
|
88 |
+
# Lấy tài liệu liên quan
|
89 |
+
retrieved_docs = vectorstore.similarity_search(query, k=5)
|
90 |
+
context = "\n".join([doc.page_content for doc in retrieved_docs]) if retrieved_docs else ""
|
91 |
+
|
92 |
+
# Lấy tương tác cũ
|
93 |
+
past_interactions = memory.load_memory_variables({})[memory.memory_key]
|
94 |
+
context_with_memory = f"{context}\n\nConversation History:\n{past_interactions}"
|
95 |
+
|
96 |
+
# Chuẩn bị prompt
|
97 |
+
messages = [
|
98 |
+
{
|
99 |
+
"role": "user",
|
100 |
+
"content": f"""Bạn là một chuyên viên tư vấn cho khách hàng về sản phẩm bảo hiểm của công ty MB Ageas Life tại Việt Nam.
|
101 |
+
Hãy trả lời chuyên nghiệp, chính xác, cung cấp thông tin trước rồi hỏi câu tiếp theo.
|
102 |
+
Tất cả các thông tin cung cấp đều trong phạm vi MBAL.
|
103 |
+
Khi có đủ thông tin khách hàng thì mới mời khách hàng đăng ký để nhận tư vấn trên https://www.mbageas.life/
|
104 |
+
|
105 |
+
{context_with_memory}
|
106 |
+
Câu hỏi: {query}
|
107 |
+
Trả lời:"""
|
108 |
+
}
|
109 |
+
]
|
110 |
+
|
111 |
+
response_content = client.chat_completion(messages=messages, max_tokens=1024, stream=False)
|
112 |
+
response = response_content.choices[0].message.content.split("Answer:")[-1].strip()
|
113 |
+
return response
|
114 |
+
|
115 |
+
|
116 |
+
def process_feedback(query, response, feedback):
|
117 |
+
# st.write(f"Feedback received: {'👍' if feedback else '👎'} for query: {query}")
|
118 |
+
if feedback:
|
119 |
+
# If thumbs up, store the response in memory buffer
|
120 |
+
memory.chat_memory.add_ai_message(response)
|
121 |
+
else:
|
122 |
+
# If thumbs down, remove the response from memory buffer and regenerate the response
|
123 |
+
# memory.chat_memory.messages = [msg for msg in memory.chat_memory.messages if msg.get("content") != response]
|
124 |
+
new_query=f"{query}. Give better response"
|
125 |
+
new_response = rag_query(new_query)
|
126 |
+
st.markdown(new_response)
|
127 |
+
memory.chat_memory.add_ai_message(new_response)
|
128 |
+
|
129 |
+
# Streamlit interface
|
130 |
+
|
131 |
+
st.title("Chào mừng bạn đã đến với MBAL Chatbot")
|
132 |
+
st.markdown("***")
|
133 |
+
st.info('''
|
134 |
+
Tôi sẽ giải đáp các thắc mắc của bạn liên quan đến các sản phẩm bảo hiểm nhân thọ của MB Ageas Life''')
|
135 |
+
|
136 |
+
col1, col2 = st.columns(2)
|
137 |
+
|
138 |
+
with col1:
|
139 |
+
chat = st.button("Chat")
|
140 |
+
if chat:
|
141 |
+
st.switch_page("pages/chatbot.py")
|
142 |
+
|
143 |
+
|
144 |
+
st.markdown("<div style='text-align:center;'></div>", unsafe_allow_html=True)
|