본문으로 건너뛰기

AI 질의응답

선택한 AI 서비스 제공자에게 텍스트 질문을 보내고, 반환된 질문 식별자로 콜백을 연결한 뒤 누적 또는 증분 방식의 답변을 표시합니다.

Animated workflow

AI 질의응답 요청 수명 주기

요청 식별자는 동기 반환값 또는 콜백으로 받을 수 있으며, 이후 스트리밍 답변 갱신과 하나의 종료 콜백이 이어집니다.

호스트 앱

질문 확인

요청을 만들기 전에 입력 양끝의 공백을 제거하고 빈 질문은 거부합니다.

호스트 앱

Agent 설정

필요하면 서비스 제공자별 agent 식별자를 선택합니다.

AI 서비스

질문 전송

AIBudsAISDK.send를 호출하고 동기적으로 반환된 식별자가 있다면 보관합니다.

시작 콜백

답변 시작

서비스 제공자가 답변을 시작할 때 질문 식별자를 저장합니다.

AI 서비스 → 앱

답변 스트리밍

fullText가 있으면 우선 사용하고, 없으면 deltaText 갱신을 이어 붙입니다.

증분 또는 전체 텍스트
최종 결과

완료 또는 실패

종료 콜백에서 UI를 다시 활성화하고 요청 상태를 닫습니다.

onFinishAnswering 또는 onError를 요청 종료로 처리하세요. isFinal은 마지막 답변 갱신만 나타냅니다.

사전 요구 사항

  • AIBudsAISDK를 초기화하고 등록된 서비스 제공자를 선택한 뒤 필요한 경우 인증을 완료합니다.
  • 선택한 서비스 제공자가 AIAskingServiceAPI를 구현하는지 확인합니다.
  • 콜백에서 수행하는 UI 작업은 메인 큐로 전달합니다.

AI를 활용해 구현

AI로 구현

AI로 이 워크플로 구현

공식 “AIBuds AI 질의응답 구현” 스킬을 사용해 앱에 맞게 구현하세요.

https://docs-aibuds.github.io/ko/skills/implement-aibuds-ai-asking을 읽고 지침을 따르세요. 이 스킬로 “AIBuds AI 질의응답 구현”을 이 iOS 프로젝트에 구현하고 검증하세요.
공식 스킬 보기

API Reference

프레임워크

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는 선택 사항이며 서비스 제공자마다 다릅니다. 제공자의 기본 agent 선택을 사용하려면 nil로 두세요. Demo의 agent 식별자가 다른 계정이나 제공자에서도 유효하다고 가정하면 안 됩니다.

사용 예제

다음 예제는 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

오류 처리

요청 생성 전 검증에 실패하면 sendnil을 반환하면서 onError를 동기적으로 호출할 수 있습니다. 이 경우 오류 콜백의 질문 식별자는 비어 있습니다. 요청이 진행되는 동안 중복 전송을 막고 두 종료 콜백 모두에서 UI 상태를 복원하세요.

참고

  • isFinal은 마지막 스트리밍 답변 갱신을 나타냅니다. 요청이 성공적으로 끝났는지는 onFinishAnswering으로 판단하세요.
  • 서비스 제공자는 fullText, deltaText 또는 둘 다 반환할 수 있습니다. 누적 텍스트가 중복되지 않도록 fullText를 우선 사용하세요.
  • 현재 공개 API는 진행 중인 질의응답 요청을 취소하는 메서드를 제공하지 않습니다.