利用 AI 评估商品评价

Maud Nalpas
Maud Nalpas
Kenji Baheux
Kenji Baheux
Alexandra Klepper
Alexandra Klepper

在线购物时,查看商品评价量和可用商品数量可能会令人不知所措。如何才能消除所有这些干扰,找到真正满足我们特定需求的产品?

例如,假设我们要选购一款工作背包。背包需要兼顾功能、美感和实用性。如此庞大的评价数量,让您几乎不可能知道自己是否找到了完美的包袋。如果我们能使用 AI 过滤杂乱无章并找到完美的产品,结果会怎样?

最好提供所有评价的摘要,以及最常见的优缺点列表。

包含正面和负面亮点的用户评价示例。
包含星级以及优缺点列表的用户评价示例。

为此,我们使用了服务器端的生成式 AI。推断是在服务器上进行的。

在本文档中,您可以跟上将 Genmini 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.

词元限制

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

请使用 countTokens() 检查令牌的数量,并在提示超出允许值时减少输入。

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 帮助用户撰写有用的商品评价

我们想听听您对这种方法的看法。告诉我们您最感兴趣的应用场景。您可以分享反馈并加入早期预览版计划,通过本地原型测试此技术。

您的贡献可以帮助我们将 AI 打造成一个强大而实用的工具,惠及每个人。

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