運用 AI 評估產品評論

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

線上購物時,可能會看到產品評論數量和供應的產品量讓您不知所措。如何精確篩選所有雜訊,找出確實能滿足 特定需求的產品?

例如,假設要購買工作背包。背包需在功能、美學和實用性之間取得平衡。評論數量幾乎無從得知是否找到了完美的包包。如果我們可以用 AI 消弭雜訊 並找出最合適的產品呢?

我們希望所有評論的摘要資訊,附有最常見的優缺點清單。

帶有正面和負面評價的使用者評論範例。
含有星級評等和優缺點清單的使用者評論範例。

為此,我們採用伺服器端的生成式 AI。系統會在伺服器上進行推論。

在本文件中,您可以按照使用 Node.js 產生 Gemini API 的教學課程,使用 Google AI JavaScript SDK 統整眾多評論的資料。這項工作的重點是生成式 AI 部分,不會探討如何儲存結果或建立工作佇列。

實務上,您可以使用任何 SDK 搭配任何 LLM API。但您可能需要調整建議提示,才能符合所選模型。

必要條件

  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 締造佳績的最佳做法,是建立詳盡的提示。在這個範例中,我們使用的是 one-shot 提示 技術來取得一致的輸出內容。

單一樣本提示是以 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 則以英文撰寫的平均評論,數量幾乎可達 600 則。

請使用 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 判斷哪個模型最符合您的用途。

後續步驟

我們建構的應用程式仰賴品質審查,產生最有效率的摘要。為了收集這些品質評論,請參閱本系列中的下一篇文章:協助使用者利用裝置端網路 AI 撰寫實用的產品評論

我們想聽聽你對這個方法的想法。請告訴我們您最感興趣的用途您可以提供意見並加入早期預先發布版計畫,使用本機原型測試這項技術。

你的貢獻可以幫助我們將 AI 打造成功能強大而實用的工具,人人受惠。

下一步:協助使用者撰寫實用的產品評論