Skip to main content

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, port= "localhost", 8085
model_name = "flan-t5-small-caikit"
grpc_client = GrpcClient(host, port, insecure=True) # plain text mode

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)

To skip certificate validation:

# http
http_client = HttpClient(f"https://{host}:{http_port}", verify=False)
# grpc
grpc_client = GrpcClient(host, port, verify=False)

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=client_cert,
)
# alternatively you can pass the paths directly to the client constructor
grpc_client = GrpcClient(
    host,
    port,
    ca_cert="ca.pem",
    client_cert="client.pem",
    client_key="client-key.pem"
)

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.

Metadata

Release files for caikit-nlp-client 0.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for caikit-nlp-client 0.0.8
File Size Uploaded
caikit_nlp_client-0.0.8.tar.gz 1.4 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for caikit-nlp-client 0.0.8
File Interpreter ABI Platform
caikit_nlp_client-0.0.8-py3-none-any.whl Python 3 none any Details

Total release size: 1.4 MB

Release files / caikit_nlp_client-0.0.8.tar.gz

Download URL caikit_nlp_client-0.0.8.tar.gz
Size 1.4 MB
Tags Source
SHA-256 checksum
How to use checksums
72f59c99db87b0c73d589bebad9452a186544b52ed706ed0f94a661f83608339
BLAKE2b-256 checksum
How to use checksums
1171c1a2793e4d1c3a57828651055df499711bb18c9740afb7b423acaa702f29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / caikit_nlp_client-0.0.8-py3-none-any.whl

Download URL caikit_nlp_client-0.0.8-py3-none-any.whl
Size 14.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8e8a31378d8f711f95b164824bc75b68c5ac433ab640005218e287bbfb53e581
BLAKE2b-256 checksum
How to use checksums
f10c94f70b7df6a6c3a72ddfdc59bc3454da7b7b410fd18a2d3f25f9dda580b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release history Release notifications | RSS feed

This release

0.0.8 This release

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page