rocsky / sweep-test

MIT License
0 stars 0 forks source link

Sweep: clean the code , remove the duplication in utils.py. #1

Open rocsky opened 5 months ago

rocsky commented 5 months ago

file: utils.py

sweep-ai[bot] commented 5 months ago

🚀 Here's the PR! #2

💎 Sweep Pro: You have unlimited Sweep issues

Actions

Step 1: 🔎 Searching

(Click to expand) Here are the code search results. I'm now analyzing these search results to write the PR. https://github.com/rocsky/sweep-test/blob/ef534454201eef22f4d7ac21bfc47c4a106cf250/src/translation_agent/utils.py#L1-L686 https://github.com/rocsky/sweep-test/blob/ef534454201eef22f4d7ac21bfc47c4a106cf250/src/translation_agent/__init__.py#L1-L1

Step 2: ⌨️ Coding

I'm going to follow the following steps to help you solve the GitHub issue:

  1. Identify and remove duplicate code in the one_chunk_reflect_on_translation function in utils.py.
  2. Identify and remove duplicate code in the multichunk_reflect_on_translation function in utils.py.
  3. Identify and remove any other instances of duplicate code in utils.py.

Here are the changes we decided to make. I'm done making edits and now I'm just validating the changes using a linter to catch any mistakes like syntax errors or undefined variables:

src/translation_agent/utils.py

Extract common reflection prompt code from `one_chunk_reflect_on_translation` into `generate_reflection_prompt` function.
--- 
+++ 
@@ -1,39 +1,27 @@
-def one_chunk_reflect_on_translation(
-    source_lang: str,
-    target_lang: str,
-    source_text: str,
-    translation_1: str,
-    country: str = "",
-) -> str:
+def generate_reflection_prompt(source_lang: str, target_lang: str, country: str = "") -> str:
     """
-    Use an LLM to reflect on the translation, treating the entire text as one chunk.
+    Generate a reflection prompt for improving a translation.

     Args:
         source_lang (str): The source language of the text.
         target_lang (str): The target language of the translation.
-        source_text (str): The original text in the source language.
-        translation_1 (str): The initial translation of the source text.
         country (str): Country specified for target language.

     Returns:
-        str: The LLM's reflection on the translation, providing constructive criticism and suggestions for improvement.
+        str: The generated reflection prompt.
     """
-
-    system_message = f"You are an expert linguist specializing in translation from {source_lang} to {target_lang}. \
-You will be provided with a source text and its translation and your goal is to improve the translation."
-
     if country != "":
-        reflection_prompt = f"""Your task is to carefully read a source text and a translation from {source_lang} to {target_lang}, and then give constructive criticism and helpful suggestions to improve the translation. \
+        return f"""Your task is to carefully read a source text and a translation from {source_lang} to {target_lang}, and then give constructive criticism and helpful suggestions to improve the translation. \
 The final style and tone of the translation should match the style of {target_lang} colloquially spoken in {country}.

 The source text and initial translation, delimited by XML tags <SOURCE_TEXT></SOURCE_TEXT> and <TRANSLATION></TRANSLATION>, are as follows:

 <SOURCE_TEXT>
-{source_text}
+{{source_text}}
 </SOURCE_TEXT>

 <TRANSLATION>
