Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,7 @@ from langchain_groq import ChatGroq
|
|
12 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
13 |
from langchain_huggingface import HuggingFaceEmbeddings
|
14 |
from langchain_community.vectorstores import Chroma
|
15 |
-
from
|
16 |
from langchain.chains import RetrievalQA
|
17 |
from langchain_community.document_loaders import WebBaseLoader, TextLoader
|
18 |
from langchain.prompts import PromptTemplate
|
@@ -44,6 +44,7 @@ def load_documents(sources):
|
|
44 |
return documents
|
45 |
|
46 |
sources = [
|
|
|
47 |
"TaxQADataSet_kctax.txt",
|
48 |
"TaxQADataSet_chutax.txt",
|
49 |
"HouseTaxAct1130103.txt",
|
@@ -53,19 +54,7 @@ sources = [
|
|
53 |
"AmusementTaxAct960523.txt",
|
54 |
"StampTaxAct910515.txt",
|
55 |
"DeedTaxAct990505.txt",
|
56 |
-
"ProgressiveHouseTaxRates1130701.txt"
|
57 |
-
"https://www.tax.ntpc.gov.tw/lp-2158-1-1-20.html",
|
58 |
-
"https://www.tax.ntpc.gov.tw/lp-2158-1-2-20.html",
|
59 |
-
"https://www.tax.ntpc.gov.tw/lp-2158-1-3-20.html",
|
60 |
-
"https://www.tax.ntpc.gov.tw/lp-2158-1-4-20.html",
|
61 |
-
"https://www.tax.ntpc.gov.tw/lp-2158-1-5-20.html",
|
62 |
-
"https://www.tax.ntpc.gov.tw/lp-2158-1-6-20.html",
|
63 |
-
"https://www.tax.ntpc.gov.tw/lp-2158-1-7-20.html",
|
64 |
-
"https://www.tax.ntpc.gov.tw/lp-2158-1-8-20.html",
|
65 |
-
"https://www.tax.ntpc.gov.tw/lp-2158-1-9-20.html",
|
66 |
-
"https://www.tax.ntpc.gov.tw/lp-2158-1-10-20.html",
|
67 |
-
"https://www.tax.ntpc.gov.tw/lp-2158-1-11-20.html",
|
68 |
-
"https://www.tax.ntpc.gov.tw/lp-2158-1-12-20.html"
|
69 |
]
|
70 |
|
71 |
documents = load_documents(sources)
|
@@ -90,7 +79,7 @@ print(f"成功建立 Chroma 向量資料庫")
|
|
90 |
retriever = vectorstore.as_retriever()
|
91 |
|
92 |
template = """Let's work this out in a step by step way to be sure we have the right answer. Must reply to me in Taiwanese Traditional Chinese.
|
93 |
-
|
94 |
如果檢索到的多個上下文之間存在聯繫,請整合這些訊息以提供全面的回答,但要避免過度推斷。
|
95 |
如果檢索到的上下文不包含足夠回答問題的訊息,請誠實的說明,不要試圖編造答案。
|
96 |
|
@@ -118,16 +107,16 @@ print(f"成功建立 RAG Chain")
|
|
118 |
def generate_insight_questions(answer, api_key):
|
119 |
llm = initialize_llm(api_key)
|
120 |
prompt = f"""
|
121 |
-
|
122 |
|
123 |
-
|
124 |
|
125 |
-
請提供3
|
126 |
1. 與原始回答緊密相關
|
127 |
2. 能夠引導更深入的討論
|
128 |
3. 涵蓋不同的方面或角度
|
129 |
|
130 |
-
請直接列出這3
|
131 |
"""
|
132 |
try:
|
133 |
response = llm.invoke(prompt)
|
@@ -135,12 +124,9 @@ def generate_insight_questions(answer, api_key):
|
|
135 |
questions = response.content.split('\n')
|
136 |
else:
|
137 |
questions = str(response).split('\n')
|
138 |
-
|
139 |
-
# 確保我們有至少3個問題
|
140 |
while len(questions) < 3:
|
141 |
questions.append("需要更多資訊嗎?")
|
142 |
-
|
143 |
-
return questions[:3] # 只返回前3個問題
|
144 |
except Exception as e:
|
145 |
print(f"Error generating insight questions: {str(e)}")
|
146 |
return ["需要更多資訊嗎?", "有其他問題嗎?", "還有什麼想了解的嗎?"]
|
@@ -151,14 +137,9 @@ def answer_question(query, api_key):
|
|
151 |
chain = create_chain(llm)
|
152 |
result = chain({"query": query})
|
153 |
answer = result["result"]
|
154 |
-
|
155 |
insight_questions = generate_insight_questions(answer, api_key)
|
156 |
-
|
157 |
-
# 確保有三個問題,如果不足則添加默認問題
|
158 |
while len(insight_questions) < 3:
|
159 |
insight_questions.append("需要更多資訊嗎?")
|
160 |
-
|
161 |
-
# 分開返回答案和洞見問題
|
162 |
return answer, insight_questions[:3]
|
163 |
except Exception as e:
|
164 |
return f"抱歉,處理您的問題時發生錯誤:{str(e)}", []
|
@@ -166,13 +147,9 @@ def answer_question(query, api_key):
|
|
166 |
def handle_interaction(query, api_key, state):
|
167 |
if state is None:
|
168 |
state = {"history": []}
|
169 |
-
|
170 |
answer, insight_questions = answer_question(query, api_key)
|
171 |
-
|
172 |
state["history"].append((query, answer))
|
173 |
-
|
174 |
insight_questions = [q if q.strip() else "需要更多資訊" for q in insight_questions]
|
175 |
-
|
176 |
return answer, insight_questions[0], insight_questions[1], insight_questions[2], state, query
|
177 |
|
178 |
custom_css = """
|
@@ -274,3 +251,4 @@ with gr.Blocks(theme=gr.themes.Monochrome(), css=custom_css) as iface:
|
|
274 |
|
275 |
if __name__ == "__main__":
|
276 |
iface.launch(share=True, debug=True)
|
|
|
|
12 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
13 |
from langchain_huggingface import HuggingFaceEmbeddings
|
14 |
from langchain_community.vectorstores import Chroma
|
15 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
16 |
from langchain.chains import RetrievalQA
|
17 |
from langchain_community.document_loaders import WebBaseLoader, TextLoader
|
18 |
from langchain.prompts import PromptTemplate
|
|
|
44 |
return documents
|
45 |
|
46 |
sources = [
|
47 |
+
"TaxQADataSet_ntpc.txt",
|
48 |
"TaxQADataSet_kctax.txt",
|
49 |
"TaxQADataSet_chutax.txt",
|
50 |
"HouseTaxAct1130103.txt",
|
|
|
54 |
"AmusementTaxAct960523.txt",
|
55 |
"StampTaxAct910515.txt",
|
56 |
"DeedTaxAct990505.txt",
|
57 |
+
"ProgressiveHouseTaxRates1130701.txt"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
]
|
59 |
|
60 |
documents = load_documents(sources)
|
|
|
79 |
retriever = vectorstore.as_retriever()
|
80 |
|
81 |
template = """Let's work this out in a step by step way to be sure we have the right answer. Must reply to me in Taiwanese Traditional Chinese.
|
82 |
+
在回答之前,請仔細分析檢索到的上下文,確保你的回答準確完整反映了上下文中的訊息,而不是依賴先前的知識,但在回應答案中不要提到是根據上下文回答。
|
83 |
如果檢索到的多個上下文之間存在聯繫,請整合這些訊息以提供全面的回答,但要避免過度推斷。
|
84 |
如果檢索到的上下文不包含足夠回答問題的訊息,請誠實的說明,不要試圖編造答案。
|
85 |
|
|
|
107 |
def generate_insight_questions(answer, api_key):
|
108 |
llm = initialize_llm(api_key)
|
109 |
prompt = f"""
|
110 |
+
根據以下回答,生成3個相關的洞見問題:
|
111 |
|
112 |
+
回答: {answer}
|
113 |
|
114 |
+
請提供3個簡短但有深度的問題,這些問題應該:
|
115 |
1. 與原始回答緊密相關
|
116 |
2. 能夠引導更深入的討論
|
117 |
3. 涵蓋不同的方面或角度
|
118 |
|
119 |
+
請直接列出這3個問題,每個問題一行,不要添加編號或其他文字。
|
120 |
"""
|
121 |
try:
|
122 |
response = llm.invoke(prompt)
|
|
|
124 |
questions = response.content.split('\n')
|
125 |
else:
|
126 |
questions = str(response).split('\n')
|
|
|
|
|
127 |
while len(questions) < 3:
|
128 |
questions.append("需要更多資訊嗎?")
|
129 |
+
return questions[:3]
|
|
|
130 |
except Exception as e:
|
131 |
print(f"Error generating insight questions: {str(e)}")
|
132 |
return ["需要更多資訊嗎?", "有其他問題嗎?", "還有什麼想了解的嗎?"]
|
|
|
137 |
chain = create_chain(llm)
|
138 |
result = chain({"query": query})
|
139 |
answer = result["result"]
|
|
|
140 |
insight_questions = generate_insight_questions(answer, api_key)
|
|
|
|
|
141 |
while len(insight_questions) < 3:
|
142 |
insight_questions.append("需要更多資訊嗎?")
|
|
|
|
|
143 |
return answer, insight_questions[:3]
|
144 |
except Exception as e:
|
145 |
return f"抱歉,處理您的問題時發生錯誤:{str(e)}", []
|
|
|
147 |
def handle_interaction(query, api_key, state):
|
148 |
if state is None:
|
149 |
state = {"history": []}
|
|
|
150 |
answer, insight_questions = answer_question(query, api_key)
|
|
|
151 |
state["history"].append((query, answer))
|
|
|
152 |
insight_questions = [q if q.strip() else "需要更多資訊" for q in insight_questions]
|
|
|
153 |
return answer, insight_questions[0], insight_questions[1], insight_questions[2], state, query
|
154 |
|
155 |
custom_css = """
|
|
|
251 |
|
252 |
if __name__ == "__main__":
|
253 |
iface.launch(share=True, debug=True)
|
254 |
+
|