你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

如何将 Cohere Embed V3 模型与 Azure AI Studio 配合使用

重要

本文介绍的某些功能可能仅在预览版中提供。 此预览版未提供服务级别协议,不建议将其用于生产工作负载。 某些功能可能不受支持或者受限。 有关详细信息,请参阅 Microsoft Azure 预览版补充使用条款

本文介绍 Cohere Embed V3 模型以及如何将其与 Azure AI Studio 配合使用。 Cohere 模型系列包括针对不同用例优化的各种模型,其中包括聊天补全、嵌入和重新排名。 Cohere 模型针对各种用例进行了优化,包括推理、总结和问答。

Cohere 嵌入模型

用于嵌入的 Cohere 模型系列包括以下模型:

Cohere Embed 英语模型是文本表示模型,用于进行语义搜索、检索增强生成 (RAG)、分类和聚类。 Embed 英语模型在 HuggingFace(海量文本嵌入)MTEB 基准测试中性能优异,并在金融、法律和通用语料库等各种行业的用例中表现出色。 Embed 英语版还具有以下属性:

  • Embed 英语版有 1024 个维度。
  • 模型的上下文窗口为 512 个标记

先决条件

若要将 Cohere Embed V3 模型与 Azure AI Studio 配合使用,需要满足以下先决条件:

模型部署

部署到无服务器 API

Cohere Embed V3 模型可以部署到无服务器 API 终结点,并采用即用即付计费。 这种部署可以将模型作为 API 使用,而无需将它们托管在你的订阅上,同时保持组织所需的企业安全性和合规性。

部署到无服务器 API 终结点不需要消耗订阅的配额。 如果尚未部署模型,可使用 Azure AI Studio、适用于 Python 的 Azure 机器学习 SDK、Azure CLI 或 ARM 模板将模型部署为无服务器 API

已安装推理包

可以通过将 azure-ai-inference 包与 Python 配合使用来使用此模型中的预测。 若要安装此包,需要满足以下先决条件:

  • 已安装 Python 3.8 或更高版本,包括 pip。
  • 终结点 URL。 若要构造客户端库,需要传入终结点 URL。 终结点 URL 采用 https://your-host-name.your-azure-region.inference.ai.azure.com 的形式,其中 your-host-name 是唯一的模型部署主机名,your-azure-region 是部署模型的 Azure 区域(例如 eastus2)。
  • 根据模型部署和身份验证首选项,需要密钥来对服务进行身份验证,或者需要 Microsoft Entra ID 凭据。 密钥是一个包含 32 个字符的字符串。

满足这些先决条件后,使用以下命令安装 Azure AI 推理包:

pip install azure-ai-inference

详细了解 Azure AI 推理包和参考

提示

此外,Cohere 还支持将定制的 API 与模型的特定功能配合使用。 若要使用特定于模型提供商的 API,请查看 Cohere 文档

处理嵌入

在本部分中,将 Azure AI 模型推理 API 与嵌入模型配合使用。

创建客户端以使用模型

首先,创建客户端以使用模型。 以下代码使用存储在环境变量中的终结点 URL 和密钥。

import os
from azure.ai.inference import EmbeddingsClient
from azure.core.credentials import AzureKeyCredential

model = EmbeddingsClient(
    endpoint=os.environ["AZURE_INFERENCE_ENDPOINT"],
    credential=AzureKeyCredential(os.environ["AZURE_INFERENCE_CREDENTIAL"]),
)

获取模型的功能

/info 路由返回有关部署到终结点的模型的信息。 通过调用以下方法返回模型的信息:

model_info = model.get_model_info()

响应如下所示:

print("Model name:", model_info.model_name)
print("Model type:", model_info.model_type)
print("Model provider name:", model_info.model_provider)
Model name: Cohere-embed-v3-english
Model type": embeddings
Model provider name": Cohere

创建嵌入

创建嵌入请求以查看模型的输出。

response = model.embed(
    input=["The ultimate answer to the question of life"],
)

提示

Cohere Embed V3 模型的上下文窗口为 512。 请确保在创建嵌入时不要超过此限制。

响应如下所示,可从中查看模型的使用统计信息:

import numpy as np

for embed in response.data:
    print("Embeding of size:", np.asarray(embed.embedding).shape)

print("Model:", response.model)
print("Usage:", response.usage)

在输入批处理中计算嵌入可能很有用。 参数 inputs 可以是字符串列表,其中每个字符串都是不同的输入。 反过来,响应是嵌入列表,其中每个嵌入对应于同一位置的输入。

response = model.embed(
    input=[
        "The ultimate answer to the question of life", 
        "The largest planet in our solar system is Jupiter",
    ],
)

响应如下所示,可从中查看模型的使用统计信息:

