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: allow overriding OPENAI prompt with a template #293

Merged
merged 1 commit into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Rosetta can be configured via the following parameters, to be defined in your pr
* ``ROSETTA_SHOW_OCCURRENCES``: Determines whether occurrences (where the original text appears) should be shown next to the translations for context. Defaults to ``True``.
* ``ROSETTA_CASE_SENSITIVE_FILESYSTEM``: Overrides auto-detection of case sensitive OS. Defaults to ``None`` which enables auto-detection. Useful when running case sensitive OS (e.g. Ubuntu) in docker on case insensitive OS (e.g. MacOS).
* ``OPENAI_API_KEY``: Translation suggestions using the OpenAI API. To use this service, you must first `register for the service <https://beta.openai.com/signup/>`, and set ``OPENAI_API_KEY`` to the key listed for your subscription. Requires `openai-python`. Defaults to ``None``.
* ``OPENAI_PROMPT_TEMPLATE``: Format template used to generate prompt. variables `from_language`, `to_language` and `text` are available for substitution. Defaults to ``Translate the following text from {from_language} to {to_language}:\n\n{text}``.



Expand Down
1 change: 1 addition & 0 deletions rosetta/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class RosettaSettings(object):
"GOOGLE_PROJECT_ID": ("GOOGLE_PROJECT_ID", None),
"DEEPL_AUTH_KEY": ("DEEPL_AUTH_KEY", None),
"OPENAI_API_KEY": ("OPENAI_API_KEY", None),
"OPENAI_PROMPT_TEMPLATE": ("OPENAI_PROMPT_TEMPLATE", None),
"ROSETTA_MAIN_LANGUAGE": ("MAIN_LANGUAGE", None),
"ROSETTA_MESSAGES_SOURCE_LANGUAGE_CODE": (
"MESSAGES_SOURCE_LANGUAGE_CODE",
Expand Down
9 changes: 8 additions & 1 deletion rosetta/translate_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,14 @@ def translate_by_openai(text: str, from_language: str, to_language: str, api_key

client = OpenAI(api_key=api_key)

prompt = f"Translate the following text from {from_language} to {to_language}:\n\n{text}"
default_template = "Translate the following text from {from_language} to {to_language}:\n\n{text}"
prompt_template = getattr(settings, "OPENAI_PROMPT_TEMPLATE", default_template)

prompt = prompt_template.format(**{
'from_language': from_language,
'to_language': to_language,
'text': text
})

try:
response = client.completions.create(
Expand Down
Loading