Skip to content

Commit

Permalink
optimize: use GTP-3.5 role based prompt design
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone committed Mar 3, 2023
1 parent 2b2ecc5 commit 196c85e
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,24 @@ function translate(query, completion) {
const api_key = api_keys[Math.floor(Math.random() * api_keys.length)];
const header = {
"Content-Type": "application/json",
Authorization: "Bearer " + api_key,
Authorization: `Bearer ${api_key}`,
};
let prompt = `translate from ${lang.langMap.get(query.detectFrom) || query.detectFrom
} to ${lang.langMap.get(query.detectTo) || query.detectTo}:\n\n"${query.text
}" =>`;
} to ${lang.langMap.get(query.detectTo) || query.detectTo}`;
if (query.detectTo === "wyw" || query.detectTo === "yue") {
prompt = `请翻译成${lang.langMap.get(query.detectTo) || query.detectTo
}:\n\n"${query.text}" =>`;
prompt = `请翻译成${lang.langMap.get(query.detectTo) || query.detectTo}`;
}
if (
query.detectFrom === "wyw" ||
query.detectFrom === "zh-Hans" ||
query.detectFrom === "zh-Hant"
) {
if (query.detectTo === "zh-Hant") {
prompt = `请翻译成繁体白话文:\n\n"${query.text}"=>`;
prompt = "请翻译成繁体白话文";
} else if (query.detectTo === "zh-Hans") {
prompt = `请翻译成简体白话文:\n\n"${query.text}"=>`;
prompt = "请翻译成简体白话文";
} else if (query.detectTo === "yue") {
prompt = `请翻译成粤语白话文:\n\n"${query.text}"=>`;
prompt = "请翻译成粤语白话文";
}
}
const body = {
Expand All @@ -42,9 +40,12 @@ function translate(query, completion) {
};
const isChatGPTModel = ChatGPTModels.indexOf($option.model) > -1;
if (isChatGPTModel) {
body.messages = [{ role: "user", content: prompt }];
body.messages = [
{ role: "system", content: prompt },
{ role: "user", content: query.text },
];
} else {
body.prompt = prompt;
body.prompt = `${prompt}:\n\n"${query.text}" =>`;
}
(async () => {
const resp = await $http.request({
Expand Down Expand Up @@ -87,11 +88,13 @@ function translate(query, completion) {
} else {
targetTxt = choices[0].text.trim();
}
if (targetTxt.startsWith('"')) {
targetTxt = targetTxt.slice(1);
}
if (targetTxt.endsWith('"')) {
targetTxt = targetTxt.slice(0, -1);
if (!isChatGPTModel) {
if (targetTxt.startsWith('"')) {
targetTxt = targetTxt.slice(1);
}
if (targetTxt.endsWith('"')) {
targetTxt = targetTxt.slice(0, -1);
}
}
completion({
result: {
Expand Down

0 comments on commit 196c85e

Please sign in to comment.