import numpy as np

for embed in response.data:
    print("Embeding of size:", np.asarray(embed.embedding).shape)

print("Model:", response.model)
print("Usage:", response.usage)

提示

Cohere Embed V3 模型一次可以处理 1024 个批次。 创建批处理时,请确保不要超过此限制。

创建不同类型的嵌入

Cohere Embed V3 模型可以为同一输入生成多个嵌入,具体取决于计划如何使用它们。 此功能允许检索 RAG 模式的更准确的嵌入。

以下示例演示如何创建嵌入,这些嵌入用于为要存储在矢量数据库中的文档创建嵌入:

from azure.ai.inference.models import EmbeddingInputType

response = model.embed(
    input=["The answer to the ultimate question of life, the universe, and everything is 42"],
    input_type=EmbeddingInputType.DOCUMENT,
)

处理查询以检索此类文档时,可以使用以下代码片段为查询创建嵌入,并最大程度地提高检索性能。

from azure.ai.inference.models import EmbeddingInputType

response = model.embed(
    input=["What's the ultimate meaning of life?"],
    input_type=EmbeddingInputType.QUERY,
)

Cohere Embed V3 模型可以根据其用例优化嵌入。

Cohere 嵌入模型

用于嵌入的 Cohere 模型系列包括以下模型:

Cohere Embed 英语模型是文本表示模型,用于进行语义搜索、检索增强生成 (RAG)、分类和聚类。 Embed 英语模型在 HuggingFace(海量文本嵌入)MTEB 基准测试中性能优异,并在金融、法律和通用语料库等各种行业的用例中表现出色。 Embed 英语版还具有以下属性:

  • Embed 英语版有 1024 个维度。
  • 模型的上下文窗口为 512 个标记

先决条件

若要将 Cohere Embed V3 模型与 Azure AI Studio 配合使用,需要满足以下先决条件:

模型部署

部署到无服务器 API

Cohere Embed V3 模型可以部署到无服务器 API 终结点,并采用即用即付计费。 这种部署可以将模型作为 API 使用,而无需将它们托管在你的订阅上,同时保持组织所需的企业安全性和合规性。

部署到无服务器 API 终结点不需要消耗订阅的配额。 如果尚未部署模型,可使用 Azure AI Studio、适用于 Python 的 Azure 机器学习 SDK、Azure CLI 或 ARM 模板将模型部署为无服务器 API

已安装推理包

可以通过使用来自 npm@azure-rest/ai-inference 包来使用此模型中的预测。 若要安装此包,需要满足以下先决条件:

  • 带有 npmNode.js 的 LTS 版本。
  • 终结点 URL。 若要构造客户端库,需要传入终结点 URL。 终结点 URL 采用 https://your-host-name.your-azure-region.inference.ai.azure.com 的形式,其中 your-host-name 是唯一的模型部署主机名,your-azure-region 是部署模型的 Azure 区域(例如 eastus2)。
  • 根据模型部署和身份验证首选项,需要密钥来对服务进行身份验证,或者需要 Microsoft Entra ID 凭据。 密钥是一个包含 32 个字符的字符串。

满足这些先决条件后,使用以下命令安装适用于 JavaScript 的 Azure 推理库:

npm install @azure-rest/ai-inference

提示

此外,Cohere 还支持将定制的 API 与模型的特定功能配合使用。 若要使用特定于模型提供商的 API,请查看 Cohere 文档

处理嵌入

在本部分中,将 Azure AI 模型推理 API 与嵌入模型配合使用。

创建客户端以使用模型

首先,创建客户端以使用模型。 以下代码使用存储在环境变量中的终结点 URL 和密钥。

import ModelClient from "@azure-rest/ai-inference";
import { isUnexpected } from "@azure-rest/ai-inference";
import { AzureKeyCredential } from "@azure/core-auth";

const client = new ModelClient(
    process.env.AZURE_INFERENCE_ENDPOINT, 
    new AzureKeyCredential(process.env.AZURE_INFERENCE_CREDENTIAL)
);

获取模型的功能

/info 路由返回有关部署到终结点的模型的信息。 通过调用以下方法返回模型的信息:

await client.path("/info").get()

响应如下所示:

console.log("Model name: ", model_info.body.model_name);
console.log("Model type: ", model_info.body.model_type);
console.log("Model provider name: ", model_info.body.model_provider_name);
Model name: Cohere-embed-v3-english
Model type": embeddings
Model provider name": Cohere

创建嵌入

创建嵌入请求以查看模型的输出。

var response = await client.path("/embeddings").post({
    body: {
        input: ["The ultimate answer to the question of life"],
    }
});

提示

Cohere Embed V3 模型的上下文窗口为 512。 请确保在创建嵌入时不要超过此限制。