-{translation_1}
+{{translation_1}}
 </TRANSLATION>

 When writing suggestions, pay attention to whether there are ways to improve the translation's \n\
@@ -47,16 +35,16 @@
 Output only the suggestions and nothing else."""

     else:
-        reflection_prompt = f"""Your task is to carefully read a source text and a translation from {source_lang} to {target_lang}, and then give constructive criticism and helpful suggestions to improve the translation. \
+        return f"""Your task is to carefully read a source text and a translation from {source_lang} to {target_lang}, and then give constructive criticism and helpful suggestions to improve the translation. \

 The source text and initial translation, delimited by XML tags <SOURCE_TEXT></SOURCE_TEXT> and <TRANSLATION></TRANSLATION>, are as follows:

 <SOURCE_TEXT>
-{source_text}
+{{source_text}}
 </SOURCE_TEXT>

 <TRANSLATION>
-{translation_1}
+{{translation_1}}
 </TRANSLATION>

 When writing suggestions, pay attention to whether there are ways to improve the translation's \n\
@@ -67,13 +55,4 @@

 Write a list of specific, helpful and constructive suggestions for improving the translation.
 Each suggestion should address one specific part of the translation.
-Output only the suggestions and nothing else."""
-
-    prompt = reflection_prompt.format(
-        source_lang=source_lang,
-        target_lang=target_lang,
-        source_text=source_text,
-        translation_1=translation_1,
-    )
-    reflection = get_completion(prompt, system_message=system_message)
-    return reflection
+Output only the suggestions and nothing else."""

src/translation_agent/utils.py

Replace duplicate prompt code in `one_chunk_reflect_on_translation` with call to `generate_reflection_prompt`.
--- 
+++ 
@@ -22,56 +22,9 @@
     system_message = f"You are an expert linguist specializing in translation from {source_lang} to {target_lang}. \
 You will be provided with a source text and its translation and your goal is to improve the translation."

-    if country != "":
-        reflection_prompt = f"""Your task is to carefully read a source text and a translation from {source_lang} to {target_lang}, and then give constructive criticism and helpful suggestions to improve the translation. \
-The final style and tone of the translation should match the style of {target_lang} colloquially spoken in {country}.
-
-The source text and initial translation, delimited by XML tags <SOURCE_TEXT></SOURCE_TEXT> and <TRANSLATION></TRANSLATION>, are as follows:
-
-<SOURCE_TEXT>
-{source_text}
-</SOURCE_TEXT>
-
-<TRANSLATION>
-{translation_1}
-</TRANSLATION>
-
-When writing suggestions, pay attention to whether there are ways to improve the translation's \n\
-(i) accuracy (by correcting errors of addition, mistranslation, omission, or untranslated text),\n\
-(ii) fluency (by applying {target_lang} grammar, spelling and punctuation rules, and ensuring there are no unnecessary repetitions),\n\
-(iii) style (by ensuring the translations reflect the style of the source text and takes into account any cultural context),\n\
-(iv) terminology (by ensuring terminology use is consistent and reflects the source text domain; and by only ensuring you use equivalent idioms {target_lang}).\n\
-
-Write a list of specific, helpful and constructive suggestions for improving the translation.
-Each suggestion should address one specific part of the translation.
-Output only the suggestions and nothing else."""
-
-    else:
-        reflection_prompt = f"""Your task is to carefully read a source text and a translation from {source_lang} to {target_lang}, and then give constructive criticism and helpful suggestions to improve the translation. \
-
-The source text and initial translation, delimited by XML tags <SOURCE_TEXT></SOURCE_TEXT> and <TRANSLATION></TRANSLATION>, are as follows:
-
-<SOURCE_TEXT>
-{source_text}
-</SOURCE_TEXT>
-
-<TRANSLATION>
-{translation_1}
-</TRANSLATION>
-
-When writing suggestions, pay attention to whether there are ways to improve the translation's \n\
-(i) accuracy (by correcting errors of addition, mistranslation, omission, or untranslated text),\n\
-(ii) fluency (by applying {target_lang} grammar, spelling and punctuation rules, and ensuring there are no unnecessary repetitions),\n\
-(iii) style (by ensuring the translations reflect the style of the source text and takes into account any cultural context),\n\
-(iv) terminology (by ensuring terminology use is consistent and reflects the source text domain; and by only ensuring you use equivalent idioms {target_lang}).\n\
-
-Write a list of specific, helpful and constructive suggestions for improving the translation.
-Each suggestion should address one specific part of the translation.
-Output only the suggestions and nothing else."""
+    reflection_prompt = generate_reflection_prompt(source_lang, target_lang, country)

     prompt = reflection_prompt.format(
-        source_lang=source_lang,
-        target_lang=target_lang,
         source_text=source_text,
         translation_1=translation_1,
     )

src/translation_agent/utils.py

Replace duplicate prompt code in `multichunk_reflect_on_translation` with call to `generate_reflection_prompt`.
--- 
+++ 
@@ -22,68 +22,7 @@
     system_message = f"You are an expert linguist specializing in translation from {source_lang} to {target_lang}. \
 You will be provided with a source text and its translation and your goal is to improve the translation."

-    if country != "":
-        reflection_prompt = """Your task is to carefully read a source text and part of a translation of that text from {source_lang} to {target_lang}, and then give constructive criticism and helpful suggestions for improving the translation.
-The final style and tone of the translation should match the style of {target_lang} colloquially spoken in {country}.
-
-The source text is below, delimited by XML tags <SOURCE_TEXT> and </SOURCE_TEXT>, and the part that has been translated
-is delimited by <TRANSLATE_THIS> and </TRANSLATE_THIS> within the source text. You can use the rest of the source text
-as context for critiquing the translated part.
-
-<SOURCE_TEXT>
-{tagged_text}
-</SOURCE_TEXT>
-
-To reiterate, only part of the text is being translated, shown here again between <TRANSLATE_THIS> and </TRANSLATE_THIS>:
-<TRANSLATE_THIS>
-{chunk_to_translate}
-</TRANSLATE_THIS>
-
-The translation of the indicated part, delimited below by <TRANSLATION> and </TRANSLATION>, is as follows:
-<TRANSLATION>
-{translation_1_chunk}
-</TRANSLATION>
-
-When writing suggestions, pay attention to whether there are ways to improve the translation's:\n\
-(i) accuracy (by correcting errors of addition, mistranslation, omission, or untranslated text),\n\
-(ii) fluency (by applying {target_lang} grammar, spelling and punctuation rules, and ensuring there are no unnecessary repetitions),\n\
-(iii) style (by ensuring the translations reflect the style of the source text and takes into account any cultural context),\n\
-(iv) terminology (by ensuring terminology use is consistent and reflects the source text domain; and by only ensuring you use equivalent idioms {target_lang}).\n\
-
-Write a list of specific, helpful and constructive suggestions for improving the translation.
-Each suggestion should address one specific part of the translation.
-Output only the suggestions and nothing else."""
-
-    else: 
-        reflection_prompt = """Your task is to carefully read a source text and part of a translation of that text from {source_lang} to {target_lang}, and then give constructive criticism and helpful suggestions for improving the translation.
-
-The source text is below, delimited by XML tags <SOURCE_TEXT> and </SOURCE_TEXT>, and the part that has been translated
-is delimited by <TRANSLATE_THIS> and </TRANSLATE_THIS> within the source text. You can use the rest of the source text
-as context for critiquing the translated part.
-
-<SOURCE_TEXT>
-{tagged_text}
-</SOURCE_TEXT>
-
-To reiterate, only part of the text is being translated, shown here again between <TRANSLATE_THIS> and </TRANSLATE_THIS>:
-<TRANSLATE_THIS>
-{chunk_to_translate}
-</TRANSLATE_THIS>
-
-The translation of the indicated part, delimited below by <TRANSLATION> and </TRANSLATION>, is as follows:
-<TRANSLATION>
-{translation_1_chunk}
-</TRANSLATION>
-
-When writing suggestions, pay attention to whether there are ways to improve the translation's:\n\
-(i) accuracy (by correcting errors of addition, mistranslation, omission, or untranslated text),\n\
-(ii) fluency (by applying {target_lang} grammar, spelling and punctuation rules, and ensuring there are no unnecessary repetitions),\n\
-(iii) style (by ensuring the translations reflect the style of the source text and takes into account any cultural context),\n\
-(iv) terminology (by ensuring terminology use is consistent and reflects the source text domain; and by only ensuring you use equivalent idioms {target_lang}).\n\
-
-Write a list of specific, helpful and constructive suggestions for improving the translation.
-Each suggestion should address one specific part of the translation.
-Output only the suggestions and nothing else."""
+    reflection_prompt = generate_reflection_prompt(source_lang, target_lang, country)

     reflection_chunks = []
     for i in range(len(source_text_chunks)):
@@ -95,23 +34,12 @@
             + "</TRANSLATE_THIS>"
             + "".join(source_text_chunks[i + 1 :])
         )
-        if country != "":
-            prompt = reflection_prompt.format(
-                source_lang=source_lang,
-                target_lang=target_lang,
-                tagged_text=tagged_text,
-                chunk_to_translate=source_text_chunks[i],
-                translation_1_chunk=translation_1_chunks[i],
-                country=country,
-            )
-        else:
-            prompt = reflection_prompt.format(
-                source_lang=source_lang,
-                target_lang=target_lang,
-                tagged_text=tagged_text,
-                chunk_to_translate=source_text_chunks[i],
-                translation_1_chunk=translation_1_chunks[i],
-            )
+
+        prompt = reflection_prompt.format(
+            tagged_text=tagged_text,
+            chunk_to_translate=source_text_chunks[i],
+            translation_1_chunk=translation_1_chunks[i],
+        )

         reflection = get_completion(prompt, system_message=system_message)
         reflection_chunks.append(reflection)

src/translation_agent/utils.py

Rewritten instructions to resolve the error. Update the original_code and new_code blocks as required, ensuring that the
--- 
+++ 
@@ -22,68 +22,7 @@
     system_message = f"You are an expert linguist specializing in translation from {source_lang} to {target_lang}. \
 You will be provided with a source text and its translation and your goal is to improve the translation."

-    if country != "":
-        reflection_prompt = """Your task is to carefully read a source text and part of a translation of that text from {source_lang} to {target_lang}, and then give constructive criticism and helpful suggestions for improving the translation.
-The final style and tone of the translation should match the style of {target_lang} colloquially spoken in {country}.
-
-The source text is below, delimited by XML tags <SOURCE_TEXT> and </SOURCE_TEXT>, and the part that has been translated
-is delimited by <TRANSLATE_THIS> and </TRANSLATE_THIS> within the source text. You can use the rest of the source text
-as context for critiquing the translated part.
-
-<SOURCE_TEXT>
-{tagged_text}
-</SOURCE_TEXT>
-
-To reiterate, only part of the text is being translated, shown here again between <TRANSLATE_THIS> and </TRANSLATE_THIS>:
-<TRANSLATE_THIS>
-{chunk_to_translate}
-</TRANSLATE_THIS>
-
-The translation of the indicated part, delimited below by <TRANSLATION> and </TRANSLATION>, is as follows:
-<TRANSLATION>
-{translation_1_chunk}
-</TRANSLATION>
-
-When writing suggestions, pay attention to whether there are ways to improve the translation's:\n\
-(i) accuracy (by correcting errors of addition, mistranslation, omission, or untranslated text),\n\
-(ii) fluency (by applying {target_lang} grammar, spelling and punctuation rules, and ensuring there are no unnecessary repetitions),\n\
-(iii) style (by ensuring the translations reflect the style of the source text and takes into account any cultural context),\n\
-(iv) terminology (by ensuring terminology use is consistent and reflects the source text domain; and by only ensuring you use equivalent idioms {target_lang}).\n\
-
-Write a list of specific, helpful and constructive suggestions for improving the translation.
-Each suggestion should address one specific part of the translation.
-Output only the suggestions and nothing else."""
-
-    else:
-        reflection_prompt = """Your task is to carefully read a source text and part of a translation of that text from {source_lang} to {target_lang}, and then give constructive criticism and helpful suggestions for improving the translation.
-
-The source text is below, delimited by XML tags <SOURCE_TEXT> and </SOURCE_TEXT>, and the part that has been translated
-is delimited by <TRANSLATE_THIS> and </TRANSLATE_THIS> within the source text. You can use the rest of the source text
-as context for critiquing the translated part.
-
-<SOURCE_TEXT>
-{tagged_text}
-</SOURCE_TEXT>
-
-To reiterate, only part of the text is being translated, shown here again between <TRANSLATE_THIS> and </TRANSLATE_THIS>:
-<TRANSLATE_THIS>
-{chunk_to_translate}
-</TRANSLATE_THIS>
-
-The translation of the indicated part, delimited below by <TRANSLATION> and </TRANSLATION>, is as follows:
-<TRANSLATION>
-{translation_1_chunk}
-</TRANSLATION>
-
-When writing suggestions, pay attention to whether there are ways to improve the translation's:\n\
-(i) accuracy (by correcting errors of addition, mistranslation, omission, or untranslated text),\n\
-(ii) fluency (by applying {target_lang} grammar, spelling and punctuation rules, and ensuring there are no unnecessary repetitions),\n\
-(iii) style (by ensuring the translations reflect the style of the source text and takes into account any cultural context),\n\
-(iv) terminology (by ensuring terminology use is consistent and reflects the source text domain; and by only ensuring you use equivalent idioms {target_lang}).\n\
-
-Write a list of specific, helpful and constructive suggestions for improving the translation.
-Each suggestion should address one specific part of the translation.
-Output only the suggestions and nothing else."""
+    reflection_prompt = generate_reflection_prompt(source_lang, target_lang, country)

     reflection_chunks = []
     for i in range(len(source_text_chunks)):
@@ -95,23 +34,12 @@
             + "</TRANSLATE_THIS>"
             + "".join(source_text_chunks[i + 1 :])
         )
-        if country != "":
-            prompt = reflection_prompt.format(
-                source_lang=source_lang,
-                target_lang=target_lang,
-                tagged_text=tagged_text,
-                chunk_to_translate=source_text_chunks[i],
-                translation_1_chunk=translation_1_chunks[i],
-                country=country,
-            )
-        else:
-            prompt = reflection_prompt.format(
-                source_lang=source_lang,
-                target_lang=target_lang,
-                tagged_text=tagged_text,
-                chunk_to_translate=source_text_chunks[i],
-                translation_1_chunk=translation_1_chunks[i],
-            )
+
+        prompt = reflection_prompt.format(
+            tagged_text=tagged_text,
+            chunk_to_translate=source_text_chunks[i],
+            translation_1_chunk=translation_1_chunks[i],
+        )

         reflection = get_completion(prompt, system_message=system_message)
         reflection_chunks.append(reflection)

Step 3: 🔄️ Validating

Your changes have been successfully made to the branch sweep/clean_the_code_remove_the_duplication. I have validated these changes using a syntax checker and a linter.


[!TIP] To recreate the pull request, edit the issue title or description.

This is an automated message generated by Sweep AI.