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 Halfred

# Initialize the client with your API key
client = Halfred(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 Halfred

client = Halfred(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 Halfred

client = Halfred(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 Halfred, HalfredAPIError, HalfredAuthenticationError

client = Halfred(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 Halfred

# Custom base URL and timeout
client = Halfred(
    api_key="halfred_xxxxxxxxxxxxxxxxx",
    base_url="https://api.halfred.ai/v1",  # Optional, defaults to https://api.halfred.ai/v1
    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.

The response objects use attribute-based access (e.g., completion.choices[0].message.content) just like the OpenAI Python SDK, ensuring full compatibility with code written for OpenAI's client library.

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.4.0.tar.gz (7.7 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.4.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for halfred-0.4.0.tar.gz
Algorithm Hash digest
SHA256 2615cf63d4fd64966ba682e55438a8f17ba8a063401dfde7696545c8b74d7f0e
MD5 939c42fdac3a754280fc3fc7f7e6956d
BLAKE2b-256 ae78f4b261e661022f00fbafc4f1d06d74da8295f0c72bf334071ae76eaa1533

See more details on using hashes here.

File details

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

File metadata

  • Download URL: halfred-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 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.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df6362a268102268b55c865be913c5a156a7ebcab6a29acffd4b7644d5ad87cd
MD5 33fa4f72598b13055d63e8694e48b47c
BLAKE2b-256 a0f25b00b232ad10983e8ae9104675d38244d2b74b2aa14d1cc5fe995a9deb8a

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