aifail


Nameaifail JSON
Version 0.3.0 PyPI version JSON
download
home_pageNone
SummaryNone
upload_time2024-07-27 18:31:52
maintainerNone
docs_urlNone
authorDev Aggarwal
requires_python<4.0,>=3.10
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # AiFail
Python Library for retrying openai/gcp API calls    

### Installation

```bash
pip install aifail
```

### Usage

To get started, simply wrap your functions with `@retry_if`, specifying the condition to retry on.

https://github.com/GooeyAI/aifail/blob/a540c05a2a9436c0b6b1caab8ed823387999d5f9/examples/basic_openai.py#L5

https://github.com/GooeyAI/aifail/blob/a540c05a2a9436c0b6b1caab8ed823387999d5f9/examples/basic_openai.py#L13-L30


### Custom logic

You can use this with anything that needs retrying, e.g. google sheets -


```py
def sheets_api_should_retry(e: Exception) -> bool:
    return isinstance(e, HttpError) and (
        e.resp.status in (408, 429) or e.resp.status > 500
    )


@retry_if(sheets_api_should_retry)
def update_cell(spreadsheet_id: str, row: int, col: int, value: str):
    get_spreadsheet_service().values().update(
        spreadsheetId=spreadsheet_id,
        range=f"{col_i2a(col)}{row}:{col_i2a(col)}{row}",
        body={"values": [[value]]},
        valueInputOption="RAW",
    ).execute()
```

### Advanced Usage

This library is used by GooeyAI in production to handle thousands of API calls every day. 
To save costs and handle rate limits, you can intelligently specify quick fallbacks (eg azure openai) -

https://github.com/GooeyAI/aifail/blob/a540c05a2a9436c0b6b1caab8ed823387999d5f9/examples/azure_openai_fallback.py#L7-L16

https://github.com/GooeyAI/aifail/blob/a540c05a2a9436c0b6b1caab8ed823387999d5f9/examples/azure_openai_fallback.py#L19-L35

### Traceable Errors

AiFail comes with a built in logger, and outputs complete stack traces tracing the error back to the original call site.

```bash
# python examples/azure_openai_fallback.py 
2023-11-18 04:36:01.364 | WARNING  | aifail.aifail:try_all:63 - [2/2] tyring next fn, prev_exc=NotFoundError("Error code: 404 - {'error': {'code': 'DeploymentNotFound', 'message': 'The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.'}}")
2023-11-18 04:36:01.778 | WARNING  | aifail.aifail:wrapper:98 - [1/1] captured error, retry_delay=0.4117675457681431s, exc=NotFoundError("Error code: 404 - {'error': {'message': 'The model `gpt-4-x` does not exist', 'type': 'invalid_request_error', 'param': None, 'code': 'model_not_found'}}")
2023-11-18 04:36:02.483 | WARNING  | aifail.aifail:try_all:63 - [2/2] tyring next fn, prev_exc=NotFoundError("Error code: 404 - {'error': {'code': 'DeploymentNotFound', 'message': 'The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.'}}")
2023-11-18 04:36:04.093 | WARNING  | aifail.aifail:wrapper:98 - [2/1] captured error, retry_delay=0.9974197744911488s, exc=NotFoundError("Error code: 404 - {'error': {'message': 'The model `gpt-4-x` does not exist', 'type': 'invalid_request_error', 'param': None, 'code': 'model_not_found'}}")
Traceback (most recent call last):
  File "/Users/dev/Projects/dara/aifail/aifail/aifail.py", line 65, in try_all
    return fn()
  File "/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py", line 28, in <lambda>
    lambda: azure_client.chat.completions.create(
  ...
openai.NotFoundError: Error code: 404 - {'error': {'code': 'DeploymentNotFound', 'message': 'The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.'}}

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/dev/Projects/dara/aifail/aifail/aifail.py", line 86, in wrapper
    return fn(*args, **kwargs)
  File "/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py", line 26, in chad_gpt4
    response = try_all(
  File "/Users/dev/Projects/dara/aifail/aifail/aifail.py", line 69, in try_all
    raise prev_exc
  File "/Users/dev/Projects/dara/aifail/aifail/aifail.py", line 65, in try_all
    return fn()
  File "/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py", line 34, in <lambda>
    lambda: openai_client.chat.completions.create(
  ...
openai.NotFoundError: Error code: 404 - {'error': {'message': 'The model `gpt-4-x` does not exist', 'type': 'invalid_request_error', 'param': None, 'code': 'model_not_found'}}

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/dev/Projects/dara/aifail/aifail/aifail.py", line 65, in try_all
    return fn()
  File "/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py", line 28, in <lambda>
    lambda: azure_client.chat.completions.create(
  ...
openai.NotFoundError: Error code: 404 - {'error': {'code': 'DeploymentNotFound', 'message': 'The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.'}}

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py", line 43, in <module>
    chad_gpt4(
  File "/Users/dev/Projects/dara/aifail/aifail/aifail.py", line 102, in wrapper
    raise prev_exc
  File "/Users/dev/Projects/dara/aifail/aifail/aifail.py", line 86, in wrapper
    return fn(*args, **kwargs)
  File "/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py", line 26, in chad_gpt4
    response = try_all(
  File "/Users/dev/Projects/dara/aifail/aifail/aifail.py", line 69, in try_all
    raise prev_exc
  File "/Users/dev/Projects/dara/aifail/aifail/aifail.py", line 65, in try_all
    return fn()
  File "/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py", line 34, in <lambda>
    lambda: openai_client.chat.completions.create(
  ...
openai.NotFoundError: Error code: 404 - {'error': {'message': 'The model `gpt-4-x` does not exist', 'type': 'invalid_request_error', 'param': None, 'code': 'model_not_found'}}

Process finished with exit code 1
```

