text-generation-inference 文档

消息 API

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

消息 API

文本生成推理 (TGI) 现在支持消息 API,它与 OpenAI Chat Completion API 完全兼容。此功能从 1.4.0 版本开始提供。您可以使用 OpenAI 的客户端库或期望 OpenAI 架构的第三方库来与 TGI 的消息 API 进行交互。以下是如何利用此兼容性的一些示例。

注意: 消息 API 从 TGI 1.4.0 版本及更高版本开始支持。请确保您使用兼容的版本来访问此功能。

目录

发送请求

您可以使用 curl 向 TGI 的消息 API 发送请求。这是一个示例

curl localhost:3000/v1/chat/completions \
    -X POST \
    -d '{
  "model": "tgi",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": "What is deep learning?"
    }
  ],
  "stream": true,
  "max_tokens": 20
}' \
    -H 'Content-Type: application/json'

流式传输

您还可以使用 OpenAI 的 Python 客户端库来发送流式请求。方法如下

from openai import OpenAI

# init the client but point it to TGI
client = OpenAI(
    base_url="https://#:3000/v1",
    api_key="-"
)

chat_completion = client.chat.completions.create(
    model="tgi",
    messages=[
        {"role": "system", "content": "You are a helpful assistant." },
        {"role": "user", "content": "What is deep learning?"}
    ],
    stream=True
)

# iterate and print stream
for message in chat_completion:
    print(message)

同步

如果您希望发送同步请求,可以这样做

from openai import OpenAI

# init the client but point it to TGI
client = OpenAI(
    base_url="https://#:3000/v1",
    api_key="-"
)

chat_completion = client.chat.completions.create(
    model="tgi",
    messages=[
        {"role": "system", "content": "You are a helpful assistant." },
        {"role": "user", "content": "What is deep learning?"}
    ],
    stream=False
)

print(chat_completion)

Hugging Face 推理终端节点

消息 API 已与 推理终端节点 集成。每个使用“文本生成推理”和 LLM 的终端节点(具有聊天模板)现在都可以使用。以下是如何使用 IE 和 TGI 以及 OpenAI 的 Python 客户端库的示例

注意: 请务必将 base_url 替换为您的终端节点 URL,并在 URL 末尾包含 v1/api_key 应替换为您的 Hugging Face API 密钥。

from openai import OpenAI

# init the client but point it to TGI
client = OpenAI(
    # replace with your endpoint url, make sure to include "v1/" at the end
    base_url="https://vlzz10eq3fol3429.us-east-1.aws.endpoints.huggingface.cloud/v1/",
    # replace with your API key
    api_key="hf_XXX"
)

chat_completion = client.chat.completions.create(
    model="tgi",
    messages=[
        {"role": "system", "content": "You are a helpful assistant." },
        {"role": "user", "content": "What is deep learning?"}
    ],
    stream=True
)

# iterate and print stream
for message in chat_completion:
    print(message.choices[0].delta.content, end="")

云服务提供商

TGI 可以部署在各种云服务提供商上,以实现可扩展且稳健的文本生成。其中一个提供商是 Amazon SageMaker,它最近增加了对 TGI 的支持。以下是如何在 Amazon SageMaker 上部署 TGI

Amazon SageMaker

要在 Amazon SageMaker 中启用消息 API,您需要设置环境变量 MESSAGES_API_ENABLED=true

这将修改 /invocations 路由以接受由角色和内容组成的消息字典。请参阅下面的示例,了解如何使用新的消息 API 部署 Llama。

import json
import sagemaker
import boto3
from sagemaker.huggingface import HuggingFaceModel, get_huggingface_llm_image_uri

try:
 role = sagemaker.get_execution_role()
except ValueError:
 iam = boto3.client('iam')
 role = iam.get_role(RoleName='sagemaker_execution_role')['Role']['Arn']

# Hub Model configuration. https://huggingface.co/models
hub = {
 'HF_MODEL_ID':'HuggingFaceH4/zephyr-7b-beta',
 'SM_NUM_GPUS': json.dumps(1),
 'MESSAGES_API_ENABLED': True
}

# create Hugging Face Model Class
huggingface_model = HuggingFaceModel(
 image_uri=get_huggingface_llm_image_uri("huggingface",version="1.4.0"),
 env=hub,
 role=role,
)

# deploy model to SageMaker Inference
predictor = huggingface_model.deploy(
 initial_instance_count=1,
 instance_type="ml.g5.2xlarge",
 container_startup_health_check_timeout=300,
  )

# send request
predictor.predict({
"messages": [
        {"role": "system", "content": "You are a helpful assistant." },
        {"role": "user", "content": "What is deep learning?"}
    ]
})
< > 在 GitHub 上更新