Use the OpenAI Python SDK Client with HTTP or HTTP/SSL

The OpenAI Python client works with the container when using HTTP or HTTP/SSL. You just need to specify the correct HTTP endpoint, along with a valid API KEY when using HTTP/SSL.

The OpenAI Python client requires Python 3.8 or greater.

  1. Install Python 3.12 using the uv or equivalent Python version manager.
    cd ~
    
    curl -LsSf https://astral.sh/uv/install.sh | sh
    
    uv
    
    cd ~
    
    uv venv --python 3.12
    
    source .venv/bin/activate
    
    python -V
    
    mkdir /home/opc/python
    cd /home/opc/python
  2. Install the OpenAI Python client.
    uv pip install openai
  3. You can now create a Python program to list the available embedding models using HTTP. The API_KEY value is not checked with HTTP, but the API_KEY parameter is still needed.
    from openai import OpenAI
      
    my_url = "http://localhost:8080/v1"
    my_key = "Any string will do"
    
    client = OpenAI(base_url=my_url, api_key=my_key)
    
    models = client.models.list()
    for model in models:
       print(f"- {model.id}, {model.modelSize}, {model.modelCapabilities}")

    You can also create a Python program to create a vector using HTTP.

    from openai import OpenAI
      
    my_endpoint = "http://localhost:8080/v1"
    my_api_key  = "Any string will do"
    my_sentence = "Just some sample text."
    my_model    = "clip-vit-base-patch32-txt"
    
    client = OpenAI(base_url=my_endpoint, api_key=my_api_key)
    
    embeddings = client.embeddings.create(model=my_model,input=my_sentence)
    print(embeddings.data[0].embedding)

    Note:

    The /health and /metrics endpoints are helper endpoints and are not part of the OpenAI API.
  4. You can now create a Python program to list the embedding models using HTTP/SSL.

    Note:

    Keep in mind, the following conditions must be true:

    • The protocol must be HTTPS.
    • The HTTPS Port must be correct, for example 8443.
    • The API_KEY must match the value of $SECRETS_DIR/api-key on the container machine.
    • The certificate must match the certificate in the $SECRETS_DIR/cert.pem file.
    • The OpenAI function must use the valid API_KEY and certificate values.
    from openai import OpenAI
    import httpx
    
    my_url = "https://your_FQDN:8443/v1"
    my_key = "Your_API_KEY_value"
    my_cert = "/home/opc/secrets/cert.pem"
    my_string = "Just some sample text."
    
    client = OpenAI(base_url=my_url,
                    api_key=my_key,
                    http_client=httpx.Client(verify=my_cert))
    
    models = client.models.list()
    for model in models:
       print(f"- {model.id}, {model.modelSize}, {model.modelCapabilities}")

    You can also create a Python program to create a vector embedding using HTTP/SSL.

    from openai import OpenAI
    import httpx
    
    my_url = "https://your_FQDN:9443/v1"
    my_key = "Your_API_KEY_value"
    my_cert = "/home/opc/secrets/cert.pem"
    my_model = "tinybert"
    my_string = "Just some sample text."
    
    client = OpenAI(base_url=my_url,
                    api_key=my_key,
                    http_client=httpx.Client(verify=my_cert))
    
    embeddings = client.embeddings.create(model=my_model,input=my_string)
    print(embeddings.data[0].embedding)