Decrement instead of increment, nb_retry default to VALIDATE_JSON_RETRY

pull/31/head^2^2
nervousapps 2023-04-18 13:48:13 +02:00 zatwierdzone przez GitHub
rodzic 3f333a4df7
commit 5c91c4ccba
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
1 zmienionych plików z 4 dodań i 4 usunięć

Wyświetl plik

@ -45,14 +45,14 @@ def run_script(script_name: str, script_args: List) -> str:
return result.decode("utf-8"), 0
def json_validated_response(model: str, messages: List[Dict], nb_retry: int = 0) -> Dict:
def json_validated_response(model: str, messages: List[Dict], nb_retry: int = VALIDATE_JSON_RETRY) -> Dict:
"""
This function is needed because the API can return a non-json response.
This will run recursively VALIDATE_JSON_RETRY times.
If VALIDATE_JSON_RETRY is -1, it will run recursively until a valid json response is returned.
"""
json_response = {}
if VALIDATE_JSON_RETRY == -1 or nb_retry < VALIDATE_JSON_RETRY:
if nb_retry != 0:
response = openai.ChatCompletion.create(
model=model,
messages=messages,
@ -80,8 +80,8 @@ def json_validated_response(model: str, messages: List[Dict], nb_retry: int = 0)
"content": "Your response could not be parsed by json.loads. Please restate your last message as pure JSON.",
}
)
# inc nb_retry
nb_retry+=1
# dec nb_retry
nb_retry-=1
# rerun the api call
return json_validated_response(model, messages, nb_retry)
except Exception as e: