GenAI Summarization API ของ ML Kit ช่วยให้คุณสร้างข้อมูลสรุปของบทความและการสนทนาเป็นรายการหัวข้อย่อยได้โดยอัตโนมัติ ซึ่งจะช่วยให้ผู้ใช้ เข้าใจข้อความจำนวนมาก
การสรุปจะได้รับประโยชน์จาก Generative AI ในอุปกรณ์เนื่องจากช่วยแก้ปัญหา ความกังวลเกี่ยวกับความเป็นส่วนตัวของข้อมูลและประสิทธิภาพด้านต้นทุน แอปที่สรุปแชทส่วนตัว อีเมล โน้ต และการช่วยเตือนมักจะจัดการข้อมูลที่ละเอียดอ่อน ซึ่งทำให้การประมวลผลในอุปกรณ์มีความสำคัญต่อความเป็นส่วนตัวของผู้ใช้ นอกจากนี้ งานสรุป โดยเฉพาะงานที่มีบริบทยาวหรือมีรายการจำนวนมากอาจต้องใช้ กำลังประมวลผลอย่างมาก การประมวลผลเนื้อหานี้ในอุปกรณ์จะช่วยลดภาระของเซิร์ฟเวอร์และลดต้นทุนการแสดงโฆษณา ขณะเดียวกันก็ยังคงรักษาข้อมูลผู้ใช้ให้เป็นส่วนตัว
ความสามารถหลัก
API การสรุปด้วย GenAI ครอบคลุมความสามารถต่อไปนี้
- สรุปข้อความที่จัดหมวดหมู่เป็นบทความหรือการสนทนา
- สรุปเอาต์พุตเป็นหัวข้อย่อย 1, 2 หรือ 3 รายการ
เริ่มต้นใช้งาน
เพิ่ม ML Kit Summarization API เป็นการขึ้นต่อกันในการกำหนดค่า 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 คำ
API การสรุปด้วย GenAI รองรับภาษาอังกฤษ ญี่ปุ่น และเกาหลี และมีการกำหนดไว้ใน SummarizerOptions.Language
ความพร้อมใช้งานของการกำหนดค่าฟีเจอร์ที่เฉพาะเจาะจง (ระบุโดย
SummarizerOptions
) อาจแตกต่างกันไปตามการกำหนดค่าของอุปกรณ์ที่เฉพาะเจาะจง
และโมเดลที่ดาวน์โหลดลงในอุปกรณ์
วิธีที่เชื่อถือได้มากที่สุดสำหรับนักพัฒนาแอปในการตรวจสอบว่าฟีเจอร์ API ที่ต้องการ
รองรับในอุปกรณ์ที่มี SummarizerOptions
ที่ขอคือการเรียกใช้เมธอด
checkFeatureStatus()
วิธีนี้จะแสดงสถานะที่แน่นอน
ของความพร้อมใช้งานฟีเจอร์ในอุปกรณ์ขณะรันไทม์
ปัญหาที่พบบ่อยเกี่ยวกับการตั้งค่า
API ของ GenAI ใน ML Kit จะใช้แอป 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: แก้ไขโฮสต์ ... ไม่ได้ | เชื่อมต่อเครือข่ายไว้ รอสักครู่ แล้วลองอีกครั้ง |