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.1.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: hrai_python-0.2.1.tar.gz
  • Upload date:
  • Size: 5.9 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.1.tar.gz
Algorithm Hash digest
SHA256 b11624014878501b82096af5c629177e68ae3465d483d3a1b1fc6e72bfca2581
MD5 2e413b889876d672b1c0c30dc848ac15
BLAKE2b-256 3899d6d701ced5615faa6319f5de1eb20788b4588f9de77be64fa7b661eea06f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hrai_python-0.2.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a01a4522bab6ff029ff05a8487d83be7ab22c6cccc3d2ad822152d6814deee1e
MD5 87c0e415a91cb78b939132ea23df337b
BLAKE2b-256 10131535ec07e5111cfa3148f1ba95b5fa28b6330cd698142ef21490947abbe4

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