跳到主要内容

选择 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.xcframeworkAIBudsMagicHelper.xcframework

导入

Swift
import AIBudsAI
import AIBudsAIFoundation
import AIBudsStarBurst
import AIBudsMagicHelper

声明

服务商生命周期由 AIBudsAISDK 管理。服务商实现遵循 AIConnectSDK

Swift
/// 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

AI 服务商

SwiftObjective-C描述
.noneAIBudsAIServiceVendorNone未选择服务商,这是默认状态。
.starBurstAIBudsAIServiceVendorStarBurst星芒 AI(字节跳动)。
.mltcloudAIBudsAIServiceVendorMltcloud骆方案 AI(美乐创)。

完整枚举定义请参阅 AIServiceVendor

使用示例

注册服务商 SDK

服务商通常在 App 启动时注册一次。

Swift
import AIBudsAI
import AIBudsStarBurst
import AIBudsMagicHelper

let initialized = AIBudsAISDK.initialize([
    StarBurstSDK.shared,
    MagicHelperSDK.shared,
])

guard initialized else {
    print("AIBudsAISDK initialization failed")
    return
}

如果 App 使用 AIBudsAllInOneSDK,其初始化可安装随附的 AI 服务商。不要再次初始化 AIBudsAISDK

选择服务商

Swift
let vendor: AIServiceVendor = .starBurst
AIBudsAISDK.setAIServiceVendor(vendor)

print("Selected provider: \(AIBudsAISDK.aiServiceVendor)")
print("Supported languages: \(AIBudsAISDK.allSupportedLanguages(for: vendor))")

检查鉴权要求

配置已连接设备的 AI 信息后,对鉴权模式由 App 发起的服务商执行鉴权。

Swift
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")
    }
}

注意事项

  • setAIServiceVendor(_:) 不会注册或安装服务商 SDK。
  • 选择 .none 后,依赖 AI 服务的操作没有可用服务商。
  • 多次调用 initialize(_:) 会返回 false,并保留现有初始化。
  • 多个实现声明同一服务商时,保留首个注册的实现。
  • 不同服务商支持的功能和语言可能不同。