响应如下所示,可从中查看模型的使用统计信息:

if (isUnexpected(response)) {
    throw response.body.error;
}

console.log(response.embedding);
console.log(response.body.model);
console.log(response.body.usage);

在输入批处理中计算嵌入可能很有用。 参数 inputs 可以是字符串列表,其中每个字符串都是不同的输入。 反过来,响应是嵌入列表,其中每个嵌入对应于同一位置的输入。

var response = await client.path("/embeddings").post({
    body: {
        input: [
            "The ultimate answer to the question of life", 
            "The largest planet in our solar system is Jupiter",
        ],
    }
});

响应如下所示,可从中查看模型的使用统计信息:

if (isUnexpected(response)) {
    throw response.body.error;
}

console.log(response.embedding);
console.log(response.body.model);
console.log(response.body.usage);

提示

Cohere Embed V3 模型一次可以处理 1024 个批次。 创建批处理时,请确保不要超过此限制。

创建不同类型的嵌入

Cohere Embed V3 模型可以为同一输入生成多个嵌入,具体取决于计划如何使用它们。 此功能允许检索 RAG 模式的更准确的嵌入。

以下示例演示如何创建嵌入,这些嵌入用于为要存储在矢量数据库中的文档创建嵌入:

var response = await client.path("/embeddings").post({
    body: {
        input: ["The answer to the ultimate question of life, the universe, and everything is 42"],
        input_type: "document",
    }
});

处理查询以检索此类文档时,可以使用以下代码片段为查询创建嵌入,并最大程度地提高检索性能。

var response = await client.path("/embeddings").post({
    body: {
        input: ["What's the ultimate meaning of life?"],
        input_type: "query",
    }
});

Cohere Embed V3 模型可以根据其用例优化嵌入。

Cohere 嵌入模型

用于嵌入的 Cohere 模型系列包括以下模型:

Cohere Embed 英语模型是文本表示模型,用于进行语义搜索、检索增强生成 (RAG)、分类和聚类。 Embed 英语模型在 HuggingFace(海量文本嵌入)MTEB 基准测试中性能优异,并在金融、法律和通用语料库等各种行业的用例中表现出色。 Embed 英语版还具有以下属性:

  • Embed 英语版有 1024 个维度。
  • 模型的上下文窗口为 512 个标记

先决条件

若要将 Cohere Embed V3 模型与 Azure AI Studio 配合使用,需要满足以下先决条件:

模型部署

部署到无服务器 API

Cohere Embed V3 模型可以部署到无服务器 API 终结点,并采用即用即付计费。 这种部署可以将模型作为 API 使用,而无需将它们托管在你的订阅上,同时保持组织所需的企业安全性和合规性。

部署到无服务器 API 终结点不需要消耗订阅的配额。 如果尚未部署模型,可使用 Azure AI Studio、适用于 Python 的 Azure 机器学习 SDK、Azure CLI 或 ARM 模板将模型部署为无服务器 API

一个 REST 客户端

通过 Azure AI 模型推理 API 部署的模型可以通过任何 REST 客户端使用。 若要使用 REST 客户端,需要满足以下先决条件:

  • 若要构造请求,需要传入终结点 URL。 终结点 URL 采用 https://your-host-name.your-azure-region.inference.ai.azure.com 的形式,其中 your-host-name 是唯一的模型部署主机名,your-azure-region 是部署模型的 Azure 区域(例如 eastus2)。
  • 根据模型部署和身份验证首选项,需要密钥来对服务进行身份验证,或者需要 Microsoft Entra ID 凭据。 密钥是一个包含 32 个字符的字符串。

提示

此外,Cohere 还支持将定制的 API 与模型的特定功能配合使用。 若要使用特定于模型提供商的 API,请查看 Cohere 文档

处理嵌入

在本部分中,将 Azure AI 模型推理 API 与嵌入模型配合使用。

创建客户端以使用模型

首先,创建客户端以使用模型。 以下代码使用存储在环境变量中的终结点 URL 和密钥。

获取模型的功能

/info 路由返回有关部署到终结点的模型的信息。 通过调用以下方法返回模型的信息:

GET /info HTTP/1.1
Host: <ENDPOINT_URI>
Authorization: Bearer <TOKEN>
Content-Type: application/json

响应如下所示:

{
    "model_name": "Cohere-embed-v3-english",
    "model_type": "embeddings",
    "model_provider_name": "Cohere"
}

创建嵌入

创建嵌入请求以查看模型的输出。

{
    "input": [
        "The ultimate answer to the question of life"
    ]
}

提示

Cohere Embed V3 模型的上下文窗口为 512。 请确保在创建嵌入时不要超过此限制。

响应如下所示,可从中查看模型的使用统计信息:

