Skip to main content

Python SDK for Hapax LLM Gateway with OpenLit observability

Project description

Hapax Python SDK

Hapax is a powerful toolkit for building, deploying, and monitoring LLM-powered applications. This Python SDK provides decorators and utilities to seamlessly integrate LLM functions with observability powered by OpenLit.

Features

  • LLM Function Decorators: Easily mark functions that interact with LLMs using the @llm_function decorator
  • OpenLit Observability: Built-in tracing and metrics for your LLM operations
  • Rich Metadata: Capture detailed information about your LLM requests, responses, and performance
  • Easy Integration: Works with popular LLM providers like OpenAI

Installation

pip install hapax

Quick Start

  1. Set up your environment:
# Set your OpenAI API key
export OPENAI_API_KEY=your-api-key-here

# Start the observability stack (requires Docker)
docker compose up -d
  1. Create your first LLM function:
from hapax import llm_function
import openlit
from openai import OpenAI

# Initialize OpenLit with the collector endpoint
openlit.init(otlp_endpoint="http://127.0.0.1:4328")

# Initialize OpenAI client
client = OpenAI()

@llm_function(
    name="generate_text",
    model="gpt-3.5-turbo",
    provider="openai"
)
def generate_text(prompt: str) -> str:
    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content

# Use the function with tracing
with openlit.start_trace(name="my_llm_request") as trace:
    result = generate_text("Write a haiku about coding")
    trace.set_metadata({
        "gen_ai.prompt": prompt,
        "gen_ai.completion": result
    })

Project Structure

  • hapax/: Core SDK implementation
    • decorators.py: LLM function decorators and tracing utilities
    • types.py: Type definitions and interfaces
  • examples/: Example applications
    • simple_completion.py: Basic example of generating text with tracing
    • evaluation_example.py: Advanced example with text generation and analysis

Observability Setup

Prerequisites

  • Docker and Docker Compose
  • Python 3.8+
  • OpenAI API key

Starting the Observability Stack

  1. The project includes a docker-compose.yaml that sets up:

    • OpenTelemetry Collector
    • Jaeger UI for trace visualization
  2. Start the services:

docker compose up -d
  1. Access the interfaces:

Trace Attributes

Hapax captures rich metadata about your LLM operations:

  • gen_ai.system: LLM provider (e.g., "openai")
  • gen_ai.request.model: Model name (e.g., "gpt-3.5-turbo")
  • gen_ai.operation.name: Operation type (e.g., "text_generation")
  • gen_ai.prompt: Input prompt
  • gen_ai.completion: Generated text
  • gen_ai.usage.*: Token usage metrics

Examples

Simple Text Generation

See examples/simple_completion.py for a basic example of generating haikus with tracing.

Text Generation with Analysis

See examples/evaluation_example.py for an advanced example that includes:

  • Text generation
  • Content analysis
  • Detailed trace attributes
  • Nested traces for complex operations

Known Issues

  1. The langchain integration warning can be safely ignored if you're not using langchain:
Failed to instrument langchain: No module named 'langchain_community'
  1. The metrics setup warning is a known issue that doesn't affect tracing functionality:
OpenLIT metrics setup failed. Metrics will not be available

Contributing

We welcome contributions! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

hapax-0.1.1.tar.gz (81.0 kB view details)

Uploaded Source

Built Distribution

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

hapax-0.1.1-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file hapax-0.1.1.tar.gz.

File metadata

  • Download URL: hapax-0.1.1.tar.gz
  • Upload date:
  • Size: 81.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.18

File hashes

Hashes for hapax-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6ba280bb5bbb91e149e334186da0657939690f82bc80918c361c68700af2c224
MD5 242f7c0a6e3635c45e049b510cf3f7df
BLAKE2b-256 3eb931cd275709a44801c698140a9f914d5729ba2ca096b0a810c85b8227d2bd

See more details on using hashes here.

File details

Details for the file hapax-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: hapax-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.18

File hashes

Hashes for hapax-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f6e25ccd4ba6496d6f48f069907150c455657f8ffe6cbda0aa6ffb82d66253b5
MD5 a214c70da0c4a27c25bdb9b0c8c06bfc
BLAKE2b-256 be3790414774db9c4cbf5b569234c2b658a532ea290f2677b6a113571382f1ef

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