From 1756764a864b776bfbc258c2efcd124fc74c15b6 Mon Sep 17 00:00:00 2001 From: Marco Bonetti Date: Sat, 6 Jul 2024 17:28:14 +0200 Subject: [PATCH] Update changelog for #293 --- CHANGES | 1 + docs/settings.rst | 2 +- rosetta/translate_utils.py | 23 ++++++++++++----------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/CHANGES b/CHANGES index 1704bb1..3ea7726 100644 --- a/CHANGES +++ b/CHANGES @@ -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 diff --git a/docs/settings.rst b/docs/settings.rst index b9de74c..44cc51a 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -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 `, 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}``. diff --git a/rosetta/translate_utils.py b/rosetta/translate_utils.py index 2b673bb..5e5b08c 100644 --- a/rosetta/translate_utils.py +++ b/rosetta/translate_utils.py @@ -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. @@ -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: