Skip to content

Commit

Permalink
feat: improved translation features and settings
Browse files Browse the repository at this point in the history
  • Loading branch information
kangfenmao committed Jan 2, 2025
1 parent 09e6756 commit aa72794
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 46 deletions.
17 changes: 15 additions & 2 deletions src/renderer/src/config/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,21 @@ export const AGENT_PROMPT = `
export const SUMMARIZE_PROMPT =
'你是一名擅长会话的助理,你需要将用户的会话总结为 10 个字以内的标题,标题语言与用户的首要语言一致,不要使用标点符号和其他特殊符号'

export const TRANSLATE_PROMPT =
'You are a translation expert. Translate from input language to {{target_language}}, provide the translation result directly without any explanation and keep original format. Do not translate if the target language is the same as the source language.'
export const TRANSLATE_PROMPT = `You are a translation expert.
Translate from input language to {{target_language}}
Requirements:
1. provide the translation result directly without any explanation and keep original format.
2. Do not translate if the target language is the same as the source language.
Example:
input: 你好,今天天气怎么样?
target_language: english
output: Hello, how is the weather today?
`

export const REFERENCE_PROMPT = `请根据参考资料回答问题,并使用脚注格式引用数据来源。请忽略无关的参考资料。
Expand Down
95 changes: 52 additions & 43 deletions src/renderer/src/pages/home/Messages/MessageMenubar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,51 +156,9 @@ const MessageMenubar: FC<Props> = (props) => {
key: 'edit',
icon: <EditOutlined />,
onClick: onEdit
},
{
label: t('chat.translate'),
key: 'translate',
icon: isTranslating ? <SyncOutlined spin /> : <TranslationOutlined />,
children: [
{
label: '🇨🇳 ' + t('languages.chinese'),
key: 'translate-chinese',
onClick: () => handleTranslate('chinese')
},
{
label: '🇭🇰 ' + t('languages.chinese-traditional'),
key: 'translate-chinese-traditional',
onClick: () => handleTranslate('chinese-traditional')
},
{
label: '🇬🇧 ' + t('languages.english'),
key: 'translate-english',
onClick: () => handleTranslate('english')
},
{
label: '🇯🇵 ' + t('languages.japanese'),
key: 'translate-japanese',
onClick: () => handleTranslate('japanese')
},
{
label: '🇰🇷 ' + t('languages.korean'),
key: 'translate-korean',
onClick: () => handleTranslate('korean')
},
{
label: '🇷🇺 ' + t('languages.russian'),
key: 'translate-russian',
onClick: () => handleTranslate('russian')
},
{
label: '✖ ' + t('translate.close'),
key: 'translate-close',
onClick: () => onEditMessage?.({ ...message, translatedContent: undefined })
}
]
}
],
[handleTranslate, isTranslating, message, onEdit, onEditMessage, t]
[message, onEdit, t]
)

const onAtModelRegenerate = async () => {
Expand Down Expand Up @@ -261,6 +219,57 @@ const MessageMenubar: FC<Props> = (props) => {
</ActionButton>
</Tooltip>
)}
{!isUserMessage && (
<Dropdown
menu={{
items: [
{
label: '🇨🇳 ' + t('languages.chinese'),
key: 'translate-chinese',
onClick: () => handleTranslate('chinese')
},
{
label: '🇭🇰 ' + t('languages.chinese-traditional'),
key: 'translate-chinese-traditional',
onClick: () => handleTranslate('chinese-traditional')
},
{
label: '🇬🇧 ' + t('languages.english'),
key: 'translate-english',
onClick: () => handleTranslate('english')
},
{
label: '🇯🇵 ' + t('languages.japanese'),
key: 'translate-japanese',
onClick: () => handleTranslate('japanese')
},
{
label: '🇰🇷 ' + t('languages.korean'),
key: 'translate-korean',
onClick: () => handleTranslate('korean')
},
{
label: '🇷🇺 ' + t('languages.russian'),
key: 'translate-russian',
onClick: () => handleTranslate('russian')
},
{
label: '✖ ' + t('translate.close'),
key: 'translate-close',
onClick: () => onEditMessage?.({ ...message, translatedContent: undefined })
}
]
}}
trigger={['click']}
placement="topRight"
arrow>
<Tooltip title={t('chat.translate')} mouseEnterDelay={1.2}>
<ActionButton className="message-action-button">
<TranslationOutlined />
</ActionButton>
</Tooltip>
</Dropdown>
)}
<Popconfirm
title={t('message.message.delete.content')}
okButtonProps={{ danger: true }}
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/src/providers/OpenAIProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ export default class OpenAIProvider extends BaseProvider {
model: model.id,
messages: messages as ChatCompletionMessageParam[],
stream: false,
keep_alive: this.keepAliveTime
keep_alive: this.keepAliveTime,
temperature: assistant?.settings?.temperature
})

return response.choices[0].message?.content || ''
Expand Down
5 changes: 5 additions & 0 deletions src/renderer/src/services/AssistantService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export function getDefaultTranslateAssistant(targetLanguage: string, text: strin
const translateModel = getTranslateModel()
const assistant: Assistant = getDefaultAssistant()
assistant.model = translateModel

assistant.settings = {
temperature: 0.7
}

assistant.prompt = store
.getState()
.settings.translateModelPrompt.replace('{{target_language}}', targetLanguage)
Expand Down
20 changes: 20 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2639,6 +2639,13 @@ __metadata:
languageName: node
linkType: hard

"@types/trusted-types@npm:^2.0.7":
version: 2.0.7
resolution: "@types/trusted-types@npm:2.0.7"
checksum: 10c0/4c4855f10de7c6c135e0d32ce462419d8abbbc33713b31d294596c0cc34ae1fa6112a2f9da729c8f7a20707782b0d69da3b1f8df6645b0366d08825ca1522e0c
languageName: node
linkType: hard

"@types/unist@npm:*, @types/unist@npm:^3.0.0":
version: 3.0.3
resolution: "@types/unist@npm:3.0.3"
Expand Down Expand Up @@ -2888,6 +2895,7 @@ __metadata:
dexie: "npm:^4.0.8"
dexie-react-hooks: "npm:^1.1.7"
docx: "npm:^9.0.2"
dompurify: "npm:^3.2.3"
dotenv-cli: "npm:^7.4.2"
electron: "npm:31.7.6"
electron-builder: "npm:^24.13.3"
Expand Down Expand Up @@ -4982,6 +4990,18 @@ __metadata:
languageName: node
linkType: hard

"dompurify@npm:^3.2.3":
version: 3.2.3
resolution: "dompurify@npm:3.2.3"
dependencies:
"@types/trusted-types": "npm:^2.0.7"
dependenciesMeta:
"@types/trusted-types":
optional: true
checksum: 10c0/0ce5cb89b76f396d800751bcb48e0d137792891d350ccc049f1bc9a5eca7332cc69030c25007ff4962e0824a5696904d4d74264df9277b5ad955642dfb6f313f
languageName: node
linkType: hard

"domutils@npm:^3.0.1":
version: 3.1.0
resolution: "domutils@npm:3.1.0"
Expand Down

0 comments on commit aa72794

Please sign in to comment.