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["content"])
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["content"])
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.7.0.tar.gz
(4.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fq_test_release-0.7.0.tar.gz.
File metadata
- Download URL: fq_test_release-0.7.0.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e4977badb4f469eef79260c0eb90866dc32bad347bbe258e254314c2061f8f8
|
|
| MD5 |
dc231a67eaf1cc182afab9b2c1054836
|
|
| BLAKE2b-256 |
269de10533a5f38507039bfd5879394cdc9c196a579ca142fc26cf9ef9bbe7db
|
File details
Details for the file fq_test_release-0.7.0-py3-none-any.whl.
File metadata
- Download URL: fq_test_release-0.7.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d799c44b91bf5b58cbd908e60c42cc9d761cfb563b5c0db2d267e31d42ff907
|
|
| MD5 |
2c1257e259b5f288c5431bea59b90298
|
|
| BLAKE2b-256 |
da4512b8fbebe5149144d3814c833a35d736e7d1fa8ddd2c8dd4a0b149b24919
|