Hugging Face Inference Providers 上的 Cohere 🔥

发布于 2025 年 4 月 16 日
在 GitHub 上更新

banner image

我们很高兴地宣布,**Cohere** 现已成为 HF Hub 上支持的推理提供商!这标志着首个模型创建者直接在 Hub 上共享和提供其模型。

Cohere 致力于构建和提供专为企业用例设计的模型。他们全面的安全 AI 解决方案套件,从尖端生成式 AI 到强大的嵌入和排名模型,旨在解决实际业务挑战。此外,Cohere Labs,Cohere 内部的研究实验室,支持基础研究并寻求改变研究发生的空间。

从现在开始,您可以通过 Cohere 和推理提供商对以下模型进行无服务器推理:

立即使用 Cohere 和 Cohere Labs 点亮您的项目!

Cohere 模型

Cohere 和 Cohere Labs 将其大量模型引入 Inference Providers,这些模型在特定的业务应用中表现出色。让我们详细探讨一些模型。

CohereLabs/c4ai-command-a-03-2025 🔗

针对需要快速、安全、高质量 AI 的企业进行优化。其 256k 的上下文长度(是大多数领先模型的两倍)可以处理更长的企业文档。其他主要功能包括 Cohere 先进的检索增强生成 (RAG) 和可验证引用、代理工具使用、企业级安全以及强大的多语言性能(支持 23 种语言)。

CohereLabs/aya-expanse-32b 🔗

专注于最先进的多语言支持,应用最新的多语言预训练研究。支持阿拉伯语、中文(简体和繁体)、捷克语、荷兰语、英语、法语、德语、希腊语、希伯来语、印地语、印度尼西亚语、意大利语、日语、韩语、波斯语、波兰语、葡萄牙语、罗马尼亚语、俄语、西班牙语、土耳其语、乌克兰语和越南语,上下文长度为 128K。

CohereLabs/c4ai-command-r7b-12-2024 🔗

适用于低成本或低延迟用例,在其开放权重模型类别中,在实际任务中提供最先进的性能。该模型提供 128k 的上下文长度。它提供了多语言支持、引用验证的检索增强生成 (RAG)、推理、工具使用和代理行为的强大组合。也支持 23 种语言。

CohereLabs/aya-vision-32b 🔗

320 亿参数模型,具有先进的功能,针对各种视觉语言用例进行了优化,包括 OCR、字幕、视觉推理、摘要、问答、代码等。它将多模态功能扩展到全球一半以上人口使用的 23 种语言。

工作原理

您可以在 Hub 网站 UI 或通过客户端 SDK 直接使用 Cohere 模型。

您可以在Cohere 文档页面上找到本节中提到的所有示例。

在网站 UI 中

您可以通过在模型中心中按推理提供商进行过滤来搜索 Cohere 模型。

Cohere provider UI

在模型卡片中,您可以选择推理提供商并直接在 UI 中运行推理。

gif screenshot of Cohere inference provider in the UI

从客户端 SDK

让我们逐步了解如何使用客户端 SDK 中的 Cohere 模型。我们还提供了一个包含这些代码片段的 colab notebook,以防您想立即尝试。

从 Python 使用 huggingface_hub

以下示例展示了如何使用 Cohere 作为推理提供商来使用 Command A。您可以使用 Hugging Face token 进行 Hugging Face 的自动路由,或者如果您有自己的 cohere API 密钥,也可以使用它。

安装 huggingface_hub v0.30.0 或更高版本

pip install -U "huggingface_hub>=0.30.0"

使用 huggingface_hub python 库通过定义 provider 参数来调用 Cohere 端点。

from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="cohere",
    api_key="xxxxxxxxxxxxxxxxxxxxxxxx",
)

messages = [
        {
            "role": "user",
            "content": "How to make extremely spicy Mayonnaise?"
        }
]

completion = client.chat.completions.create(
    model="CohereLabs/c4ai-command-r7b-12-2024",
    messages=messages,
    temperature=0.7,
    max_tokens=512,
)

print(completion.choices[0].message)

Cohere Labs 的多语言多模态模型 Aya Vision 也受支持。您可以按以下方式包含 base64 编码的图像

image_path = "img.jpg"
with open(image_path, "rb") as f:
    base64_image = base64.b64encode(f.read()).decode("utf-8")
image_url = f"data:image/jpeg;base64,{base64_image}"

from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="cohere",
    api_key="xxxxxxxxxxxxxxxxxxxxxxxx",
)

messages = [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "What's in this image?"
                },
                {
                    "type": "image_url",
                    "image_url": {"url": image_url},
                },
            ]
        }
]

