Skip to main content

A custom wrapper for the OpenAI Python package with modified response handling

Project description

custom-openai

A robust, user-friendly wrapper for the OpenAI Python package, featuring simplified response formatting and explicit error handling.

Features

  • Clean and consistent responses: Always get predictable output from your completions.
  • Automatic error handling: Exceptions are caught and returned in a structured way.
  • Async and sync support: Use the API in both synchronous and asynchronous Python projects.

Installation

pip install custom-openai

Usage

Synchronous

from custom_openai import CustomOpenAIClient

client = CustomOpenAIClient(api_key="your-api-key")
response = client.chat_completions_create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}],
    max_tokens=100
)
if response["success"]:
    print(response["text"])
else:
    print("Error:", response["error_type"], response["error_message"])

Asynchronous

import asyncio
from custom_openai import CustomAsyncOpenAIClient

async def main():
    client = CustomAsyncOpenAIClient(api_key="your-api-key")
    response = await client.chat_completions_create(
        model="gpt-4",
        messages=[{"role": "user", "content": "Hello!"}],
        max_tokens=100
    )
    if response["success"]:
        print(response["text"])
    else:
        print("Error:", response["error_type"], response["error_message"])

asyncio.run(main())

Response Format

All responses follow the same structure:

{
    "success": True, # or False if there was an error
    "text": "...", # model output (only if success)
    "model": "...", # model used
    "usage": {
        "prompt_tokens": ...,
        "completion_tokens": ...,
        "total_tokens": ...
    },
    "finish_reason": "...", # reason for completion
    # Error fields (only present if success is False):
    "error_type": "...",
    "error_message": "..."
}

License

MIT License

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

fq_test_release-0.1.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fq_test_release-0.1.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fq_test_release-0.1.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fq_test_release-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8833072b4c8d640e1494f93a8d0490377f8ea942d5d19c66b59d20a2a145dcb7
MD5 96b87a438ced1ce370071c93ad03593a
BLAKE2b-256 9a74c9e6bb1ff64e5d750e2098109105bf8e5ff6e0b22aab0d85666710f6c14a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fq_test_release-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3e3ecbbde3a6c3a592b13b7c6b955cc151bea66bc8d4ed3eebf4d7916229de2e
MD5 bd3f86396dbefe2d4ccbd73b46dbe56a
BLAKE2b-256 5f9b328c4b0fad766d5493c417448debebd6e5126f74261e6cb6779ae25c2c67

See more details on using hashes here.

Supported by

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