Skip to main content

A decorator for logging OpenAI API requests and responses.

Project description

Installation

pip install hrai_python

Configuration

from hrai_python.hrai_logger import logger

#Initialize the Logger

logger_instance = Logger(
    client_attr_name='client',        # Attribute name of the OpenAI client in your class
    enable_remote=True,               # Enable remote logging
    enable_async=True,                # Use asynchronous remote logging
    return_type=Logger.Return_Type.content_only
)

Basic Usage

import os
from openai import OpenAI
from dotenv import load_dotenv
from hrai_python.hrai_logger import logger


logger = logger()

class gpt:
    def __init__(self):
        load_dotenv()
        self.model = os.environ.get("OPENAI_MODEL")
        self.client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
            
    @logger.readable
    def basic_completion(self, messages):
        completion = self.client.chat.completions.create(
        model = self.model,
        messages=messages
        )
        return(completion)

Configuration

hrai_python offers several configuration options when initializing the Logger class:

  • client_instance (Optional[openai.ChatCompletion]): OpenAI client instance if standalone.
  • client_attr_name (Optional[str]): Attribute name of the OpenAI client in your class (default: "client").
  • base_url (Optional[str]): Remote server URL for logging (default: "https://api.humanreadable.ai/").
  • apikey (Optional[str]): API key for authenticating with the remote logging server.
  • log_file (str): Log file name (default: "hrai.log").
  • log_level (str): Logging level, e.g., "INFO", "DEBUG", "WARNING".
  • log_format (Optional[str]): Log message format (default: '%(asctime)s - %(levelname)s - %(message)s').
  • enable_remote (bool): Enable/disable remote logging (default: True).
  • enable_async (bool): Use asynchronous remote logging (default: False).
  • return_type (Return_Type): Defines the format of the returned response from API calls.

Return Types

The Return_Type enum in hrai_python defines the format of the response from OpenAI API calls:

  • content_only (1): Returns only the message content. (default)
  • json (2): Returns the response as a JSON object.
  • openai_object (3): Returns the full OpenAI response object.

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

hrai_python-0.2.2.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

hrai_python-0.2.2-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file hrai_python-0.2.2.tar.gz.

File metadata

  • Download URL: hrai_python-0.2.2.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.13.0 Darwin/23.5.0

File hashes

Hashes for hrai_python-0.2.2.tar.gz
Algorithm Hash digest
SHA256 986303bd03da10a7e610e4c50c436ee038ac874399e4cbef9b8ea32b3fd08689
MD5 f9fa76497b9e6038e2bef166256adb07
BLAKE2b-256 a4d42c56935256a200bb5d908605c2bd3175278cb217996c560172c82066f007

See more details on using hashes here.

File details

Details for the file hrai_python-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: hrai_python-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.13.0 Darwin/23.5.0

File hashes

Hashes for hrai_python-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8373ab9343dd39d0d7e30423992e0d0cc54b110190f3c396a9059dc32d82524b
MD5 a1634ec3599cb5a66b2dbd1c70f6074a
BLAKE2b-256 872de766dc15f16539ea72e649ccacc186640cf32e2e19fc785c985d2973b24b

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