Skip to main content

Python SDK for the Halfred service API

Project description

Halfred Python SDK

A Python SDK for consuming the Halfred service API. The API is fully compatible with the OpenAI API standard.

Installation

pip install halfred

Or install from source:

pip install -e .

Quick Start

from halfred import HalfredClient

# Initialize the client with your API key
client = HalfredClient(api_key="halfred_xxxxxxxxxxxxxxxx")

# List available models
models = client.models.list()
print(models)

# Create a chat completion
completion = client.chat.completions.create(
    model="lite",
    messages=[
        {"role": "user", "content": "Hello!"}
    ]
)
print(completion["choices"][0]["message"]["content"])

Usage

List Models

Get a list of available models:

from halfred import HalfredClient

client = HalfredClient(api_key="halfred_xxxxxxxxxxxxxxxxx")
models = client.models.list()

# Access the list of models
for model in models["data"]:
    print(f"Model ID: {model['id']}")

Create Chat Completions

Create a chat completion:

from halfred import HalfredClient

client = HalfredClient(api_key="halfred_xxxxxxxxxxxxxxxxx")

completion = client.chat.completions.create(
    model="lite",
    messages=[
        {"role": "user", "content": "What is Python?"}
    ]
)

print(completion["choices"][0]["message"]["content"])

Chat Completion Parameters

completion = client.chat.completions.create(
    model="lite",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ],
    temperature=0.7,              # Sampling temperature (0-2)
    max_tokens=100,              # Maximum tokens to generate
    max_completion_tokens=50,     # Maximum completion tokens
    response_format={"type": "json_object"},  # Response format (optional)
    stream=False                 # Whether to stream the response
)

Error Handling

from halfred import HalfredClient, HalfredAPIError, HalfredAuthenticationError

client = HalfredClient(api_key="halfred_xxxxxxxxxxxxxxxxx")

try:
    completion = client.chat.completions.create(
        model="dev",
        messages=[{"role": "user", "content": "Hello"}]
    )
except HalfredAuthenticationError:
    print("Authentication failed. Check your API key.")
except HalfredAPIError as e:
    print(f"API error: {e}")
    print(f"Status code: {e.status_code}")

Custom Configuration

from halfred import HalfredClient

# Custom base URL and timeout
client = HalfredClient(
    api_key="halfred_xxxxxxxxxxxxxxxxx",
    base_url="https://api.halfred.com",  # Optional, defaults to production
    timeout=30  # Optional, defaults to 30 seconds
)

Development

Setup

# Install development dependencies
pip install -e ".[dev]"

Running Tests

pytest

Code Formatting

black halfred/

API Compatibility

This SDK is designed to be compatible with the OpenAI API standard. The API endpoints and request/response formats follow the same structure as OpenAI's API, making it easy to switch between providers.

License

MIT

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

halfred-0.1.1.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

halfred-0.1.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: halfred-0.1.1.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.24

File hashes

Hashes for halfred-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7f5a6425279a4927ee921515447acd5c7c5bddd71292a70456b7df9850f006e5
MD5 686c6144305acd332635f74c9330518b
BLAKE2b-256 3c276d396dbda9c5d9030e5cae5aad2a39dbc789b78940228f684212bb1a7d1e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: halfred-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.24

File hashes

Hashes for halfred-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 138bfbf40e728712cf5c224d3e7d7e48295aef267e8959ebac2c8351cfb61bc2
MD5 3cd1c95c31389858344f20932fb4abe5
BLAKE2b-256 71ab03253770f48cb839485cf72ba72381e5da3df48c166324f3e91841cf9de6

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