借助 ML Kit 的 GenAI Rewriting API,您可以自动帮助用户以不同的风格或语气改写聊天消息或短篇内容。
在以下情况下,向用户提供有关如何重写内容片段的建议可能会很有帮助:
- 在与利益相关者沟通时,重新组织消息以使其更专业
- 缩短消息,使其更适合发布到社交媒体平台
- 为非母语人士重新措辞,以便他们寻找其他方式来传达信息
主要功能
ML Kit 的 GenAI 重写 API 可以使用以下样式之一重写简短的内容:
- 详细说明:使用更多细节和描述性语言扩展输入文本。
- Emojify:为输入文本添加相关表情符号,使其更具表现力和趣味性。
- 精简:将输入文本压缩为更短的版本,同时保持核心信息不变。
- 友好:使用对话式语气重写输入文本,使其更随意、更平易近人。
- 专业:重写输入文本,使其更正式、更商务化,并使用尊敬的语气。
- 改写:使用不同的字词和句子结构重新撰写输入文本,同时保持原意不变。
请求将返回至少一个建议。如果返回多个建议,结果将按置信度降序排序。
示例结果
输入 | 改写样式 | 输出 |
您是否想安排一次会议,以便我们进一步详谈? | 专业 | 您是否有兴趣再次会面,进一步讨论此事? |
本人诚挚邀请您于本周六晚间莅临寒舍,参加一场轻松的聚会 | 精简 | 您想在周六晚上来我家参加一个休闲聚会吗? |
活动已成功完成 | 详细阐述 | 此次活动取得了巨大成功,超出了我们的所有预期,堪称一次辉煌的胜利。 |
我们找个时间一起喝杯咖啡吧 | 添加表情符号 | 我们找个时间一起喝咖啡吧☕👋。 |
在当天结束前提供报告 | 友谊赛 | 您能否在今天结束前分享该报告? |
嘿,需要尽快拿到那件东西 | 专业 | 能否请您尽快提供相应文件? |
项目进度滞后 | 改写 | 项目时间表需要调整,以满足原始截止日期 |
使用入门
如需开始使用 GenAI Rewriting API,请将此依赖项添加到项目的 build 文件中。
implementation("com.google.mlkit:genai-rewriting:1.0.0-beta1")
然后,使用所需选项实例化 Rewriter
客户端,检查所需的设备端模型功能是否可用(并在必要时下载它们),将输入文本准备为请求,运行重写流程以获取建议,并释放资源。
Kotlin
val textToRewrite = "The event was successful"
// Define task with selected input and output format
val rewriterOptions = RewriterOptions.builder(context)
// OutputType can be one of the following: ELABORATE, EMOJIFY, SHORTEN,
// FRIENDLY, PROFESSIONAL, REPHRASE
.setOutputType(RewriterOptions.OutputType.ELABORATE)
// Refer to RewriterOptions.Language for available languages
.setLanguage(RewriterOptions.Language.ENGLISH)
.build()
val rewriter = Rewriting.getClient(rewriterOptions)
suspend fun prepareAndStartRewrite() {
// Check feature availability, status will be one of the following:
// UNAVAILABLE, DOWNLOADABLE, DOWNLOADING, AVAILABLE
val featureStatus = rewriter.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.
rewriter.downloadFeature(object : DownloadCallback {
override fun onDownloadStarted(bytesToDownload: Long) { }
override fun onDownloadFailed(e: GenAiException) { }
override fun onDownloadProgress(totalBytesDownloaded: Long) {}
override fun onDownloadCompleted() {
startRewritingRequest(textToRewrite, rewriter)
}
})
} 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.
startRewritingRequest(textToRewrite, rewriter)
} else if (featureStatus == FeatureStatus.AVAILABLE) {
startRewritingRequest(textToRewrite, rewriter)
}
}
suspend fun startRewritingRequest(text: String, rewriter: Rewriter) {
// Create task request
val rewritingRequest = RewritingRequest.builder(text).build()
// Start rewriting request with non-streaming response
// More than 1 result may be returned. If multiple suggestions are
// returned, results will be sorted by descending confidence.
val rewriteResults =
rewriter.runInference(rewritingRequest).await().results
// You can also start a streaming request
// rewriter.runInference(rewritingRequest) { newText ->
// // Show new text in UI
// }
}
// Be sure to release the resource when no longer needed
// For example, on viewModel.onCleared() or activity.onDestroy()
rewriter.close()
Java
String textToRewrite = "The event was successful";
// Define task with required input and output format
RewriterOptions rewriterOptions =
RewriterOptions.builder(context)
// OutputType can be one of the following: ELABORATE,
// EMOJIFY, SHORTEN, FRIENDLY, PROFESSIONAL, REPHRASE
.setOutputType(RewriterOptions.OutputType.ELABORATE)
// Refer to RewriterOptions.Language for available
// languages
.setLanguage(RewriterOptions.Language.ENGLISH)
.build();
Rewriter rewriter = Rewriting.getClient(rewriterOptions);
void prepareAndStartRewrite()
throws ExecutionException, InterruptedException {
// Check feature availability, status will be one of the
// following: UNAVAILABLE, DOWNLOADABLE, DOWNLOADING, AVAILABLE
try {
int featureStatus = rewriter.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.
rewriter.downloadFeature(
new DownloadCallback() {
@Override
public void onDownloadCompleted() {
startRewritingRequest(textToRewrite, rewriter);
}
@Override
public void onDownloadFailed(GenAIException e) {}
@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.
startRewritingRequest(textToRewrite, rewriter);
} else if (featureStatus == FeatureStatus.AVAILABLE) {
startRewritingRequest(textToRewrite, rewriter);
}
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}
void startRewritingRequest(String text, Rewriter rewriter) {
// Create task request
RewritingRequest rewritingRequest =
RewritingRequest.builder(text).build();
try {
// Start rewriting request with non-streaming response
// More than 1 result may be returned. If multiple
// suggestions are returned, results will be sorted by
// descending confidence.
rewriter.runInference(rewritingRequest).get().getResults();
// You can also start a streaming request
// rewriter.runInference(rewritingRequest, newText -> {
// // Show new text in UI
// });
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}
// Be sure to release the resource when no longer needed
// For example, on viewModel.onCleared() or activity.onDestroy()
rewriter.close();
支持的功能和限制
生成式 AI 重写 API 支持以下语言:英语、日语、法语、德语、意大利语、西班牙语和韩语,这些语言在 RewriterOptions.Language
中定义。输入应少于 256 个 token。
特定功能配置(由 RewriterOptions
指定)的可用性可能因特定设备的配置和已下载到设备的模型而异。
对于开发者而言,确保设备支持具有所请求 RewriterOptions
的预期 API 功能的最可靠方法是调用 checkFeatureStatus()
方法。此方法可在运行时提供设备上功能可用性的确定状态。
常见设置问题
机器学习套件生成式 AI 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 尚未完成下载最新配置,可能会发生这种情况。当设备连接到互联网时,更新通常需要几分钟到几小时。重新启动设备可以加快更新速度。
请注意,如果设备的引导加载程序处于解锁状态,您也会看到此错误消息,因为此 API 不支持引导加载程序处于解锁状态的设备。 |
AICore 失败,错误类型为 1-DOWNLOAD_ERROR,错误代码为 0-UNKNOWN:功能 ... 失败,失败状态为 0,错误 esz 为:UNAVAILABLE:无法解析主机 ... | 保持网络连接,等待几分钟,然后重试。 |