Endpoint generate reading kinda working.
This commit is contained in:
@@ -8,7 +8,6 @@ from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
openai.api_key = os.getenv("OPENAI_API_KEY")
|
||||
|
||||
|
||||
MAX_TOKENS = 4097
|
||||
TOP_P = 0.9
|
||||
FREQUENCY_PENALTY = 0.5
|
||||
@@ -16,6 +15,7 @@ FREQUENCY_PENALTY = 0.5
|
||||
TRY_LIMIT = 1
|
||||
|
||||
try_count = 0
|
||||
|
||||
def process_response(input_string, quotation_check_field):
|
||||
if '{' in input_string:
|
||||
try:
|
||||
@@ -24,25 +24,42 @@ def process_response(input_string, quotation_check_field):
|
||||
# Extract everything after the first '{' (inclusive)
|
||||
result = input_string[index:]
|
||||
if re.search(r"'" + quotation_check_field + "':\s*'(.*?)'", result, re.DOTALL | re.MULTILINE):
|
||||
parsed_string = result.replace("\"", "\\\"")
|
||||
pattern = r"(?<!\w)'|'(?!\w)"
|
||||
parsed_string = re.sub(pattern, '"', parsed_string)
|
||||
parsed_string = parsed_string.replace("\\\"", "'")
|
||||
parsed_string = parsed_string.replace("\n\n", " ")
|
||||
|
||||
json_obj = json.loads(parsed_string)
|
||||
json_obj = json.loads(parse_string(result))
|
||||
return json_obj
|
||||
else:
|
||||
json_obj = json.loads(result)
|
||||
parsed_string = result.replace("\n\n", " ")
|
||||
json_obj = json.loads(parsed_string)
|
||||
return json_obj
|
||||
except Exception as e:
|
||||
print(f"Invalid JSON string! Exception: {e}")
|
||||
else:
|
||||
return input_string
|
||||
|
||||
def parse_string(to_parse: str):
|
||||
parsed_string = to_parse.replace("\"", "\\\"")
|
||||
pattern = r"(?<!\w)'|'(?!\w)"
|
||||
parsed_string = re.sub(pattern, '"', parsed_string)
|
||||
parsed_string = parsed_string.replace("\\\"", "'")
|
||||
parsed_string = parsed_string.replace("\n\n", " ")
|
||||
return parsed_string
|
||||
|
||||
|
||||
def remove_special_chars_and_escapes(input_string):
|
||||
parsed_string = input_string.replace("\\\"", "'")
|
||||
parsed_string = parsed_string.replace("\n\n", " ")
|
||||
# Define a regular expression pattern to match special characters and escapes
|
||||
pattern = r'(\\[nrt])|[^a-zA-Z0-9\s]'
|
||||
|
||||
# Use re.sub() to replace the matched patterns with an empty string
|
||||
cleaned_string = re.sub(pattern, '', parsed_string)
|
||||
|
||||
return cleaned_string
|
||||
|
||||
|
||||
def check_fields(obj, fields):
|
||||
return all(field in obj for field in fields)
|
||||
|
||||
|
||||
def make_openai_call(model, messages, token_count, fields_to_check, temperature):
|
||||
global try_count
|
||||
result = openai.ChatCompletion.create(
|
||||
@@ -69,4 +86,26 @@ def make_openai_call(model, messages, token_count, fields_to_check, temperature)
|
||||
try_count = 0
|
||||
return processed_response
|
||||
|
||||
def make_openai_instruct_call(model, message: str, token_count, fields_to_check, temperature):
|
||||
global try_count
|
||||
response = openai.Completion.create(
|
||||
model=model,
|
||||
prompt=message,
|
||||
max_tokens=int(4097 - token_count - 300),
|
||||
temperature=0.7
|
||||
)["choices"][0]["text"]
|
||||
|
||||
if fields_to_check is None:
|
||||
return remove_special_chars_and_escapes(response)
|
||||
|
||||
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)
|
||||
elif try_count >= TRY_LIMIT:
|
||||
try_count = 0
|
||||
return remove_special_chars_and_escapes(response)
|
||||
else:
|
||||
try_count = 0
|
||||
return processed_response
|
||||
|
||||
Reference in New Issue
Block a user