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

Uploaded Source

Built Distribution

hrai_python-0.2.3-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hrai_python-0.2.3.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.3.tar.gz
Algorithm Hash digest
SHA256 a49368aa9fa7e0e810ab1493754fd81f333c01ff7c698c7ed90b6cd80b475119
MD5 b5beace6bb7e586faca10323a01d831a
BLAKE2b-256 c1a18b78519be2ed82135d1bda45c62da2166867f4f13a5607c927f4023bbbe5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hrai_python-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 7.5 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9bc606bc421ba02c4df48816a8f12099eb145af21ae6255aa29c71013c9d3619
MD5 b0ebeeedd2dabf016bd1796baaf21a1f
BLAKE2b-256 92f8fc910177d70c0277c9b2aba4992139c490fc394d10417225d29f57c0b1c8

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