diff --git a/requirements.txt b/requirements.txt index 635fa5f..c983d1f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ Flask==3.0.0 pytest==7.4.0 openai==1.54.3 -python-dotenv==1.0.1 \ No newline at end of file +python-dotenv==1.0.1 +mock==4.0.3 \ No newline at end of file diff --git a/src/translator.py b/src/translator.py index 19f8501..38aa498 100644 --- a/src/translator.py +++ b/src/translator.py @@ -28,6 +28,8 @@ def get_language(post: str) -> str: ) return response.choices[0].message.content + +def translate_content(post: str) -> tuple[bool, str]: def query_llm_robust(post: str) -> tuple[bool, str]: translation_context = "The team is implementing a translation feature for NodeBB, a forum that allows instructors and students to make posts. Your task is to translate the content of English and non-English posts into English. For inputs that are non-english strings, you will translate into college-level English. If inputs are in English, return the input but with spelling and grammar corrections." diff --git a/test/unit/test_translator.py b/test/unit/test_translator.py index 67cf52b..c64dc9f 100644 --- a/test/unit/test_translator.py +++ b/test/unit/test_translator.py @@ -1,45 +1,78 @@ from src.translator import translate_content -# from mock import patch +from src.translator import query_llm_robust +from openai import AzureOpenAI +import os +from mock import patch + +client = AzureOpenAI( + api_key = os.getenv("AZURE_OPENAI_API_KEY"), + api_version = "2024-02-15-preview", + azure_endpoint = "https://translator-service-hunan-hunters.azurewebsites.net/" +) def test_chinese(): - is_english, translated_content = translate_content("这是一条中文消息") + is_english, translated_content = query_llm_robust("这是一条中文消息") assert is_english == False assert translated_content == "This is a Chinese message" def test_english(): - is_english, translated_content = translate_content("This is an English message") + is_english, translated_content = query_llm_robust("This is an English message") assert is_english == True assert translated_content == "This is an English message" -# @patch.object(client.chat.completions, 'create') -# def test_unexpected_language(mocker): -# # we mock the model's response to return a random message -# mocker.return_value.choices[0].message.content = "I don't understand your request" -# assert query_llm_robust("Hola, cómo estás.") == (False, "Sorry, we are unable to understand the post.") +def test_mixed(): + is_english, translated_content = query_llm_robust("Hello 世界") + assert is_english == False + assert translated_content == "Hello World" -# @patch.object(client.chat.completions, 'create') -# def test_empty_response(mocker): -# mock_response = mocker.return_value -# mock_response.choices[0].message.content = "" +def test_numbers(): + is_english, translated_content = query_llm_robust("12345 世界") + assert is_english == False + assert translated_content == "12345 World" -# assert query_llm_robust("Hola, cómo estás.") == (False, "Sorry, we are unable to provide a translation at this moment. Please try again later!") +def test_puncutation(): + is_english, translated_content = query_llm_robust("Hello, how are you?") + assert is_english == True + assert translated_content == "Hello, how are you?" -# @patch.object(client.chat.completions, 'create') -# def test_none_response(mocker): -# mock_response = mocker.return_value -# mock_response.choices[0].message.content = None +def test_emojis(): + is_english, translated_content = query_llm_robust("Bonjour 🌟") + assert is_english == False + assert translated_content == "Hello 🌟" -# assert query_llm_robust("Hola, cómo estás.") == (False, "Sorry, we are unable to provide a translation at this moment. Please try again later!") +def test_llm_normal_response(): + is_english, translated_content = query_llm_robust("Bonjour tout le monde") + assert is_english == False + assert translated_content == "Hello everyone" -# @patch.object(client.chat.completions, 'create') -# def test_wrong_language_response(mocker): -# mock_response = mocker.return_value -# mock_response.choices[0].message.content = "Hola, cómo estás." +def test_llm_gibberish_response(): + is_english, translated_content = query_llm_robust("asd8&*(!@#") + assert is_english == False + assert translated_content == "Not Translatable" -# assert query_llm_robust("Hola, cómo estás.") == (False, "Sorry, we are unable to provide an English Translation.") +@patch.object(client.chat.completions, 'create') +def test_unexpected_language(mocker): + # we mock the model's response to return a random message + mocker.return_value.choices[0].message.content = "I don't understand your request" + assert query_llm_robust("Hola, cómo estás.") == (False, "Sorry, we are unable to understand the post.") -def test_llm_normal_response(): - pass +@patch.object(client.chat.completions, 'create') +def test_empty_response(mocker): + mock_response = mocker.return_value + mock_response.choices[0].message.content = "" -def test_llm_gibberish_response(): - pass \ No newline at end of file + assert query_llm_robust("Hola, cómo estás.") == (False, "Sorry, we are unable to provide a translation at this moment. Please try again later!") + +@patch.object(client.chat.completions, 'create') +def test_none_response(mocker): + mock_response = mocker.return_value + mock_response.choices[0].message.content = None + + assert query_llm_robust("Hola, cómo estás.") == (False, "Sorry, we are unable to provide a translation at this moment. Please try again later!") + +@patch.object(client.chat.completions, 'create') +def test_wrong_language_response(mocker): + mock_response = mocker.return_value + mock_response.choices[0].message.content = "Hola, cómo estás." + + assert query_llm_robust("Hola, cómo estás.") == (False, "Sorry, we are unable to provide an English Translation.") \ No newline at end of file