Skip to content

Commit

Permalink
Update changelog for #293
Browse files Browse the repository at this point in the history
  • Loading branch information
mbi committed Jul 6, 2024
1 parent f899930 commit 1756764
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Version 0.10.1 (unreleased)
* Improve error handling when using the Deepl translation service (#288, thank you @halitcelik)
* Add support for translation using OpenAI API (#275, #291, thank you @ydf, PR by @vusallyv)
* Adds Azerbaijani translation (#284 thanks @vusallyv and @nemoralis)
* Allow overriding OpenAI prompt with a template (PR #293, thanks @danleyb2)


Version 0.10.0
Expand Down
2 changes: 1 addition & 1 deletion docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +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}``.
* ``OPENAI_PROMPT_TEMPLATE``: Format template used to generate prompt when translating with OpenAI. 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
23 changes: 12 additions & 11 deletions rosetta/translate_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ def translate_by_google(
return str(api_response.translations[0].translated_text)


def translate_by_openai(text: str, from_language: str, to_language: str, api_key: str) -> str:
def translate_by_openai(
text: str, from_language: str, to_language: str, api_key: str
) -> str:
"""
Translate text using OpenAI's GPT-3.5-turbo-instruct engine.
param text: The text to translate.
Expand All @@ -185,20 +187,19 @@ def translate_by_openai(text: str, from_language: str, to_language: str, api_key
from openai import OpenAI

client = OpenAI(api_key=api_key)
prompt_template = getattr(
settings,
"OPENAI_PROMPT_TEMPLATE",
"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
})
prompt = prompt_template.format(
**{"from_language": from_language, "to_language": to_language, "text": text}
)

try:
response = client.completions.create(
model="gpt-3.5-turbo-instruct",
prompt=prompt
model="gpt-3.5-turbo-instruct", prompt=prompt
)
translation = response.choices[0].text.strip()
except Exception as e:
Expand Down

0 comments on commit 1756764

Please sign in to comment.