From c062462277cdfe230a4c44b6311f9b96e48ab747 Mon Sep 17 00:00:00 2001 From: WisleyWang <903953316@Qq.com> Date: Fri, 10 Oct 2025 20:12:11 +0800 Subject: [PATCH 1/6] fix(app_history):fix history type --- app.py | 3 ++- app_local_model.py | 36 +++++++++++++++++++++--------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/app.py b/app.py index c9f9eb2..a10b640 100644 --- a/app.py +++ b/app.py @@ -551,7 +551,8 @@ def predict(question, ) with gr.Column(scale=4): with gr.Row(): - chatbot = gr.Chatbot(label='TrustRAG Application', height=650) + chatbot = gr.Chatbot([{"role": "assistant", "content": "Hi~ I am your assistant. I'm glad to serve you."}], + label='TrustRAG Application', height=650, type="messages") with gr.Row(): message = gr.Textbox(label='Please enter a question') with gr.Row(): diff --git a/app_local_model.py b/app_local_model.py index 12320ca..f07c8e9 100644 --- a/app_local_model.py +++ b/app_local_model.py @@ -19,6 +19,7 @@ from datetime import datetime import pytz from tqdm import tqdm +import re from trustrag.modules.document.common_parser import CommonParser from trustrag.modules.document.chunk import TextChunker @@ -354,6 +355,15 @@ def on_file_select(files_df, chunk_size, evt: gr.SelectData): def clear_session(): return '', None +def remove_think_blocks(s: str) -> str: + # 删除 之间的所有内容(非贪婪,跨行,多处) + return re.sub( + r"]*>.*?", # 允许 带属性 + "", + s, + flags=re.IGNORECASE | re.DOTALL + ).strip() + def shorten_label(text, max_length=10): if len(text) > 2 * max_length: @@ -362,8 +372,6 @@ def shorten_label(text, max_length=10): def predict(question, - large_language_model, - embedding_model, top_k, use_web, use_pattern, @@ -384,15 +392,13 @@ def predict(question, loguru.logger.info('Only LLM Mode:') # result = application.llm.chat(query=question, web_content=web_content) - system_prompt = "You are a helpful assistant." - user_input = [ - {"role": "user", "content": question} - ] # 调用 chat 方法进行对话 - result, total_tokens = application.llm.chat(system=system_prompt, history=user_input) - history.append((question, result)) + result, total_tokens = application.llm.chat(prompt=question, history=history,llm_only=True) + user_input ={"role": "user", "content": question} + history.append(user_input) + history.append({"role":"assistant","content":result}) search_text += web_content - + loguru.logger.info('Only LLM result:',result) # Return empty judge results for Q&A mode checkboxes = [] for item in range(5): @@ -408,8 +414,11 @@ def predict(question, question=question, top_k=top_k, ) + # Filfer thinking + response = remove_think_blocks(response) loguru.logger.info(f"User Question: {response}") - history.append((question, response)) + history.append({"role": "user", "content": question}) + history.append({"role":"assistant","content":response}) # Format search results for idx, source in enumerate(contents): sep = f'----------【搜索结果{idx + 1}:】---------------\n' @@ -611,7 +620,8 @@ def predict(question, ) with gr.Column(scale=4): with gr.Row(): - chatbot = gr.Chatbot(label='TrustRAG Application', height=650) + chatbot = gr.Chatbot([{"role": "assistant", "content": "Hi~ I am your assistant. I'm glad to serve you."}], + label='TrustRAG Application', height=650, type="messages") with gr.Row(): message = gr.Textbox(label='Please enter a question') with gr.Row(): @@ -635,8 +645,6 @@ def predict(question, send.click(predict, inputs=[ message, - large_language_model, - embedding_model, top_k, use_web, use_pattern, @@ -653,8 +661,6 @@ def predict(question, message.submit(predict, inputs=[ message, - large_language_model, - embedding_model, top_k, use_web, use_pattern, From 1db0193b5ba93017face6b2000e90aac53da0ce6 Mon Sep 17 00:00:00 2001 From: WisleyWang <903953316@Qq.com> Date: Fri, 10 Oct 2025 20:13:57 +0800 Subject: [PATCH 2/6] fix(search):fix search hight --- app_local_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app_local_model.py b/app_local_model.py index f07c8e9..d1e9192 100644 --- a/app_local_model.py +++ b/app_local_model.py @@ -639,7 +639,7 @@ def predict(question, # gr.Markdown("Document Judge") checkbox_outputs = [gr.Checkbox(visible=False, interactive=True) for _ in range(5)] with gr.Row(): - search = gr.Textbox(label='Claim Attribute') + search = gr.Textbox(label='Claim Attribute', lines=6) # submit send.click(predict, From 96a64c7a8193b05dd7c4aa31b9fd51c2a3a79f33 Mon Sep 17 00:00:00 2001 From: WisleyWang <903953316@Qq.com> Date: Fri, 10 Oct 2025 20:18:41 +0800 Subject: [PATCH 3/6] fix(response):fix only LLM Qwen3 chat respose --- trustrag/modules/generator/llm.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/trustrag/modules/generator/llm.py b/trustrag/modules/generator/llm.py index 0e946a5..47834dd 100644 --- a/trustrag/modules/generator/llm.py +++ b/trustrag/modules/generator/llm.py @@ -343,14 +343,19 @@ def chat(self, prompt: str, history: List = [], content: str = '', llm_only: boo generated_ids = self.model.generate( **model_inputs, max_new_tokens=32768, # 支持更大的生成长度 - do_sample=False, - top_k=10 + # do_sample=False, + # top_k=10 ) # 提取生成的部分 output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist() - response = self.tokenizer.decode(output_ids, skip_special_tokens=True) + try: + # rindex finding 151668 () + index = len(output_ids) - output_ids[::-1].index(151668) + except ValueError: + index = 0 + response = self.tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n") return response, history def load_model(self): From cf6893b1f0d4877e8cb052a780309862be93cb18 Mon Sep 17 00:00:00 2001 From: WisleyWang <903953316@Qq.com> Date: Fri, 10 Oct 2025 20:29:55 +0800 Subject: [PATCH 4/6] fix(app history):fix app.py history --- app.py | 11 +++++------ app_local_model.py | 7 ++----- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/app.py b/app.py index a10b640..88e48a6 100644 --- a/app.py +++ b/app.py @@ -320,18 +320,17 @@ def predict(question, for search_result in results: web_content += search_result['title'] + " " + search_result['body'] + "\n" search_text = '' + history.append({"role": "user", "content": question}) if use_pattern == 'Only LLM': # Handle model Q&A mode loguru.logger.info('Only LLM Mode:') # result = application.llm.chat(query=question, web_content=web_content) system_prompt = "You are a helpful assistant." - user_input = [ - {"role": "user", "content": question} - ] + # 调用 chat 方法进行对话 - result, total_tokens = application.llm.chat(system=system_prompt, history=user_input) - history.append((question, result)) + result, total_tokens = application.llm.chat(system=system_prompt, history=history) + history.append({"role":"system", "content":result}) search_text += web_content # Return empty judge results for Q&A mode @@ -349,7 +348,7 @@ def predict(question, question=question, top_k=top_k, ) - history.append((question, response)) + history.append({"role":"system", "content":response}) # Format search results for idx, source in enumerate(contents): sep = f'----------【搜索结果{idx + 1}:】---------------\n' diff --git a/app_local_model.py b/app_local_model.py index d1e9192..8e2f33e 100644 --- a/app_local_model.py +++ b/app_local_model.py @@ -387,15 +387,14 @@ def predict(question, for search_result in results: web_content += search_result['title'] + " " + search_result['body'] + "\n" search_text = '' + history.append({"role": "user", "content": question}) + loguru.logger.info(f"User Question: {response}") if use_pattern == 'Only LLM': # Handle model Q&A mode loguru.logger.info('Only LLM Mode:') - # result = application.llm.chat(query=question, web_content=web_content) # 调用 chat 方法进行对话 result, total_tokens = application.llm.chat(prompt=question, history=history,llm_only=True) - user_input ={"role": "user", "content": question} - history.append(user_input) history.append({"role":"assistant","content":result}) search_text += web_content loguru.logger.info('Only LLM result:',result) @@ -416,8 +415,6 @@ def predict(question, ) # Filfer thinking response = remove_think_blocks(response) - loguru.logger.info(f"User Question: {response}") - history.append({"role": "user", "content": question}) history.append({"role":"assistant","content":response}) # Format search results for idx, source in enumerate(contents): From 450c06cb43d686182099fc38364b6a9a6f87b142 Mon Sep 17 00:00:00 2001 From: WisleyWang <903953316@Qq.com> Date: Fri, 10 Oct 2025 20:31:27 +0800 Subject: [PATCH 5/6] fix(app history):fix app.py history --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index 88e48a6..de65d95 100644 --- a/app.py +++ b/app.py @@ -550,7 +550,7 @@ def predict(question, ) with gr.Column(scale=4): with gr.Row(): - chatbot = gr.Chatbot([{"role": "assistant", "content": "Hi~ I am your assistant. I'm glad to serve you."}], + chatbot = gr.Chatbot([{"role": "system", "content": "Hi~ I am your assistant. I'm glad to serve you."}], label='TrustRAG Application', height=650, type="messages") with gr.Row(): message = gr.Textbox(label='Please enter a question') From 7293f46ebbe23250470919f791dcb4ea8b86d69d Mon Sep 17 00:00:00 2001 From: WisleyWang <903953316@Qq.com> Date: Fri, 10 Oct 2025 20:45:13 +0800 Subject: [PATCH 6/6] fix(makefileCI):fix workflows/makefile.yaml python-version:3.12.11 --- .github/workflows/makefile.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/makefile.yml b/.github/workflows/makefile.yml index 9db9dbc..8bb1f6a 100644 --- a/.github/workflows/makefile.yml +++ b/.github/workflows/makefile.yml @@ -17,7 +17,7 @@ jobs: - name: Setup Python version uses: actions/setup-python@v1 with: - python-version: 3.8.18 + python-version: 3.12.11 - name: Install requirements run: make init