This commit is contained in:
恍兮惚兮 2024-07-24 16:52:17 +08:00
parent 6baf3bad5e
commit cd136a2ef0
6 changed files with 151 additions and 5 deletions

View File

@ -380,7 +380,9 @@ def initsome2(self, l, label=None, btnplus=None):
type="grid", type="grid",
title="大模型", title="大模型",
grid=is_gpt_likes, grid=is_gpt_likes,
internallayoutname="damoxinggridinternal" + btnplus, internallayoutname=(
("damoxinggridinternal" + btnplus) if btnplus else None
),
parent=self, parent=self,
), ),
0, 0,
@ -546,20 +548,19 @@ def setTabTwo_lazy(self, basel):
D_getspinbox(0, 65535, globalconfig, "debugport"), D_getspinbox(0, 65535, globalconfig, "debugport"),
"", "",
], ],
[(functools.partial(createstatuslabel, self), 0)],
), ),
), ),
0, 0,
"group", "group",
) )
], ],
[(functools.partial(createstatuslabel, self), 16)],
[],
] ]
lixians, pre, mianfei, develop, shoufei = splittranslatortypes() lixians, pre, mianfei, develop, shoufei = splittranslatortypes()
offlinegrid = initsome2(self, lixians, btnplus="offline") offlinegrid = initsome2(self, lixians, btnplus="offline")
onlinegrid = initsome11(self, mianfei) onlinegrid = initsome11(self, mianfei)
developgrid += initsome11(self, develop) developgrid += initsome2(self, develop)
online_reg_grid += initsome2(self, shoufei, btnplus="api") online_reg_grid += initsome2(self, shoufei, btnplus="api")
pretransgrid += initsome11(self, pre) pretransgrid += initsome11(self, pre)
vw, vl = getvboxwidget() vw, vl = getvboxwidget()

View File

@ -25,7 +25,6 @@ class basetransdev(basetrans):
state = self.Runtime_evaluate(expression) state = self.Runtime_evaluate(expression)
try: try:
value = state["result"]["value"] value = state["result"]["value"]
print(value)
if multi: if multi:
if not (value in badresult): if not (value in badresult):
return value return value

View File

@ -0,0 +1,46 @@
const originalFetch = window.fetch;
var hasdone = true
var thistext = ''
window.fetch = function (input, init) {
const fetchPromise = originalFetch.apply(this, arguments);
if (!input.endsWith("completion/stream")) return fetchPromise;
hasdone = false;
thistext = ''
fetchPromise.then(response => {
const clone = response.clone()
const contentType = clone.headers.get('content-type');
if (contentType && contentType.includes('text/event-stream')) {
const reader = clone.body.getReader();
reader.read().then(function processStream({ done, value }) {
if (done) {
hasdone = true;
return;
}
let lines = new TextDecoder('utf-8').decode(value);
lines = lines.split('\n')
for (let i = 0; i < lines.length; i++) {
let line = lines[i]
line = line.trim()
if (line.length == 0) continue;
try {
const chunk = JSON.parse(line.substring(6));
if(chunk.event!='cmpl')continue;
console.log(chunk)
if(chunk.text)
thistext += chunk.text
} catch { }
}
return reader.read().then(processStream);
}).catch(error => {
console.error('Error reading stream:', error);
});
}
}).catch(error => {
console.error('Fetch error:', error);
});
return fetchPromise;
};

View File

