Skip to main content

A lightweight utility library for modern Python development

Project description

ZGI SDK

A lightweight Python SDK with OpenAI-compatible API interface.

Installation

pip install zgisdk

Quick Start

OpenAI-Compatible API Client

from zgi import ZGI

# Initialize client
client = ZGI(api_key="your-api-key")

# Or use environment variable
# export ZGI_API_KEY="your-api-key"
client = ZGI()

# Chat completions
response = client.chat.completions.create(
    model="gpt-4",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ]
)
print(response['choices'][0]['message']['content'])

# Upload a file
with open("training_data.jsonl", "rb") as f:
    file = client.files.create(
        file=f,
        purpose="fine-tune"
    )
print(f"Uploaded file: {file['id']}")

# Create fine-tuning job
job = client.fine_tuning.jobs.create(
    training_file=file['id'],
    model="gpt-3.5-turbo",
    hyperparameters={
        "n_epochs": 3
    }
)
print(f"Fine-tuning job created: {job['id']}")

# Check job status
job_status = client.fine_tuning.jobs.retrieve(job['id'])
print(f"Status: {job_status['status']}")

# List models
models = client.models.list()
for model in models['data']:
    print(model['id'])

Utility Functions

from zgi import is_empty, deep_clone, debounce, sleep, random_string, format_date

# Check if a value is empty
is_empty('')  # True
is_empty([])  # True
is_empty({})  # True

# Deep clone an object
original = {'a': 1, 'b': {'c': 2}}
cloned = deep_clone(original)

# Debounce a function
@debounce(wait=0.3)
def my_function():
    print('Debounced!')

# Sleep for a duration
sleep(1)  # Sleep for 1 second

# Generate a random string
random_str = random_string(10)  # Random 10-character string

# Format a date
from datetime import datetime
formatted = format_date(datetime.now())  # "2024-01-01"

API Reference

ZGI Client

ZGI(api_key, base_url, organization, timeout)

Initialize the ZGI client.

Parameters:

  • api_key (str): Your ZGI API key (or set ZGI_API_KEY env var)
  • base_url (str, optional): Base URL for API (default: https://api.zgi.ai/v1)
  • organization (str, optional): Organization ID
  • timeout (int, optional): Request timeout in seconds (default: 60)

Chat API

client.chat.completions.create(**kwargs)

Create a chat completion.

Parameters:

  • model (str): Model ID (e.g., "gpt-4", "gpt-3.5-turbo")
  • messages (list): List of message dicts with 'role' and 'content'
  • temperature (float): Sampling temperature (0-2, default: 1.0)
  • max_tokens (int, optional): Maximum tokens to generate
  • stream (bool): Whether to stream responses (default: False)

Files API

client.files.create(file, purpose)

Upload a file.

Parameters:

  • file (BinaryIO): File object to upload
  • purpose (str): Purpose ("fine-tune", "assistants", etc.)

client.files.list(purpose=None)

List all files.

client.files.retrieve(file_id)

Get file information.

client.files.delete(file_id)

Delete a file.

Fine-tuning API

client.fine_tuning.jobs.create(training_file, model, **kwargs)

Create a fine-tuning job.

Parameters:

  • training_file (str): File ID of training data
  • model (str): Base model to fine-tune
  • validation_file (str, optional): File ID of validation data
  • hyperparameters (dict, optional): Training hyperparameters
  • suffix (str, optional): Custom model name suffix

client.fine_tuning.jobs.list(limit=20, after=None)

List fine-tuning jobs.

client.fine_tuning.jobs.retrieve(job_id)

Get job details.

client.fine_tuning.jobs.cancel(job_id)

Cancel a job.

Models API

client.models.list()

List all available models.

client.models.retrieve(model_id)

Get model information.

Utility Functions

is_empty(value: Any) -> bool

Check if a value is empty (None, empty string, empty list, or empty dict).

deep_clone(obj: T) -> T

Create a deep copy of an object.

debounce(wait: float = 0.3)

Decorator to debounce a function with an optional wait time (default: 0.3 seconds).

sleep(seconds: float) -> None

Sleep for a specified duration in seconds.

random_string(length: int = 10) -> str

Generate a random alphanumeric string with specified length (default: 10).

format_date(date: datetime = None, fmt: str = "%Y-%m-%d") -> str

Format a date to a string (default: current date, format: YYYY-MM-DD).

Environment Variables

  • ZGI_API_KEY: Your ZGI API key
  • ZGI_BASE_URL: Custom API base URL (optional)
  • ZGI_ORGANIZATION: Organization ID (optional)

Requirements

  • Python >= 3.7
  • requests >= 2.20.0

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

zgisdk-1.0.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

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

zgisdk-1.0.0-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file zgisdk-1.0.0.tar.gz.

File metadata

  • Download URL: zgisdk-1.0.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.2

File hashes

Hashes for zgisdk-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e229bd4ce495f0d0f3a250f04536ad3c76fe1f9e842a64c37090976c8cc0f3b0
MD5 5f565851f4652ec63c64c33fc344616a
BLAKE2b-256 ab618a2f482894c00a155c9b923895c2b397e169d368092c1fb298ddb338ad7d

See more details on using hashes here.

File details

Details for the file zgisdk-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: zgisdk-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.2

File hashes

Hashes for zgisdk-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 77872329b7743062bd675d4bb3409a874e86473aaef6164bd6295a61adbcc4da
MD5 7ab5ddc326c6edccb9914fb07776c716
BLAKE2b-256 d3db80458f6186e87d61abcd5a73963775fa1c3f382ea45b0d1e4272cc043922

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