nikkie-ftnextの日記

イベントレポートや読書メモを発信

openai-python にはコマンドラインインターフェースがある 〜uvx openai api〜

はじめに

七尾百合子さん、お誕生日 172日目 おめでとうございます! nikkieです。

OpenAI の公式 SDK openai1コマンドラインインターフェースを試しました。

目次

openai api chat.completions.create

執筆時点のバージョンは 1.106.1。
pyproject.toml に定義されています。
https://github.com/openai/openai-python/blob/v1.106.1/pyproject.toml#L43-L44

[project.scripts]
openai = "openai.cli:main"

では openai コマンドを使ってみましょう。
環境変数 OPENAI_API_KEY を設定しています。

見なよ...

% uvx openai api chat.completions.create -g user こんにちは -m gpt-5
こんにちは!今日はどのようにお手伝いできますか?

-hを見ながら呼び出し方を探りました。

-g{role} {content}形式で指定しています。

-mのモデルの指定はこちらを参照。
https://platform.openai.com/docs/models
Web UI では GPT-5 シリーズに絞られて2います(一部 GPT-4o などもある)が、API では他のモデルも利用可能です。

https://platform.openai.com/docs/api-reference/chat/create を叩いているという理解です。

開発経緯

いつからあったのか辿ってみました。

v0.7.0CLI の存在が README に記載されています。
https://github.com/openai/openai-python/tree/v0.7.0?tab=readme-ov-file#command-line-interface
このときは openai api completions.create です。

v0.27.0chat_completions.create 追加。
https://github.com/openai/openai-python/tree/v0.27.0?tab=readme-ov-file#command-line-interface

V1chat.completions.create となりました。
ただこのタイミングで README からは消えているような

終わりに

openai-python 自体は少しの Python コードを書いて始められますが、CLI を使うともっと手軽です。
simonw さんの llm3 を使っていましたが、OpenAI の LLM をすぐ試すなら uvx openai api が一番手軽だと思います。

APIドキュメントを見て、Responses API をアピールしているように見えました。
キャッチアップしないとなあ
https://platform.openai.com/docs/api-reference/responses


  1. 過去にもいくつか記事を書いています。例えば
  2. openai.com