text-generation-inference 文件

訊息 API

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

訊息 API

文字生成推理 (TGI) 現在支援訊息 API,該 API 與 OpenAI 聊天補全 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 的“文字生成推理”端點現在都可以使用。以下是如何使用 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 中啟用訊息 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 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.