Skip to content

Commit

Permalink
Set max failures
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-XT committed May 7, 2024
1 parent 5b012ed commit 4e8e717
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions convertanything/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
from enum import Enum
from pydantic import BaseModel
from typing import Type, get_args, get_origin, Union, List
import json
import openai
import uuid
from enum import Enum
import logging
import os


logging.basicConfig(
level=os.environ.get("LOGLEVEL", "INFO"),
format="%(asctime)s | %(levelname)s | %(message)s",
)


def convertanything(
input_string: str,
model: Type[BaseModel],
server="https://api.openai.com",
api_key=None,
llm="gpt-3.5-turbo-16k",
server: str = "https://api.openai.com",
api_key: str = None,
llm: str = "gpt-3.5-turbo-16k",
max_failures: int = 3,
**kwargs,
):
input_string = str(input_string)
Expand Down Expand Up @@ -65,15 +74,30 @@ def convertanything(
response = json.loads(response)
return model(**response)
except Exception as e:
print(e)
print(response)
print("Failed to convert the response to the model, trying again.")
if "failures" in kwargs:
failures = int(kwargs["failures"]) + 1
if failures > max_failures:
logging.error(
f"Error: {e} . Failed to convert the response to the model after 3 attempts. Response: {response}"
)
return (
response
if response
else "Failed to convert the response to the model."
)
else:
failures = 1
logging.warning(
f"Error: {e} . Failed to convert the response to the model, trying again. {failures}/3 failures. Response: {response}"
)
return convertanything(
input_string=input_string,
model=model,
server=server,
api_key=api_key,
llm=llm,
max_failures=max_failures,
failures=failures,
**kwargs,
)

Expand Down

0 comments on commit 4e8e717

Please sign in to comment.