From 13c939e9f2b130856d26841f4f2ab61ad529bf3f Mon Sep 17 00:00:00 2001 From: Ayanaa Rahman Date: Mon, 26 Feb 2024 14:12:08 -0500 Subject: [PATCH] Stylistic improvements --- pontoon/gpt/openai_connection.py | 51 -------------- pontoon/machinery/gpt/openai_connection.py | 68 +++++++++++++++++++ .../management/commands/refine_translation.py | 12 ++-- 3 files changed, 74 insertions(+), 57 deletions(-) delete mode 100644 pontoon/gpt/openai_connection.py create mode 100644 pontoon/machinery/gpt/openai_connection.py rename pontoon/{base => machinery}/management/commands/refine_translation.py (72%) diff --git a/pontoon/gpt/openai_connection.py b/pontoon/gpt/openai_connection.py deleted file mode 100644 index 1d8ff2c056..0000000000 --- a/pontoon/gpt/openai_connection.py +++ /dev/null @@ -1,51 +0,0 @@ -from openai import OpenAI - - -class OpenAIService: - def __init__(self): - self.client = OpenAI() - - def get_translation( - self, english_text, translated_text, characteristic, target_locale_name - ): - system_messages = { - "informal": f"""You will be provided with text in English, along with its machine-generated translation in {target_locale_name}. Your objective is to revise the translation to ensure it utilizes simpler language. Please adhere to the following guidelines to achieve this: - - Clarity is Key: Ensure the translation conveys the original message in the clearest possible manner, without ambiguity or unnecessary complexity. - - Consistent Simplicity: Maintain a consistent level of simplicity throughout the translation. - The goal is to produce a translation that accurately reflects the original English text, but in a way that is more approachable and easier to understand for all {target_locale_name} speakers, regardless of their language proficiency.""", - "formal": f"""You will be presented with text in English, accompanied by its machine-generated translation in {target_locale_name}. Your task is to refine the {target_locale_name} translation to ensure it adheres to a higher level of formality. In doing so, consider the following guidelines: - - Adjust the Tone: Ensure the tone is respectful, polished, and devoid of colloquialisms or informal expressions commonly used in casual conversation. - - Formal Addressing: Where applicable, use formal modes of address. - - Consistency: Maintain a consistent level of formality throughout the translation, avoiding shifts in tone or style. - Your goal is to produce a translation that not only accurately conveys the meaning of the English text but also meets the expectations for formality in {target_locale_name}-speaking professional or formal settings.""", - "alternative": f"""You will receive text in English along with its machine-generated translation in {target_locale_name}. Your task is to provide an alternative translation, considering the following guidelines: - - Cultural Nuances: Pay attention to cultural nuances and idiomatic expressions, ensuring they are appropriately translated for the {target_locale_name}-speaking audience. - - Clarification and Accuracy: Where the source text is ambiguous or idiomatic, offer clarifications or alternative expressions in {target_locale_name}. - Just provide the text for the alternative translation.""", - } - - system_message = system_messages.get(characteristic) - if system_message is None: - raise ValueError(f"Unrecognized characteristic: '{characteristic}'") - - # Construct the user prompt with the locale name - user_prompt = f"Refine the following {target_locale_name} machine translation to make it {characteristic}: '{translated_text}' based on the original English text: '{english_text}'." - - # Call the OpenAI API with the constructed prompt - response = self.client.chat.completions.create( - model="gpt-4-0125-preview", - messages=[ - {"role": "system", "content": system_message}, - {"role": "user", "content": user_prompt}, - ], - temperature=0, # Set temperature to 0 for deterministic output - top_p=1, # Set top_p to 1 to consider the full distribution - ) - - # Extract the content attribute from the response - translation = ( - response.choices[0].message.content - if response.choices[0].message.content - else "" - ) - return translation.strip() diff --git a/pontoon/machinery/gpt/openai_connection.py b/pontoon/machinery/gpt/openai_connection.py new file mode 100644 index 0000000000..0f0db340b9 --- /dev/null +++ b/pontoon/machinery/gpt/openai_connection.py @@ -0,0 +1,68 @@ +from openai import OpenAI +from pontoon.base.models import Locale + + +class OpenAIService: + def __init__(self): + self.client = OpenAI() + + def get_translation( + self, english_text, translated_text, characteristic, target_language_name + ): + try: + target_language = Locale.objects.get(name=target_language_name) + except Locale.DoesNotExist: + raise ValueError( + f"The target language '{target_language_name}' is not supported." + ) + + informal = ( + f"You will be provided with text in English, along with its machine-generated translation in {target_language}. " + "Your objective is to revise the {target_language} translation to ensure it utilizes simpler language. Adhere to the following guidelines to achieve this:\n" + "- Clarity is Key: Ensure the translation conveys the original message in the clearest possible manner, without ambiguity or unnecessary complexity.\n" + "- Consistent Simplicity: Maintain a consistent level of simplicity throughout the translation.\n" + "The goal is to produce a translation that accurately reflects the original English text, but in a way that is more approachable and easier to understand for all {target_language} speakers." + ) + + formal = ( + f"You will be provided with text in English, along with its machine-generated translation in {target_language}. " + "Your objective is to revise the {target_language} translation to ensure it adheres to a higher level of formality. Adhere to the following guidelines to achieve this:\n" + "- Adjust the Tone: Ensure the tone is respectful, polished, and devoid of colloquialisms or informal expressions commonly used in casual conversation.\n" + "- Formal Addressing: Where applicable, use formal modes of address.\n" + "- Consistency: Maintain a consistent level of formality throughout the translation, avoiding shifts in tone or style.\n" + "Your goal is to produce a translation that not only accurately conveys the meaning of the English text but also meets the expectations for formality in {target_language}-speaking professional or formal settings." + ) + + alternative = ( + f"You will provided text in English along with its machine-generated translation in {target_language}. " + "Your objective is to provide an alternative translation, Adhere to the following guidelines to achieve this:\n" + "- Cultural Nuances: Pay attention to cultural nuances and idiomatic expressions, ensuring they are appropriately translated for the {target_language}-speaking audience.\n" + "- Clarification and Accuracy: Where the source text is ambiguous or idiomatic, offer clarifications or alternative expressions in {target_language}.\n" + "Just provide the text for the alternative translation." + ) + + system_messages = { + "informal": informal, + "formal": formal, + "alternative": alternative, + } + + system_message = system_messages.get(characteristic) + if system_message is None: + raise ValueError(f"Unrecognized characteristic: '{characteristic}'") + + # Construct the user prompt with the language name + user_prompt = f"Refine the following {target_language} machine translation to make it {characteristic}: '{translated_text}' based on the original English text: '{english_text}'." + + # Call the OpenAI API with the constructed prompt + response = self.client.chat.completions.create( + model="gpt-4-0125-preview", + messages=[ + {"role": "system", "content": system_message}, + {"role": "user", "content": user_prompt}, + ], + temperature=0, # Set temperature to 0 for deterministic output + top_p=1, # Set top_p to 1 to consider the full distribution + ) + + return response.choices[0].message.content.strip() diff --git a/pontoon/base/management/commands/refine_translation.py b/pontoon/machinery/management/commands/refine_translation.py similarity index 72% rename from pontoon/base/management/commands/refine_translation.py rename to pontoon/machinery/management/commands/refine_translation.py index a9371db2c8..e369e3241e 100644 --- a/pontoon/base/management/commands/refine_translation.py +++ b/pontoon/machinery/management/commands/refine_translation.py @@ -1,9 +1,9 @@ from django.core.management.base import BaseCommand -from pontoon.gpt.openai_connection import OpenAIService +from pontoon.machinery.gpt.openai_connection import OpenAIService class Command(BaseCommand): - help = "Refines machine translations using OpenAI with specified characteristics" + help = "Refines machine translations using OpenAI's GPT-4 API with specified characteristics" def add_arguments(self, parser): parser.add_argument( @@ -19,19 +19,19 @@ def add_arguments(self, parser): "target_text", type=str, help="The machine-generated translation to refine" ) parser.add_argument( - "locale_name", + "language_name", type=str, - help="The name of the target locale for the translation", + help="The name of the target language for the translation", ) def handle(self, *args, **options): characteristic = options["characteristic"] english_text = options["english_text"] target_text = options["target_text"] - locale_name = options["locale_name"] + language_name = options["language_name"] translator = OpenAIService() translation = translator.get_translation( - english_text, target_text, characteristic, locale_name + english_text, target_text, characteristic, language_name ) self.stdout.write(self.style.SUCCESS(translation))