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?

Functionから呼び出しているAzure OpenAI ServiceのAPIをGPT3.5から画像入力可能なGPT4oに変えてみた

Posted at

はじめに

Azure Functionsを使って開発した関数で、Azure OpenAI ServiceのGPT3.5のAPIを呼び出して利用していましたが、先月末GPT4oがGAしたのでこっちに移行したいと思います

関数のベースは以前作成した下記の記事のものを修正する形で使います

米国東部リージョンでAzure OpenAIのリソースを作成する

GPT4oは日本リージョンでは利用できないので米国東部リージョンを利用します
image.png

リソース作成後、Azure OpenAI Studioでモデルをデプロイします
image.png

Functionで画像データがあった場合の処理を追加

HTTPリクエストの要求本文の形としては下記のイメージです
ユーザの入力に相当する"type"="human"のところに、imagesのリストを入れるようにします。
画像が複数入ってきてもいいようにリスト形式にしています。
画像データはbase64でエンコードされたものを受け取ります。

{
    "messagelist":[
        {"type":"system","message":"あなたは優秀なアシスタントです。日本語で回答してください。"},
        {"type":"human","message":"こんにちは","images":[]},
        {"type":"ai","message":"こんにちは!今日はどんなお手伝いができますか?"},
        {"type":"human","message":"なんて書いてある?","images":["data:image/png;base64,<エンコードした文字列>","data:image/png;base64,<エンコードした文字列>"]}
    ]
}

imagesのリストに要素があった場合は、画像データがついてるものと判断しHumanMessageのContentに含めるように処理を修正します。

import azure.functions as func
import logging
import os
from langchain_core.messages import HumanMessage,SystemMessage,AIMessage
from langchain_openai import AzureChatOpenAI

app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)

@app.route(route="simplechat")
def simplechat(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    model = AzureChatOpenAI(
        openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],
        azure_deployment=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"],
    )

    data = req.get_json()
    message_lists = []
    for item in data['messagelist']:
        if item['type'] == 'system':
            message_lists.append(SystemMessage(content=item['message']))
        elif item['type'] == 'human':
            message_content = [{"type": "text", "text": item['message']}]
            if 'images' in item and len(item['images']) > 0:
                for image_url in item['images']:
                    message_content.append({"type": "image_url", "image_url": {"url": image_url}})
            message_lists.append(HumanMessage(content=message_content))
            
        elif item['type'] == 'ai':
            message_lists.append(AIMessage(content=item['message']))
    
    response = model.invoke(message_lists)

    if response.content:
        return func.HttpResponse(response.content)
    else :
        return func.HttpResponse(
            "This HTTP triggered function executed successfully. But an unexpected error occurred with the Azure OpenAI Service API.",
            status_code=200
        )

AzureにデプロイしたらFunctionの環境変数の値をGPT4oのものに変更しておきましょう。

実行

実際Functionに対して画像を送ってみて、画像認識された回答が返ってくることを確認
image.png

単価の高いGPT4は飛ばしてよいので、GPT4oが早く日本リージョンで使えるようになると良いですね

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?