跳到主要内容
版本:V1.1.0

Cohere

Cohere 提供多种 Embedding 模型,可用于文本语义检索、聚类与推荐等场景。seekdb 提供了 CohereEmbeddingFunction 封装(基于 LiteLLM),用于在写入 documents 或使用 query_texts 查询时,自动调用 Cohere 生成向量并完成相似性检索。

提示

使用 Cohere 服务需要遵循 Cohere 的收费规则,可能产生相应费用。请在继续前,访问其官网或查阅相关文档,确认并接受其收费标准。如不同意,请勿继续操作。

依赖与鉴权

CohereEmbeddingFunction 基于 LiteLLM 调用 Cohere 的 Embedding 接口。通常需要:

  • Cohere API Key(具备调用 Embedding 模型的权限)
  • Python 环境安装 pyseekdblitellm

鉴权通常通过环境变量提供(默认读取 COHERE_API_KEY);如果您使用了不同的环境变量名,可通过 api_key_env 指定。

示例:创建 Cohere Embedding Function

  • 基础用法

    from pyseekdb.utils.embedding_functions import CohereEmbeddingFunction

    ef = CohereEmbeddingFunction(model_name="embed-english-v3.0")
  • 多语言模型

    from pyseekdb.utils.embedding_functions import CohereEmbeddingFunction

    ef = CohereEmbeddingFunction(model_name="embed-multilingual-v3.0")
  • 指定 API Key 环境变量名(api_key_env)

    当您的 API Key 不在默认的 COHERE_API_KEY 环境变量中时,可以通过 api_key_env 指定变量名:

    from pyseekdb.utils.embedding_functions import CohereEmbeddingFunction

    ef = CohereEmbeddingFunction(
    model_name="embed-english-v3.0",
    api_key_env="YOUR_API_KEY_ENV",
    )
  • 指定 input_type(推荐用于检索场景)

    在检索类场景中,可通过 input_type 提示模型当前输入的用途,从而获得更贴合检索的向量表示:

    from pyseekdb.utils.embedding_functions import CohereEmbeddingFunction

    ef = CohereEmbeddingFunction(
    model_name="embed-english-v3.0",
    input_type="search_document",
    )

    # ef = CohereEmbeddingFunction(
    # model_name="embed-english-v3.0",
    # input_type="search_query",
    # )

参数说明:

  • model_name:Cohere Embedding 模型名称,例如 embed-english-v3.0embed-multilingual-v3.0
  • api_key_env:API Key 所在的环境变量名,默认是 COHERE_API_KEY
  • input_type:输入类型提示,用于优化检索效果。常见取值:
    • search_document:用于文档入库(documents)
    • search_query:用于查询(query texts)

相关文档

  • 关于 Cohere 可用模型与参数,请参考官方文档
  • 关于 LiteLLM 对 embedding 的支持范围,请参考官方文档