2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

この記事は、Qiita公式キャンペーン「OpenAI・Anthropic互換APIを無料で使おう!『さくらのAI Engine』3,000リクエスト使い切りチャレンジ」への応募記事です。

さくらのAI Engine で利用できるモデル一覧を取得するため、GET /v1/models を試しました。

参照した Inference APIRAG API のマニュアルには、/v1/models の記載を見つけられませんでした。

しかし、エンドポイントへ直接リクエストすると、モデル一覧を取得できます。curlだけでなく、OpenAI SDKとAnthropic SDKからも取得できました。

curlで取得する

APIトークンを環境変数に設定します。

export SAKURA_AI_ENGINE_ACCOUNT_TOKEN="発行したアカウントトークン"

あとは次のcurlを実行します。

curl --location 'https://api.ai.sakura.ad.jp/v1/models' \
  --header 'Accept: application/json' \
  --header "Authorization: Bearer $SAKURA_AI_ENGINE_ACCOUNT_TOKEN"

レスポンスは次のような形式です。

{
  "object": "list",
  "data": [
    {
      "id": "gpt-oss-120b",
      "created": 1755765369,
      "object": "model",
      "owned_by": "sakura"
    }
  ]
}

data にモデル情報が配列で返ります。モデルの提供状況は変わり得るため、モデルIDを確認したい場合に便利です。

Anthropic用のヘッダーを付ける

anthropic-version ヘッダーを付けても取得できます。

curl --location 'https://api.ai.sakura.ad.jp/v1/models' \
  --header 'Accept: application/json' \
  --header "Authorization: Bearer $SAKURA_AI_ENGINE_ACCOUNT_TOKEN" \
  --header 'anthropic-version: 2023-06-01'

今回、ヘッダーなしとヘッダーありで比較した結果は次のとおりでした。

比較項目 結果
モデル件数 どちらも12件
モデルIDの集合 一致
各レコードの内容 順序を無視すると一致
data の配列順 異なる

つまり、今回の取得では、レスポンスのモデル一覧は同じですが、並び順が変わりました

SDKから取得する

今回使用したSDKのバージョンは、以下の通りです。

  • openai==2.52.0
  • anthropic==0.120.2

OpenAI SDK

OpenAI SDKでは、base_url/v1 を含めます。

import os

from openai import OpenAI


client = OpenAI(
    api_key=os.environ["SAKURA_AI_ENGINE_ACCOUNT_TOKEN"],
    base_url="https://api.ai.sakura.ad.jp/v1",
)

models = client.models.list()
for model in models.data:
    print(model.id)

Anthropic SDK

Anthropic SDKでは、base_url/v1 を含めません。Bearerトークンを使うため、auth_token にトークンを渡します。

import os

import anthropic


client = anthropic.Anthropic(
    auth_token=os.environ["SAKURA_AI_ENGINE_ACCOUNT_TOKEN"],
    base_url="https://api.ai.sakura.ad.jp",
)

models = client.models.list()
for model in models.data:
    print(model.id)

こちらもモデル一覧を取得できました。今回の実行結果をまとめると、次のとおりです。

呼び出し方法 件数 モデルID 順序
curl(ヘッダーなし) 12 同じ 変わる
curl(anthropic-version あり) 12 同じ 変わる
OpenAI SDK 12 同じ 変わる
Anthropic SDK 12 同じ 変わる

openai==2.52.0model_dump() では、モデル1件のフィールドは idcreatedobjectowned_by でした。anthropic==0.120.2 では、これらに加えて capabilitiescreated_atdisplay_namemax_input_tokensmax_tokenstype が含まれ、今回取得した12件では追加フィールドの値がすべて null でした。そのため、SDKのモデルオブジェクト全体を比較すると差が出ますが、モデルIDの集合は同じです。

まとめ

さくらのAI Engineでは、マニュアルに掲載されていない GET /v1/models を直接呼び出して、利用可能なモデル一覧を取得できます。

  • curlで取得できる
  • anthropic-version ヘッダーを付けても取得できる
  • OpenAI SDK、Anthropic SDKの models.list() でも取得できる
  • 今回はどの方法でも同じ12件のモデルIDを取得できた
  • ただし、data の並び順は変わる

参考リンク

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?