简介

本指南将引导您完成使用 Cohere 嵌入设置 Meilisearch 的过程,以启用语义搜索功能。通过利用 Meilisearch 的 AI 功能和 Cohere 的嵌入 API,您可以增强搜索体验并检索到更相关的结果。

要求

要遵循本指南,您需要

  • 一个运行版本 >=1.13 的 Meilisearch Cloud 项目
  • 一个拥有用于嵌入生成 API 密钥的 Cohere 账户。您可以在 Cohere 注册 Cohere 账户。
  • 无需后端。

设置 Meilisearch

要在 Meilisearch 中设置嵌入器,您需要将其配置到您的设置中。您可以参考 Meilisearch 文档,了解有关更新嵌入器设置的更多详细信息。

Cohere 提供多种嵌入模型

  • embed-english-v3.0embed-multilingual-v3.0:1024 维
  • embed-english-light-v3.0embed-multilingual-light-v3.0:384 维

以下是 Cohere 嵌入器设置示例

{
  "cohere": {
    "source": "rest",
    "apiKey": "<Cohere API Key>",
    "dimensions": 1024,
    "documentTemplate": "<Custom template (Optional, but recommended)>",
    "url": "https://api.cohere.com/v1/embed",
    "request": {
      "model": "embed-english-v3.0",
      "texts": [
        "{{text}}",
        "{{..}}"
      ],
      "input_type": "search_document"
    },
    "response": {
      "embeddings": [
        "{{embedding}}",
        "{{..}}"
      ]
    },
  }
}

在此配置中

  • source:指定嵌入器的来源,设置为“rest”表示使用 REST API。
  • apiKey:将 <Cohere API Key> 替换为您的实际 Cohere API 密钥。
  • dimensions:指定嵌入的维度,对于 embed-english-v3.0 模型,设置为 1024。
  • documentTemplate:可选地,您可以提供一个自定义模板,用于从您的文档生成嵌入。
  • url:指定 Cohere API 端点的 URL。
  • request:定义 Cohere API 的请求结构,包括模型名称和输入参数。
  • response:定义从 Cohere API 预期的响应结构,包括嵌入数据。

配置好嵌入器设置后,Meilisearch 将自动为您的文档生成嵌入并将其存储在向量存储中。

请注意,大多数第三方工具都有速率限制,这由 Meilisearch 管理。如果您是免费账户,索引过程可能需要一些时间,但 Meilisearch 将通过重试策略处理它。

建议监控任务队列,以确保一切运行顺畅。您可以使用 Cloud UI 或 Meilisearch API 访问任务队列。

设置好嵌入器后,您现在可以使用 Meilisearch 执行语义搜索。当您发送搜索查询时,Meilisearch 将使用配置的嵌入器为查询生成嵌入,然后使用它在向量存储中查找语义最相似的文档。要执行语义搜索,您只需发出一个普通搜索请求,但需要包含 hybrid 参数

{
  "q": "<Query made by the user>",
  "hybrid": {
    "semanticRatio": 1,
    "embedder": "cohere"
  }
}

在此请求中

  • q:表示用户的搜索查询。
  • hybrid:指定混合搜索的配置。
    • semanticRatio:允许您控制语义搜索和传统搜索之间的平衡。值为 1 表示纯语义搜索,而值为 0 表示全文搜索。您可以调整此参数以实现混合搜索体验。
    • embedder:用于生成嵌入的嵌入器名称。请确保使用与嵌入器配置中指定的相同名称,在此示例中为“cohere”。

您可以使用 Meilisearch API 或客户端库执行搜索,并根据语义相似性检索相关文档。

结论

通过遵循本指南,您现在应该已经使用 Cohere 嵌入设置了 Meilisearch,使您能够在应用程序中利用语义搜索功能。Meilisearch 的自动批处理和高效的嵌入处理使其成为将语义搜索集成到您的项目中的强大选择。

要探索嵌入器的更多配置选项,请查阅关于嵌入器设置可能性的详细文档