LinuxCommandLibrary

openai

Interact with OpenAI models in terminal

TLDR

List models

$ openai api models.list
copy

Create a completion
$ openai api completions.create --model [ada] --prompt "[Hello world]"
copy

Create a chat completion
$ openai api chat_completions.create --model [gpt-3.5-turbo] --message [user "Hello world"]
copy

Generate images via DALLĀ·E API
$ openai api image.create --prompt "[two dogs playing chess, cartoon]" --num-images [1]
copy

SYNOPSIS

openai <resource> <action> [<options>]
openai --help
openai <resource> --help

PARAMETERS

--api-key <key>
    Specifies the OpenAI API key for authentication. Overrides the `OPENAI_API_KEY` environment variable.

--organization <id>
    Specifies the OpenAI organization ID for billing and usage. Overrides `OPENAI_ORGANIZATION`.

--help, -h
    Displays help information for the `openai` command, or for a specific resource/action.

--model <model_id>
    Specifies the ID of the OpenAI model to use (e.g., `gpt-4o`, `text-embedding-3-small`). Common for `chat completions` and `embeddings`.

--messages <json_string>
    For `chat completions`, a JSON string representing an array of message objects (e.g., `"[{'role': 'user', 'content': 'Hello'}]"`).

--file <path>
    Specifies the path to a file, typically used for `files upload` or `fine_tuning create`.

--id <resource_id>
    Specifies the unique identifier of a resource (e.g., file ID, model ID, assistant ID) for actions like `retrieve` or `delete`.

DESCRIPTION

The `openai` command is the official command-line interface (CLI) for interacting with the OpenAI API. Unlike traditional Linux utilities, it's a Python-based tool, typically installed via `pip`. It provides a convenient way to access various OpenAI services directly from your terminal, including generating text and chat completions, managing files, fine-tuning models, and utilizing embedding or assistant APIs. Users can programmatically send requests for tasks such as creating conversational agents, performing embeddings for semantic search, or moderating content. The CLI simplifies common API operations, allowing for quick prototyping, scripting, and direct interaction without writing extensive code. It requires an API key, which authenticates requests and determines access to specific models and services, making it a powerful tool for developers and researchers leveraging OpenAI's advanced AI capabilities.

CAVEATS

Not a native Linux command: It is a Python package CLI and must be installed separately, typically using `pip`.
API Key Required: Usage necessitates an active OpenAI API key, usually set as an environment variable (`OPENAI_API_KEY`) or passed via the `--api-key` option.
Cost Implications: Interactions with the OpenAI API via this command incur charges based on usage (e.g., tokens processed, model type, compute time).
API Evolution: The command's functionality, available resources, and options are subject to ongoing changes and updates in the underlying OpenAI API.

INSTALLATION

The `openai` CLI is distributed as part of the `openai` Python package. To install it, ensure you have Python and `pip` configured, then execute:
pip install openai
It is highly recommended to install Python packages, including `openai`, within a virtual environment to manage dependencies and avoid conflicts.

COMMON RESOURCES AND ACTIONS

The `openai` CLI organizes its functionality around resources and actions. Common top-level resources include `chat`, `completions`, `embeddings`, `files`, `fine_tuning`, `models`, `moderations`, `assistants`, `threads`, `messages`, `runs`, and `vector_stores`. Typical actions performed on these resources are `create`, `list`, `retrieve`, and `delete`.

Examples:
To create a chat completion:
openai chat completions create --model gpt-4o --messages "[{'role': 'user', 'content': 'Hello!'}]"

To list all available OpenAI models:
openai models list

To upload a file for fine-tuning or assistants:
openai files upload --file ./my_data.jsonl --purpose fine-tune

HISTORY

The `openai` command-line interface emerged as a component of the official Python client library for the OpenAI API. Its development has closely paralleled the growth and evolution of OpenAI's suite of AI models and services, from early text completion engines to sophisticated chat, embedding, and assistant APIs. The CLI has undergone significant architectural updates, particularly with the release of the `openai` Python client library version 1.0.0 and subsequent versions, which introduced a more structured and resource-oriented subcommand design. This evolution aimed to provide a consistent and intuitive way for developers and researchers to quickly test, script, and manage their interactions with OpenAI's capabilities directly from the command line, facilitating rapid prototyping and integration of AI services.

SEE ALSO

pip(1): The Python package installer, crucial for installing the `openai` CLI., python(1): The runtime environment for the `openai` CLI, as it is a Python application., curl(1): A command-line tool for transferring data with URLs, often used for direct HTTP API interaction, which the `openai` CLI simplifies and abstracts.

Copied to clipboard