fix: translate prompt and translateText funciton

This commit is contained in:
kangfenmao 2024-12-03 17:18:18 +08:00
parent 3b569131a5
commit 1e273834b8
7 changed files with 30 additions and 7 deletions

View File

@ -48,4 +48,4 @@ export const SUMMARIZE_PROMPT =
'你是一名擅长会话的助理,你需要将用户的会话总结为 10 个字以内的标题,不要使用标点符号和其他特殊符号。'
export const TRANSLATE_PROMPT =
'Translate from input language to {{target_language}}, provide the translation result directly without any explanation, keep original format. If the target language is the same as the source language, do not translate. The text to be translated is as follows:\n\n{{text}}'
'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.'

View File

@ -130,7 +130,8 @@ const Inputbar: FC<Props> = ({ assistant, setActiveTopic }) => {
try {
setIsTranslating(true)
setText(await translateText(text, 'english'))
const translatedText = await translateText(text, 'english')
translatedText && setText(translatedText)
setTimeout(() => resizeTextArea(), 0)
} catch (error) {
console.error('Translation failed:', error)

View File

@ -90,6 +90,7 @@ const ModelSettings: FC = () => {
style={{ width: 360 }}
onChange={(value) => setDefaultModel(find(allModels, JSON.parse(value)) as Model)}
options={selectOptions}
showSearch
placeholder={t('settings.models.empty')}
/>
<Button icon={<SettingOutlined />} style={{ marginLeft: 8 }} onClick={() => AssistantSettingsPopup.show()} />
@ -110,6 +111,7 @@ const ModelSettings: FC = () => {
style={{ width: 360 }}
onChange={(value) => setTopicNamingModel(find(allModels, JSON.parse(value)) as Model)}
options={selectOptions}
showSearch
placeholder={t('settings.models.empty')}
/>
<Button icon={<SettingOutlined />} style={{ marginLeft: 8 }} onClick={TopicNamingModalPopup.show} />
@ -130,6 +132,7 @@ const ModelSettings: FC = () => {
style={{ width: 360 }}
onChange={(value) => setTranslateModel(find(allModels, JSON.parse(value)) as Model)}
options={selectOptions}
showSearch
placeholder={t('settings.models.empty')}
/>
<Button icon={<SettingOutlined />} style={{ marginLeft: 8 }} onClick={onUpdateTranslateModel} />

View File

@ -69,11 +69,13 @@ export async function locateToMessage(navigate: NavigateFunction, message: Messa
export function getUserMessage({
assistant,
topic,
type
type,
content
}: {
assistant: Assistant
topic: Topic
type: Message['type']
content?: string
}): Message {
const defaultModel = getDefaultModel()
const model = assistant.model || defaultModel
@ -81,7 +83,7 @@ export function getUserMessage({
return {
id: uuid(),
role: 'user',
content: '',
content: content || '',
assistantId: assistant.id,
topicId: topic.id,
modelId: model.id,

View File

@ -21,10 +21,17 @@ export const translateText = async (text: string, targetLanguage: string) => {
const message = getUserMessage({
assistant,
topic: getDefaultTopic('default'),
type: 'text'
type: 'text',
content: text
})
const translatedText = await fetchTranslate({ message, assistant })
return translatedText
const trimmedText = translatedText.trim()
if (!trimmedText) {
return Promise.reject(new Error(i18n.t('translate.error.failed')))
}
return trimmedText
}

View File

@ -26,7 +26,7 @@ const persistedReducer = persistReducer(
{
key: 'cherry-studio',
storage,
version: 45,
version: 46,
blacklist: ['runtime'],
migrate
},

View File

@ -708,6 +708,16 @@ const migrateConfig = {
'45': (state: RootState) => {
state.settings.enableTopicNaming = true
return state
},
'46': (state: RootState) => {
if (
state.settings.translateModelPrompt.includes(
'If the target language is the same as the source language, do not translate'
)
) {
state.settings.translateModelPrompt = TRANSLATE_PROMPT
}
return state
}
}