Skip to content

Commit

Permalink
fixed test cases and added dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Tin Chi Lo committed Nov 15, 2024
1 parent de8459b commit bae94ea
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 28 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Flask==3.0.0
pytest==7.4.0
openai==1.54.3
python-dotenv==1.0.1
python-dotenv==1.0.1
mock==4.0.3
2 changes: 2 additions & 0 deletions src/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
87 changes: 60 additions & 27 deletions test/unit/test_translator.py
Original file line number Diff line number Diff line change
@@ -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
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.")

0 comments on commit bae94ea

Please sign in to comment.