AI サービスプロバイダーを選択
アプリに組み込んだ AI プロバイダー SDK を登録し、その後の AI 処理で AIBudsAISDK が使用するプロバイダーを選択します。
前提条件
- AIBuds SDK 本体が初期化済みであること。
- 1 つ以上のプロバイダー SDK がインストールされ、アプリにリンクされていること。
- 選択するプロバイダーが
AIBudsAISDK.initialize(_:)に渡した配列へ含まれていること。 - プロバイダー認証やデバイスに依存する AI 処理より先に、デバイス情報を設定していること。
AI を活用して実装
AI で実装
AI でこのワークフローを実装
公式の「AIBuds AI プロバイダーの選択」スキルを使い、アプリに合わせて実装します。
https://docs-aibuds.github.io/ja/skills/select-aibuds-ai-provider を読み、その指示に従ってください。このスキルで「AIBuds AI プロバイダーの選択」をこの iOS プロジェクトに実装し、検証してください。API リファレンス
フレームワーク
AIBudsAI.xcframework
プロバイダー実装は、AIBudsStarBurst.xcframework や AIBudsMagicHelper.xcframework などの個別フレームワークとして提供されます。
インポート
- Swift
- Objective-C
import AIBudsAI
import AIBudsAIFoundation
import AIBudsStarBurst
import AIBudsMagicHelper#import <AIBudsAI/AIBudsAI-Swift.h>
#import <AIBudsMagicHelper/AIBudsMagicHelper-Swift.h>
#import <AIBudsStarBurst/AIBudsStarBurst-Swift.h>宣言
プロバイダーのライフサイクルは AIBudsAISDK が管理します。各プロバイダー実装は AIConnectSDK に準拠します。
- Swift
- Objective-C
/// Initializes the SDK with the specified provider implementations.
/// - Parameter aiSDKs: The providers to register. Their order determines
/// recognition priority; a later provider using an already-registered
/// Bluetooth data protocol type is ignored.
/// - Returns: `true` when initialization succeeds; otherwise `false`.
public static func initialize( _ aiSDKs: [AIConnectSDK] ) -> Bool
/// The current AI service vendor.
static var aiServiceVendor: AIServiceVendor { get }
/// Sets the AI service vendor for the SDK.
/// - Parameter aiServiceVendor: The vendor used by subsequent AI services.
/// - Important: Call this method before using AI service-dependent functionality.
public static func setAIServiceVendor(_ aiServiceVendor: AIServiceVendor) -> Void
/// Returns all languages supported by the specified vendor.
/// - Parameter vendor: The vendor whose languages are requested.
/// - Returns: Supported languages, or an empty array when the vendor is `.none`
/// or its provider SDK is not registered.
public static func allSupportedLanguages(for vendor: AIServiceVendor) -> [AIServiceLanguage]
/// Returns the authentication initiation mode for the specified vendor.
/// - Parameter vendor: The vendor whose authentication mode is requested.
/// - Returns: The provider authentication mode.
public static func authenticationMode(for vendor: AIServiceVendor) -> AIAuthenticationMode
/// Indicates whether the device is authenticated for the specified vendor.
/// - Parameter vendor: The vendor whose authentication state is requested.
/// - Returns: `true` when the provider reports an authenticated device.
public static func isAuthenticated(for vendor: AIServiceVendor) -> Bool/// Initializes the SDK with the specified provider implementations.
/// - Parameter aiSDKs: The providers to register. Their order determines
/// recognition priority; a later provider using an already-registered
/// Bluetooth data protocol type is ignored.
/// - Returns: `YES` when initialization succeeds; otherwise `NO`.
+ (BOOL)initWithAISDKs:(NSArray<id <AIBudsAIConnectSDK>> * _Nonnull)aiSDKs;
/// The current AI service vendor.
@property(nonatomic, class, readonly) AIBudsAIServiceVendor aiServiceVendor;
/// Sets the AI service vendor for the SDK.
/// - Parameter aiServiceVendor: The vendor used by subsequent AI services.
/// - Important: Call this method before using AI service-dependent functionality.
+ (void)setAIServiceVendor:(enum AIBudsAIServiceVendor)aiServiceVendor;
/// Returns all languages supported by the specified vendor.
/// - Parameter vendor: The vendor whose languages are requested.
/// - Returns: Supported languages, or an empty array when the vendor is
/// `AIBudsAIServiceVendorNone` or its provider SDK is not registered.
+ (NSArray<AIBudsAIServiceLanguage *> * _Nonnull)allSupportedLanguagesForVendor:(enum AIBudsAIServiceVendor)vendor;
/// Returns the authentication initiation mode for the specified vendor.
/// - Parameter vendor: The vendor whose authentication mode is requested.
/// - Returns: The provider authentication mode.
+ (enum AIBudsAIAuthenticationMode)authenticationModeForVendor:(enum AIBudsAIServiceVendor)vendor;
/// Indicates whether the device is authenticated for the specified vendor.
/// - Parameter vendor: The vendor whose authentication state is requested.
/// - Returns: `YES` when the provider reports an authenticated device.
+ (BOOL)isAuthenticatedForVendor:(enum AIBudsAIServiceVendor)vendor;サービスプロバイダー
| Swift | Objective-C | 説明 |
|---|---|---|
.none | AIBudsAIServiceVendorNone | プロバイダー未選択。既定の状態です。 |
.starBurst | AIBudsAIServiceVendorStarBurst | 星芒 AI(ByteDance)。 |
.mltcloud | AIBudsAIServiceVendorMltcloud | 駱方案 AI(Meilc)。 |
正式な列挙値は AIServiceVendor を参照してください。
使用例
プロバイダー SDK を登録
通常、プロバイダーの登録はアプリ起動時に一度だけ行います。
- Swift
- Objective-C
import AIBudsAI
import AIBudsStarBurst
import AIBudsMagicHelper
let initialized = AIBudsAISDK.initialize([
StarBurstSDK.shared,
MagicHelperSDK.shared,
])
guard initialized else {
print("AIBudsAISDK initialization failed")
return
}#import <AIBudsAI/AIBudsAI-Swift.h>
#import <AIBudsMagicHelper/AIBudsMagicHelper-Swift.h>
#import <AIBudsStarBurst/AIBudsStarBurst-Swift.h>
BOOL initialized = [AIBudsAISDK initWithAISDKs:@[
[AIBudsStarBurstSDK shared],
[AIBudsMagicHelperSDK shared],
]];
if (!initialized) {
NSLog(@"AIBudsAISDK initialization failed");
return;
}アプリで AIBudsAllInOneSDK を使用する場合は、その初期化時に同梱の AI プロバイダーを登録できます。AIBudsAISDK を重ねて初期化しないでください。
プロバイダーを選択
- Swift
- Objective-C
let vendor: AIServiceVendor = .starBurst
AIBudsAISDK.setAIServiceVendor(vendor)
print("Selected provider: \(AIBudsAISDK.aiServiceVendor)")
print("Supported languages: \(AIBudsAISDK.allSupportedLanguages(for: vendor))")AIBudsAIServiceVendor vendor = AIBudsAIServiceVendorStarBurst;
[AIBudsAISDK setAIServiceVendor:vendor];
NSLog(@"Selected provider: %ld", (long)AIBudsAISDK.aiServiceVendor);
NSLog(@"Supported languages: %@", [AIBudsAISDK allSupportedLanguagesForVendor:vendor]);認証要件を確認
接続中のデバイスに AI 情報を設定した後、アプリ起点の認証が必要なプロバイダーを認証します。
- Swift
- Objective-C
let vendor = AIBudsAISDK.aiServiceVendor
if AIBudsAISDK.authenticationMode(for: vendor) == .appInitiated,
!AIBudsAISDK.isAuthenticated(for: vendor)
{
AIBudsAISDK.authenticateDevice(deviceInfo) { success, error in
guard success else {
print(error?.localizedDescription ?? "Authentication failed")
return
}
print("AI provider authenticated")
}
}AIBudsAIServiceVendor vendor = AIBudsAISDK.aiServiceVendor;
if ([AIBudsAISDK authenticationModeForVendor:vendor] == AIBudsAIAuthenticationModeAppInitiated &&
![AIBudsAISDK isAuthenticatedForVendor:vendor]) {
[AIBudsAISDK
authenticateDevice:deviceInfo
completion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"%@", error.localizedDescription ?: @"Authentication failed");
return;
}
NSLog(@"AI provider authenticated");
}];
}注意事項
setAIServiceVendor(_:)はプロバイダー SDK の登録やインストールを行いません。.noneを選択すると、AI サービスに依存する処理で使用できるプロバイダーがなくなります。initialize(_:)を複数回呼び出すとfalseが返り、既存の初期化状態が維持されます。- 複数の実装が同じプロバイダー列挙値を宣言した場合は、最初に登録された実装が保持されます。
- 対応機能や対応言語はプロバイダーによって異なります。