Skip to content

Commit

Permalink
feat: support the polishing of Japanese and other languages
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone committed Mar 3, 2023
1 parent 5643942 commit 195c2e8
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/main.js
Expand Up @@ -12,11 +12,24 @@ function translate(query, completion) {
"Content-Type": "application/json",
Authorization: `Bearer ${api_key}`,
};
let prompt = "";
if (query.detectFrom === "zh-Hant" || query.detectFrom === "zh-Hans") {
prompt = "请润色一下这句话";
} else {
prompt = "please polish this sentence";
let prompt = "please polish this sentence";
switch (query.detectFrom) {
case "zh-Hant":
case "zh-Hans":
prompt = "请润色一下这句话";
break;
case "ja":
prompt = "この文章を装飾してください";
break;
case "ru":
prompt = "Пожалуйста, приукрасьте это предложение";
break;
case "wyw":
prompt = "请润色一下这句古文";
break;
case "yue":
prompt = "请润色一下这句粤语";
break;
}
const body = {
model: $option.model,
Expand All @@ -30,7 +43,7 @@ function translate(query, completion) {
if (isChatGPTModel) {
body.messages = [
{ role: "system", content: prompt },
{ role: "user", content: query.text },
{ role: "user", content: `"${query.text}"` },
];
} else {
body.prompt = `${prompt}:\n\n"${query.text}" =>`;
Expand Down Expand Up @@ -76,13 +89,11 @@ function translate(query, completion) {
} else {
targetTxt = choices[0].text.trim();
}
if (!isChatGPTModel) {
if (targetTxt.startsWith('"')) {
targetTxt = targetTxt.slice(1);
}
if (targetTxt.endsWith('"')) {
targetTxt = targetTxt.slice(0, -1);
}
if (targetTxt.startsWith('"') || targetTxt.startsWith("「")) {
targetTxt = targetTxt.slice(1);
}
if (targetTxt.endsWith('"') || targetTxt.endsWith("」")) {
targetTxt = targetTxt.slice(0, -1);
}
completion({
result: {
Expand Down

0 comments on commit 195c2e8

Please sign in to comment.