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_functiondecorator - 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
- 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
- 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 implementationdecorators.py: LLM function decorators and tracing utilitiestypes.py: Type definitions and interfaces
examples/: Example applicationssimple_completion.py: Basic example of generating text with tracingevaluation_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
-
The project includes a
docker-compose.yamlthat sets up:- OpenTelemetry Collector
- Jaeger UI for trace visualization
-
Start the services:
docker compose up -d
- Access the interfaces:
- Jaeger UI: http://localhost:16687
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 promptgen_ai.completion: Generated textgen_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
- The langchain integration warning can be safely ignored if you're not using langchain:
Failed to instrument langchain: No module named 'langchain_community'
- 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ba280bb5bbb91e149e334186da0657939690f82bc80918c361c68700af2c224
|
|
| MD5 |
242f7c0a6e3635c45e049b510cf3f7df
|
|
| BLAKE2b-256 |
3eb931cd275709a44801c698140a9f914d5729ba2ca096b0a810c85b8227d2bd
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6e25ccd4ba6496d6f48f069907150c455657f8ffe6cbda0aa6ffb82d66253b5
|
|
| MD5 |
a214c70da0c4a27c25bdb9b0c8c06bfc
|
|
| BLAKE2b-256 |
be3790414774db9c4cbf5b569234c2b658a532ea290f2677b6a113571382f1ef
|