😽

Azure Container Apps でo1-mini LLMを叩く

2025/03/02に公開

Azure Container Appsから o1-mini LLMに対して推論試せるところまで環境作成しました。

アプリケーション

Managed ID認証を利用してAzureOpenAI (o1-mini)にアクセスします。
AzureOpenAI Service のoシリーズモデル、o1-miniのみ解放されていたので、o1-miniでの実装になります。

AzureOpenAI Serviceのo1-miniは、Global Standardのみの提供です。リージョンは、westus3 (今回は使用しませんが、Azure Container AppsのGPU Quota 解放してもらえたリージョンなのでこちらを利用

使用したモジュール

dependencies = [
    "azure-identity==1.20.0",
    "datamodel-code-generator==0.26.4",
    "flask==3.1.0",
    "flask-cors==5.0.0",
    "flask-restful==0.3.10",
    "gunicorn==23.0.0",
    "langchain-core==0.3.37",
    "langchain-openai==0.3.6",
    "openai==1.63.2",
    "prometheus-flask-exporter==0.23.0",
]

AOAIの場合、ManageID認証でOpenAIにアクセスすることが推奨されます。

from langchain_core.messages.ai import AIMessage
from langchain_core.messages.system import SystemMessage
from langchain_core.messages import HumanMessage
from langchain_core.prompts import PromptTemplate, ChatPromptTemplate
from langchain_openai import AzureChatOpenAI
from langchain_core.output_parsers import StrOutputParser
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
import os

token_provider = get_bearer_token_provider(
    DefaultAzureCredential(),
    "https://cognitiveservices.azure.com/.default"
)

推論部分

def chatcompletion(userMessage:str) -> str:

    prompt = ChatPromptTemplate.from_messages([
        ("human","{input}")
    ])

    llm = AzureChatOpenAI(
         api_version="2024-12-01-preview",
         azure_deployment="o1-mini",
         azure_endpoint="https://$azure_endpoint/",
         azure_ad_token_provider=token_provider,
         temperature=1.0
    )
    try:
        chain =   prompt | llm | StrOutputParser()
        outputText = chain.invoke({"input":userMessage})

    except Exception as error:
        raise error
    # ユーザーからのメッセージを受け取る
    return outputText

max_tokensパラメータが使えないので、gpt-4oとは使えるパラメータ違うので注意

https://community.openai.com/t/why-was-max-tokens-changed-to-max-completion-tokens/938077

APIバージョンは
https://zenn.dev/microsoft/articles/azure-openai-latest-api-schema

必要なリソースをAVMで作成

Container registry、
Log Analytics workspace、Container Apps EnvironmentはAVMで作成。
https://azure.github.io/Azure-Verified-Modules/

モジュールにバージョンを指定する必要があるが、バージョンどこで調べるのだが、バージョンは、GitリポジトリのTagで確認はできる。

https://github.com/Azure/bicep-registry-modules

targetScope = 'subscription'
 
@maxLength(7)
 
@description('Optional. The location to deploy resources to.')
param location string
 
resource coreResourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' = {
  name: 'rg-azure-container-apps-west-us-3'
  location: location
}

module registry 'br/public:avm/res/container-registry/registry:0.9.0' = {
  scope: coreResourceGroup
  name: 'registryDeployment'
  params: {
    // Required parameters
    name: 'flaskaoaiapi'
    // Non-required parameters
    acrSku: 'Standard'
  }
}
 
module workspace 'br/public:avm/res/operational-insights/workspace:0.7.0' = {
  scope: coreResourceGroup
  name: 'workspaceDeployment'
  params: {
    name: 'workspaceProperty'
  }
}
 
module managedEnvironment 'br/public:avm/res/app/managed-environment:0.8.0' = {
  scope: coreResourceGroup
  name: 'managedEnvironmentDeployment'
  params: {
    // Required parameters
    logAnalyticsWorkspaceResourceId: workspace.outputs.resourceId
    name: 'amemin001'
    zoneRedundant: false
  }
}
az deployment sub create --location 'westus3' --name 'lab02' --template-file main2.bicep --parameters parameters.json

Container Apps

ACRにアプリケーションイメージをプッシュしたあと、
Container Apps、システムマネージドIDが作成時に払い出されるので、quickstartのダミーコンテナで一度作成する必要がある。

https://qiita.com/kk31108424/items/b10ad1560ce153cbd74a

https://learn.microsoft.com/ja-jp/azure/container-apps/managed-identity-image-pull?tabs=bash&pivots=bicep

Container Resitory

AzureOpenAIのRole assignmentsにContainerAppsを登録しておく
ロール権限は、AcrPullで大丈夫。

AzureOpenAI

こちらも同様、AzureOpenAIのRole assignmentsにContainerAppsを登録しておく
ロール権限は、Cognitive Services OpenAI User

レスポンス

スケールインした状態でAPI Testerで確認。
モデルは、o1-mini (2024-12-01-preview)
0.25 0.5G

No Response time
一回目 32.61s
二回目 6.71s

Clade 3.5 Sonnet v2になるが、Cloud Run初回は、24.43s

hello worldを返すだけのGETリクエストなので、単純な比較はできないのですが、さくらのAppRun だと、初回39.14sかかりました。

Discussion