liveBook · Manning

OpenAI API

As explained in Generative AI in Action

The OpenAI API is a programming interface that provides access to OpenAI models and related services.

The OpenAI API provides programmatic access to OpenAI models and related services. Applications connect through the OpenAI or Azure OpenAI endpoint, authenticate with an API key, and select an available model for tasks such as text generation. section 3.1.1 section 3.1.2

Implementation choices include:

  • Use the OpenAI Python library or an SDK for another language. section 3.1.1
  • Configure Azure OpenAI with an API key, endpoint, API type, API base, API version, and deployment name; OpenAI uses model rather than engine for model selection. section 3.1.1
  • Choose models according to capabilities, including chat completions, regular completions, embeddings, or fine-tuning. section 3.1.2
  • Store keys and endpoints in environment variables or secret stores instead of hardcoding them. section 3.1.1

Discovering available models

An Azure OpenAI client can call the models API, retrieve available models, save their details to JSON, and print each model’s identifier, lifecycle status, and capabilities before generating text. section 3.1.2

Listing 3.1 Listing Azure OpenAI models available
import os
import json
from openai import AzureOpenAI                  #1

client = AzureOpenAI(
    azure_endpoint=os.getenv("AOAI_ENDPOINT"),  #2
    api_version="2023-05-15",                   #3
    api_key=os.getenv("AOAI_KEY")   #4
    )

# Call the models API to retrieve a list of available models
models = client.models.list()

# save to file
with open('azure-oai-models.json', 'w') as file:
    models_dict = [model.__dict__ for model in models]
    json.dump(models_dict, file)

# Print out the names of all the available models, and their capabilities
for model in models:
    print("ID:", model.id)
    print("Current status:", model.lifecycle_status)
    print("Model capabilities:", model.capabilities)
    print("-------------------")

Inspect a complete Azure OpenAI Python example that loads credentials from environment variables, configures the client, lists models, and reports capabilities. section 3.1.2

How Generative AI in Action covers it

How deep it goes
Covers dependencies, authentication, endpoint configuration, model discovery, and capability selection without developing model internals.
Examples
Uses AzureOpenAI Python code to list deployed models, persist model metadata, and print identifiers, statuses, and capabilities.
What it assumes
Assumes basic Python and endpoint access while teaching installation, credentials, client setup, and model discovery.
What you can do after
Operate/configure an OpenAI or Azure OpenAI client, inspect accessible models, and select a model for text generation.
How it connects
Connects Azure OpenAI, Python SDKs, chat completions, completions, embeddings, and fine-tuning.

Also explained in

Compare how 4 books explain OpenAI API →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Generative AI in Action. The section links point to the parts of the book it draws on.