{
    "id": "0ab1234c-d5e6-7fgh-i890-j1234k123456",
    "object": "list",
    "data": [
        {
            "index": 0,
            "object": "embedding",
            "embedding": [
                0.017196655,
                // ...
                -0.000687122,
                -0.025054932,
                -0.015777588
            ]
        }
    ],
    "model": "Cohere-embed-v3-english",
    "usage": {
        "prompt_tokens": 9,
        "completion_tokens": 0,
        "total_tokens": 9
    }
}

在输入批处理中计算嵌入可能很有用。 参数 inputs 可以是字符串列表,其中每个字符串都是不同的输入。 反过来,响应是嵌入列表,其中每个嵌入对应于同一位置的输入。

{
    "input": [
        "The ultimate answer to the question of life", 
        "The largest planet in our solar system is Jupiter"
    ]
}

响应如下所示,可从中查看模型的使用统计信息:

{
    "id": "0ab1234c-d5e6-7fgh-i890-j1234k123456",
    "object": "list",
    "data": [
        {
            "index": 0,
            "object": "embedding",
            "embedding": [
                0.017196655,
                // ...
                -0.000687122,
                -0.025054932,
                -0.015777588
            ]
        },
        {
            "index": 1,
            "object": "embedding",
            "embedding": [
                0.017196655,
                // ...
                -0.000687122,
                -0.025054932,
                -0.015777588
            ]
        }
    ],
    "model": "Cohere-embed-v3-english",
    "usage": {
        "prompt_tokens": 19,
        "completion_tokens": 0,
        "total_tokens": 19
    }
}

提示

Cohere Embed V3 模型一次可以处理 1024 个批次。 创建批处理时,请确保不要超过此限制。

创建不同类型的嵌入

Cohere Embed V3 模型可以为同一输入生成多个嵌入,具体取决于计划如何使用它们。 此功能允许检索 RAG 模式的更准确的嵌入。

以下示例演示如何创建嵌入,这些嵌入用于为要存储在矢量数据库中的文档创建嵌入:

{
    "input": [
        "The answer to the ultimate question of life, the universe, and everything is 42"
    ],
    "input_type": "document"
}

处理查询以检索此类文档时,可以使用以下代码片段为查询创建嵌入,并最大程度地提高检索性能。

{
    "input": [
        "What's the ultimate meaning of life?"
    ],
    "input_type": "query"
}

Cohere Embed V3 模型可以根据其用例优化嵌入。

更多推理示例

说明 语言 示例
Web 请求 Bash cohere-embed.ipynb
适用于 JavaScript 的 Azure AI 推理包 JavaScript 链接
适用于 Python 的 Azure AI 推理包 Python 链接
OpenAI SDK(实验性) Python 链接
LangChain Python 链接
Cohere SDK Python 链接
LiteLLM SDK Python 链接

增强了检索功能的生成 (RAG) 和工具使用示例

说明 示例
使用 Cohere 嵌入项创建本地 Facebook AI 相似性搜索 (FAISS) 矢量索引 - Langchain langchainlangchain_cohere cohere_faiss_langchain_embed.ipynb
使用 Cohere Command R/R+ 根据本地 FAISS 矢量索引中的数据来回答问题 - Langchain langchainlangchain_cohere command_faiss_langchain.ipynb
使用 Cohere Command R/R+ 根据 AI 搜索矢量索引中的数据来回答问题 - Langchain langchainlangchain_cohere cohere-aisearch-langchain-rag.ipynb
使用 Cohere Command R/R+ 根据 AI 搜索矢量索引中的数据来回答问题 - Cohere SDK cohereazure_search_documents cohere-aisearch-rag.ipynb
使用 LangChain 调用 Command R+ 工具/函数 coherelangchainlangchain_cohere command_tools-langchain.ipynb

部署为无服务器 API 终结点的 Cohere 模型系列的成本和配额注意事项

部署为无服务器 API 的 Cohere 模型由 Cohere 通过 Microsoft Azure 市场提供,并与 Azure AI Studio 集成以供使用。 部署模型时,可以看到 Azure 市场定价。

每次项目从 Azure 市场订阅给定产品/服务时,都会创建一个新资源来跟踪与其消耗相关的成本。 同一资源用于跟踪与推理相关的成本。但是,可以使用多个计量器来独立跟踪每个方案。

有关如何跟踪成本的详细信息,请参阅监视通过 Azure 市场提供的模型的成本。

配额是按部署管理的。 每个部署的速率限制为每分钟 200,000 个令牌和每分钟 1,000 个 API 请求。 但是,我们目前的限制为每个项目每个模型一个部署。 如果当前速率限制不能满足你的方案,请联系 Microsoft Azure 支持部门。