Add paragraphMatch.
This commit is contained in:
@@ -61,8 +61,12 @@ def process_response(input_string, quotation_check_field):
|
||||
json_obj = json.loads(parse_string(result))
|
||||
return json_obj
|
||||
else:
|
||||
parsed_string = result.replace("\n\n", " ")
|
||||
parsed_string = parsed_string.replace("\n", " ")
|
||||
if "title" in result:
|
||||
parsed_string = result.replace("\n\n", "\n")
|
||||
parsed_string = parsed_string.replace("\n", "**paragraph**")
|
||||
else:
|
||||
parsed_string = result.replace("\n\n", " ")
|
||||
parsed_string = parsed_string.replace("\n", " ")
|
||||
parsed_string = re.sub(r',\s*]', ']', parsed_string)
|
||||
parsed_string = re.sub(r',\s*}', '}', parsed_string)
|
||||
if (parsed_string.find('[') == -1) and (parsed_string.find(']') == -1):
|
||||
@@ -177,9 +181,11 @@ def make_openai_instruct_call(model, message: str, token_count, fields_to_check,
|
||||
try_count = try_count + 1
|
||||
return make_openai_instruct_call(model, message, token_count, fields_to_check, temperature)
|
||||
elif has_blacklisted_words(response) and try_count >= TRY_LIMIT:
|
||||
try_count = 0
|
||||
return ""
|
||||
|
||||
if fields_to_check is None:
|
||||
try_count = 0
|
||||
return response.replace("\n\n", " ").strip()
|
||||
|
||||
response = remove_special_characters_from_beginning(response)
|
||||
@@ -189,13 +195,13 @@ def make_openai_instruct_call(model, message: str, token_count, fields_to_check,
|
||||
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:
|
||||
reparagraphed_response = replace_expression_in_object(processed_response, "**paragraph**", "\n")
|
||||
if check_fields(reparagraphed_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
|
||||
return reparagraphed_response
|
||||
except Exception as e:
|
||||
return make_openai_instruct_call(model, message, token_count, fields_to_check, temperature)
|
||||
|
||||
@@ -300,3 +306,15 @@ def remove_special_characters_from_beginning(string):
|
||||
return cleaned_string[:-1]
|
||||
else:
|
||||
return cleaned_string
|
||||
|
||||
|
||||
def replace_expression_in_object(obj, expression, replacement):
|
||||
if isinstance(obj, dict):
|
||||
for key in obj:
|
||||
if isinstance(obj[key], str):
|
||||
obj[key] = obj[key].replace(expression, replacement)
|
||||
elif isinstance(obj[key], list):
|
||||
obj[key] = [replace_expression_in_object(item, expression, replacement) for item in obj[key]]
|
||||
elif isinstance(obj[key], dict):
|
||||
obj[key] = replace_expression_in_object(obj[key], expression, replacement)
|
||||
return obj
|
||||
|
||||
Reference in New Issue
Block a user