text-generation-inference 文档

HTTP API 参考

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

HTTP API 参考

目录

HTTP API 是一个 RESTful API,允许您与 text-generation-inference 组件进行交互。有两个可用的端点:

文本生成推理自定义API

有关如何与文本生成推理 API 交互的更多信息,请查看API 文档

OpenAI Messages API

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

注意: Messages API 从 TGI 1.4.0及以上版本开始支持。请确保您使用的是兼容的版本以访问此功能。

发起请求

您可以使用 curl 向 TGI 的 Messages 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 推理端点

Messages API 已与推理端点集成。现在,每个使用“文本生成推理”和具有聊天模板的 LLM 的端点都可以使用。以下是如何使用 OpenAI 的 Python 客户端库与 TGI 结合使用 IE 的示例:

注意: 请确保将 `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 原生支持 message API

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),
}

# create Hugging Face Model Class
huggingface_model = HuggingFaceModel(
 image_uri=get_huggingface_llm_image_uri("huggingface",version="3.3.4"),
 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 上更新