ngcanh commited on
Commit
304b860
·
verified ·
1 Parent(s): 417e55b

Delete app1.py

Browse files
Files changed (1) hide show
  1. app1.py +0 -160
app1.py DELETED
@@ -1,160 +0,0 @@
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
- # Retrieve relevant documents using similarity search
89
- retrieved_docs = vectorstore.similarity_search(query, k=3)
90
-
91
- # Prepare context for LLaMA
92
- if retrieved_docs:
93
- context = "\n".join([doc.page_content for doc in retrieved_docs])
94
- else:
95
- context = ""
96
-
97
- # Append new interaction to memory
98
- memory.chat_memory.add_user_message(query)
99
-
100
- # Retrieve past interactions for context
101
- past_interactions = memory.load_memory_variables({})[memory.memory_key]
102
- context_with_memory = f"{context}\n\nConversation History:\n{past_interactions}"
103
-
104
- # Debugging: Display context and past interactions
105
- # st.write("Debugging Info:")
106
- # st.write("Context Sent to Model:", context_with_memory)
107
- # st.write("Retrieved Documents:", [doc.page_content for doc in retrieved_docs])
108
- # st.write("Past Interactions:", past_interactions)
109
-
110
- # Generate response using LLaMA
111
- messages = [
112
- {"role": "user", "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. Hãy trả lời chuyên nghiệp, chính xác. Tất cả các thông tin cung cấp đều trong phạm vi MBAL. Mời khách hàng đăng ký để nhận tư vấn kỹ hơn trên https://www.mbageas.life/{context_with_memory} Câu hỏi: {query} Trả lời:"}
113
- ]
114
-
115
- # Get the response from the client
116
- response_content = client.chat_completion(messages=messages, max_tokens=1024, stream=False)
117
-
118
- # Process the response content
119
- response = response_content.choices[0].message.content.split("Answer:")[-1].strip()
120
-
121
- # # If the response is empty or very short, or if no relevant documents were found, use the LLM's default knowledge
122
- # if not context or len(response.split()) < 35 or not retrieved_docs:
123
- # messages = [{"role": "user", "content": query}]
124
- # response_content = client.chat_completion(messages=messages, max_tokens=1024, stream=False)
125
- # response = response_content.choices[0].message.content
126
-
127
- # # Append the response to memory
128
- # memory.chat_memory.add_ai_message(response)
129
-
130
- # return response
131
-
132
- def process_feedback(query, response, feedback):
133
- # st.write(f"Feedback received: {'👍' if feedback else '👎'} for query: {query}")
134
- if feedback:
135
- # If thumbs up, store the response in memory buffer
136
- memory.chat_memory.add_ai_message(response)
137
- else:
138
- # If thumbs down, remove the response from memory buffer and regenerate the response
139
- # memory.chat_memory.messages = [msg for msg in memory.chat_memory.messages if msg.get("content") != response]
140
- new_query=f"{query}. Give better response"
141
- new_response = rag_query(new_query)
142
- st.markdown(new_response)
143
- memory.chat_memory.add_ai_message(new_response)
144
-
145
- # Streamlit interface
146
-
147
- st.title("Chào mừng bạn đã đến với MBAL Chatbot")
148
- st.markdown("***")
149
- st.info('''
150
- 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''')
151
-
152
- col1, col2 = st.columns(2)
153
-
154
- with col1:
155
- chat = st.button("Chat")
156
- if chat:
157
- st.switch_page("pages/chatbot.py")
158
-
159
-
160
- st.markdown("<div style='text-align:center;'></div>", unsafe_allow_html=True)