Sentry too, will capture the entire retry loop

<img width="573" alt="image" src="https://github.com/GooeyAI/aifail/assets/19492893/958e4a74-4159-4784-a69c-e50e45b47494">


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "aifail",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.10",
    "maintainer_email": null,
    "keywords": null,
    "author": "Dev Aggarwal",
    "author_email": "devxpy@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/d4/23/077dea2ad51ed1b4590d66b852ea88641e53f6cc13d178f616b5b4fcdc8a/aifail-0.3.0.tar.gz",
    "platform": null,
    "description": "# AiFail\nPython Library for retrying openai/gcp API calls    \n\n### Installation\n\n```bash\npip install aifail\n```\n\n### Usage\n\nTo get started, simply wrap your functions with `@retry_if`, specifying the condition to retry on.\n\nhttps://github.com/GooeyAI/aifail/blob/a540c05a2a9436c0b6b1caab8ed823387999d5f9/examples/basic_openai.py#L5\n\nhttps://github.com/GooeyAI/aifail/blob/a540c05a2a9436c0b6b1caab8ed823387999d5f9/examples/basic_openai.py#L13-L30\n\n\n### Custom logic\n\nYou can use this with anything that needs retrying, e.g. google sheets -\n\n\n```py\ndef sheets_api_should_retry(e: Exception) -> bool:\n    return isinstance(e, HttpError) and (\n        e.resp.status in (408, 429) or e.resp.status > 500\n    )\n\n\n@retry_if(sheets_api_should_retry)\ndef update_cell(spreadsheet_id: str, row: int, col: int, value: str):\n    get_spreadsheet_service().values().update(\n        spreadsheetId=spreadsheet_id,\n        range=f\"{col_i2a(col)}{row}:{col_i2a(col)}{row}\",\n        body={\"values\": [[value]]},\n        valueInputOption=\"RAW\",\n    ).execute()\n```\n\n### Advanced Usage\n\nThis library is used by GooeyAI in production to handle thousands of API calls every day. \nTo save costs and handle rate limits, you can intelligently specify quick fallbacks (eg azure openai) -\n\nhttps://github.com/GooeyAI/aifail/blob/a540c05a2a9436c0b6b1caab8ed823387999d5f9/examples/azure_openai_fallback.py#L7-L16\n\nhttps://github.com/GooeyAI/aifail/blob/a540c05a2a9436c0b6b1caab8ed823387999d5f9/examples/azure_openai_fallback.py#L19-L35\n\n### Traceable Errors\n\nAiFail comes with a built in logger, and outputs complete stack traces tracing the error back to the original call site.\n\n```bash\n# python examples/azure_openai_fallback.py \n2023-11-18 04:36:01.364 | WARNING  | aifail.aifail:try_all:63 - [2/2] tyring next fn, prev_exc=NotFoundError(\"Error code: 404 - {'error': {'code': 'DeploymentNotFound', 'message': 'The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.'}}\")\n2023-11-18 04:36:01.778 | WARNING  | aifail.aifail:wrapper:98 - [1/1] captured error, retry_delay=0.4117675457681431s, exc=NotFoundError(\"Error code: 404 - {'error': {'message': 'The model `gpt-4-x` does not exist', 'type': 'invalid_request_error', 'param': None, 'code': 'model_not_found'}}\")\n2023-11-18 04:36:02.483 | WARNING  | aifail.aifail:try_all:63 - [2/2] tyring next fn, prev_exc=NotFoundError(\"Error code: 404 - {'error': {'code': 'DeploymentNotFound', 'message': 'The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.'}}\")\n2023-11-18 04:36:04.093 | WARNING  | aifail.aifail:wrapper:98 - [2/1] captured error, retry_delay=0.9974197744911488s, exc=NotFoundError(\"Error code: 404 - {'error': {'message': 'The model `gpt-4-x` does not exist', 'type': 'invalid_request_error', 'param': None, 'code': 'model_not_found'}}\")\nTraceback (most recent call last):\n  File \"/Users/dev/Projects/dara/aifail/aifail/aifail.py\", line 65, in try_all\n    return fn()\n  File \"/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py\", line 28, in <lambda>\n    lambda: azure_client.chat.completions.create(\n  ...\nopenai.NotFoundError: Error code: 404 - {'error': {'code': 'DeploymentNotFound', 'message': 'The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.'}}\n\nThe above exception was the direct cause of the following exception:\n\nTraceback (most recent call last):\n  File \"/Users/dev/Projects/dara/aifail/aifail/aifail.py\", line 86, in wrapper\n    return fn(*args, **kwargs)\n  File \"/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py\", line 26, in chad_gpt4\n    response = try_all(\n  File \"/Users/dev/Projects/dara/aifail/aifail/aifail.py\", line 69, in try_all\n    raise prev_exc\n  File \"/Users/dev/Projects/dara/aifail/aifail/aifail.py\", line 65, in try_all\n    return fn()\n  File \"/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py\", line 34, in <lambda>\n    lambda: openai_client.chat.completions.create(\n  ...\nopenai.NotFoundError: Error code: 404 - {'error': {'message': 'The model `gpt-4-x` does not exist', 'type': 'invalid_request_error', 'param': None, 'code': 'model_not_found'}}\n\nThe above exception was the direct cause of the following exception:\n\nTraceback (most recent call last):\n  File \"/Users/dev/Projects/dara/aifail/aifail/aifail.py\", line 65, in try_all\n    return fn()\n  File \"/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py\", line 28, in <lambda>\n    lambda: azure_client.chat.completions.create(\n  ...\nopenai.NotFoundError: Error code: 404 - {'error': {'code': 'DeploymentNotFound', 'message': 'The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.'}}\n\nThe above exception was the direct cause of the following exception:\n\nTraceback (most recent call last):\n  File \"/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py\", line 43, in <module>\n    chad_gpt4(\n  File \"/Users/dev/Projects/dara/aifail/aifail/aifail.py\", line 102, in wrapper\n    raise prev_exc\n  File \"/Users/dev/Projects/dara/aifail/aifail/aifail.py\", line 86, in wrapper\n    return fn(*args, **kwargs)\n  File \"/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py\", line 26, in chad_gpt4\n    response = try_all(\n  File \"/Users/dev/Projects/dara/aifail/aifail/aifail.py\", line 69, in try_all\n    raise prev_exc\n  File \"/Users/dev/Projects/dara/aifail/aifail/aifail.py\", line 65, in try_all\n    return fn()\n  File \"/Users/dev/Projects/dara/aifail/examples/azure_openai_fallback.py\", line 34, in <lambda>\n    lambda: openai_client.chat.completions.create(\n  ...\nopenai.NotFoundError: Error code: 404 - {'error': {'message': 'The model `gpt-4-x` does not exist', 'type': 'invalid_request_error', 'param': None, 'code': 'model_not_found'}}\n\nProcess finished with exit code 1\n```\n\nSentry too, will capture the entire retry loop\n\n<img width=\"573\" alt=\"image\" src=\"https://github.com/GooeyAI/aifail/assets/19492893/958e4a74-4159-4784-a69c-e50e45b47494\">\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": null,
    "version": "0.3.0",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "17b0e74d7e9d47075783b67e6a71a12f646df2f01e41076e679515ed7a9d1805",
                "md5": "6fad99ac2936c0cd4cffaf98d7e124dd",
                "sha256": "d9726cc2121c977c283cab3411a46dbc714b01b23d050f6ba0cbb841ce12b8f8"
            },
            "downloads": -1,
            "filename": "aifail-0.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6fad99ac2936c0cd4cffaf98d7e124dd",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.10",
            "size": 5304,
            "upload_time": "2024-07-27T18:31:51",
            "upload_time_iso_8601": "2024-07-27T18:31:51.332092Z",
            "url": "https://files.pythonhosted.org/packages/17/b0/e74d7e9d47075783b67e6a71a12f646df2f01e41076e679515ed7a9d1805/aifail-0.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d423077dea2ad51ed1b4590d66b852ea88641e53f6cc13d178f616b5b4fcdc8a",
                "md5": "de9708fc57e78a1b5ef46860e34eb70c",
                "sha256": "bae17258f2c8f221eb4ab847f0a17982d27f2af8c14ef2bbee32cee94fc10fb4"
            },
            "downloads": -1,
            "filename": "aifail-0.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "de9708fc57e78a1b5ef46860e34eb70c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.10",
            "size": 4703,
            "upload_time": "2024-07-27T18:31:52",
            "upload_time_iso_8601": "2024-07-27T18:31:52.641899Z",
            "url": "https://files.pythonhosted.org/packages/d4/23/077dea2ad51ed1b4590d66b852ea88641e53f6cc13d178f616b5b4fcdc8a/aifail-0.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-27 18:31:52",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "aifail"
}
        
Elapsed time: 0.49061s