मुख्य कंटेंट तक स्किप करें

AI सवाल-जवाब

चुने गए AI सेवा प्रदाता को टेक्स्ट में सवाल भेजें, callbacks को मिले question identifier से जोड़ें और पूरा या क्रमशः आता उत्तर दिखाएँ।

Animated workflow

AI सवाल-जवाब request lifecycle

Request identifier synchronously या callback से मिल सकता है; इसके बाद streamed answer updates और एक terminal callback आता है।

Host app

सवाल जाँचें

Input trim करें और request बनाने से पहले खाली prompt अस्वीकार करें।

Host app

Agent कॉन्फ़िगर करें

Optional provider-specific agent identifier चुनें।

AI service

सवाल भेजें

AIBudsAISDK.send call करें और synchronously मिला identifier सहेजें।

Start callback

उत्तर शुरू

Provider के उत्तर शुरू करते समय question identifier लें।

AI service → app

उत्तर stream करें

fullText मिले तो उसे प्राथमिकता दें; अन्यथा deltaText update जोड़ें।

delta या पूरा text
अंतिम परिणाम

पूरा या विफल

Terminal callback से UI फिर चालू करें और request state बंद करें।

onFinishAnswering या onError को terminal मानें; isFinal केवल अंतिम answer update दर्शाता है।

आवश्यक शर्तें

  • AIBudsAISDK initialize करें, registered सेवा प्रदाता चुनें और आवश्यकता होने पर authentication पूरा करें।
  • पुष्टि करें कि चुना गया सेवा प्रदाता AIAskingServiceAPI implement करता है।
  • Callback से होने वाले UI updates को main queue पर भेजें।

AI की सहायता से लागू करें

AI से बनाएँ

इस वर्कफ़्लो को AI से लागू करें

आधिकारिक “AIBuds AI प्रश्नोत्तर लागू करें” स्किल से वर्कफ़्लो को अपने ऐप के अनुसार लागू करें।

https://docs-aibuds.github.io/hi/skills/implement-aibuds-ai-asking को पढ़ें और निर्देशों का पालन करें। इस स्किल से “AIBuds AI प्रश्नोत्तर लागू करें” को इस iOS प्रोजेक्ट में लागू करें और परिणाम सत्यापित करें।
आधिकारिक स्किल देखें

API संदर्भ

Framework

AIBudsAI.xcframework

Import

Swift
import AIBudsAI
import AIBudsAIFoundation

Declaration

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?

send और AIAskingConfig देखें।

कॉन्फ़िगरेशन

AIAskingConfig.specifiedAgent optional और सेवा प्रदाता के अनुसार अलग होता है। इसे nil रखने पर सेवा प्रदाता सामान्य agent चुनता है। Demo का agent identifier किसी दूसरे account या सेवा प्रदाता के लिए मान्य न मानें।

उपयोग के उदाहरण

उदाहरण AIAskingDemoController के flow का अनुसरण करते हैं: वे पूरा संचित उत्तर या delta स्वीकार करते हैं, नवीनतम non-empty question identifier सुरक्षित रखते हैं और अंतिम उत्तर update को request completion से अलग संभालते हैं।

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

त्रुटि प्रबंधन

Request बनने से पहले validation विफल होने पर send, nil लौटा सकता है और onError को synchronously call कर सकता है। तब error callback का question identifier खाली होता है। Request active रहने तक duplicate submission रोकें और दोनों terminal callbacks से UI state बहाल करें।

ध्यान देने योग्य बातें

  • isFinal streamed उत्तर के अंतिम update को दर्शाता है; सफल request completion के लिए onFinishAnswering उपयोग करें।
  • सेवा प्रदाता fullText, deltaText या दोनों दे सकता है। संचित टेक्स्ट दोहरने से बचाने के लिए fullText को प्राथमिकता दें।
  • Public API अभी active सवाल request को cancel करने की method उपलब्ध नहीं कराती।