简介

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

要求

要遵循本指南,您需要
  • 一个运行版本 >=1.13 的 Meilisearch Cloud 项目
  • 一个带有用于 embedding 生成的 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 将使用配置的嵌入器为查询生成嵌入,然后使用它在向量存储中查找语义最相似的文档。要执行语义搜索,您只需发出正常的搜索请求,但要包含混合参数
{
  "q": "<Query made by the user>",
  "hybrid": {
    "semanticRatio": 1,
    "embedder": "cohere"
  }
}
在此请求中
  • q:表示用户的搜索查询。
  • hybrid:指定混合搜索的配置。
    • semanticRatio:允许您控制语义搜索和传统搜索之间的平衡。值为 1 表示纯语义搜索,而值为 0 表示全文搜索。您可以调整此参数以实现混合搜索体验。
    • embedder:用于生成嵌入的嵌入器名称。确保使用与嵌入器配置中指定的名称相同的名称,在本例中为“cohere”。
您可以使用 Meilisearch API 或客户端库执行搜索并根据语义相似性检索相关文档。

结论

通过遵循本指南,您现在应该已经设置了带有 Cohere 嵌入的 Meilisearch,使您能够在应用程序中利用语义搜索功能。Meilisearch 的自动批处理和高效处理嵌入使其成为将语义搜索集成到您的项目中的强大选择。 要探索嵌入器的进一步配置选项,请查阅 关于嵌入器设置可能性的详细文档
© . This site is unofficial and not affiliated with Meilisearch.