📞

tsuzumiを使おうとした話③ ~ tsuzumiのAPIを使ってみよう~

に公開

あらすじ

今度新しいモデルを発表することが決まっているtsuzumiを使ってみようということで、
1ではMaaSとしてtsuzumiを使うためのAzure設定をし、
2では公式ドキュメントに沿ってアダプタチューニングのやり方を学びましたが、
今回はそこから外部連携したアプリを作るためにAPIを使ってみます。

というのもちょっとした目標を立ててみたからです。

Azureでデプロイしたモデルでチャットを作りたい!

Azure AI Fandryでデプロイしたtsuzumiのオリジナル版モデルをAPIで利用して
チャットアプリケーションを作ってみたいです。
成果としてもわかりやすいかな、と思ったので。
アプリを作るために今回はAPIの動かし方を少し学んでおこうと思いました。

↓NTTの資料はこちら
https://www.nttdata.com/jp/ja/-/media/nttdatajapan/files/lineup/tsuzumi/tsuzumionazuremaasv10-2.pdf?rev=9b5e30a9378a41c183a914b76b5153be

デプロイからエンドポイント情報とAPIキーを取得

Azure AI Foundryの「モデル+エンドデプロイ」(25/8/15現在, この辺の名前は時々変わる)からデプロイしたtsuzumiモデルを選びます。
※もしデプロイしていなかったら新しくデプロイします。

私が見た画面の枠で囲んだ部分のようにデプロイしたモデルのエンドポイント情報が載っているところを探してメモしておきます。

サンプルコードを実行してみる

上の図の右側に少し見切れていますが、親切なことにちょっとした使い方のインストラクションやコードサンプルも載っています。
まずはこれに沿ってコーディングをしてみましょう。

適当に環境を用意して(私はvenvを使いました)必要なパッケージをインストールします。
私の場合は特にモデルを使うために新しくこれを導入しました。

pip install azure-ai-inference

さらにサンプルコードとして公開されていたコードがあったので、試しにこれを実行してみます。

import os
from azure.ai.inference import ChatCompletionsClient
from azure.ai.inference.models import SystemMessage, UserMessage
from azure.core.credentials import AzureKeyCredential

endpoint = "*********" 
model_name = "************"
#↑*の部分には自分のモデルの値を使います

client = ChatCompletionsClient(
    endpoint=endpoint,
    credential=AzureKeyCredential("<API_KEY>"), #←ここにAPIキーを入れる
    api_version="2024-05-01-preview"
)

response = client.complete(
    messages=[
        SystemMessage(content="You are a helpful assistant."),
        UserMessage(content="I am going to Tokyo, what should I see?"),
    ],
    max_tokens=4096,
    temperature=0.15,
    #top_k=-1,
    #repetition_penalty=1.0,
    model=model_name
)

print(response.choices[0].message.content)

モデルのデプロイに載っているテストコードとしては、
15行目付近に「top_k」「repetition_penalty」が設定されていますが、
実行したところ25/8/15現在のライブラリのバージョンではサポートされていないようで、
コメントアウトしてエラーを回避しました。

出力結果がこれです。

That sounds like a wonderful trip! If you're interested in Japanese culture, history, or technology, I would recommend visiting the following places:

1. Tokyo Skytree: This is the tallest tower in the world and offers stunning views of the city. You can also visit the observation deck on the 41st floor for a panoramic experience.

2. Meiji Shrine: This is a Shinto shrine dedicated to Emperor Meiji and Empress Shoken. It's a beautiful place to learn about Japanese history and culture.

3. Sensoji Temple: This is one of Tokyo's most famous temples, known for its colorful temple buildings and the famous Kaminari-no-Himatsuri (fireworks festival) held annually in May.

4. Ueno Zoo: If you're an animal lover, this zoo is a must-visit. It's home to over 3,000 animals from around the world. 

5. Asakusa Kannon Temple: This temple is located in the heart of Tokyo and is known for its beautiful pond and the famous Kannon statue.

6. Shibuya Crossing: This is one of the most famous crossings in the world, where you can see people from all over the world crossing the street.

7. Harajuku: This is a trendy neighborhood with lots of shops, cafes, and street art. It's a great place to explore Japanese fashion and culture.

8. Ginza: This is one of Tokyo's most famous shopping districts, known for its high-end boutiques and department stores. 

9. Yanaka Cemetery: This is a peaceful cemetery in the heart of Tokyo, where you can learn about Japanese history and culture.

10. Imperial Palace: If you're interested in history, this is a must-visit. It's the former imperial palace of Japan and now serves as the official residence of the Emperor.

Remember to check the opening hours and any potential restrictions due to current events before planning your visit. Enjoy your trip!

おお。答えが返ってくるとちょっと感激する。
さて、次回はいよいよこのAPIキーを使ってアプリケーションの実装をしましょう。
今回はここまで。

Discussion