File size: 10,663 Bytes
221b17d
 
 
 
 
 
131ab57
221b17d
 
 
 
 
8e2dc9e
221b17d
 
 
 
 
131ab57
221b17d
 
 
 
 
 
 
131ab57
221b17d
131ab57
221b17d
 
 
 
 
 
 
 
 
 
 
 
131ab57
221b17d
 
 
 
 
131ab57
 
 
 
3331097
 
7d5a1f9
 
d4c6110
 
 
 
 
 
 
 
 
5052ae3
221b17d
 
 
131ab57
221b17d
 
 
 
 
 
 
 
 
8e2dc9e
221b17d
3331097
221b17d
 
8e2dc9e
221b17d
 
 
3331097
221b17d
 
8e2dc9e
221b17d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131ab57
 
221b17d
5052ae3
221b17d
131ab57
221b17d
 
131ab57
221b17d
5052ae3
221b17d
 
 
 
 
 
 
 
131ab57
5052ae3
221b17d
 
131ab57
221b17d
 
 
 
 
 
 
 
 
131ab57
221b17d
 
 
 
131ab57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221b17d
 
 
131ab57
 
221b17d
131ab57
 
221b17d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3331097
 
 
221b17d
 
 
 
3331097
221b17d
3331097
 
221b17d
 
131ab57
 
 
221b17d
 
131ab57
221b17d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2c78b1
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# -*- coding: utf-8 -*-
# 財政部財政資訊中心 江信宗

import os
from dotenv import load_dotenv
load_dotenv()
os.environ["LANGCHAIN_COMMUNITY__USER_AGENT"] = "Taiwan_Tax_Knowledge-base"
from langchain_community.utils import user_agent
from langchain_groq import ChatGroq
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain.chains import RetrievalQA
from langchain_community.document_loaders import WebBaseLoader, TextLoader
from langchain.prompts import PromptTemplate
from langchain.schema import Document
import gradio as gr
import re

def initialize_llm(api_key):
    os.environ["GROQ_API_KEY"] = api_key
    return ChatGroq(
        groq_api_key=api_key,
        model_name='llama-3.1-70b-versatile'
    )
print(f"成功初始化大型語言模型(LLM)")

print(f"開始載入知識檔案", end="")
def load_documents(sources):
    documents = []
    for source in sources:
        try:
            if isinstance(source, str):
                if source.startswith('http'):
                    loader = WebBaseLoader(source)
                else:
                    loader = TextLoader(source)
                documents.extend(loader.load())
            elif isinstance(source, dict):
                documents.append(Document(page_content=source['content'], metadata=source.get('metadata', {})))
            print(".", end="")
        except Exception as e:
            print(f"Error loading source {source}: {str(e)}")
    return documents

sources = [
    "TaxQADataSet_Slim1.txt",
    "TaxQADataSet_Slim2.txt",
    "TaxQADataSet_Slim3.txt",
    "TaxQADataSet_Slim4.txt",
    "TaxQADataSet_Slim5.txt",
    "TaxQADataSet_Slim6.txt",
    "TaxQADataSet_ntpc1.txt",
    "TaxQADataSet_ntpc2.txt",
    "TaxQADataSet_kctax.txt",
    "TaxQADataSet_chutax.txt",
    "HouseTaxAct1130103.txt",
    "VehicleLicenseTaxAct1101230.txt",
    "TaxCollectionAct1101217.txt",
    "LandTaxAct1100623.txt",
    "AmusementTaxAct960523.txt",
    "StampTaxAct910515.txt",
    "DeedTaxAct990505.txt",
    "ProgressiveHouseTaxRates1130701.txt"
]

documents = load_documents(sources)
print(f"\n成功載入 {len(documents)} 個網址或檔案")

text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=512,
    chunk_overlap=50,
    length_function=len,
    separators=["\n\n\n","\n\n", "\n", "。"]
)

split_docs = text_splitter.split_documents(documents)
print(f"分割後的文件數量:{len(split_docs)}")

embeddings = HuggingFaceEmbeddings(model_name="BAAI/bge-m3")
print(f"\n成功初始化嵌入模型")

print(f"開始建立向量資料庫")
vectorstore = Chroma.from_documents(split_docs, embeddings, persist_directory="./Knowledge-base")
print(f"成功建立 Chroma 向量資料庫")

retriever = vectorstore.as_retriever(search_kwargs={"k": 3})

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.
在回答之前,請仔細分析檢索到的上下文,確保你的回答準確完整反映了上下文中的訊息,而不是依賴先前的知識,在回應的答案中不要提到是根據上下文回答。
如果檢索到的多個上下文之間存在聯繫,請整合這些訊息以提供全面的回答,但要避免過度推斷。
如果檢索到的上下文不包含足夠回答問題的訊息,請誠實的說明,不要試圖編造答案。

上下文: {context}

問題: {question}

