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?

Azure Open AI GPT4を使ってみた

Posted at

1. インストール

Open AI Pythonライブラリーをインストール

pip install openai

2. デプロイ

Azure OpenAIのGPT4をデプロイ

export RES_GRP="MyResGrp"
export OPENAI_RES_NAME1="MyOpenAIResName"
export DEPLOY_NAME="MyModel"

export REGION="eastus"

export MODEL_NAME="gpt-4"
export MODEL_VERSION="0613"

##########################################
# Set subscription
az account set --subscription 'Your subscription'

#################################
# Create Resource Group
az group create --name $RES_GRP --location $REGION

#################################
# Create OpenAI Resource
az cognitiveservices account create --name $OPENAI_RES_NAME1 --resource-group $RES_GRP --location $REGION --kind OpenAI --sku s0

#################################
# Deploy model
az cognitiveservices account deployment create --name $OPENAI_RES_NAME1 --resource-group $RES_GRP --deployment-name $DEPLOY_NAME --model-name $MODEL_NAME --model-version $MODEL_VERSION --model-format OpenAI --sku-capacity "1" --sku-name "Standard"

3. アクセスキー・エンドポイントの設定

アプリケーションからAzure OpenAIにアクセスするためには、アクセスキーとエンドポイントが必要。

エンドポイント、および、アクセスキーを取得

# エンドポイントのURLを取得
az cognitiveservices account show --name $OPENAI_RES_NAME1 --resource-group $RES_GRP | jq -r .properties.endpoint

# アクセスキーを取得
az cognitiveservices account keys list --name $OPENAI_RES_NAME1 --resource-group  $RES_GRP | jq -r .key1

取得したエンドポイント、および、アクセスキーを.profileに記載

export AZURE_OPENAI_API_KEY="0123456a3c6a401234567c41f3212345"
export AZURE_OPENAI_ENDPOINT="https://xxx.yyy.zzzzzz.microsoft.com/"

シェルを再起動

exec $SHELL -l

4. アプリケーションの実行

MSDNのチュートリアルのサンプルを実行

import os
from openai import AzureOpenAI

client = AzureOpenAI(
  api_key = os.getenv("AZURE_OPENAI_API_KEY"),  
  api_version = "2024-02-01",
  azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT") 
)

conversation=[{"role": "system", "content": "You are a helpful assistant."}]

while True:
    user_input = input("Q:")      
    conversation.append({"role": "user", "content": user_input})

    response = client.chat.completions.create(
        model="MyModel",        
        messages=conversation
    )

    conversation.append({"role": "assistant", "content": response.choices[0].message.content})
    print("\n" + response.choices[0].message.content + "\n")
    

5. アプリケーションの実行結果

アプリケーションの実行結果は、例えば以下のようになる

rungpt4.png

6. リソースの削除

課金されないように、アプリケーションのテストが済んだらリソースを削除する

az cognitiveservices account deployment delete --name $OPENAI_RES_NAME1 --resource-group  $RES_GRP --deployment-name $DEPLOY_NAME

az group delete --name $RES_GRP

7. 他のモデルの使用

GPT3.5 Turboを使用する場合、モデル名・バージョンを変更する

export MODEL_NAME="gpt-35-turbo"
export MODEL_VERSION="0125"
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?