发布时间:2024 年 5 月 16 日
正面和负面评价可以为买家的购买决策提供参考。
外部研究表明,82% 的在线购物者在购买前会主动寻找负面评价。这些负面评价对客户和商家都有用,因为负面评价的存在有助于降低退货率,并帮助制造商改进产品。
以下是一些可以提高评价质量的方法:
- 在提交评价之前,检查每条评价是否包含有害内容。我们可以鼓励用户删除冒犯性语言和其他无用的评论,以便他们的评价能够更好地帮助其他用户做出更好的购买决策。
- 负面评价:这个包太烂了,我讨厌它。
- 包含实用反馈的负面评价 :拉链很硬,材质感觉很廉价。我退了这个包。
- 根据评价中使用的语言自动生成评分。
- 确定评价是负面还是正面。
最终,用户应有权对产品评分做出最终决定。
以下 Codelab 提供了客户端解决方案,包括设备端和浏览器端。无需 AI 开发知识、服务器或 API 密钥。
前提条件
虽然具有解决方案(例如 Gemini API 或 OpenAI API)的服务器端 AI 为 许多应用提供了强大的解决方案,但本指南重点介绍客户端 Web AI。客户端 AI 推理发生在浏览器中,通过消除服务器往返来改善 Web 用户的体验。
在此 Codelab 中,我们结合使用多种技术向您展示客户端 AI 工具箱中的内容。
我们使用以下库和模型:
- 用于有害内容分析的 TensforFlow.js TensorFlow.js 是一个开源机器学习库,用于在 Web 上进行推理和训练。
- 用于情感 分析的 transformers.js。Transformers.js 是 Hugging Face 的 Web AI 库。
- Gemma 2B 用于星级评分。Gemma 是一系列轻量级开放模型,基于 Google 用于创建 Gemini 模型的研究和技术构建而成。如需在浏览器中运行 Gemma,我们 将其与 MediaPipe's 实验性 LLM Inference API 搭配使用。
用户体验和安全注意事项
为确保最佳用户体验和安全性,请注意以下几点:
- 允许用户修改评分。最终,用户应有权对产品评分做出最终决定。
- 向用户明确说明评分和评价是自动生成的。
- 允许用户发布被归类为有害内容的评价,但在服务器上运行第二次检查。这样可以避免将无害评价错误地归类为有害内容(假正例)而导致令人沮丧的体验。这也涵盖了恶意用户设法绕过客户端检查的情况。
- 客户端有害内容检查很有用,但可以绕过。请确保您也在服务器端运行检查。
使用 TensorFlow.js 分析有害内容
使用 TensorFlow.js 快速开始分析用户评价的有害内容。
- 安装 并导入 TensorFlow.js 库和有害内容模型。
- 设置最低预测置信度。默认值为 0.85,在我们的示例中,我们将其设置为 0.9。
- 异步加载模型。
- 异步对评价进行分类。我们的代码会识别任何类别中超过 0.9 阈值的预测。
此模型可以对身份攻击、侮辱、淫秽等有害内容进行分类。
例如:
import * as toxicity from '@tensorflow-models/toxicity';
// Minimum prediction confidence allowed
const TOXICITY_COMMENT_THRESHOLD = 0.9;
const toxicityModel = await toxicity.load(TOXICITY_COMMENT_THRESHOLD);
const toxicityPredictions = await toxicityModel.classify([review]);
// `predictions` is an array with the raw toxicity probabilities
const isToxic = toxicityPredictions.some(
(prediction) => prediction.results[0].match
);
使用 Transformers.js 确定情绪
安装 并导入 Transformers.js 库。
异步对评价进行分类。使用自定义阈值设置您认为可用于应用的置信度水平。
例如:
import { pipeline } from '@xenova/transformers';
const SENTIMENT_THRESHOLD = 0.9;
// Create a pipeline (don't block rendering on this function)
const transformersjsClassifierSentiment = await pipeline(
'sentiment-analysis'
);
// When the user finishes typing
const sentimentResult = await transformersjsClassifierSentiment(review);
const { label, score } = sentimentResult[0];
if (score > SENTIMENT_THRESHOLD) {
// The sentiment is `label`
} else {
// Classification is not conclusive
}
使用 Gemma 和 MediaPipe 建议星级评分
借助 LLM Inference API,您可以在浏览器中完全运行大语言模型 (LLM)。
考虑到 LLM 的内存和计算需求,这项新功能尤其具有变革性,因为 LLM 的内存和计算需求比客户端模型大一百倍以上。Web 堆栈中的优化(包括新运算、量化、缓存和权重共享)使这一切成为可能。 来源:“使用 MediaPipe 和 TensorFlow Lite 的设备端大语言模型”。
- 安装并导入 MediaPipe LLM Inference API。
- 下载模型。 在这里,我们使用 Gemma 2B, 从 Kaggle 下载。 Gemma 2B 是 Google 开放权重模型中最小的一个。
- 使用
FilesetResolver将代码指向正确的模型文件。这一点很重要,因为生成式 AI 模型可能具有特定的资源目录结构。 - 使用 MediaPipe 的 LLM 接口加载和配置模型。准备好模型以供使用:指定其模型位置、首选响应长度以及使用温度的首选创意水平。
- 为模型提供提示(请参阅示例)。
- 等待模型的响应。
- 解析评分:从模型的响应中提取星级评分。
import { FilesetResolver, LlmInference } from '@mediapipe/tasks-genai';
const mediaPipeGenAi = await FilesetResolver.forGenAiTasks();
const llmInference = await LlmInference.createFromOptions(mediaPipeGenAi, {
baseOptions: {
modelAssetPath: '/gemma-2b-it-gpu-int4.bin',
},
maxTokens: 1000,
topK: 40,
temperature: 0.5,
randomSeed: 101,
});
const prompt = …
const output = await llmInference.generateResponse(prompt);
const int = /\d/;
const ratingAsString = output.match(int)[0];
rating = parseInt(ratingAsString);
提示示例
const prompt = `Analyze a product review, and then based on your analysis give me the
corresponding rating (integer). The rating should be an integer between 1 and 5.
1 is the worst rating, and 5 is the best rating. A strongly dissatisfied review
that only mentions issues should have a rating of 1 (worst). A strongly
satisfied review that only mentions positives and upsides should have a rating
of 5 (best). Be opinionated. Use the full range of possible ratings (1 to 5). \n\n
\n\n
Here are some examples of reviews and their corresponding analyses and ratings:
\n\n
Review: 'Stylish and functional. Not sure how it'll handle rugged outdoor use,
but it's perfect for urban exploring.'
Analysis: The reviewer appreciates the product's style and basic
functionality. They express some uncertainty about its ruggedness but overall
find it suitable for their intended use, resulting in a positive, but not
top-tier rating.
Rating (integer): 4
\n\n
Review: 'It's a solid backpack at a decent price. Does the job, but nothing
particularly amazing about it.'
Analysis: This reflects an average opinion. The backpack is functional and
fulfills its essential purpose. However, the reviewer finds it unremarkable
and lacking any standout features deserving of higher praise.
Rating (integer): 3
\n\n
Review: 'The waist belt broke on my first trip! Customer service was
unresponsive too. Would not recommend.'
Analysis: A serious product defect and poor customer service experience
naturally warrants the lowest possible rating. The reviewer is extremely
unsatisfied with both the product and the company.
Rating (integer): 1
\n\n
Review: 'Love how many pockets and compartments it has. Keeps everything
organized on long trips. Durable too!'
Analysis: The enthusiastic review highlights specific features the user loves
(organization and durability), indicating great satisfaction with the product.
This justifies the highest rating.
Rating (integer): 5
\n\n
Review: 'The straps are a bit flimsy, and they started digging into my
shoulders under heavy loads.'
Analysis: While not a totally negative review, a significant comfort issue
leads the reviewer to rate the product poorly. The straps are a key component
of a backpack, and their failure to perform well under load is a major flaw.
Rating (integer): 1
\n\n
Now, here is the review you need to assess:
\n
Review: "${review}" \n`;
要点总结
无需 AI/机器学习专业知识 。设计提示需要迭代,但其余代码是标准的 Web 开发。
客户端模型相当准确 。如果您运行本文档中的代码段,您会发现有害内容分析和情感分析都会给出准确的结果。对于一些经过测试的参考评价,Gemma 评分在很大程度上与 Gemini 模型评分相符。为了验证准确性,需要进行更多测试。
也就是说,为 Gemma 2B 设计提示需要付出努力。由于 Gemma 2B 是一个小型 LLM,因此需要详细的提示才能产生令人满意的结果,尤其是比 Gemini API 所需的提示详细得多。
推理速度可能非常快 。如果您运行本文档中的代码段,您应该会发现,在许多设备上,推理速度可能会很快,甚至可能比服务器往返更快。也就是说,推理速度可能会有很大差异。需要在目标设备上进行全面的基准评测。我们预计,随着 WebGPU、WebAssembly 和库更新的不断发展,浏览器推理速度会越来越快。 例如,Transformers.js 在 v3 中添加了 Web GPU 支持, 这可以将设备端推理速度提高许多倍。
下载大小可能非常大 。在浏览器中进行推理的速度很快,但加载 AI 模型可能是一个挑战。如需在浏览器中执行 AI,您通常需要一个库和一个模型,这会增加 Web 应用的下载大小。
虽然 Tensorflow 有害内容模型(一种经典自然语言处理模型)只有几 KB,但 Transformers.js 的默认情感分析模型等生成式 AI 模型达到了 60MB。Gemma 等大语言模型的大小可达 1.3GB。这远远超过了 2.2MB 的网页大小中位数 ,而 2.2MB 已经远远大于建议的最佳性能大小。客户端生成式 AI 在特定场景中是可行的。
Web 上的生成式 AI 领域正在迅速发展!预计未来会出现更小、 针对 Web 优化的模型。
后续步骤
Chrome 正在尝试另一种在浏览器中运行生成式 AI 的方法。 您可以注册参加抢先体验计划 来测试它。