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

向 OpenAI 翻译服务中提供更多的上下文信息 #1715

Open
Vigilans opened this issue May 16, 2024 · 1 comment
Open

向 OpenAI 翻译服务中提供更多的上下文信息 #1715

Vigilans opened this issue May 16, 2024 · 1 comment
Labels
discuss enhancement New update or improvement request

Comments

@Vigilans
Copy link

Vigilans commented May 16, 2024

您希望的更新和改进是什么 | Update or Improve

目前的 OpenAI 服务中支持{{ }}插值语法插入若干动态信息,这是一个非常好的用来自定义更强大的 Prompt 的工具。

因此,希望沉浸式翻译可以支持在模板中提供更多的原文/源网页信息,让 GPT 可以更好地定位到上下文从而提高翻译质量。

补充说明 | Additional context

我目前在使用体验中总结出的几个应该会很有用的上下文信息:

网页的 Host / URL

将当前翻译的网页域名提供给 GPT,使其能根据域名定位到译文所应在的知识领域。例如,将github.com提供给 prompt,GPT 可以知道译文中有计算机相关专业术语,按照开发者的理解进行翻译(如 native -> 原生,而非本地)。

网页的 Title

在新闻类和论坛类网站中(例如雅虎、reddit等等),网页的标题能很好地约束译文所在的上下文;并且在翻译中间段落时,标题也能作为全局信息让 GPT 更不容易偏题。

使用目标语言(而非英语)表示的目标语言字段

目前,{{to}} 字段返回的是用英语表示的目标语言,如简体中文返回Simplified Chinese / Simplified Chinese Language。这导致发送给 GPT 的原文 user prompt 中,完全不包含目标语言的文字。

在翻译一些日文网站时,我发现 GPT 3.5 有时会输出英文翻译,有时会直接输出日文原文不翻译。一定程度上这可能是因为原 Prompt 中不包含任何中文汉字造成的。如果{{ to }}字段(或一个新字段,然后两个字段可以在样例原文/翻译中结合使用)能插值为用目标语言表示的文字(如简体中文日本語),那么就可以为 GPT 的 prompt 添加一定目标语言的上下文信息。

@Vigilans Vigilans added the enhancement New update or improvement request label May 16, 2024
@Vigilans
Copy link
Author

Vigilans commented May 16, 2024

附:个人目前使用的 Prompt:

System prompt

You are a professional, authentic translation engine.
Policy:

  1. Input are multi-document YAML-formatted text. The first and second document are example request and response, and the last document is the actual request you need to translate. Your response should continue the YAML documents to provide translation of the last request document.
  2. Each response's element should be the translation of {{imt_source_field}} field in request's element according to its id, with target language specified in translate-to field in comment.
  3. Request texts are picked from same source (e.g. a website, or video subtitle), you can try to take them as a whole for better understanding of the context, but response's YAML list length must be the same as request's YAML list length.

User prompt

# type: source
# scenario: Website Paragraphs
# translate-to: Simplified Chinese Language
- id: 1
  {{imt_source_field}}: Translate all `{{imt_source_field}}` fields and write the translation result into the `{{imt_trans_field}}` field.
- id: 2
  {{imt_source_field}}: If the image has not been updated in the target registry, the cached image is served from the proxy cache project.
- id: 3
  {{imt_source_field}}: Monocle will be implemented as a native layout using stacking as its backing implementation.
---
# type: translation
# scenario: Website Paragraphs
# translate-to: 简体中文
- id: 1
  {{imt_trans_field}}: 翻译所有`{{imt_source_field}}`字段,并将翻译结果写入`{{imt_trans_field}}`字段。
- id: 2
  {{imt_trans_field}}: 如果镜像在目标注册表中未更新,则通过代理缓存项目提供缓存的镜像。
- id: 3
  {{imt_trans_field}}: Monocle 将实现为原生布局,使用 Stacking 作为其底层实现。
---
# type: source
# scenario: Website Paragraphs
# translate-to: {{to}}
{{yaml}}

设计考量

  1. 输入是完全符合语法规范的 Mutli-document YAML。GPT也将据此输出符合语法规范的YAML格式。
  2. 上下文语义信息通过 YAML 注释进行添加。这可以不破坏 YAML 的语法结构,并且 GPT 输出/复读上下文信息时也会写在注释里面,从而不会干扰沉浸式翻译的 YAML 解析(这些上下文注释会被丢弃掉)。
  3. 遵循 Zero, One and Few Shot Prompting ChatGPTNextWeb/ChatGPT-Next-Web#138 中的规范,提供了样例的输入/输出 YAML 文档作为 Few Shot。这样可以:
    1. 通过提供样例对话,进一步规范 GPT 的输出格式;
    2. 可以支持用户提供自定义的例句翻译参考,从而使 GPT 提供更符合特定知识领域/用户风格喜好的翻译结果。
    3. 在样例回复中,这个 Prompt 让 GPT 复读了上下文注释,并修改了若干字段,如type: source -> type: translation, translate-to: Simplified Chinese Language -> translate-to: 简体中文)。这刷新了 GPT 上下文信息的距离,避免原文太长 GPT 可能忘了最初的上下文信息导致乱输出。并且让 GPT 在正式输出翻译前输出“简体中文”几个字,使其在后面翻译不会输出英文/日文原文。

P.S 如果沉浸式翻译能按照 ChatGPTNextWeb/ChatGPT-Next-Web#138 的设计,给用户提供例句和例答的设置框用以提供样例对话就更好了。发给 GPT 的对话流如下:

  1. System Prompt
  2. User Prompt 1 (例句原文输入)
  3. Assitant Prompt 1 (例句译文输出)
  4. User Prompt 2 (真正的原文输入)

支持本 Issue 的功能后的 User Prompt

# type: source
# scenario: Website Paragraphs -> 在字幕里是 Video Subtitle
# host: {{host}} -> github.com
# title: {{title}} -> '向 OpenAI 翻译服务中提供更多的上下文信息 · Issue #1715 · immersive-translate/immersive-translate'
# translate-to: {{to}} -> 简体中文

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discuss enhancement New update or improvement request
Projects
None yet
Development

No branches or pull requests

2 participants