Skip to main content

The official PeakPrivacy Python Client

Project description

PeakPrivacy Python Client

The PeakPrivacy Python Client provides convenient access to the PeakPrivacy REST API from any Python 3.7+ application. The client includes type definitions for all request params and response fields.

Documentation

The REST API documentation can be found on peakprivacy.ch. The full API of this client can be found in api.md.

Installation

pip install peakprivacy

Usage

The full API of this client can be found in api.md.

import os
from dotenv import load_dotenv

load_dotenv()
from peakprivacy import PeakPrivacy

client = PeakPrivacy(
    # This is the default and can be omitted
    api_token=os.environ.get("PEAKPRIVACY_API_TOKEN")
)

chat_completion = client.chat.completions.create(
    model='mistral-swiss',
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ]
)

While you can provide an api_token keyword argument, we recommend using python-dotenv to add PEAKPRIVACY_API_TOKEN="My API Token" to your .env file so that your API Token is not stored in source control.

Streaming Responses

We provide support for streaming responses using Server Side Events (SSE).

Simply add stream=True parameter into the client.chat.completions.create method.

Pay attention to the fact that handling streaming responses is different from handling regular responses.

Check out the example below.

load_dotenv()
from peakprivacy import PeakPrivacy

client = PeakPrivacy(
    # This is the default and can be omitted
    api_token=os.environ.get("PEAKPRIVACY_API_TOKEN")
)

stream = client.chat.completions.create(
    model='mistral-swiss',
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ],
    stream=True
)

for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")

Supported models

As long as you can utilize a model from the code example above, model='mistral-swiss', there is a range of options available for use.

You can find the full list of supported models on peakprivacy.ch or by using the client.models.list() method.

For a complete description of the method, refer to api.md.

load_dotenv()
from peakprivacy import PeakPrivacy

client = PeakPrivacy(
    # This is the default and can be omitted
    api_token=os.environ.get("PEAKPRIVACY_API_TOKEN")
)

models = client.models.list()
print(models)

Handling errors

When the client is unable to connect to the API (for example, due to network connection problems or a timeout), a subclass of peakprivacy.ApiConnectionError is raised.

When the API returns a non-success status code (that is, 4xx or 5xx response), a subclass of peakprivacy.ApiStatusError is raised, containing status_code and response properties.

All errors inherit from peakprivacy.ApiError.

import os
from dotenv import load_dotenv

load_dotenv()
from peakprivacy import PeakPrivacy, ApiTokenNotFoundError, AuthenticationError, PermissionDeniedError, ApiError,
    ApiConnectionError, ApiStatusError

try:
    client = PeakPrivacy(
        # This is the default and can be omitted
        api_token=os.environ.get("PEAKPRIVACY_API_TOKEN")
    )
except ApiTokenNotFoundError as err:
    # exception if token not found
    print(err)
except Exception as err:
    # exception if something went incredibly wrong
    print(err)

try:
    chat_completion = client.chat.completions.create(
        model='mistral-swiss',
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "Hello!"}
        ]
    )
except AuthenticationError as e:
    # 401 catch example ("You Shall Not Pass")
    print(f"AuthenticationError: {e.status_code}, {e.message}, {e.response}")
except PermissionDeniedError as e:
    # 403 catch example (Nope, Not Today, Not Ever)
    print(f"PermissionDeniedError: {e.status_code}, {e.message}, {e.response}")
except ApiStatusError as e:
    # 400, 401, 403, 404, 422, 429, >=500 catch example ("Gremlins in the API: Someone fed them after midnight!")
    print(f"ApiStatusError: {e.status_code}, {e.message}, {e.response}")
except ApiConnectionError as e:
    # request errors
    print(f"ApiConnectionError: {e.status_code}")
except ApiError as e:
    # parent for all errors
    print(f"ApiError: {e.status_code}")
except Exception as err:
    # exception if something went incredibly wrong ("Houston, We’ve Had a Problem")
    print(err)

Error codes are as followed:

Status Code Error Type
400 BadRequestError
401 AuthenticationError
403 PermissionDeniedError
404 NotFoundError
422 UnprocessableEntityError
429 RateLimitError
>=500 InternalServerError
N/A ApiConnectionError

Requirements

Python 3.7 or higher.

Package structure

Client API

client = PeakPrivacy(
    # This is the default and can be omitted
    api_token=os.environ.get("PEAKPRIVACY_API_TOKEN")
)

Chat api_token validation API

client.chat.check_api_token()

Chat supported models list API

client.models.list()

Chat Completions API

client.chat.completions.create(
    model='mistral-swiss',
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ]
)

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

peakprivacy-0.1.0.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

peakprivacy-0.1.0-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

File details

Details for the file peakprivacy-0.1.0.tar.gz.

File metadata

  • Download URL: peakprivacy-0.1.0.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.9

File hashes

Hashes for peakprivacy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 37bb7876eb3bc9d5e968b1002b488b43c4cb2b906e2e3955ce66ea1d79e71b1e
MD5 8b6f8ec163fd9775e6b809cec0865e88
BLAKE2b-256 13f2f5ae9761ccfe20f00f5a242ce23d8031e609234aa1f9462a2ec018c688ab

See more details on using hashes here.

File details

Details for the file peakprivacy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: peakprivacy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.9

File hashes

Hashes for peakprivacy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 402bb382d515d6255d6045cbaee63ec999d8363ff2eda55e0c1f570618b1eb42
MD5 733338c412ebaca0cc938cba53b5198b
BLAKE2b-256 38a3ddef1a0c0764274ba232c58d1b28712a5c02c4e2b81ba07a99798041cdab

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page