GenAI Summarization API ของ ML Kit ช่วยให้คุณสร้างสรุปบทความและการสนทนาเป็นรายการหัวข้อย่อยโดยอัตโนมัติ ซึ่งจะช่วยให้ผู้ใช้เข้าใจข้อความจำนวนมาก
สรุปได้รับประโยชน์จาก Generative AI ในอุปกรณ์เนื่องจากช่วยคลายความกังวลเกี่ยวกับความเป็นส่วนตัวของข้อมูลและประสิทธิภาพด้านต้นทุน แอปที่สรุปแชท อีเมล โน้ต และการช่วยเตือนส่วนตัวมักจะจัดการข้อมูลที่ละเอียดอ่อน ซึ่งทำให้การประมวลผลในอุปกรณ์มีความสำคัญต่อความเป็นส่วนตัวของผู้ใช้ นอกจากนี้ งานสรุป โดยเฉพาะงานที่มีบริบทยาวหรือมีรายการจำนวนมากอาจต้องใช้กำลังประมวลผลอย่างมาก การประมวลผลเนื้อหานี้ในอุปกรณ์จะช่วยลดภาระของเซิร์ฟเวอร์และค่าใช้จ่ายในการแสดงโฆษณา ขณะเดียวกันก็รักษาข้อมูลผู้ใช้ให้เป็นส่วนตัว
ความสามารถหลัก
GenAI Summarization API มีความสามารถดังต่อไปนี้
- สรุปข้อความที่จัดหมวดหมู่เป็นบทความหรือการสนทนา
- แสดงข้อมูลสรุปเป็นหัวข้อย่อย 1, 2 หรือ 3 หัวข้อ
เริ่มต้นใช้งาน
เพิ่ม API การสรุปของ ML Kit เป็นส่วนที่ต้องพึ่งพาในการกำหนดค่า build.gradle
implementation("com.google.mlkit:genai-summarization:1.0.0-beta1")
ต่อไป ให้ติดตั้งใช้งานโค้ดในโปรเจ็กต์
- สร้างออบเจ็กต์
Summarizer
- ดาวน์โหลดฟีเจอร์หากดาวน์โหลดได้
- สร้างคำขอสรุป
- เรียกใช้การอนุมานและดึงข้อมูลผลลัพธ์
Kotlin
val articleToSummarize = "Announcing a set of on-device GenAI APIs..."
// Define task with required input type, output type, and language
val summarizerOptions = SummarizerOptions.builder(context)
.setInputType(InputType.ARTICLE)
.setOutputType(OutputType.ONE_BULLET)
.setLanguage(Language.ENGLISH)
.build()
val summarizer = Summarization.getClient(summarizerOptions)
suspend fun prepareAndStartSummarization() {
// Check feature availability. Status will be one of the following:
// UNAVAILABLE, DOWNLOADABLE, DOWNLOADING, AVAILABLE
val featureStatus = summarizer.checkFeatureStatus().await()
if (featureStatus == FeatureStatus.DOWNLOADABLE) {
// Download feature if necessary. If downloadFeature is not called,
// the first inference request will also trigger the feature to be
// downloaded if it's not already downloaded.
summarizer.downloadFeature(object : DownloadCallback {
override fun onDownloadStarted(bytesToDownload: Long) { }
override fun onDownloadFailed(e: GenAiException) { }
override fun onDownloadProgress(totalBytesDownloaded: Long) {}
override fun onDownloadCompleted() {
startSummarizationRequest(articleToSummarize, summarizer)
}
})
} else if (featureStatus == FeatureStatus.DOWNLOADING) {
// Inference request will automatically run once feature is
// downloaded. If Gemini Nano is already downloaded on the device,
// the feature-specific LoRA adapter model will be downloaded
// quickly. However, if Gemini Nano is not already downloaded, the
// download process may take longer.
startSummarizationRequest(articleToSummarize, summarizer)
} else if (featureStatus == FeatureStatus.AVAILABLE) {
startSummarizationRequest(articleToSummarize, summarizer)
}
}
fun startSummarizationRequest(text: String, summarizer: Summarizer) {
// Create task request
val summarizationRequest = SummarizationRequest.builder(text).build()
// Start summarization request with streaming response
summarizer.runInference(summarizationRequest) { newText ->
// Show new text in UI
}
// You can also get a non-streaming response from the request
// val summarizationResult = summarizer.runInference(
// summarizationRequest).get().summary
}
// Be sure to release the resource when no longer needed
// For example, on viewModel.onCleared() or activity.onDestroy()
summarizer.close()
Java
String articleToSummarize = "Announcing: a set of on-device GenAI AI APIs.";
// Define task with required input type, output type, and language
SummarizerOptions summarizerOptions =
SummarizerOptions.builder(context)
.setInputType(SummarizerOptions.InputType.ARTICLE)
.setOutputType(SummarizerOptions.OutputType.ONE_BULLET)
.setLanguage(SummarizerOptions.Language.ENGLISH)
.build();
Summarizer summarizer = Summarization.getClient(summarizerOptions);
void prepareAndStartSummarization()
throws ExecutionException, InterruptedException {
// Check feature availability. Status will be one of the following:
// UNAVAILABLE, DOWNLOADABLE, DOWNLOADING, AVAILABLE
try {
int featureStatus = summarizer.checkFeatureStatus().get();
if (featureStatus == FeatureStatus.DOWNLOADABLE) {
// Download feature if necessary.
// If downloadFeature is not called, the first inference request
// will also trigger the feature to be downloaded if it's not
// already downloaded.
summarizer.downloadFeature(new DownloadCallback() {
@Override
public void onDownloadCompleted() {
startSummarizationRequest(articleToSummarize, summarizer);
}
@Override
public void onDownloadFailed(GenAiException e) { /* handle error */ }
@Override
public void onDownloadProgress(long totalBytesDownloaded) {}
@Override
public void onDownloadStarted(long bytesDownloaded) {}
});
} else if (featureStatus == FeatureStatus.DOWNLOADING) {
// Inference request will automatically run once feature is
// downloaded. If Gemini Nano is already downloaded on the
// device, the feature-specific LoRA adapter model will be
// downloaded quickly. However, if Gemini Nano is not already
// downloaded, the download process may take longer.
startSummarizationRequest(articleToSummarize, summarizer);
} else if (featureStatus == FeatureStatus.AVAILABLE) {
startSummarizationRequest(articleToSummarize, summarizer);
}
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}
void startSummarizationRequest(String text, Summarizer summarizer) {
// Create task request
SummarizationRequest summarizationRequest =
SummarizationRequest.builder(text).build();
// Start summarization request with streaming response
summarizer.runInference(summarizationRequest, newText -> {
// Show new text in UI
});
// You can also get a non-streaming response from the request
// ListenableFuture<SummarizationResult> summarizationResult
// = summarizer.runInference(summarizationRequest);
// String summary = summarizationResult.get().getSummary();
}
// Be sure to release the resource when no longer needed
// For example, on viewModel.onCleared() or activity.onDestroy()
summarizer.close();
วิธีที่โมเดลจัดการอินพุตประเภทต่างๆ
เมื่อระบุอินพุตข้อความเป็น InputType.CONVERSATION
โมเดลจะคาดหวังว่าอินพุตอยู่ในรูปแบบต่อไปนี้
<name>: <message>
<name2>: <message2>
<name>: <message3>
<name3>: <message4>
ซึ่งช่วยให้โมเดลสร้างสรุปที่แม่นยำยิ่งขึ้นด้วยการทำความเข้าใจการสนทนาและการโต้ตอบได้ดียิ่งขึ้น
ฟีเจอร์ที่รองรับและข้อจํากัด
อินพุตต้องมีโทเค็นไม่เกิน 4,000 รายการ (หรือประมาณ 3,000 คำภาษาอังกฤษ) หากอินพุตมีโทเค็นเกิน 4, 000 รายการ ให้พิจารณาใช้ตัวเลือกต่อไปนี้
- ให้ความสําคัญกับการสรุปโทเค็น 4,000 รายการแรก การทดสอบแสดงให้เห็นว่าวิธีนี้มักจะให้ผลลัพธ์ที่ดีสำหรับอินพุตที่ยาวขึ้น ลองเปิดการตัดอัตโนมัติโดยเรียกใช้
setLongInputAutoTruncationEnabled
เพื่อให้ระบบตัดอินพุตส่วนเกินโดยอัตโนมัติ - แบ่งอินพุตออกเป็นกลุ่มๆ ละ 4, 000 โทเค็น แล้วสรุปทีละกลุ่ม
- ลองใช้โซลูชันระบบคลาวด์ที่เหมาะกับอินพุตขนาดใหญ่มากกว่า
สําหรับ InputType.ARTICLE
อินพุตต้องมากกว่า 400 อักขระด้วย โดยโมเดลจะทํางานได้ดีที่สุดเมื่อบทความมีความยาวอย่างน้อย 300 คำ
GenAI Summarization API รองรับภาษาอังกฤษ ญี่ปุ่น และเกาหลี และกำหนดไว้ใน SummarizerOptions.Language
ความพร้อมใช้งานของการกําหนดค่าฟีเจอร์ที่เฉพาะเจาะจง (ที่ระบุโดย SummarizerOptions
) อาจแตกต่างกันไปตามการกําหนดค่าของอุปกรณ์หนึ่งๆ และรุ่นที่ดาวน์โหลดลงในอุปกรณ์
วิธีที่เชื่อถือได้มากที่สุดสำหรับนักพัฒนาแอปในการยืนยันว่าฟีเจอร์ API ที่ต้องการรองรับในอุปกรณ์ที่มี SummarizerOptions
ที่ขอคือเรียกใช้เมธอด checkFeatureStatus()
วิธีนี้จะแสดงสถานะความพร้อมใช้งานของฟีเจอร์ในอุปกรณ์อย่างแน่ชัดขณะรันไทม์
ปัญหาการตั้งค่าที่พบได้ทั่วไป
ไม่รองรับ API นี้ML Kit GenAI API ต้องใช้แอป Android AICore เพื่อเข้าถึง Gemini Nano เมื่อเพิ่งตั้งค่าอุปกรณ์ (รวมถึงรีเซ็ต) หรือเพิ่งรีเซ็ตแอป AICore (เช่น ล้างข้อมูล ถอนการติดตั้งแล้วติดตั้งอีกครั้ง) แอป AICore อาจไม่มีเวลาเพียงพอในการเริ่มต้น (รวมถึงการดาวน์โหลดการกำหนดค่าล่าสุดจากเซิร์ฟเวอร์) ด้วยเหตุนี้ ML Kit GenAI API จึงอาจไม่ทำงานตามที่คาดไว้ ข้อความแสดงข้อผิดพลาดที่พบบ่อยในการตั้งค่าที่คุณอาจเห็นและวิธีจัดการมีดังนี้
ตัวอย่างข้อความแสดงข้อผิดพลาด | วิธีจัดการ |
AICore ไม่สําเร็จด้วยข้อผิดพลาดประเภท 4-CONNECTION_ERROR และรหัสข้อผิดพลาด 601-BINDING_FAILURE: บริการ AICore เชื่อมโยงไม่สําเร็จ | กรณีนี้อาจเกิดขึ้นเมื่อคุณติดตั้งแอปโดยใช้ ML Kit GenAI API ทันทีหลังจากตั้งค่าอุปกรณ์ หรือเมื่อถอนการติดตั้ง AICore หลังจากติดตั้งแอปแล้ว การอัปเดตแอป AICore แล้วติดตั้งแอปอีกครั้งน่าจะช่วยแก้ปัญหาได้ |
AICore ไม่สําเร็จเนื่องจากข้อผิดพลาดประเภท 3-PREPARATION_ERROR และรหัสข้อผิดพลาด 606-FEATURE_NOT_FOUND: ฟีเจอร์ ... ไม่พร้อมใช้งาน |
กรณีนี้อาจเกิดขึ้นเมื่อ AICore ดาวน์โหลดการกําหนดค่าล่าสุดไม่เสร็จ เชื่อมต่อเครือข่ายไว้และรอ 2-3 นาทีถึง 2-3 ชั่วโมง
โปรดทราบว่าหากอุปกรณ์ปลดล็อก bootloader ไว้ คุณจะเห็นข้อผิดพลาดนี้ด้วย เนื่องจาก API นี้ไม่รองรับอุปกรณ์ที่ปลดล็อก bootloader ไว้ |
AICore ไม่สําเร็จด้วยประเภทข้อผิดพลาด 1-DOWNLOAD_ERROR และรหัสข้อผิดพลาด 0-UNKNOWN: ฟีเจอร์ ... ไม่สําเร็จด้วยสถานะการไม่สําเร็จ 0 และข้อผิดพลาด esz: UNAVAILABLE: ไม่สามารถแก้ไขโฮสต์ ... | เชื่อมต่อเครือข่ายไว้ รอ 2-3 นาที แล้วลองอีกครั้ง |