إنشاء "ردود سريعة" باستخدام "حزمة تعلُّم الآلة" على أجهزة iOS
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يمكنك استخدام حزمة تعلّم الآلة لإنشاء ردود على الرسائل باستخدام جهاز.
الأمثل.
لإنشاء ردود سريعة، يتم تمرير سجلّ الرسائل الحديثة إلى مجموعة أدوات تعلُّم الآلة في
محادثة. إذا حدّدت حزمة تعلّم الآلة أنّ المحادثة باللغة الإنجليزية،
عدم احتواء المحادثة على موضوع قد يكون حساسًا أو ربما
على إنشاء ما يصل إلى ثلاثة ردود، والتي يمكنك اقتراحها على المستخدم.
قبل البدء
إذا لم يسبق لك إضافة Firebase إلى تطبيقك، يمكنك إجراء ذلك من خلال اتّباع
الخطوات الأولى في دليل البدء.
تضمين مكتبات ML Kit في Podfile:
pod 'Firebase/MLCommon', '6.25.0'
pod 'Firebase/MLNLSmartReply', '6.25.0'
بعد تثبيت مجموعات مشروعك الصغيرة أو تحديثها، احرص على فتح ملف Xcode
باستخدام .xcworkspace.
في تطبيقك، استورد Firebase:
Swift
importFirebase
Objective-C
@importFirebase;
1- إنشاء كائن سجلّ المحادثات
لإنشاء ردود سريعة، يتم ضبط صفيف من "مجموعة تعلُّم الآلة" على مصفوفة مرتّبة زمنيًا
TextMessage عنصرًا، مع أقدم طابع زمني أولاً كلما شغّل المستخدم
يرسل رسالة أو يتلقاها، وإضافة الرسالة وطابعها الزمني والرسالة
معرف المستخدم للمرسل إلى سجل المحادثات.
يمكن أن يكون رقم تعريف المستخدم أي سلسلة تعرّف المُرسِل بشكل فريد داخل
محادثة. لا يلزم أن يتوافق رقم تعريف المستخدم مع أي بيانات للمستخدم،
ولا يلزم أن يكون معرّف المستخدم متسقًا بين المحادثات أو
ذات الصلة بمنشئ الردود الذكية.
إذا أرسل المستخدم الذي تريد اقتراح ردود عليه، اضبط
isLocalUser على "صحيح".
Swift
varconversation:[TextMessage]=[]// Then, for each message sent and received:letmessage=TextMessage(text:"How are you?",timestamp:Date().timeIntervalSince1970,userID:"userId",isLocalUser:false)conversation.append(message)
Objective-C
NSMutableArray*conversation=[NSMutableArrayarray];// Then, for each message sent and received:FIRTextMessage*message=[[FIRTextMessagealloc]initWithText:@"How are you?"timestamp:[NSDatedate].timeIntervalSince1970userID:userIdisLocalUser:NO];[conversationaddObject:message];
يبدو عنصر سجلّ المحادثات كما يلي:
الطابع الزمني
رقم تعريف المستخدم
هل أنت مستخدم محلي؟
الرسالة
الخميس 21 شباط (فبراير) الساعة 13:13:39 بتوقيت المحيط الهادئ 2019
صحيح
هل أنت في طريقك؟
الخميس 21 شباط (فبراير)، 13:15:03 بتوقيت المحيط الهادئ 2019
الصديق0
خطأ
عذرًا، سأتأخر!
لاحظ أن أحدث رسالة في المثال أعلاه مأخوذة من رسالة غير محلية
المستخدم. هذا الأمر مهم لأنّ حزمة تعلّم الآلة تقترح ردودًا من المفترض إرسالها.
مستخدم تطبيقك: المستخدم المحلي. يجب أن تتأكد من أنك تجتاز
"تعلُّم الآلة" هو سجلّ محادثات ينتهي برسالة قد ينتقل إليها المستخدم
نريد الرد عليها.
2- الحصول على ردود على الرسائل
لإنشاء ردود سريعة على رسالة، عليك الحصول على نسخة افتراضية من "SmartReply" واجتياز.
سجلّ المحادثات بالطريقة suggestReplies(for:completion:) الخاصة به:
Swift
letnaturalLanguage=NaturalLanguage.naturalLanguage()naturalLanguage.smartReply().suggestReplies(for:conversation){result,erroringuarderror==nil,letresult=resultelse{return}if(result.status==.notSupportedLanguage){// The conversation's language isn't supported, so the// the result doesn't contain any suggestions.}elseif(result.status==.success){// Successfully suggested smart replies.// ...}}
Objective-C
FIRNaturalLanguage*naturalLanguage=[FIRNaturalLanguagenaturalLanguage];FIRSmartReply*smartReply=[naturalLanguagesmartReply];[smartReplysuggestRepliesForMessages:inputTextcompletion:^(FIRSmartReplySuggestionResult*_Nullableresult,NSError*_Nullableerror){if(error||!result){return;}if(result.status==FIRSmartReplyResultStatusNotSupportedLanguage){// The conversation's language isn't supported, so the// the result doesn't contain any suggestions.}elseif(result.status==FIRSmartReplyResultStatusSuccess){// Successfully suggested smart replies.// ...}}];]
إذا نجحت العملية، يتم تمرير كائن SmartReplySuggestionResult إلى
معالِج الإكمال. يحتوي هذا العنصر على قائمة تضم ما يصل إلى 3 عناصر مقترَحة.
الردود، التي يمكنك عرضها للمستخدم:
لاحظ أن ML Kit قد لا يعرض نتائج إذا لم يثق النموذج في
مدى صلة الردود المقترحة، فإن محادثة الإدخال ليست ضمن
باللغة الإنجليزية، أو إذا اكتشف النموذج موضوعًا حساسًا.
تاريخ التعديل الأخير: 2025-09-04 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-09-04 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["You can use ML Kit to generate message replies using an on-device\nmodel.\n\nTo generate smart replies, you pass ML Kit a log of recent messages in a\nconversation. If ML Kit determines the conversation is in English, and that\nthe conversation doesn't have potentially sensitive subject matter, ML Kit\ngenerates up to three replies, which you can suggest to your user.\n\n\u003cbr /\u003e\n\nBefore you begin\n\n1. If you have not already added Firebase to your app, do so by following the steps in the [getting started guide](/docs/ios/setup).\n2. Include the ML Kit libraries in your Podfile: \n\n ```\n pod 'Firebase/MLCommon', '6.25.0'\n pod 'Firebase/MLNLSmartReply', '6.25.0'\n ```\n After you install or update your project's Pods, be sure to open your Xcode project using its `.xcworkspace`.\n3. In your app, import Firebase: \n\n Swift \n\n ```swift\n import Firebase\n ```\n\n Objective-C \n\n ```objective-c\n @import Firebase;\n ```\n\n1. Create a conversation history object\n\nTo generate smart replies, you pass ML Kit a chronologically-ordered array of\n`TextMessage` objects, with the earliest timestamp first. Whenever the user\nsends or receives a message, add the message, its timestamp, and the message\nsender's user ID to the conversation history.\n\nThe user ID can be any string that uniquely identifies the sender within the\nconversation. The user ID doesn't need to correspond to any user data,\nand the user ID doesn't need to be consistent between conversations or\ninvocations of the smart reply generator.\n\nIf the message was sent by the user you want to suggest replies to, set\n`isLocalUser` to true. \n\nSwift \n\n var conversation: [TextMessage] = []\n\n // Then, for each message sent and received:\n let message = TextMessage(\n text: \"How are you?\",\n timestamp: Date().timeIntervalSince1970,\n userID: \"userId\",\n isLocalUser: false)\n conversation.append(message)\n\nObjective-C \n\n NSMutableArray *conversation = [NSMutableArray array];\n\n // Then, for each message sent and received:\n FIRTextMessage *message = [[FIRTextMessage alloc]\n initWithText:@\"How are you?\"\n timestamp:[NSDate date].timeIntervalSince1970\n userID:userId\n isLocalUser:NO];\n [conversation addObject:message];\n\nA conversation history object looks like the following example:\n\n| Timestamp | User ID | Local User? | Message |\n|------------------------------|---------|-------------|----------------------|\n| Thu Feb 21 13:13:39 PST 2019 | | true | are you on your way? |\n| Thu Feb 21 13:15:03 PST 2019 | FRIEND0 | false | Running late, sorry! |\n\nNote that the most recent message in the example above is from a non-local\nuser. This is important because ML Kit suggests replies intended to be sent\nby the user of your app: the local user. You should be sure you're passing\nML Kit a conversation log that ends with a message to which your user might\nwant to reply.\n\n2. Get message replies\n\nTo generate smart replies to a message, get an instance of `SmartReply` and pass\nthe conversation history to its `suggestReplies(for:completion:)` method: \n\nSwift \n\n let naturalLanguage = NaturalLanguage.naturalLanguage()\n naturalLanguage.smartReply().suggestReplies(for: conversation) { result, error in\n guard error == nil, let result = result else {\n return\n }\n if (result.status == .notSupportedLanguage) {\n // The conversation's language isn't supported, so the\n // the result doesn't contain any suggestions.\n } else if (result.status == .success) {\n // Successfully suggested smart replies.\n // ...\n }\n }\n\nObjective-C \n\n FIRNaturalLanguage *naturalLanguage = [FIRNaturalLanguage naturalLanguage];\n FIRSmartReply *smartReply = [naturalLanguage smartReply];\n [smartReply suggestRepliesForMessages:inputText\n completion:^(FIRSmartReplySuggestionResult * _Nullable result,\n NSError * _Nullable error) {\n if (error || !result) {\n return;\n }\n if (result.status == FIRSmartReplyResultStatusNotSupportedLanguage) {\n // The conversation's language isn't supported, so the\n // the result doesn't contain any suggestions.\n } else if (result.status == FIRSmartReplyResultStatusSuccess) {\n // Successfully suggested smart replies.\n // ...\n }\n }];\n ]\n\nIf the operation succeeds, a `SmartReplySuggestionResult` object is passed to\nthe completion handler. This object contains a list of up to 3 suggested\nreplies, which you can present to your user: \n\nSwift \n\n for suggestion in result.suggestions {\n print(\"Suggested reply: \\(suggestion.text)\")\n }\n\nObjective-C \n\n for (FIRSmartReplySuggestion *suggestion in result.suggestions) {\n NSLog(@\"Suggested reply: %@\", suggestion.text);\n }\n\nNote that ML Kit might not return results if the model isn't confident in\nthe relevance of the suggested replies, the input conversation isn't in\nEnglish, or if the model detects sensitive subject matter.\n| **Known issue**: Currently, on 32-bit iOS devices, ML Kit doesn't return suggestions for any input."]]