利用 AI 评估商品评价

Kenji Baheux
Kenji Baheux
Alexandra Klepper
Alexandra Klepper

发布时间:2024 年 5 月 16 日

在网上购物时,看到大量的商品评价和可供选择的商品,可能会让人不知所措。我们如何从这些杂乱的信息中过滤出真正满足我们特定需求的产品?

例如,假设我们正在购买一款工作背包。背包需要兼顾功能、美观和实用性。评价数量众多,让人几乎无法确定是否找到了完美的背包。如果我们能使用 AI 过滤出杂乱的信息,找到完美的商品呢?

如果能提供所有评价的摘要,以及最常见的优缺点列表,那就太有帮助了。

包含星级评分和优缺点列表的用户评价示例。

为了实现这一点,我们使用了服务器端生成式 AI。推理在服务器上进行。

在本文档中,您可以按照 Gemini API 的 Node.js教程, 使用 Google AI JavaScript SDK 汇总来自许多评价的数据。我们重点介绍这项工作的生成式 AI 部分;不会介绍如何存储结果或创建作业队列。

实际上,您可以将任何 LLM API 与任何 SDK 搭配使用。不过,您可能需要调整建议的提示,以适应您选择的模型。

前提条件

  1. 为 Gemini API 创建密钥, 并在环境文件中定义该密钥。

  2. 安装 Google AI JavaScript SDK,例如使用 npm: npm install @google/generative-ai

构建评价摘要应用

  1. 初始化生成式 AI 对象
  2. 创建一个用于生成评价摘要的函数。
    1. 选择生成式 AI 模型。对于我们的应用场景,我们将使用 Gemini Pro。使用特定于您的应用场景的模型(例如,gemini-pro-vision 用于多模态输入)。
    2. 添加提示。
    3. 调用 generateContent 以将提示作为实参传递。
    4. 生成并返回回答。
const { GoogleGenerativeAI } = require("@google/generative-ai");

// Access the API key env
const genAI = new GoogleGenerativeAI(process.env.API_KEY_GEMINI);

async function generateReviewSummary(reviews) {
  // Use gemini-pro model for text-only input
  const model = genAI.getGenerativeModel({ model: "gemini-pro" });
  // Shortened for legibility. See "Write an effective prompt" for
  // writing an actual production-ready prompt.
  const prompt = `Summarize the following product reviews:\n\n${reviews}`;
  const result = await model.generateContent(prompt);
  const response = await result.response;
  const summary = response.text();
  return summary;
}

撰写有效的提示

要成功使用生成式 AI,最好的方法是创建详尽的提示。 在本示例中,我们使用了 单样本提示 技术来获得一致的输出。

单样本提示由 Gemini 的示例输出表示。

const prompt =
`I will give you user reviews for a product. Generate a short summary of the
reviews, with focus on the common positive and negative aspects across all of
the reviews. Use the exact same output format as in the example (list of
positive highlights, list of negative aspects, summary). In the summary,
address the potential buyer with second person ("you", "be aware").

Input (list of reviews):
// ... example

Output (summary of reviews):
// ... example

**Positive highlights**
// ... example
**Negative aspects**
// ... example
**Summary**
// ... example

Input (list of reviews):
${reviews}

Output (summary of all input reviews):`;

以下是此提示的示例输出,其中包含所有评价的摘要,以及常见优缺点列表。

## Summary of Reviews:

**Positive highlights:**

* **Style:** Several reviewers appreciate the backpack's color and design.
* **Organization:** Some users love the compartments and find them useful for
  organization.
* **Travel & School:** The backpack seems suitable for both travel and school
  use, being lightweight and able to hold necessary items.

**Negative aspects:**

* **Durability:** Concerns regarding the zipper breaking and water bottle holder
  ripping raise questions about the backpack's overall durability.
* **Size:** A few reviewers found the backpack smaller than expected.
* **Material:** One user felt the material was cheap and expressed concern about
  its longevity.

**Summary:**

This backpack seems to be stylish and appreciated for its organization and
suitability for travel and school. However, you should be aware of potential
durability issues with the zippers and water bottle holder. Some users also
found the backpack smaller than anticipated and expressed concerns about the
material's quality.

token 数量上限

许多评价可能会达到模型的 token 数量上限。token 并不总是等于单个字词;token 可以是字词的一部分,也可以是多个字词的组合。例如,Gemini Pro 的 token 数量上限为 30,720。这意味着,提示最多可以包含 600 条平均 30 个字词的英文评价,减去提示说明的其余部分。

使用 countTokens() 检查 token 数量,如果提示超出 允许的范围,则减少输入。

const MAX_INPUT_TOKENS = 30720
const { totalTokens } = await model.countTokens(prompt);
if (totalTokens > MAX_INPUT_TOKENS) {
    // Shorten the prompt.
}

为企业打造产品

如果您是 Google Cloud 用户,或者需要企业支持,则可以使用 Vertex AI访问 Gemini Pro 和更多模型,例如 Anthropic 的 Claude 模型。您可能需要使用 Model Garden来确定哪个模型 最符合您的特定应用场景。

后续步骤

我们构建的应用在很大程度上依赖于优质评价来提供最有效的摘要。如需收集这些优质评价,请阅读本系列中的下一篇文章: 使用设备端 Web AI 帮助用户撰写有用的商品评价

我们希望听到您对此方法的反馈。请告诉我们您最感兴趣的应用场景。您可以 分享反馈并加入 Early Preview 试用计划 ,以便使用本地原型测试此技术。

您的贡献可以帮助我们将 AI 打造成为一种强大而实用的工具,让每个人都能受益。

下一篇:帮助用户撰写有用的商品评价