跳到主要内容

AI 问答

向当前 AI 服务商发送文本问题,通过返回的问题 ID 关联后续回调,并展示完整或增量回答。

Animated workflow

AI 问答请求生命周期

问题 ID 可能由方法同步返回,也可能通过回调返回;随后会收到流式回答和最终回调。

宿主 App

检查问题

提交请求前去除多余空白,并拒绝空提示词。

宿主 App

配置智能体

按需设置当前 AI 服务商支持的 Agent ID。

AI 服务

发送问题

调用 AIBudsAISDK.send 并保留同步返回的标识符。

启动回调

开始回答

AI 服务商开始回答时保存问题 ID。

AI 服务 → 应用

流式回答

存在 fullText 时优先使用,否则追加 deltaText 更新。

增量或完整文本
最终结果

完成或失败

收到最终回调后恢复 UI,并结束当前请求。

以 onFinishAnswering 或 onError 判断请求结束;isFinal 只表示最后一次回答更新。

前提条件

  • 初始化 AIBudsAISDK、选择已注册的 AI 服务商,并在需要时完成鉴权。
  • 确认所选 AI 服务商实现 AIAskingServiceAPI
  • 将回调触发的 UI 操作派发到主队列。

使用 AI 辅助实现

使用 AI 开发

让 AI 帮助实现此工作流

使用官方“实现 AIBuds AI 问答”技能,根据你的 App 完成实现。

请阅读并遵循 https://docs-aibuds.github.io/zh-Hans/skills/implement-aibuds-ai-asking,使用该技能在当前 iOS 项目中完成“实现 AIBuds AI 问答”,并验证结果。
查看官方技能

API 参考

框架

AIBudsAI.xcframework

导入

Swift
import AIBudsAI
import AIBudsAIFoundation

声明

Swift
/// Sends a text question to the currently selected AI service and receives
/// the answer as a stream of updates.
///
/// - Parameters:
///   - question: The question or prompt to send.
///   - config: Configuration for the request. Defaults to `.default`.
///   - onStartAnswering: Called when the service starts answering. The
///     question identifier may be `nil` if it has not yet been assigned.
///   - onAnswer: Called for each answer update.
///     - questionId: The identifier of the question being answered.
///     - deltaText: Newly generated text in this update, if available.
///     - fullText: The accumulated answer text, if available.
///     - isFinal: `true` when this is the final answer update.
///   - onFinishAnswering: Called when answering finishes successfully.
///   - onError: Called when validation fails or the provider reports an error.
/// - Returns: The question identifier when the request is created; otherwise `nil`.
public static func send(question: String,
                          config: AIAskingConfig = .default,
                onStartAnswering: ((_ questionId: String?) -> Void)? = nil,
                        onAnswer: ((
                            _ questionId: String,
                            _ deltaText: String?,
                            _ fullText: String?,
                            _ isFinal: Bool
                        ) -> Void)? = nil,
               onFinishAnswering: ((_ questionId: String) -> Void)? = nil,
                         onError: ((_ questionId: String, _ error: Error) -> Void)? = nil) -> String?

请参阅 sendAIAskingConfig

配置

AIAskingConfig.specifiedAgent 是 AI 服务商专用的可选值。设为 nil 时,由服务商自动选择 Agent。Demo 中的 Agent ID 不一定适用于其他账号或 AI 服务商,请勿直接复用。

使用示例

示例参考 AIAskingDemoController:既能处理完整累积回答,也能处理增量回答;保留最新的非空问题标识符,并区分最终回答更新与请求完成。

Swift
let question = input.trimmingCharacters(in: .whitespacesAndNewlines)
guard !question.isEmpty else { return }

let config = AIAskingConfig()
config.specifiedAgent = selectedAgentID

var activeQuestionID: String?
var answer = ""

let returnedID = AIBudsAISDK.send(
    question: question,
    config: config,
    onStartAnswering: { questionID in
        DispatchQueue.main.async {
            activeQuestionID = questionID ?? activeQuestionID
        }
    },
    onAnswer: { questionID, deltaText, fullText, isFinal in
        DispatchQueue.main.async {
            activeQuestionID = questionID
            if let fullText {
                answer = fullText
            } else if let deltaText {
                answer += deltaText
            }
            render(answer: answer, isFinalUpdate: isFinal)
        }
    },
    onFinishAnswering: { questionID in
        DispatchQueue.main.async {
            activeQuestionID = questionID
            setAsking(false)
        }
    },
    onError: { questionID, error in
        DispatchQueue.main.async {
            if !questionID.isEmpty { activeQuestionID = questionID }
            setAsking(false)
            show(error)
        }
    }
)

activeQuestionID = returnedID ?? activeQuestionID

错误处理

如果提交前的检查失败,send 可能返回 nil 并同步调用 onError,此时错误回调中的问题 ID 为空。请求进行期间应避免重复提交,并在 onFinishAnswering 或 onError 中恢复 UI。

注意事项

  • isFinal 标记最后一次流式回答更新;使用 onFinishAnswering 表示请求成功完成。
  • AI 服务商可能返回 fullTextdeltaText 或同时返回两者。优先使用 fullText,避免重复拼接文本。
  • 公开 API 当前没有提供取消活动提问请求的方法。