@ -0,0 +1,71 @@
from translator.basetranslator_dev import basetransdev
import time, os
class TS(basetransdev):
target_url = "https://kimi.moonshot.cn/"
def langmap(self):
return {
"zh": "Simplified Chinese",
"ja": "Japanese",
"en": "English",
"ru": "Russian",
"es": "Spanish",
"ko": "Korean",
"fr": "French",
"cht": "Traditional Chinese",
"vi": "Vietnamese",
"tr": "Turkish",
"pl": "Polish",
"uk": "Ukrainian",
"it": "Italian",
"ar": "Arabic",
"th": "Thai",
}
def injectjs(self):
if self.Runtime_evaluate("window.injectedjs")["result"]["type"] != "undefined":
return
with open(
os.path.join(os.path.dirname(__file__), "dev_moonshot.js"),
"r",
encoding="utf8",
) as ff:
js = ff.read()
self.Runtime_evaluate(js)
self.Runtime_evaluate("window.injectedjs=true")
def translate(self, content):
self.injectjs()
self.Runtime_evaluate("hasdone=false")
self.Runtime_evaluate('thistext=""')
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
)
content = prompt + content
self.Runtime_evaluate(
'document.querySelector("#root > div.MuiBox-root.css-qmryj6 > div > div.layoutContent___NvxZ_ > div.MuiBox-root.css-ar9pyi > div > div > div > div.MuiBox-root.css-8atqhb > div.chatInput___bMC0h.matchHomePageLayout___WewPT > div > div > div.editor___KShcc.editor___DSPKC.matchHomePageLayout___XTlpC > div")? document.querySelector("#root > div.MuiBox-root.css-qmryj6 > div > div.layoutContent___NvxZ_ > div.MuiBox-root.css-ar9pyi > div > div > div > div.MuiBox-root.css-8atqhb > div.chatInput___bMC0h.matchHomePageLayout___WewPT > div > div > div.editor___KShcc.editor___DSPKC.matchHomePageLayout___XTlpC > div").click() : document.querySelector("#root > div.MuiBox-root.css-qmryj6 > div > div.layoutContent___NvxZ_ > div.MuiBox-root.css-ar9pyi > div > div > div > div > div.chatPageLayoutRightBoxLeft___taL5l > div.content___inD6V > div > div.chatBottom___jS9Jd.MuiBox-root.css-jdjpte > div.chatInput___bMC0h.matchHomePageLayout___WewPT > div > div > div.editor___KShcc.editor___DSPKC.matchHomePageLayout___XTlpC > div.editorContentEditable___FZJd9").click()'
)
self.send_keys(content)
self.wait_for_result('document.querySelector("#send-button").disabled', True)
self.Runtime_evaluate("""document.querySelector("#send-button").click()""")
if self.config["usingstream"]:
curr = ""
while not self.Runtime_evaluate("hasdone")["result"]["value"]:
time.sleep(0.1)
thistext = self.Runtime_evaluate("thistext")["result"]["value"]
if thistext.startswith(curr):
yield thistext[len(curr) :]
else:
yield thistext
curr = thistext
else:
while not self.Runtime_evaluate("hasdone")["result"]["value"]:
time.sleep(0.1)
continue
yield self.Runtime_evaluate("thistext")["result"]["value"]

View File

@ -1575,6 +1575,13 @@
"name": "chatgpt", "name": "chatgpt",
"is_gpt_like": true "is_gpt_like": true
}, },
"dev_moonshot": {
"use": false,
"color": "blue",
"type": "dev",
"name": "moonshot",
"is_gpt_like": true
},
"deepl_dev": { "deepl_dev": {
"use": false, "use": false,
"color": "blue", "color": "blue",

View File

@ -754,6 +754,28 @@
} }
} }
}, },
"dev_moonshot": {
"args": {
"usingstream": true,
"use_custom_prompt": false,
"custom_prompt": ""
},
"argstype": {
"usingstream": {
"name": "流式输出",
"type": "switch"
},
"custom_prompt": {
"name": "自定义promt",
"type": "multiline",
"refswitch": "use_custom_prompt"
},
"use_custom_prompt": {
"type": "switch_ref",
"name": "使用自定义promt"
}
}
},
"gemini": { "gemini": {
"args": { "args": {
"注册网址": "https://ai.google.dev/tutorials/python_quickstart", "注册网址": "https://ai.google.dev/tutorials/python_quickstart",