今天突然想到可以在網頁上加一個前置詞的功能,就不用每次都要打要叫他幹麻了。效果如下
主要提供了中翻英、英翻中、還有自定義。要修改的程式 diff 如下
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 |
diff --git a/webui_pages/dialogue/dialogue.py b/webui_pages/dialogue/dialogue.py index 05c2a4f..0c6c650 100644 --- a/webui_pages/dialogue/dialogue.py +++ b/webui_pages/dialogue/dialogue.py @@ -7,6 +7,11 @@ import os from configs import LLM_MODEL, TEMPERATURE from server.utils import get_model_worker_config from typing import List, Dict +from opencc import OpenCC +cc = OpenCC('s2t') +title="" +prefix="" + chat_box = ChatBox( assistant_avatar=os.path.join( @@ -38,6 +43,9 @@ def get_messages_history(history_len: int, content_in_expander: bool = False) -> def dialogue_page(api: ApiRequest): chat_box.init_session() + global prefix + global title + with st.sidebar: # TODO: ??模型与???定 @@ -57,7 +65,7 @@ def dialogue_page(api: ApiRequest): "搜索引擎?答", "自定?Agent?答", ], - index=1, + index=0, on_change=on_mode_change, key="dialogue_mode", ) @@ -142,11 +150,22 @@ def dialogue_page(api: ApiRequest): if dialogue_mode == "LLM ??": chat_box.ai_say("正在思考...") text = "" + if prefix == "中翻英": + prefix_text="請把下面的中文翻譯成英文" + elif prefix == "英翻中": + prefix_text="請把下面的英文翻譯成中文" + else: + prefix_text=title + + prompt=prefix_text + "\n"+prompt + + r = api.chat_chat(prompt, history=history, model=llm_model, temperature=temperature) for t in r: if error_msg := check_error_msg(t): # check whether error occured st.error(error_msg) break + t = cc.convert(t) text += t chat_box.update_msg(text) chat_box.update_msg(text, streaming=False) # 更新最?的字符串,去除光? @@ -169,6 +188,7 @@ def dialogue_page(api: ApiRequest): st.error(error_msg) elif chunk := d.get("answer"): + chunk = cc.convert(chunk) text += chunk chat_box.update_msg(text, element_index=0) elif chunk := d.get("tools"): @@ -192,6 +212,7 @@ def dialogue_page(api: ApiRequest): if error_msg := check_error_msg(d): # check whether error occured st.error(error_msg) elif chunk := d.get("answer"): + chunk = cc.convert(chunk) text += chunk chat_box.update_msg(text, element_index=0) chat_box.update_msg(text, element_index=0, streaming=False) @@ -211,6 +232,7 @@ def dialogue_page(api: ApiRequest): if error_msg := check_error_msg(d): # check whether error occured st.error(error_msg) elif chunk := d.get("answer"): + chunk = cc.convert(chunk) text += chunk chat_box.update_msg(text, element_index=0) chat_box.update_msg(text, element_index=0, streaming=False) @@ -218,6 +240,16 @@ def dialogue_page(api: ApiRequest): now = datetime.now() with st.sidebar: + prefix = st.selectbox("請選擇功能:", + ["中翻英", + "英翻中", + "自定義", + ], + index=0, + key="prefix_mode", + ) + title = st.text_input('自定前置詞', '') + cols = st.columns(2) export_btn = cols[0] root@b4ed0cbf720d:~/Langchain-Chatchat/webui_pages/dialogue# |
簡單一篇,防忘記