Exploring the OpenAI API with the OpenAI CLI (with examples)

Exploring the OpenAI API with the OpenAI CLI (with examples)

The OpenAI API provides a powerful way to access various models and create AI-powered applications. To conveniently interact with the OpenAI API, OpenAI provides a CLI (Command Line Interface) tool. In this article, we will explore different use cases of the OpenAI CLI by illustrating examples of commands and their outputs.

1: Using the command ‘openai api models.list’

This command allows you to list the models available in the OpenAI API.

openai api models.list

Motivation:

By listing the available models, you can get an overview of the different language models and AI capabilities provided by OpenAI. This helps you understand the scope and capabilities of the models you can work with.

Explanation:

The openai api models.list command without any additional arguments lists all the available models in the OpenAI API.

Example Output:

{
  "models": [
    {
      "id": "gpt-3.5-turbo",
      "name": "gpt-3.5-turbo"
    },
    {
      "id": "curie",
      "name": "curie"
    },
    {
      "id": "babbage",
      "name": "babbage"
    },
    {
      "id": "ada",
      "name": "ada"
    }
  ]
}

2: Using the command ‘openai api completions.create’

This command allows you to create completions using a specific model.

openai api completions.create --model ada --prompt "Hello world"

Motivation:

Creating completions with a language model allows you to generate text based on a given prompt. This can be used for a variety of applications, such as drafting emails, generating code, or composing written content.

Explanation:

The openai api completions.create command is used to create completions. The --model ada argument specifies the model to use, which in this case is the “ada” model. The --prompt "Hello world" argument provides the prompt text for which the completion is generated.

Example Output:

{
  "id": "chatcmpl-6p9XYPYSTTRi0xEviKjjilqrWU2Ve",
  "object": "chat.completion",
  "created": 1677649420,
  "model": "ada:2021-10-01",
  "usage": {
    "prompt_tokens": 2,
    "completion_tokens": 31,
    "total_tokens": 33
  },
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "Hello, how can I assist you today?"
      },
      "finish_reason": "stop",
      "index": 0
    }
  ]
}

3: Using the command ‘openai api chat_completions.create’

This command allows you to create chat completions using a GPT-3.5 Turbo model.

openai api chat_completions.create --model gpt-3.5-turbo --message "user" --message "Hello world"

Motivation:

Chat completions provide a conversational AI experience where you can simulate a back-and-forth conversation with the model. This can be useful for building chatbots, virtual assistants, or interactive applications.

Explanation:

The openai api chat_completions.create command is used to create chat completions. The --model gpt-3.5-turbo argument specifies the model to use, which in this case is the GPT-3.5 Turbo model. The --message "user" argument indicates a user message, and the --message "Hello world" argument provides the user’s input message.

Example Output:

{
  "id": "chatcmpl-6p9XYPYSTTRi0xEviKjjilqrWU2Ve",
  "object": "chat.completion",
  "created": 1677649420,
  "model": "gpt-3.5-turbo:2021-10-01",
  "usage": {
    "prompt_tokens": 11,
    "completion_tokens": 29,
    "total_tokens": 40
  },
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "Hello, how can I assist you today?"
      },
      "finish_reason": "stop",
      "index": 0
    }
  ]
}

4: Using the command ‘openai api image.create’

This command allows you to generate images using the DALL·E API.

openai api image.create --prompt "two dogs playing chess, cartoon" --num-images 1

Motivation:

The DALL·E API enables the generation of unique images based on textual prompts. This can be useful for creating artwork, illustrations, or graphic assets for various purposes.

Explanation:

The openai api image.create command is used to generate images with the DALL·E API. The --prompt "two dogs playing chess, cartoon" argument provides a textual prompt describing the desired image. The --num-images 1 argument specifies the number of images to generate.

Example Output:

{
  "id": "img-W0FlSTv7UFjnJIZSuU_-aKC8d4R-f",
  "object": "image",
  "created": 1677649420,
  "model": "dall-e:2021-10-01",
  "prompt": "two dogs playing chess, cartoon",
  "status": "succeeded",
  "output": {
    "width": 512,
    "height": 512,
    "image": "https://api.openai.com/.../image-W0FlSTv7UFjnJIZSuU_-aKC8d4R-f.png",
    "caption": "a cartoon image of two dogs playing chess"
  }
}

Conclusion

In this article, we explored different use cases of the OpenAI CLI by providing examples of commands and their outputs. We covered listing models, creating completions, creating chat completions, and generating images with the DALL·E API. Understanding how to use these commands and their arguments allows you to leverage the OpenAI API and build various AI-powered applications.

Related Posts

How to use the command lein (with examples)

How to use the command lein (with examples)

Leiningen is a build automation and dependency management tool for Clojure projects.

Read More
How to use the command "aws-pricing" (with examples)

How to use the command "aws-pricing" (with examples)

In this article, we will explore different use cases of the “aws pricing” command, which allows us to query services, products, and pricing information from Amazon Web Services (AWS).

Read More
How to use the command 'cargo remove' (with examples)

How to use the command 'cargo remove' (with examples)

The ‘cargo remove’ command is a useful tool for removing dependencies from a Rust project’s Cargo.

Read More