From 3a457ebc38ec09e13b3ab3f9ece1d4c00c33c450 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Mon, 2 Sep 2024 19:03:25 +0800 Subject: [PATCH] fix --- .../LunaTranslator/LunaTranslator.py | 24 ++++++++++----- .../LunaTranslator/myutils/utils.py | 18 +++++++++++ .../LunaTranslator/textsource/texthook.py | 8 ++++- .../textsource/textsourcebase.py | 7 ++--- .../LunaTranslator/translator/baiduqianfan.py | 20 ++----------- .../translator/basetranslator.py | 18 ++++++++++- .../LunaTranslator/translator/claude.py | 21 +++---------- .../LunaTranslator/translator/cohere.py | 30 ++++--------------- .../translator/dev_llm_common.py | 8 +---- .../LunaTranslator/translator/gemini.py | 30 +++---------------- .../LunaTranslator/translator/gptcommon.py | 28 ++++------------- .../LunaTranslator/translator/txhunyuan.py | 27 +++-------------- plugins/CMakeLists.txt | 2 +- 13 files changed, 88 insertions(+), 153 deletions(-) diff --git a/LunaTranslator/LunaTranslator/LunaTranslator.py b/LunaTranslator/LunaTranslator/LunaTranslator.py index e7e22846..b6e4ef70 100644 --- a/LunaTranslator/LunaTranslator/LunaTranslator.py +++ b/LunaTranslator/LunaTranslator/LunaTranslator.py @@ -257,13 +257,25 @@ class MAINUI: return def textgetmethod( - self, text, is_auto_run=True, waitforresultcallback=None, onlytrans=False + self, + text, + is_auto_run=True, + waitforresultcallback=None, + onlytrans=False, + donttrans=False, ): with self.solvegottextlock: - self.textgetmethod_1(text, is_auto_run, waitforresultcallback, onlytrans) + self.textgetmethod_1( + text, is_auto_run, waitforresultcallback, onlytrans, donttrans + ) def textgetmethod_1( - self, text, is_auto_run=True, waitforresultcallback=None, onlytrans=False + self, + text, + is_auto_run=True, + waitforresultcallback=None, + onlytrans=False, + donttrans=False, ): safe_callback = waitforresultcallback if waitforresultcallback else lambda _: 1 safe_callback_none = functools.partial(safe_callback, "") @@ -309,11 +321,7 @@ class MAINUI: ) except: pass - if ( - (windows.GetKeyState(windows.VK_CONTROL) < 0) - or (windows.GetKeyState(windows.VK_SHIFT) < 0) - or (windows.GetKeyState(windows.VK_RETURN) < 0) - ): + if donttrans: return safe_callback_none() if onlytrans == False: self.currenttext = text diff --git a/LunaTranslator/LunaTranslator/myutils/utils.py b/LunaTranslator/LunaTranslator/myutils/utils.py index 24f32de7..18d80734 100644 --- a/LunaTranslator/LunaTranslator/myutils/utils.py +++ b/LunaTranslator/LunaTranslator/myutils/utils.py @@ -5,6 +5,7 @@ import socket, gobject, uuid, subprocess, functools import ctypes, importlib, json import ctypes.wintypes from qtsymbols import * +from string import Formatter from ctypes import CDLL, c_void_p, CFUNCTYPE, c_size_t, cast, c_char, POINTER from ctypes.wintypes import HANDLE from traceback import print_exc @@ -860,3 +861,20 @@ def copytree(src, dst, copy_function=shutil.copy2): copy_function(srcname, dstname) except: pass + + +class SafeFormatter(Formatter): + def format(self, format_string, must_exists=None, *args, **kwargs): + if must_exists: + check = "{" + must_exists + "}" + if check not in format_string: + format_string += check + + return super().format(format_string, *args, **kwargs) + + def get_value(self, key, args, kwargs): + if key in kwargs: + return super().get_value(key, args, kwargs) + else: + print(f"{key} is missing") + return key diff --git a/LunaTranslator/LunaTranslator/textsource/texthook.py b/LunaTranslator/LunaTranslator/textsource/texthook.py index e7e37d20..13d1b00f 100644 --- a/LunaTranslator/LunaTranslator/textsource/texthook.py +++ b/LunaTranslator/LunaTranslator/textsource/texthook.py @@ -662,7 +662,13 @@ class texthook(basetext): def dispatchtext(self, text): self.runonce_line = text - return super().dispatchtext(text) + + donttrans = ( + (windows.GetKeyState(windows.VK_CONTROL) < 0) + or (windows.GetKeyState(windows.VK_SHIFT) < 0) + or (windows.GetKeyState(windows.VK_RETURN) < 0) + ) + return super().dispatchtext(text, donttrans=donttrans) def gettextonce(self): return self.runonce_line diff --git a/LunaTranslator/LunaTranslator/textsource/textsourcebase.py b/LunaTranslator/LunaTranslator/textsource/textsourcebase.py index 013acdb0..b66297e3 100644 --- a/LunaTranslator/LunaTranslator/textsource/textsourcebase.py +++ b/LunaTranslator/LunaTranslator/textsource/textsourcebase.py @@ -46,13 +46,10 @@ class basetext: print_exc() threading.Thread(target=self.sqlitethread).start() - def dispatchtext(self, text): + def dispatchtext(self, *arg, **kwarg): if self.ending or not self.isautorunning: return - if isinstance(text, tuple): - self.textgetmethod(*text) - else: - self.textgetmethod(text) + self.textgetmethod(*arg, **kwarg) def waitfortranslation(self, text): resultwaitor = queue.Queue() diff --git a/LunaTranslator/LunaTranslator/translator/baiduqianfan.py b/LunaTranslator/LunaTranslator/translator/baiduqianfan.py index 5d118ec4..bd864233 100644 --- a/LunaTranslator/LunaTranslator/translator/baiduqianfan.py +++ b/LunaTranslator/LunaTranslator/translator/baiduqianfan.py @@ -31,13 +31,8 @@ class TS(basetrans): def createdata(self, message): temperature = self.config["Temperature"] + system = self._gptlike_createsys("use_user_prompt", "user_prompt") - if self.config["use_user_prompt"]: - system = self.config["user_prompt"] - else: - system = "You are a translator. Please help me translate the following {} text into {}, and you should only tell me the translation.".format( - self.srclang, self.tgtlang - ) data = dict( system=system, model=self.config["model"], @@ -106,18 +101,9 @@ class TS(basetrans): def translate(self, query): acss = self.checkchange() self.contextnum = int(self.config["context_num"]) - user_prompt = ( - self.config.get("user_user_prompt", "") - if self.config.get("use_user_user_prompt", False) - else "" + query = self._gptlike_createquery( + query, "use_user_user_prompt", "user_user_prompt" ) - try: - if "{sentence}" in user_prompt: - query = user_prompt.format(sentence=query) - else: - query = user_prompt + query - except: - pass message = [] for _i in range(min(len(self.context) // 2, self.contextnum)): i = ( diff --git a/LunaTranslator/LunaTranslator/translator/basetranslator.py b/LunaTranslator/LunaTranslator/translator/basetranslator.py index 8ab28747..36c8fd93 100644 --- a/LunaTranslator/LunaTranslator/translator/basetranslator.py +++ b/LunaTranslator/LunaTranslator/translator/basetranslator.py @@ -6,7 +6,7 @@ import zhconv, gobject import sqlite3, json import functools from myutils.config import globalconfig, translatorsetting -from myutils.utils import stringfyerror, autosql, PriorityQueue +from myutils.utils import stringfyerror, autosql, PriorityQueue, SafeFormatter from myutils.commonbase import ArgsEmptyExc, commonbase @@ -266,6 +266,22 @@ class basetrans(commonbase): return res + def _gptlike_createquery(self, query, usekey, tempk): + user_prompt = ( + self.config.get(tempk, "") if self.config.get(usekey, False) else "" + ) + fmt = SafeFormatter() + return fmt.format(user_prompt, must_exists="sentence", sentence=query) + + def _gptlike_createsys(self, usekey, tempk): + + fmt = SafeFormatter() + if self.config[usekey]: + template = self.config[tempk] + else: + template = "You are a translator. Please help me translate the following {srclang} text into {tgtlang}, and you should only tell me the translation." + return fmt.format(template, srclang=self.srclang, tgtlang=self.tgtlang) + def reinitandtrans(self, contentsolved, is_auto_run): if self.needreinit or self.initok == False: self.needreinit = False diff --git a/LunaTranslator/LunaTranslator/translator/claude.py b/LunaTranslator/LunaTranslator/translator/claude.py index f4e759ee..5b5fd8ce 100644 --- a/LunaTranslator/LunaTranslator/translator/claude.py +++ b/LunaTranslator/LunaTranslator/translator/claude.py @@ -1,6 +1,7 @@ from traceback import print_exc import json from translator.basetranslator import basetrans +from myutils.utils import SafeFormatter class TS(basetrans): @@ -44,26 +45,12 @@ class TS(basetrans): def translate(self, query): self.checkempty(["API_KEY", "model"]) self.contextnum = int(self.config["附带上下文个数"]) - user_prompt = ( - self.config.get("user_user_prompt", "") - if self.config.get("use_user_user_prompt", False) - else "" + query = self._gptlike_createquery( + query, "use_user_user_prompt", "user_user_prompt" ) - try: - if "{sentence}" in user_prompt: - query = user_prompt.format(sentence=query) - else: - query = user_prompt + query - except: - pass + system = self._gptlike_createsys("使用自定义promt", "自定义promt") temperature = self.config["Temperature"] - if self.config["使用自定义promt"]: - system = self.config["自定义promt"] - else: - system = "You are a translator, translate from {} to {}".format( - self.srclang, self.tgtlang - ) message = [] for _i in range(min(len(self.context) // 2, self.contextnum)): i = ( diff --git a/LunaTranslator/LunaTranslator/translator/cohere.py b/LunaTranslator/LunaTranslator/translator/cohere.py index 7f14d086..1d55d3b7 100644 --- a/LunaTranslator/LunaTranslator/translator/cohere.py +++ b/LunaTranslator/LunaTranslator/translator/cohere.py @@ -77,31 +77,13 @@ class TS(basetrans): def translate(self, query): self.checkempty(["SECRET_KEY", "model"]) self.contextnum = int(self.config["附带上下文个数"]) - user_prompt = ( - self.config.get("user_user_prompt", "") - if self.config.get("use_user_user_prompt", False) - else "" - ) - try: - if "{sentence}" in user_prompt: - query = user_prompt.format(sentence=query) - else: - query = user_prompt + query - except: - pass - temperature = self.config["Temperature"] - if self.config["使用自定义promt"]: - message = [{"role": "system", "content": self.config["自定义promt"]}] - else: - message = [ - { - "role": "system", - "message": "You are a translator. Please help me translate the following {} text into {}, and you should only tell me the translation.".format( - self.srclang, self.tgtlang - ), - }, - ] + query = self._gptlike_createquery( + query, "use_user_user_prompt", "user_user_prompt" + ) + sysprompt = self._gptlike_createsys("使用自定义promt", "自定义promt") + message = [{"role": "system", "message": sysprompt}] + temperature = self.config["Temperature"] message.append( { diff --git a/LunaTranslator/LunaTranslator/translator/dev_llm_common.py b/LunaTranslator/LunaTranslator/translator/dev_llm_common.py index 050849fc..1554142b 100644 --- a/LunaTranslator/LunaTranslator/translator/dev_llm_common.py +++ b/LunaTranslator/LunaTranslator/translator/dev_llm_common.py @@ -44,13 +44,7 @@ class commonllmdev(basetransdev): def translate(self, content): self.injectjs() - - if self.config["use_custom_prompt"]: - prompt = self.config["custom_prompt"] - else: - prompt = "You are a translator. Please help me translate the following {} text into {}, and you should only tell me the translation.\n".format( - self.srclang, self.tgtlang - ) + prompt = self._gptlike_createsys("use_custom_prompt", "custom_prompt") content = prompt + content self.Runtime_evaluate( f"document.querySelector(`{repr(self.textarea_selector)}`).foucs()" diff --git a/LunaTranslator/LunaTranslator/translator/gemini.py b/LunaTranslator/LunaTranslator/translator/gemini.py index bc33eb42..b0b11fce 100644 --- a/LunaTranslator/LunaTranslator/translator/gemini.py +++ b/LunaTranslator/LunaTranslator/translator/gemini.py @@ -40,18 +40,9 @@ class TS(basetrans): } } model = self.config["model"] - user_prompt = ( - self.config.get("user_user_prompt", "") - if self.config.get("use_user_user_prompt", False) - else "" + query = self._gptlike_createquery( + query, "use_user_user_prompt", "user_user_prompt" ) - try: - if "{sentence}" in user_prompt: - query = user_prompt.format(sentence=query) - else: - query = user_prompt + query - except: - pass safety = { "safety_settings": [ { @@ -72,21 +63,8 @@ class TS(basetrans): }, ] } - - if self.config["use_custom_prompt"]: - sys_message = { - "systemInstruction": {"parts": {"text": self.config["custom_prompt"]}} - } - else: - sys_message = { - "systemInstruction": { - "parts": { - "text": "You are a translator. Please help me translate the following {} text into {}, and you should only tell me the translation.".format( - self.srclang, self.tgtlang - ), - }, - }, - } + sysprompt = self._gptlike_createsys("use_custom_prompt", "custom_prompt") + sys_message = {"systemInstruction": {"parts": {"text": sysprompt}}} message = [] for _i in range(min(len(self.context) // 2, self.contextnum)): i = ( diff --git a/LunaTranslator/LunaTranslator/translator/gptcommon.py b/LunaTranslator/LunaTranslator/translator/gptcommon.py index cad85508..a82f46f8 100644 --- a/LunaTranslator/LunaTranslator/translator/gptcommon.py +++ b/LunaTranslator/LunaTranslator/translator/gptcommon.py @@ -1,6 +1,7 @@ from translator.basetranslator import basetrans import json, requests from traceback import print_exc +from myutils.utils import SafeFormatter class gptcommon(basetrans): @@ -119,29 +120,11 @@ class gptcommon(basetrans): def translate(self, query): self.contextnum = int(self.config["附带上下文个数"]) - user_prompt = ( - self.config.get("user_user_prompt", "") - if self.config.get("use_user_user_prompt", False) - else "" + query = self._gptlike_createquery( + query, "use_user_user_prompt", "user_user_prompt" ) - try: - if "{sentence}" in user_prompt: - query = user_prompt.format(sentence=query) - else: - query = user_prompt + query - except: - pass - if self.config["使用自定义promt"]: - message = [{"role": "system", "content": self.config["自定义promt"]}] - else: - message = [ - { - "role": "system", - "content": "You are a translator. Please help me translate the following {} text into {}, and you should only tell me the translation.".format( - self.srclang, self.tgtlang - ), - }, - ] + sysprompt = self._gptlike_createsys("使用自定义promt", "自定义promt") + message = [{"role": "system", "content": sysprompt}] for _i in range(min(len(self.context) // 2, self.contextnum)): i = ( @@ -152,7 +135,6 @@ class gptcommon(basetrans): message.append(self.context[i * 2]) message.append(self.context[i * 2 + 1]) message.append({"role": "user", "content": query}) - usingstream = self.config["流式输出"] response = self.proxysession.post( self.createurl(), diff --git a/LunaTranslator/LunaTranslator/translator/txhunyuan.py b/LunaTranslator/LunaTranslator/translator/txhunyuan.py index 24156cf8..fa7bb887 100644 --- a/LunaTranslator/LunaTranslator/translator/txhunyuan.py +++ b/LunaTranslator/LunaTranslator/translator/txhunyuan.py @@ -115,30 +115,11 @@ class TS(basetrans): def translate(self, query): self.checkempty(["secret_id", "secret_key"]) self.contextnum = int(self.config["context_num"]) - user_prompt = ( - self.config.get("user_user_prompt", "") - if self.config.get("use_user_user_prompt", False) - else "" + query = self._gptlike_createquery( + query, "use_user_user_prompt", "user_user_prompt" ) - try: - if "{sentence}" in user_prompt: - query = user_prompt.format(sentence=query) - else: - query = user_prompt + query - except: - pass - print(query) - if self.config["use_user_prompt"]: - message = [{"Role": "system", "Content": self.config["user_prompt"]}] - else: - message = [ - { - "Role": "system", - "Content": "You are a translator. Please help me translate the following {} text into {}, and you should only tell me the translation.".format( - self.srclang, self.tgtlang - ), - }, - ] + sysprompt = self._gptlike_createsys("use_user_prompt", "user_prompt") + message = [{"Role": "system", "Content": sysprompt}] for _i in range(min(len(self.context) // 2, self.contextnum)): i = ( diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index e441505e..ffdd7ef8 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -29,7 +29,7 @@ include(generate_product_version) set(VERSION_MAJOR 5) set(VERSION_MINOR 33) -set(VERSION_PATCH 2) +set(VERSION_PATCH 3) add_library(pch pch.cpp) target_precompile_headers(pch PUBLIC pch.h)