2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

EmbeddingGemma 2 を Jev のような意思決定モデルとして使用

2
Last updated at Posted at 2026-10-21

埋め込みモデル EmbeddingGemma 2 を Jev のような意思決定モデルとして使うことができると教えてもらったので試しました。GPU は使いません。CPU で実行します。

実行環境: Google Colab

Pythonパッケージ

  • litert-lm-api==0.18.1
  • mediapipe==1.1.1

EmbeddingGemma 2 を埋め込みモデルとして実行

意思決定モデルの前に、LiteRT-LM を用いて EmbeddingGemma 2 を埋め込みモデルとして実行します。意思決定モデルの手順だけを知りたい方は読み飛ばしてください。

LiteRT-LM は、さまざまなエッジデバイスで大規模言語モデルを高性能に実行するために設計された、オープンソースの推論フレームワークです。

EmbeddingGemma 2 を LiteRT-LM で動作させるには、専用のモデルファイルが必要です。

embeddinggemma-2-740m-litert-lm は、フルスペックなマルチモーダルモデルです。embeddinggemma-2-text-vision-440m-litert-lm は、軽量なマルチモーダル(テキストおよび画像)モデルです。embeddinggemma-2-text-270m-litert-lm は、テキスト専用モデルです。これらは LiteRT を介して、コンシューマー向けハードウェアやエッジデバイス上で低遅延なデプロイに最適化されています。

今回は、テキスト専用の embeddinggemma-2-text-270m-litert-lm を使います。

LiteRT-LM をインストール

!pip install litert-lm-api

EmbeddingGemma 2 LiteRT-LM モデルをダウンロード

!hf download litert-community/embeddinggemma-2-text-270m-litert-lm \
  embeddinggemma-2-text-270m.litertlm --local-dir .

Python コード

import os
from litert_lm import Backend, EmbeddingEngine

# XNNPACKキャッシュディレクトリを作成
cache_dir = "./litertlm_cache"
os.makedirs(cache_dir, exist_ok=True)

# エンジン初期化
with EmbeddingEngine(
    "embeddinggemma-2-text-270m.litertlm",
    backend=Backend.CPU(),
    cache_dir=cache_dir,
) as engine:
    # 埋め込み
    emb = engine.compute_embedding(
        "LiteRT-LMを使ってPythonから直接埋め込みを生成"
    ).embedding

    print(f"次元数: {len(emb)}")
    print(f"ベクトル値 (先頭5件): {emb[:5]}...\n")

実行結果

次元数: 768
ベクトル値 (先頭5件): [-0.06863390654325485, 0.005630367435514927, 0.021368857473134995, -0.021428363397717476, -0.043164271861314774]...

実行するとキャッシュディレクトリ cache_dir に XNNPACK の最適化キャッシュが作成されます。

ここでは省略していますが、EmbeddingGemma 2 には命令プレフィックスを使用して埋め込みを最適化する機能があります。詳しくは次のドキュメントの「Task Instruction Prefixes」を参照してください。

EmbeddingGemma 2 を意思決定モデルとして実行

MediaPipe 意思決定タスクは、カテゴリ選択、ブール条件、ルーブリックスコアなどの構造化された意思決定質問を、決定論的で較正済みのゼロデコード意思決定エンジンとして評価します。

カテゴリ選択を試します。

MediaPipe をインストール

!pip install mediapipe

EmbeddingGemma 2 LiteRT-LM モデルをダウンロード

!hf download litert-community/embeddinggemma-2-text-270m-litert-lm \
  embeddinggemma-2-text-270m.litertlm --local-dir .

DecisionMaker インスタンスを作成

from mediapipe.tasks.python.core import base_options
from mediapipe.tasks.python.decision import decision_maker

options = decision_maker.DecisionMakerOptions(
    base_options=base_options.BaseOptions(
        model_asset_path="embeddinggemma-2-text-270m.litertlm"
    ),
    max_num_tokens=512,
)

maker = decision_maker.DecisionMaker.create_from_options(options)

選択するカテゴリ「返金」,「不具合」,「一般」を定義

intent_q = decision_maker.ChoiceQuestion(
    instructions="顧客からの問い合わせを振り分ける。",
    criteria={
        "返金": "返金リクエストや請求に関するトラブル。",
        "不具合": "アプリのクラッシュや技術的な問題。",
        "一般": "製品に関する一般的な質問。"
    },
    normalize_prior=True
)

maker.prewarm_choice(intent_q)

「決済中にアプリが2回もクラッシュして、カードに2重で請求されたんだけど!」というメッセージを評価

query = "決済中にアプリが2回もクラッシュして、カードに2重で請求されたんだけど!"

# タスク実行
res = maker.evaluate_choice(query, intent_q)

maker.close()

print(f"選択キー: {res.selected_key}")
print(f"確率: {res.probabilities}")
print(f"信頼度: {res.confidence:.4f}")
print(f"90% Prediction Set: {res.prediction_set}")

実行結果

選択キー: 不具合
確率: {'一般': 0.002880726708099246, '返金': 0.2529014050960541, '不具合': 0.7442178726196289}
信頼度: 0.4681
90% Prediction Set: ['不具合', '返金']

簡単すぎて驚きました!

以上です。
最後までお読みいただきありがとうございます。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?