Fix level exam generation

This commit is contained in:
Cristiano Ferreira
2024-02-17 15:40:19 +00:00
parent a200b29dba
commit 1bd012d340
3 changed files with 84 additions and 47 deletions

View File

@@ -140,19 +140,25 @@ def make_openai_call(model, messages, token_count, fields_to_check, temperature)
top_p=float(TOP_P),
frequency_penalty=float(FREQUENCY_PENALTY),
messages=messages
)
)["choices"][0]["message"]["content"]
if has_blacklisted_words(result) and try_count < TRY_LIMIT:
try_count = try_count + 1
return make_openai_call(model, messages, token_count, fields_to_check, temperature)
elif has_blacklisted_words(result) and try_count >= TRY_LIMIT:
return ""
if fields_to_check is None:
return result["choices"][0]["message"]["content"]
return result.replace("\n\n", " ").strip()
processed_response = process_response(result["choices"][0]["message"]["content"], fields_to_check[0])
processed_response = process_response(result, fields_to_check[0])
if check_fields(processed_response, fields_to_check) is False and try_count < TRY_LIMIT:
try_count = try_count + 1
return make_openai_call(model, messages, token_count, fields_to_check, temperature)
elif try_count >= TRY_LIMIT:
try_count = 0
return result["choices"][0]["message"]["content"]
return result
else:
try_count = 0
return processed_response
@@ -176,13 +182,22 @@ def make_openai_instruct_call(model, message: str, token_count, fields_to_check,
if fields_to_check is None:
return response.replace("\n\n", " ").strip()
processed_response = process_response(response, fields_to_check[0])
if check_fields(processed_response, fields_to_check) is False and try_count < TRY_LIMIT:
try_count = try_count + 1
response = remove_special_characters_from_beginning(response)
if response[0] != "{" and response[0] != '"':
response = "{\"" + response
if not response.endswith("}"):
response = response + "}"
try:
processed_response = process_response(response, fields_to_check[0])
if check_fields(processed_response, fields_to_check) is False and try_count < TRY_LIMIT:
try_count = try_count + 1
return make_openai_instruct_call(model, message, token_count, fields_to_check, temperature)
else:
try_count = 0
return processed_response
except Exception as e:
return make_openai_instruct_call(model, message, token_count, fields_to_check, temperature)
else:
try_count = 0
return processed_response
# GRADING SUMMARY
@@ -275,3 +290,13 @@ def get_speaking_corrections(text):
def has_blacklisted_words(text: str):
text_lower = text.lower()
return any(word in text_lower for word in BLACKLISTED_WORDS)
def remove_special_characters_from_beginning(string):
cleaned_string = string.lstrip('\n')
if string.startswith("'") or string.startswith('"'):
cleaned_string = string[1:]
if cleaned_string.endswith('"'):
return cleaned_string[:-1]
else:
return cleaned_string