completion = client.chat.completions.create(
    model="CohereLabs/aya-vision-32b",
    messages=messages,
    temperature=0.7,
    max_tokens=512,
)

print(completion.choices[0].message)

从 JS 使用 @huggingface/inference

import { HfInference } from "@huggingface/inference";

const client = new HfInference("xxxxxxxxxxxxxxxxxxxxxxxx");

const chatCompletion = await client.chatCompletion({
    model: "CohereLabs/c4ai-command-a-03-2025",
    messages: [
        {
            role: "user",
            content: "How to make extremely spicy Mayonnaise?"
        }
    ],
    provider: "cohere",
    max_tokens: 512
});

console.log(chatCompletion.choices[0].message);

从 OpenAI 客户端

以下是如何通过 OpenAI 客户端库使用 Cohere 作为推理提供商调用 Command R7B。

from openai import OpenAI

client = OpenAI(
    base_url="https://router.huggingface.co/cohere/compatibility/v1",
    api_key="xxxxxxxxxxxxxxxxxxxxxxxx",
)

messages = [
        {
            "role": "user",
            "content": "How to make extremely spicy Mayonnaise?"
        }
]

completion = client.chat.completions.create(
    model="command-a-03-2025",
    messages=messages,
    temperature=0.7,
)

print(completion.choices[0].message)

使用 Cohere 模型进行工具调用

Cohere 的模型为推理提供商带来了最先进的代理工具使用功能,因此我们将详细探讨这一点。Hugging Face Hub 客户端和 OpenAI 客户端都兼容通过推理提供商进行的工具调用,因此上述示例可以扩展。

首先,我们需要定义模型要使用的工具。下面我们定义了 get_flight_info,它使用两个位置调用 API 获取最新航班信息。此工具定义将由模型的聊天模板表示。我们也可以在模型卡片中(🎉开源)探讨。

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_flight_info",
            "description": "Get flight information between two cities or airports",
            "parameters": {
                "type": "object",
                "properties": {
                    "loc_origin": {
                        "type": "string",
                        "description": "The departure airport, e.g. MIA",
                    },
                    "loc_destination": {
                        "type": "string",
                        "description": "The destination airport, e.g. NYC",
                    },
                },
                "required": ["loc_origin", "loc_destination"],
            },
        },
    }
]

接下来,我们需要将消息传递给推理客户端,以便模型在相关时使用工具。在下面的示例中,为了清晰起见,我们将助手的工具调用定义在 tool_calls 中。


messages = [
    {"role": "developer", "content": "Today is April 30th"},
    {
        "role": "user",
        "content": "When is the next flight from Miami to Seattle?",
    },
    {
        "role": "assistant",
        "tool_calls": [
            {
                "function": {
                    "arguments": '{ "loc_destination": "Seattle", "loc_origin": "Miami" }',
                    "name": "get_flight_info",
                },
                "id": "get_flight_info0",
                "type": "function",
            }
        ],
    },
    {
        "role": "tool",
        "name": "get_flight_info",
        "tool_call_id": "get_flight_info0",
        "content": "Miami to Seattle, May 1st, 10 AM.",
    },
]

最后,工具和消息被传递给创建方法。

from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="cohere",
    api_key="xxxxxxxxxxxxxxxxxxxxxxxx",
)

completion = client.chat.completions.create(
    model="CohereLabs/c4ai-command-r7b-12-2024",
    messages=messages,
    tools=tools,
    temperature=0.7,
    max_tokens=512,
)

print(completion.choices[0].message)

计费

对于直接请求,即当您使用 Cohere 密钥时,将直接在您的 Cohere 账户上计费。

对于路由请求,即当您通过 Hub 进行身份验证时,您只需支付标准的 Cohere API 费率。我们不收取额外费用,我们只是直接转嫁提供商的成本。(将来,我们可能会与我们的提供商合作伙伴建立收入分成协议。)

重要提示‼️ PRO 用户每月可获得价值 2 美元的推理积分。您可以在所有提供商之间使用它们。🔥

订阅 Hugging Face PRO 计划,即可获得推理额度、ZeroGPU、空间开发模式、20 倍更高的限制以及更多功能。

社区

重大公告!
有没有办法在使用 UI 时选择与世界分享我的数据?或者通过 API 请求获取我所有的对话(这样其他人/我们就可以以某种hacky方式构建这个选择加入功能)?

@borgr 你好!目前还没有这样的选项,但我们会考虑这个,你是为了数据标注吗?☺️ 目前你可以编程方式使用这些提供商并自行存储

·

标注,研究人们缺乏什么,了解人类对各种 LM 行为的反应等。

此评论已被隐藏(标记为无关话题)
此评论已被隐藏(标记为无关话题)

注册登录 发表评论