Qwen Omni多模态モデルとUnityの統合実装

Qwen Omni多模态モデルの概要

Alibabaが公開したQwen Omniは多模态AIモデルで、テキスト/音声/画像/動画を入力として処理し、テキストと音声を同時出力します。ローカル実行には70GB以上のVRAMが必要ですが、API経由で効率的に利用可能です。

APIインターフェース仕様

基本リクエストはOpenAI形式と互換性があり、modalitiesパラメータで出力形式を制御します。

curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
  "model": "qwen-omni-turbo",
  "messages": [{"role": "user", "content": "質問内容"}],
  "modalities": ["text"],
  "audio": {"voice": "Cherry", "format": "wav"}
}'

マルチメディア入力の処理

メディアデータはBase64エンコードで送信:

// 画像入力例
content: [
  {"type": "image_url", "image_url": {"url": "data:image/png;base64,..."}},
  {"type": "text", "text": "説明文"}
]

// 動画入力例
content: [
  {"type": "video_url", "video_url": {"url": "data:;base64,..."}},
  {"type": "text", "text": "説明文"}
]

Unity実装例

テキストリクエスト

public IEnumerator SendTextRequest(string query, Action<string> callback)
{
  using (UnityWebRequest req = new UnityWebRequest(API_URL, "POST")) 
  {
    var payload = new RequestData {
      model = modelName,
      messages = new List<Message> { new() { role = "user", content = query } },
      modalities = new List<string> { "text" }
    };
    
    byte[] jsonData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload));
    req.uploadHandler = new UploadHandlerRaw(jsonData);
    req.downloadHandler = new DownloadHandlerBuffer();
    yield return req.SendWebRequest();
    
    if (req.result == UnityWebRequest.Result.Success) 
    {
      callback(ProcessResponse(req.downloadHandler.text));
    }
  }
}

画像とテキストの統合

public IEnumerator SendImageRequest(string query, string imageBase64, Action<string> callback)
{
  var mediaContent = new List<ContentItem> {
    new ImageContent { image_url = new ImageData { url = imageBase64 } },
    new TextContent { text = query }
  };
  
  yield return SendRequest(new MediaPayload(mediaContent), callback);
}

データ構造定義

[Serializable]
public class MediaPayload {
  public string model;
  public List<Message> messages = new();
  public List<string> modalities = new();
}

[Serializable]
public class Message {
  public string role;
  public List<ContentItem> content = new();
}

[Serializable]
public class ImageContent : ContentItem {
  public ImageData image_url = new();
}

タグ: Unity Qwen 多模态AI API統合 C#

6月8日 20:54 投稿