AI सवाल-जवाब
चुने गए AI सेवा प्रदाता को टेक्स्ट में सवाल भेजें, callbacks को मिले question identifier से जोड़ें और पूरा या क्रमशः आता उत्तर दिखाएँ।
AI सवाल-जवाब request lifecycle
Request identifier synchronously या callback से मिल सकता है; इसके बाद streamed answer updates और एक terminal callback आता है।
आवश्यक शर्तें
AIBudsAISDKinitialize करें, registered सेवा प्रदाता चुनें और आवश्यकता होने पर authentication पूरा करें।- पुष्टि करें कि चुना गया सेवा प्रदाता
AIAskingServiceAPIimplement करता है। - Callback से होने वाले UI updates को main queue पर भेजें।
AI की सहायता से लागू करें
इस वर्कफ़्लो को AI से लागू करें
आधिकारिक “AIBuds AI प्रश्नोत्तर लागू करें” स्किल से वर्कफ़्लो को अपने ऐप के अनुसार लागू करें।
https://docs-aibuds.github.io/hi/skills/implement-aibuds-ai-asking को पढ़ें और निर्देशों का पालन करें। इस स्किल से “AIBuds AI प्रश्नोत्तर लागू करें” को इस iOS प्रोजेक्ट में लागू करें और परिणाम सत्यापित करें।API संदर्भ
Framework
AIBudsAI.xcframework
Import
- Swift
- Objective-C
import AIBudsAI
import AIBudsAIFoundation#import <AIBudsAI/AIBudsAI-Swift.h>Declaration
- Swift
- Objective-C
/// 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?/// Sends a text question and streams answer updates.
///
/// - Parameters:
/// - question: The question or prompt to send.
/// - config: Configuration for this request.
/// - onStartAnswering: Called when the provider starts answering.
/// - onAnswer: Returns the question ID, delta text, accumulated text, and
/// whether this is the final answer update.
/// - onFinishAnswering: Called when answering finishes successfully.
/// - onError: Called for synchronous validation or provider errors.
/// - Returns: The question identifier when created; otherwise `nil`.
+ (NSString * _Nullable)sendQuestion:(NSString * _Nonnull)question
config:(AIBudsAIAskingConfig * _Nonnull)config
onStartAnswering:(void (^ _Nullable)(NSString * _Nullable))onStartAnswering
onAnswer:(void (^ _Nullable)(NSString * _Nonnull, NSString * _Nullable, NSString * _Nullable, BOOL))onAnswer
onFinishAnswering:(void (^ _Nullable)(NSString * _Nonnull))onFinishAnswering
onError:(void (^ _Nullable)(NSString * _Nonnull, NSError * _Nonnull))onError;send और AIAskingConfig देखें।
कॉन्फ़िगरेशन
AIAskingConfig.specifiedAgent optional और सेवा प्रदाता के अनुसार अलग होता है। इसे nil रखने पर सेवा प्रदाता सामान्य agent चुनता है। Demo का agent identifier किसी दूसरे account या सेवा प्रदाता के लिए मान्य न मानें।
उपयोग के उदाहरण
उदाहरण AIAskingDemoController के flow का अनुसरण करते हैं: वे पूरा संचित उत्तर या delta स्वीकार करते हैं, नवीनतम non-empty question identifier सुरक्षित रखते हैं और अंतिम उत्तर update को request completion से अलग संभालते हैं।
- Swift
- Objective-C
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 ?? activeQuestionIDNSString *question =
[self.input stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
if (question.length == 0)
return;
AIBudsAIAskingConfig *config = [[AIBudsAIAskingConfig alloc] init];
config.specifiedAgent = self.selectedAgentID;
__block NSString *activeQuestionID = nil;
__block NSString *answer = @"";
NSString *returnedID = [AIBudsAISDK sendQuestion:question
config:config
onStartAnswering:^(NSString *questionID) {
dispatch_async(dispatch_get_main_queue(), ^{
activeQuestionID = questionID ?: activeQuestionID;
});
}
onAnswer:^(NSString *questionID, NSString *deltaText, NSString *fullText, BOOL isFinal) {
dispatch_async(dispatch_get_main_queue(), ^{
activeQuestionID = questionID;
answer = fullText ?: [answer stringByAppendingString:deltaText ?: @""];
[self renderAnswer:answer isFinalUpdate:isFinal];
});
}
onFinishAnswering:^(NSString *questionID) {
dispatch_async(dispatch_get_main_queue(), ^{
activeQuestionID = questionID;
[self setAsking:NO];
});
}
onError:^(NSString *questionID, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (questionID.length > 0)
activeQuestionID = questionID;
[self setAsking:NO];
[self showError:error];
});
}];
activeQuestionID = returnedID ?: activeQuestionID;त्रुटि प्रबंधन
Request बनने से पहले validation विफल होने पर send, nil लौटा सकता है और onError को synchronously call कर सकता है। तब error callback का question identifier खाली होता है। Request active रहने तक duplicate submission रोकें और दोनों terminal callbacks से UI state बहाल करें।
ध्यान देने योग्य बातें
isFinalstreamed उत्तर के अंतिम update को दर्शाता है; सफल request completion के लिएonFinishAnsweringउपयोग करें।- सेवा प्रदाता
fullText,deltaTextया दोनों दे सकता है। संचित टेक्स्ट दोहरने से बचाने के लिएfullTextको प्राथमिकता दें। - Public API अभी active सवाल request को cancel करने की method उपलब्ध नहीं कराती।