Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhance openai prompt #186

Merged
merged 1 commit into from
Sep 26, 2024
Merged

feat: enhance openai prompt #186

merged 1 commit into from
Sep 26, 2024

Conversation

hoilc
Copy link

@hoilc hoilc commented Sep 25, 2024

Close #141

根据 #185 #141 建议,对OpenAI prompt进行双重约束

默认请求示例

[{
	"role": "system",
	"content": "You are a professional, authentic machine translation engine. You will be provided with a sentence in English, and your task is to translate it into Simplified Chinese."
}, {
	"role": "user",
	"content": "Translate the following source text from English to Simplified Chinese. Output translation directly without any additional text.\n\nSource Text: hello world\n\nTranslated Text:"
}]

为兼容历史上作为systemPrompt的prompt,如果prompt中不包含带翻译文本,则添加文本到prompt末尾

[{
	"role": "system",
	"content": "You are a professional, authentic machine translation engine. You will be provided with a sentence in English, and your task is to translate it into Simplified Chinese."
}, {
	"role": "user",
	"content": "You will be provided with a sentence in English, and your task is to translate it into Simplified Chinese.\nSource Text: hello world"
}]

@fishjar
Copy link
Owner

fishjar commented Sep 26, 2024

我将暂时先合并你的版本,但考虑到更平滑、简洁的兼容性,字段命名上,之前的prompt还是默认为systemPrompt,无需改动,然后添加新的userPrompt字段也许更好。

兼容性主要是考虑:用户升级到了新版本,但是API的配置并没有及时重新设置的情况。

按照你目前的更新,API参数经过一次浅合并后:

apiSetting: {
...DEFAULT_TRANS_APIS[translator],
...(setting.transApis[translator] || {}),
},

大概如下:

{
prompt: `You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.`,
systemPrompt: `You are a professional, authentic machine translation engine. You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.`,
}

再经过如下追加:

 if (!prompt.includes(INPUT_PLACE_TEXT)) {
    prompt += `\nSource Text: ${INPUT_PLACE_TEXT}`;
  }

最终如下:

{
prompt: `You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.\nSource Text: ${INPUT_PLACE_TEXT}`,
systemPrompt: `You are a professional, authentic machine translation engine. You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.`,
}

如果是采用增加userPrompt的方案,API参数经过一次浅合并后大概如下:

{
prompt: `You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.`,
userPrompt: `Translate the following source text from ${INPUT_PLACE_FROM} to ${INPUT_PLACE_TO}. Output translation directly without any additional text.\n\nSource Text: ${INPUT_PLACE_TEXT}\n\nTranslated Text:`,
}

可见,离升级后的推荐格式:

{
prompt: `You are a professional, authentic machine translation engine. You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.`,
userPrompt: `Translate the following source text from ${INPUT_PLACE_FROM} to ${INPUT_PLACE_TO}. Output translation directly without any additional text.\n\nSource Text: ${INPUT_PLACE_TEXT}\n\nTranslated Text:`,
}

很接近了,无需如下代码即有很好的兼容性:

 if (!prompt.includes(INPUT_PLACE_TEXT)) {
    prompt += `\nSource Text: ${INPUT_PLACE_TEXT}`;
  }

@fishjar
Copy link
Owner

fishjar commented Sep 26, 2024

另外 OPT_TRANS_CLAUDE 的字段也可以统一,采用 promptuserPrompt的命名。

@fishjar fishjar merged commit 37facdc into fishjar:dev Sep 26, 2024
@hoilc
Copy link
Author

hoilc commented Sep 26, 2024

我将暂时先合并你的版本,但考虑到更平滑、简洁的兼容性,字段命名上,之前的prompt还是默认为systemPrompt,无需改动,然后添加新的userPrompt字段也许更好。

兼容性主要是考虑:用户升级到了新版本,但是API的配置并没有及时重新设置的情况。

按照你目前的更新,API参数经过一次浅合并后:

apiSetting: {
...DEFAULT_TRANS_APIS[translator],
...(setting.transApis[translator] || {}),
},

