Skip to main content

No project description provided

Project description

AiFail

Python Library for retrying openai/gcp API calls

Installation

pip install aifail

Usage

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

https://github.com/GooeyAI/gooey-server/blob/master/examples/basic_openai.py

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 -

https://github.com/GooeyAI/gooey-server/blob/master/examples/azure_openai_fallback.py

Inspectable Errors

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

/Users/dev/.virtualenvs/aifail-b178d07d/bin/python /Users/dev/Projects/dara/aifail/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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aifail-0.1.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

aifail-0.1.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file aifail-0.1.0.tar.gz.

File metadata

  • Download URL: aifail-0.1.0.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.5 Darwin/23.1.0

File hashes

Hashes for aifail-0.1.0.tar.gz
Algorithm Hash digest
SHA256 40f95fd45c07f9a7f0478d9702e3ea0d811f8582da7f6b841618de2a52803177
MD5 92d1fee0bdf8ef78b18ce58187038f73
BLAKE2b-256 1d371d8634e979ba5b761fdee5ac095dfdd1b8e1fefc756f50ac5380fcd22753

See more details on using hashes here.

File details

Details for the file aifail-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: aifail-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.5 Darwin/23.1.0

File hashes

Hashes for aifail-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d51fa56b1b8531a298a38cf91a3979f7d427afc88f5af635f86865b8f721bd23
MD5 7a4bf545ee0b3fbb27485e158291801e
BLAKE2b-256 bdb54fb5485f9ff1a75c7fc9a72e9818f71cd760ca98e4fc7823ad065642bd9b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page