AI 总结
从文本生成简洁总结,并在所选服务商返回增量结果时更新 UI。
前提条件
AIBudsAISDK已初始化,并已选择注册的服务商。- 服务商支持
AISummaryServiceAPI。 - 需要时服务商鉴权和网络访问均可用。
- 输入文本不为空。
使用 AI 辅助实现
使用 AI 开发
让 AI 帮助实现此工作流
使用官方“实现 AIBuds AI 摘要”技能,根据你的 App 完成实现。
请阅读并遵循 https://docs-aibuds.github.io/zh-Hans/skills/implement-aibuds-ai-summary,使用该技能在当前 iOS 项目中完成“实现 AIBuds AI 摘要”,并验证结果。API 参考
框架
AIBudsAI.xcframework
导入
- Swift
- Objective-C
import AIBudsAI#import <AIBudsAI/AIBudsAI-Swift.h>声明
- Swift
- Objective-C
/// Generates a concise summary for the provided text.
/// - Parameters:
/// - text: The text to summarize.
/// - streamResultHandler: Called repeatedly with the current summary.
/// - isFinal: `true` when the summary is complete; otherwise `false`.
/// - transcript: The current incremental summary text.
/// - error: The operation error, or `nil` when no error occurred.
public static func summary(withText text: String,
streamResultHandler: ((_ isFinal: Bool, _ transcript: String?, _ error: Error?) -> Void)? = nil)
/// Cancels the current summary operation before it completes.
public static func cancelSummary()/// Generates a concise summary for the provided text.
/// - Parameters:
/// - text: The text to summarize.
/// - streamResultHandler: Called repeatedly with the current summary.
/// - isFinal: `YES` when the summary is complete; otherwise `NO`.
/// - transcript: The current incremental summary text.
/// - error: The operation error, or `nil` when no error occurred.
+ (void)summaryWithText:(NSString * _Nonnull)text
streamResultHandler:(void (^ _Nullable)(BOOL, NSString * _Nullable, NSError * _Nullable))streamResultHandler;
/// Cancels the current summary operation before it completes.
+ (void)cancelSummary;请参阅 summary 和 cancelSummary。
使用示例
- Swift
- Objective-C
let sourceText = """
The AIBuds SDK connects supported devices to an iOS application and exposes
device control, media, AI, voice assistant, and diagnostic capabilities.
"""
AIBudsAISDK.summary(withText: sourceText) { isFinal, transcript, error in
DispatchQueue.main.async {
if let error {
summaryLabel.text = "Summary failed: \(error.localizedDescription)"
return
}
if let transcript {
summaryLabel.text = transcript
}
if isFinal {
summaryActivityIndicator.stopAnimating()
}
}
}NSString *sourceText = @"The AIBuds SDK connects supported devices to an iOS application and "
"exposes device control, media, AI, voice assistant, and diagnostic "
"capabilities.";
[AIBudsAISDK summaryWithText:sourceText
streamResultHandler:^(BOOL isFinal, NSString *transcript, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error != nil) {
self.summaryLabel.text = [NSString
stringWithFormat:@"Summary failed: %@", error.localizedDescription];
return;
}
if (transcript != nil) {
self.summaryLabel.text = transcript;
}
if (isFinal) {
[self.summaryActivityIndicator stopAnimating];
}
});
}];不再需要结果时,取消正在进行的请求:
- Swift
- Objective-C
AIBudsAISDK.cancelSummary()[AIBudsAISDK cancelSummary];注意事项
- 此 API 仅总结文本,不接收文件路径,也不提供
maxLength参数。 - 服务商返回逐步增长的总结,因此每次 transcript 可替换此前展示值。
- 即使此前收到过 transcript,只要 error 非
nil就视为失败。 cancelSummary()不提供完成回调;请求取消时应更新 App 状态。