大概如下:

{
prompt: `You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.`,
systemPrompt: `You are a professional, authentic machine translation engine. You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.`,
}

再经过如下追加:

 if (!prompt.includes(INPUT_PLACE_TEXT)) {
    prompt += `\nSource Text: ${INPUT_PLACE_TEXT}`;
  }

最终如下:

{
prompt: `You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.\nSource Text: ${INPUT_PLACE_TEXT}`,
systemPrompt: `You are a professional, authentic machine translation engine. You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.`,
}

如果是采用增加userPrompt的方案,API参数经过一次浅合并后大概如下:

{
prompt: `You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.`,
userPrompt: `Translate the following source text from ${INPUT_PLACE_FROM} to ${INPUT_PLACE_TO}. Output translation directly without any additional text.\n\nSource Text: ${INPUT_PLACE_TEXT}\n\nTranslated Text:`,
}

可见,离升级后的推荐格式:

{
prompt: `You are a professional, authentic machine translation engine. You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.`,
userPrompt: `Translate the following source text from ${INPUT_PLACE_FROM} to ${INPUT_PLACE_TO}. Output translation directly without any additional text.\n\nSource Text: ${INPUT_PLACE_TEXT}\n\nTranslated Text:`,
}

很接近了,无需如下代码即有很好的兼容性:

 if (!prompt.includes(INPUT_PLACE_TEXT)) {
    prompt += `\nSource Text: ${INPUT_PLACE_TEXT}`;
  }

确实,去掉隐藏的判断比较好

也许可以完全使用userPromptsystemPrompt,升级体验为重置成默认值

@fishjar
Copy link
Owner

fishjar commented Sep 26, 2024

我将暂时先合并你的版本,但考虑到更平滑、简洁的兼容性,字段命名上,之前的prompt还是默认为systemPrompt,无需改动,然后添加新的userPrompt字段也许更好。
兼容性主要是考虑:用户升级到了新版本,但是API的配置并没有及时重新设置的情况。
按照你目前的更新,API参数经过一次浅合并后:

apiSetting: {
...DEFAULT_TRANS_APIS[translator],
...(setting.transApis[translator] || {}),
},

大概如下:

{
prompt: `You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.`,
systemPrompt: `You are a professional, authentic machine translation engine. You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.`,
}

再经过如下追加:

 if (!prompt.includes(INPUT_PLACE_TEXT)) {
    prompt += `\nSource Text: ${INPUT_PLACE_TEXT}`;
  }

最终如下:

{
prompt: `You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.\nSource Text: ${INPUT_PLACE_TEXT}`,
systemPrompt: `You are a professional, authentic machine translation engine. You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.`,
}

如果是采用增加userPrompt的方案,API参数经过一次浅合并后大概如下:

{
prompt: `You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.`,
userPrompt: `Translate the following source text from ${INPUT_PLACE_FROM} to ${INPUT_PLACE_TO}. Output translation directly without any additional text.\n\nSource Text: ${INPUT_PLACE_TEXT}\n\nTranslated Text:`,
}

可见,离升级后的推荐格式:

{
prompt: `You are a professional, authentic machine translation engine. You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.`,
userPrompt: `Translate the following source text from ${INPUT_PLACE_FROM} to ${INPUT_PLACE_TO}. Output translation directly without any additional text.\n\nSource Text: ${INPUT_PLACE_TEXT}\n\nTranslated Text:`,
}

很接近了,无需如下代码即有很好的兼容性:

 if (!prompt.includes(INPUT_PLACE_TEXT)) {
    prompt += `\nSource Text: ${INPUT_PLACE_TEXT}`;
  }

确实,去掉隐藏的判断比较好

也许可以完全使用userPromptsystemPrompt,升级体验为重置成默认值

废弃旧字段,强制使用新字段的默认值,可能会使得部分用户丢失数据——旧的个性化prompt
另外还有几个API也用到了prompt参数,改动会比较多。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants