Skip to main content

No project description provided

Project description

AI21 Studio Client

Python client for the AI21 Studio API

API Keys

API key can be set globally, using ai21.api_key, or alternatively - be passed to any function call as a named argument. For example - ai21.Dataset.get(api_key=api_key)

Installation:

pip install -U ai21

Client customization

The ai21 namespace has parameters that one can set globally, and they will be used by all the following client calls. One can also pass each of these parameters as a named argument. If a parameter is overridden globally, and also passed as a named argument - the passed named argument will be used with the following client calls. The supported parameters are described below:

* api_key (str) - the key to be used for the Authorization header
* organization (str) - this information is sent as part of the user-agent
* application (str) - this information is sent as part of the user-agent
* api_version (str) - the api version. (i.e. v1 / v2). Default=v1
* api_host (str) - the api host. Default=https://api.ai21.com
* timeout_sec (int) - the number of seconds the client must wait before cutting the connection. Default=30
* num_retries (int) - the number of attempts to retry a failure response. Default=0 (no retries)
* log_level (str) - debug / info / error (Default=error)

Retry on Failure

By default, there is no retry on failures. If setting the num_retries parameter to an integer greater than 0, then all the following client calls will retry up to the specified number of times on the following http error codes: 429, 500, 503.

Documentation & resources

https://docs.ai21.com/

Usage

Studio API

In the following example, an AI21 API client is used with an API key and a timeout parameter set globally. This example uses a simple completion call with only prompt and maxTokens arguments supplied (all other completion settings are set to their defaults):

import ai21

ai21.api_key = 'my_api_key'
ai21.timeout_sec = 20
response = ai21.Completion.execute(
    model="j2-ultra",
    prompt="Write a news release in the voice of a global banking conglomerate announcing an unprecedented building campaign to expand and rebuild their corporate headquarters in Alpha Centauri.",
    maxTokens=200,
)
print(response)

AWS Client

This python SDK can also be used to invoke Jurassic models as a SageMaker (SM) endpoint or as an AWS Bedrock API. To activate this option, make sure to install with the extra AWS dependencies:

pip install -U "ai21[AWS]"

Sagemaker endpoint request

You can then generate a completion by running:

import ai21

response = ai21.Completion.execute(
    destination=ai21.SageMakerDestination("<your_endpoint_name>"),
    prompt="Write a news release in the voice of a global banking conglomerate announcing an unprecedented building campaign to expand and rebuild their corporate headquarters in Alpha Centauri.",
    maxTokens=200,
)

print(response)

Bedrock API request

You can use one of the models offered in the following list as model_id:

  • ai21.j2-mid
  • ai21.j2-ultra
import ai21

# The bedrock API is available only to us-east-1 scoped clients right now
ai21.aws_region = 'us-east-1'

response = ai21.Completion.execute(
    destination=ai21.BedrockDestination(model_id=ai21.BedrockModelID.J2_ULTRA),
    prompt="Write a news release in the voice of a global banking conglomerate announcing an unprecedented building campaign to expand and rebuild their corporate headquarters in Alpha Centauri.",
    maxTokens=200,
)

print(response)

Project details


Release history Release notifications | RSS feed

This version

1.3.3

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ai21-1.3.3.tar.gz (15.1 kB view details)

Uploaded Source

File details

Details for the file ai21-1.3.3.tar.gz.

File metadata

  • Download URL: ai21-1.3.3.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for ai21-1.3.3.tar.gz
Algorithm Hash digest
SHA256 f602a3f2bba8f82cd6b6f1059cb569c3fbac22ddba591888f70ebea1f34db0e2
MD5 3c617eb6904efb4e3ab971445ee7e30f
BLAKE2b-256 da1424a1c9b1bf9054f48c45b7c66dd6499ac5214f4ac21ea4107c21fdfb8447

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