0
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?

More than 1 year has passed since last update.

API キーを手元に置かず(Azure の)OpenAI を叩く

Posted at

Azure のコンソールから OpenAI の生 API キーをコピーしローカルで使うのは少し抵抗がある。
Azure OpenAI Service(AOAI)であれば Azure AD(Entra ID)の認証で API キーの管理から解放される。

事前準備

必要なロールを付与

Azure のコンソールにて Cognitive Services OpenAI User を該当のプリンシパル(ユーザ)に付与する。

image.png

Azure CLI を導入

インストール

下記は macOS の場合。

brew update && brew install azure-cli

ログイン

ターミナルにて Azure CLI にログインする。
コマンドを打つと通常のパブリッククラウドの CLI 同様 OAuth 的にブラウザ立ち上がって認証画面が出る。

az login

認証完了後~/.azure以下に認証情報やプロファイル情報が配置される。
これをソースコードが認証情報として参照するため API キーが不要になる。

実装

コードを書く

準備完了したので、Azure CLI で生成したクレデンシャルを参照するように Python コードを書く。
v1.0.0 discussions にて議論されている通り、OpenAI Python SDK は v1 からだいぶ書き味が変わったので注意。

v1 以降で書く

v1.1 にて動作確認済み。AzureOpenAI()を使う。

import openai
 from openai import AzureOpenAI
 from azure.identity import DefaultAzureCredential, get_bearer_token_provider
 
 default_credential = DefaultAzureCredential()
 token_provider = get_bearer_token_provider(
     DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
 )
 
 client = AzureOpenAI(
     azure_ad_token_provider=token_provider,
     azure_endpoint="https://YOUR-DOMAIN.openai.azure.com",
     api_version="yyyy-mm-dd",
 )
 
 resp = client.chat.completions.create(
     model="YOUR-DEPLOYMENT-NAME",
     messages=[
         {"role": "system", "content": "You are a helpful assistant."},
         {"role": "user", "content": "Who won the world series in 2020?"},
         {
             "role": "assistant",
             "content": "The Los Angeles Dodgers won the World Series in 2020.",
         },
         {"role": "user", "content": "Where was it played?"},
     ],
 )

今後頻繁にシグネチャ変更があるとは考えづらいが、公式ドキュメントやリリースノート追うのをオススメする。

DevDay で大幅なアップデートのあった今、v1 に移行する手筈を整えておくのが良さそう。

v1 より前のバージョンで書く

手元では試していない。Microsoft のドキュメント:Python を使用して OpenAI エンドポイントと Azure OpenAI エンドポイントを切り替える方法のコードを参考にした。

import openai
 from azure.identity import DefaultAzureCredential
 
 credential = DefaultAzureCredential()
 token = credential.get_token("https://cognitiveservices.azure.com/.default")
 
 openai.api_type = "azure_ad"
 openai.api_key = token.token
 openai.api_base = "https://YOUR-DOMAIN.openai.azure.com"
 openai.api_version = "yyyy-mm-dd"  # subject to change
 
 chat_completion = openai.ChatCompletion.create(
     messages="<messages>",
     deployment_id="gpt-4" # This must match the custom deployment name you chose for your model.
     #engine="gpt-4"
 ) 

補足

Zenn でも MS の方が本件に関する記事を書いていた。
よりセキュリティを高度化するための言及もある。

0
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
0
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?