LoginSignup
2
0

watsonx AssistantのExtentionをカスタマイズしてwatsonx.ai APIをデコード"Sampling"で呼び出す

Last updated at Posted at 2024-02-19

はじめに

watsonx Assistantからwatsonx.aiのAPIを呼び出す手順を説明するガイドが複数公開されています。
ガイドの中で、watsonx.aiのOpenAPIドキュメントとして以下のGitHubリポジトリを案内している場合が多いのではないでしょうか。
assistant-toolkit/integrations/extensions/starter-kits/language-model-watsonx/watsonx-openapi.json

しかし、上記のOpenAPIドキュメントは現状、watsonx.ai APIの「デコード」パラメータを"Sampling"で実行するためのパラメータが不足しています。

※Samplingモードとは
SamplingモードはGreedyモードと比較して生成される文のランダム性が大きくなります。
以下、ドキュメント「基盤モデル・パラメーター: デコードおよび停止基準」より引用

  • Greedy decoding: Selects the token with the highest probability at each step of the decoding process.
  • Sampling decoding: Offers more variability in how tokens are selected.

上記Git Hubリポジトリに"Sampling"モードに対応したパラメータの追加をリクエストしているのですが、反映されるまでの暫定策として本記事ではOpenAPIドキュメントを手動でアップデートする手順をご紹介し、"Sampling"モードでの実行を試してみたいと思います。

参考) "Greedy"モードのパラメータ(watsonx.ai UIより)
image.png

参考) "Sampling"モードのパラメータ(watsonx.ai UIより)
image.png

watsonx-openapi.jsonにパラメータを追加する

まずはGitHubにアクセスして、watsonx-openapi.jsonファイルをダウンロードしてください。

エディタで開き、"parameters"セクションに以下3つのパラメータを追加してください。

"decoding_method": {
  "type": "string",
  "description": "Decoding is the process that a model uses to choose the tokens in the generated output.",
  "example": "greedy"
},
"top_k": {
  "type": "number",
  "description": "Top-k sampling samples tokens with the highest probabilities until the specified number of tokens is reached.",
  "example": "50"
},
"top_p": {
  "type": "number",
  "description": "Top-p sampling samples tokens with the highest probability scores until the sum of the scores reaches the specified threshold value.",
  "example": "1"
}

参考) 編集後イメージ("parameters"セクション部分のみ抜粋)
image.png

watsonx Assistantでのチャットボット作成

以下のQiita記事を参考に「Step1:Action起動時のメッセージ」までを実施してください。
「Extension(カスタム拡張機能)の作成」手順では上記手順で編集したwatsonx-openapi.jsonをインポートしてください。
生成系AI活用術!watsonx Assistant × 生成系AI(watsonx.ai) 連携ガイド

続いて、「Step2: watsonx.ai用extensionの呼び出し」手順ではjsonファイルの編集によってデコード"Sampling"でもAPIを実行可能になっています。
本記事では例として以下のように設定してみます。
image.png

  • Parameters
    • version: 2023-05-29
    • input: 「Action Step Variables」→Step1で入力したメッセージを選択
    • model_id: meta-llama/llama-2-70b-chat
    • project_id: watsonx.aiの「project_id」の値を入力(36桁の英数記号)
  • Optional Parameters
    • parameters.min_new_tokens: 0
    • parameters.decoding_method: sample
    • parameters.repetition_penalty: 1
    • parameters.max_new_tokens: 200
    • parameters.top_k: 50
    • parameters.top_k: 1
    • parameters.temperature: 0.7
    • parameters.stop_sequences: 空欄(デフォルト ["\n\n"])

なお、"version", "input", "project_id"以外は別の値でも構いません。
watsonx.aiのプロンプトラボを活用しながら調整してみてください。
ドキュメント「基盤モデル・パラメーター: デコードおよび停止基準」

続けて、再度以下のQiita記事を参考にして「Step3: Extension実行結果の表示」を実施してください。
生成系AI活用術!watsonx Assistant × 生成系AI(watsonx.ai) 連携ガイド

以上でアップデート版のjsonファイルを用いたwatsonx Assistantのチャットボット作成が完了です。

動作確認

動作を確認してみましょう。
※パラメータにより応答が変わることにフォーカスしており、応答の精度については言及いたしません。
※同じパラメータでも以下と同じ応答を返さない場合があります。

Action編集画面右下のPreviewボタンでチャットボットを起動します。
image.png

アクション名(ここでは"call watsonx.ai")を入力してアクションを起動します。
Step1で設定したメッセージが表示されます。
image.png

生成AIに送信する指示文を入力します。
[例]
Write a thank you note in 5 sentences for attending a workshop. Attendees: interns Topic: codefest, AI Tone: energetic

"Sampling"モード応答例

上記パラメータ設定での"Sampling"モードでは以下の応答が返りました。
image.png

"Greedy"モード応答例

"Greedy"モードの応答と比較してみましょう。

[Greedyモードパラメータ設定例]
image.png

[Greedyモード応答例]
image.png

このようにパラメータ設定により応答を変更することができました。

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