当使用 AI 驱动的搜索时,Meilisearch 通过用每个文档的数据填充嵌入器的 documentTemplate 来生成提示。您的提示越好,搜索结果就越相关。 本指南向您展示了在编写 documentTemplate 时应做和应避免的事情。

文档示例

查看电影数据库中的此文档
{
  "id": 2,
  "title": "Ariel",
  "overview": "Taisto Kasurinen is a Finnish coal miner whose father has just committed suicide and who is framed for a crime he did not commit. In jail, he starts to dream about leaving the country and starting a new life. He escapes from prison but things don't go as planned...",
  "genres": [
    "Drama",
    "Crime",
    "Comedy"
  ],
  "poster": "https://image.tmdb.org/t/p/w500/ojDg0PGvs6R9xYFodRct2kdI6wC.jpg",
  "release_date": 593395200
}

不要使用默认的 documentTemplate

在您的嵌入器配置中使用自定义的 documentTemplate 值。 默认的 documentTemplate 包含所有具有非 null 值的可搜索字段。在大多数情况下,这会增加噪音和比嵌入器提供相关搜索结果所需更多的信息。

只包含高度相关的信息

查看您的文档并确定最相关的字段。对于示例文档,一个好的 documentTemplate 可以是
"A movie called {{doc.title}} about {{doc.overview}}"
在示例文档中,posterid 包含语义重要性较小的数据,可以安全地排除。genresrelease_date 中的数据对于过滤器非常有用,但对这部特定电影的描述不多。 这就留下了两个相关的字段:titleoverview

保持提示简短

为了获得最佳结果,请将提示保持在 15 到 45 个单词之间
"A movie called {{doc.title}} about {{doc.overview | truncatewords: 20}}"
在示例文档中,仅 overview 就有 49 个单词。使用 Liquid 的 truncatetruncatewords 来缩短它。 短提示没有足够的信息让嵌入器正确理解查询上下文。长提示则提供过多信息,使嵌入器难以识别文档中真正相关的内容。

结论

在本文中,您了解了生成能带来相关 AI 驱动搜索结果的提示的主要步骤
  • 不要使用默认的 documentTemplate
  • 只包含相关数据
  • 截断长字段
© . This site is unofficial and not affiliated with Meilisearch.