Skip to content

Commit

Permalink
Update prompts
Browse files Browse the repository at this point in the history
Fix encoding error
Add Korean
  • Loading branch information
SIPC committed Feb 24, 2024
1 parent 3c6197f commit 0dab206
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/lib/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ export const languages: Language[] = [
{ key: "de", value: "Deutsch" },
{ key: "it", value: "Italian" },
{ key: "es", value: "Spanish" },
{ key: "ko", value: "Korean" },
{ key: "pt", value: "Portuguese" },
];
5 changes: 3 additions & 2 deletions src/pages/api/lib/baidu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const languageMap: Record<string, string> = {
"ja": "jp",
"fr": "fra",
"es": "spa",
"ko": "kor",
};

export class Baidu {
Expand All @@ -24,10 +25,10 @@ export class Baidu {
const salt = String(Math.random()).slice(2)
const sign = MD5(this.APPID+text+salt+this.APPKEY)
try {
const apiUrl =`http://api.fanyi.baidu.com/api/trans/vip/translate?q=${text}&from=${source}&to=${target}&appid=${this.APPID}&salt=${salt}&sign=${sign}`;
const apiUrl =`http://api.fanyi.baidu.com/api/trans/vip/translate?q=${encodeURIComponent(text)}&from=${source}&to=${target}&appid=${this.APPID}&salt=${salt}&sign=${sign}`;
const response = await axios.get(apiUrl);
if (response.data) {
return response.data.trans_result[0].dst;
return response.data.trans_result.map((item: { dst: any; }) => item.dst).join("\n\n");
} else {
throw new Error("Invalid response from Baidu");
}
Expand Down
10 changes: 9 additions & 1 deletion src/pages/api/lib/chatgpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,17 @@ export class ChatGPT {
const data = JSON.stringify({
model: this.model,
messages: [
{
role: "system",
content: `You are a professional, authentic translation engine, only returns translations.`,
},
{
role: "user",
content: `Please translate the text from ${source} to ${target} language, without explaining my original text.`,
},
{
role: "user",
content: `请将以下文本从${source}翻译为${target}:“${text}”(要求:输出的结果只能出现翻译结果,需要准确且清晰,不得出现翻译结果以外的内容,输出结果不可以带任何特殊的文本样式,'',""不得出现)`,
content: text,
},
],
temperature: 0.7,
Expand Down
8 changes: 7 additions & 1 deletion src/pages/api/lib/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ export class Gemini {
{
parts: [
{
text: `请将以下文本从${source}翻译为${target}:“${text}”(要求:输出的结果只能出现翻译结果,需要准确且清晰,不得出现翻译结果以外的内容,输出结果不可以带任何特殊的文本样式,'',""不得出现)`,
text: `You are a professional, authentic translation engine, only returns translations.`,
},
{
text: `Please translate the text from ${source} to ${target} language, without explaining my original text.`,
},
{
text: text,
},
],
},
Expand Down
10 changes: 9 additions & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,15 @@ export default function Home() {
onChange={setFrom}
placeholder={`Source Language`}
/>
<ChevronRight className={`shrink-0 w-3 h-3 mx-2`} />
<ChevronRight className={`shrink-0 w-3 h-3 mx-2`}
onClick={() => {
const temp = from;
if (temp != 'auto') {
setFrom(to);
setTo(temp);
}
}}
/>
<LanguageSelect
value={to}
onChange={setTo}
Expand Down

0 comments on commit 0dab206

Please sign in to comment.