Skip to main content

A decorator for logging OpenAI API requests and responses.

Project description

hrai_python

A Python package designed to log and monitor interactions with OpenAI’s API, supporting both local and remote logging. It enables structured logging for OpenAI API calls and facilitates asynchronous remote logging for scalability.

Installation

Install the package using pip:

pip install hrai_python

Configuration

Initialize the logger with options for configuring the OpenAI client, logging preferences, and remote logging.

from hrai_python.hrai_logger import hrai_logger

# Initialize the Logger
logger_instance = hrai_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=hrai_logger.Return_Type.content_only
)

Basic Usage Example

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

# Initialize the logger
logger = hrai_logger()

class GPTClient:
    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 Options

hrai_logger offers flexible configuration options:

  • 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): Format of the response from API calls (default: content_only).

Return Types

Return_Type defines the format of responses from OpenAI API calls:

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

Utilities with hrai_utils

Tool Creation

hrai_utils.create_tool helps build tool structures for OpenAI function calls.

from hrai_python.hrai_utils import hrai_utils

# Define tool properties
name = "my_tool"
description = "This is a tool that does something"
properties = {
    "answer": "The answer to the tool",
    "request": "The request to the tool"
}

# Create a tool dictionary
my_tool = hrai_utils.create_tool(name, description, properties)

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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hrai_python-0.3.4-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for hrai_python-0.3.4.tar.gz
Algorithm Hash digest
SHA256 8400d2d1ef523a9ba05c972ffb2c1e6a36b52833e60853f0112cb4eca95d0914
MD5 ab33b069b839d10d76276e61f9670ba6
BLAKE2b-256 a8d193f12b6f70fa0ff68731fd2674b55450e74b9f8595e6b6a7b203830f5ad2

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for hrai_python-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 466d8b5d9f7505599e7c299407f0b8965e3165d9c706c3bb2af2e926dee195d6
MD5 32889a0ee6c4d74e61d75c725ad2af38
BLAKE2b-256 64eee06d4948ccbd41fc03f8d74c55bd24a9dcf88b22768a78dae403244f29ca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page