选择 AI 服务商
注册 App 包含的 AI 服务商 SDK,然后选择 AIBudsAISDK 后续 AI 操作使用的服务商。
前提条件
- 基础 AIBuds SDK 已初始化。
- App 至少安装并链接一个服务商 SDK。
- 所选服务商包含在传给
AIBudsAISDK.initialize(_:)的数组中。 - 在服务商鉴权或依赖设备的 AI 操作前已配置设备信息。
使用 AI 辅助实现
使用 AI 开发
让 AI 帮助实现此工作流
使用官方“选择 AIBuds AI 服务提供商”技能,根据你的 App 完成实现。
请阅读并遵循 https://docs-aibuds.github.io/zh-Hans/skills/select-aibuds-ai-provider,使用该技能在当前 iOS 项目中完成“选择 AIBuds AI 服务提供商”,并验证结果。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;AI 服务商
| Swift | Objective-C | 描述 |
|---|---|---|
.none | AIBudsAIServiceVendorNone | 未选择服务商,这是默认状态。 |
.starBurst | AIBudsAIServiceVendorStarBurst | 星芒 AI(字节跳动)。 |
.mltcloud | AIBudsAIServiceVendorMltcloud | 骆方案 AI(美乐创)。 |
完整枚举定义请参阅 AIServiceVendor。
使用示例
注册服务商 SDK
服务商通常在 App 启动时注册一次。
- 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;
}如果 App 使用 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 信息后,对鉴权模式由 App 发起的服务商执行鉴权。
- 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,并保留现有初始化。 - 多个实现声明同一服务商时,保留首个注册的实现。
- 不同服务商支持的功能和语言可能不同。