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 hrai_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 hrai_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.3.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

hrai_python-0.3.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hrai_python-0.3.0.tar.gz
  • Upload date:
  • Size: 7.6 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.3.0.tar.gz
Algorithm Hash digest
SHA256 e36350ed2eccd0a3f610ef70cecffdbd7cfe8e4c2c1aef4d61f1cb5d045e5c7f
MD5 c65fb054ed76ff09b2555844c1f4a8ca
BLAKE2b-256 791272ef80c1017ad9ce491ddc66689d950b2b0a34a9f43f5f94a15f15a49449

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hrai_python-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.6 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f3640d42977f57ffe83af8af6c8c77e974a57a5039d112f750b7240214fc8ae2
MD5 b45ca94cd56c5bbbe3e6229d430a76b2
BLAKE2b-256 58034e18858d6b4d62137b28f6e414b4b88eb70a5f140a6d62000dee99d35b9a

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