答案:"""

PROMPT = PromptTemplate(
    template=template, input_variables=["context", "question"]
)
print(f"成功定義 Prompt Template")

def create_chain(llm):
    return RetrievalQA.from_chain_type(
        llm=llm,
        chain_type="stuff",
        retriever=retriever,
        return_source_documents=True,
        chain_type_kwargs={"prompt": PROMPT}
    )
print(f"成功建立 RAG Chain")

def generate_insight_questions(answer, api_key):
    llm = initialize_llm(api_key)
    prompt = f"""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.
    根據以下回答,生成3個相關的洞察問題:

    回答: {answer}

    請提供3個簡短但有深度的問題,這些問題應該符合:
    1. 與原始回答緊密相關
    2. 能夠引導更深入的討論
    3. 涵蓋不同的面向或角度

    請直接列出這3個問題,每個問題一行,不要添加編號或其他文字。
    """
    try:
        response = llm.invoke(prompt)
        if hasattr(response, 'content'):
            questions = response.content.split('\n')
        else:
            questions = str(response).split('\n')
        while len(questions) < 3:
            questions.append("提供更多地方稅資訊")
        return questions[:3]
    except Exception as e:
        print(f"Error generating insight questions: {str(e)}")
        return ["提供更多地方稅資訊", "提供其他地方稅問題", "還想了解什麼地方稅目"]

def answer_question(query, api_key):
    try:
        llm = initialize_llm(api_key)
        chain = create_chain(llm)
        result = chain({"query": query})
        answer = result["result"]
        insight_questions = generate_insight_questions(answer, api_key)
        while len(insight_questions) < 3:
            insight_questions.append("提供更多地方稅資訊")
        return answer, insight_questions[:3]
    except Exception as e:
        return f"抱歉,處理您的問題時發生錯誤:{str(e)}", []

def split_questions(query):
    questions = re.split(r'[?!。 ]', query)
    return [q.strip() for q in questions if q.strip()]

def answer_multiple_questions(query, api_key):
    questions = split_questions(query)
    all_answers = []
    all_insight_questions = []
    for question in questions:
        answer, insight_questions = answer_question(question, api_key)
        if len(questions) > 1:
            all_answers.append(f"【問題】{question}\n答案:{answer}")
        else:
            all_answers.append(answer)
        all_insight_questions.extend(insight_questions)
    if len(questions) > 1:
      combined_answer = "\n\n\n".join(all_answers)
    else:
      combined_answer = "\n".join(all_answers)
    selected_insight_questions = all_insight_questions[:3]
    return combined_answer, selected_insight_questions

def convert_punctuation(text):
    return text.replace('?', '?').replace(',', ',').replace('!', '!').replace(' ', ' ')

def handle_interaction(query, api_key, state):
    if state is None:
        state = {"history": []}
    query = convert_punctuation(query)
    answer, insight_questions = answer_multiple_questions(query, api_key)
    state["history"].append((query, answer))
    while len(insight_questions) < 3:
        insight_questions.append("提供更多地方稅資訊")
    return answer, insight_questions[0], insight_questions[1], insight_questions[2], state, query

custom_css = """
body {
    background-color: #e8f5e9;
}
#answer-box textarea, #query-input textarea {
    font-size: 18px !important;
    background-color: #ffffff;
    border: 1px solid #81c784;
    border-radius: 8px;
}
.center-text {
    text-align: center !important;
    color: #2e7d32 !important;
}
.gradio-container {
    background-color: #c8e6c9 !important;
    border-radius: 15px !important;
    padding: 20px !important;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1) !important;
}
.gr-button {
    color: white !important;
    border: none !important;
    border-radius: 20px !important;
    transition: all 0.3s ease !important;
    font-weight: bold !important;
}
.gr-button:hover {
    transform: translateY(-2px) !important;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2) !important;
}
#submit-btn {
    background-color: #ff4081 !important;
}
#submit-btn:hover {
    background-color: #f50057 !important;
}
.insight-btn {
    background-color: #00bcd4 !important;
}
.insight-btn:hover {
    background-color: #00acc1 !important;
}
.gr-form {
    background-color: #e8f5e9 !important;
    padding: 15px !important;
    border-radius: 10px !important;
}
.api-key-input {
    max-width: 512px !important;
}
"""

with gr.Blocks(theme=gr.themes.Monochrome(), css=custom_css) as iface:
    gr.Markdown("# 地方稅知識庫系統 - 財政部財政資訊中心", elem_classes=["center-text"])
    gr.Markdown("※ RAG-based 系統建置:江信宗,LLM:Llama-3.1-70B,以地方稅極少知識資料示範,僅供參考,準確資訊請依地方稅稽徵機關回覆為準。", elem_classes=["center-text"])
    with gr.Row():
        query_input = gr.Textbox(lines=2, placeholder="請輸入您的問題(支援同時輸入多個問題)......", label="輸入您的問題,系統將基於學習到的知識資料提供相關答案。", elem_id="query-input")
        api_key_input = gr.Textbox(type="password", placeholder="請輸入您的 API Key", label="API authentication key for large language models", elem_classes="api-key-input")
    answer_output = gr.Textbox(lines=6, label="答案:", elem_id="answer-box")
    with gr.Row():
        insight_q1 = gr.Button("洞察問題 1", visible=False, elem_classes=["insight-btn"])
        insight_q2 = gr.Button("洞察問題 2", visible=False, elem_classes=["insight-btn"])
        insight_q3 = gr.Button("洞察問題 3", visible=False, elem_classes=["insight-btn"])
    state = gr.State()
    current_question = gr.Textbox(lines=2, label="當前問題", visible=False)
    submit_btn = gr.Button("傳送", elem_id="submit-btn")
    def update_ui(answer, q1, q2, q3, state, current_q):
        return [
            answer,
            gr.update(value=q1, visible=bool(q1)),
            gr.update(value=q2, visible=bool(q2)),
            gr.update(value=q3, visible=bool(q3)),
            state,
            current_q
        ]
    submit_btn.click(
        fn=handle_interaction,
        inputs=[query_input, api_key_input, state],
        outputs=[answer_output, insight_q1, insight_q2, insight_q3, state, current_question]
    ).then(
        fn=update_ui,
        inputs=[answer_output, insight_q1, insight_q2, insight_q3, state, current_question],
        outputs=[answer_output, insight_q1, insight_q2, insight_q3, state, current_question]
    )
    for btn in [insight_q1, insight_q2, insight_q3]:
        btn.click(
            lambda x: x,
            inputs=[btn],
            outputs=[query_input]
        )

if __name__ == "__main__":
    iface.launch(share=True, debug=True)