Skip to main content

caikit-nlp client

Project description

caikit-nlp-client

PyPI version Tests codecov

A client library for caikit-nlp

Installation

Install from PyPi

pip install caikit-nlp-client

Usage

A few examples follow, see example.py

http

To use the http protocol

from caikit_nlp_client import HttpClient

host = "localhost"
port = 8080
model_name = "flan-t5-small-caikit"
http_client = HttpClient(f"http://{host}:{port}")

text = http_client.generate_text(model_name, "What is the boiling point of Nitrogen?")

gRPC

To use the gRPC protocol

from caikit_nlp_client import GrpcClient

host = "localhost"
port = 8085
model_name = "flan-t5-small-caikit"
grpc_client = GrpcClient(f"http://{host}:{port}", insecure=True)
# or, with https:
grpc_client = GrpcClient(f"https://{host}:{port}")

text = grpc_client.generate_text(model_name, "What is the boiling point of Nitrogen?")

Text generation methods may accept text generation parameters, which can be provided as kwargs to generate_text and generate_text_stream.

Available values and types be retrieved as a dict for both the grpc and http clients:

for param, default_value in client.get_text_generation_parameters():
    print(f"{param=}, {default_value=}")

Self-signed certificates

To use a self signed certificate, assuming we have a certificate authority cert ca.pem

http_client = HttpClient(f"https://{host}:{http_port}", ca_cert_path="ca.pem")

with open("ca.pem", "rb") as fh:
    ca_cert = fh.read()
grpc_client = GrpcClient(host, grpc_port, ca_cert=ca_cert)

mTLS

Assuming we have a client.pem and client-key.pem certificate files, and we require ca.pem to validate the server certificate:

# http
http_client = HttpClient(
    f"https://{host}:{http_port}",
    ca_cert_path="ca.pem",
    client_cert_path="client.pem",
    client_key_path="client-key.pem"
)

# grpc
with open("ca.pem", "rb") as fh:
    ca_cert = fh.read()
with open("client.pem", "rb") as fh:
    client_cert = fh.read()
with open("client-key.pem", "rb") as fh:
    client_key = fh.read()

grpc_client = GrpcClient(
    host,
    port,
    ca_cert=ca_cert,
    client_key=client_key,
    client_cert
)

Contributing

Set up pre-commit for linting/style/misc fixes:

pip install pre-commit
pre-commit install

This project uses nox to manage test automation:

pip install nox
nox --list  # list available sessions
nox --python 3.10 -s tests # run tests session for a specific python version

Testing against a real caikit+tgis stack

Tests are run against a mocked instance of a TGIS backend by default. To test against a real caikit+tgis stack, it is sufficient to run using the --real-caikit flag when running pytest:

nox -s tests -- --real-caikit tests

Notes:

  • The required images (caikit-tgis-serving, text-generation-inference), so it could take a while for tests to start while compose is pulling the required images, it may seem like the tests are hanging.
  • This uses a real model (google/flan-t5-small), which will be downloaded to tests/fixtures/resources/flan-t5-small-caikit and is around 300MB in size.

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

caikit_nlp_client-0.0.4.tar.gz (1.4 MB view details)

Uploaded Source

Built Distribution

caikit_nlp_client-0.0.4-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file caikit_nlp_client-0.0.4.tar.gz.

File metadata

  • Download URL: caikit_nlp_client-0.0.4.tar.gz
  • Upload date:
  • Size: 1.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for caikit_nlp_client-0.0.4.tar.gz
Algorithm Hash digest
SHA256 b3a885200f98562911d3a5bb91fabb1a329b3dfa0e790a11bfd179863ecf5687
MD5 038bd6fe1385aae37373221c1e6d9072
BLAKE2b-256 ab165fec2ee8690c440dfdb620c281abc2be55593c6ac1d6c1ea10ee62291d25

See more details on using hashes here.

File details

Details for the file caikit_nlp_client-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for caikit_nlp_client-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 381cee2b888eb771b1c9f35a39a89417675252e36eeb5302ee45eeccd1027688
MD5 424b7538f91a295452e03d7e0e164892
BLAKE2b-256 d3adc09a21fff5d3aedf76b696cee408961026150dd32086af0f785ff3d83706

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