Skip to main content
Version: v1.6.8 🚧

Python SDK

Overview​

With the "openapi-generator-cli" Python package and the OpenAPI specification file for the h2oGPTe REST API, we generated SDKs in multiple programming languages, enabling quick integration with the API. One of the SDKs generated is the Python SDK.

  • Download Python SDK

Steps to generate and test the Python SDK​

  1. Create a folder with the following name:

    python-sdk
  2. Download the OpenAPI specification file of the h2oGPTe REST API and move it to the "python-sdk" folder: Download api-spec.yaml.

  3. In the "python-sdk" directory, set up a Python environment with Python 3.8 or later and install the OpenAPI Generator CLI v7.10.0:

    python3 -m venv venv
    source venv/bin/activate
    pip install openapi-generator-cli==7.10.0
  4. In the "python-sdk" directory, run the following command (CLI) to create the Python SDK using the OpenAPI Generator CLI:

    openapi-generator-cli generate \
    -i rest_api_spec_h2ogpte.yaml \
    -g python \
    -o sdk \
    --additional-properties=packageName=h2ogpte_rest_client,packageVersion=1.6.2,projectName=h2ogpte-rest-client
    note

    The CLI creates a folder named "sdk" containing the Python SDK.

  5. In the "sdk" directory, install the dependencies of the Python SDK:

    cd sdk
    pip install -r requirements.txt
  6. In the "sdk" directory, install the Python SDK:

    pip install setuptools
    python setup.py install
  7. In the"sdk" directory, create the following file (script) to test the newly created Python SDK (in particular, let's create a Collection to test the SDK):

    create_a_collection.py
    # This example with the Python SDK creates a new Collection with the following 
    # name and description (while testing whether the Python SDK was properly installed):
    # Name = The name of my Collection
    # Description = The description of my Collection
    import h2ogpte_rest_client
    import os
    from h2ogpte_rest_client.rest import ApiException
    from pprint import pprint

    configuration = h2ogpte_rest_client.Configuration(
    # This line specifies the URL where Enterprise h2oGPTe is hosted.
    # The address should be the location where the API key was created.
    host="https://h2ogpte.genai.h2o.ai/"
    )

    configuration = h2ogpte_rest_client.Configuration(
    access_token=os.environ["BEARER_TOKEN"]
    )

    with h2ogpte_rest_client.ApiClient(configuration) as api_client:
    api_instance = h2ogpte_rest_client.CollectionsApi(api_client)

    collection_create_request = h2ogpte_rest_client.CollectionCreateRequest(
    name="The name of my Collection",
    description="The description of my Collection"
    )

    try:
    api_response = api_instance.create_collection(collection_create_request)
    print("The response of CollectionsApi->create_collection:\n")
    pprint(api_response)
    except Exception as e:
    print("Exception when calling CollectionsApi->create_collection: %s\n" % e)

  8. Before you can run the Python script, you need to export a global API key within the "sdk" directory:

    export BEARER_TOKEN="sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    Global API key
    • The value exported for the "BEARER_TOKEN" should be a global API key.
    • To learn more about what is a global API key and how to create one, see APIs.
  9. In the "sdk" directory, run the "create_a_collection.py" file:

    python3 create_a_collection.py
    The response of CollectionsApi->create_collection:

    Collection(id='091c6ab1-a031-4eb7-9e3e-7d0939911d00', name='The name of my Collection', description='The description of my Collection', embedding_model='BAAI/bge-large-en-v1.5', document_count=0, document_size=0, updated_at=datetime.datetime(2024, 12, 3, 21, 39, 49, 442087, tzinfo=TzInfo(UTC)), user_count=0, is_public=False, username='sergio.perez@h2o.ai', sessions_count=0)

Feedback