Text Generation Inference 文件
HTTP API 參考
並獲得增強的文件體驗
開始使用
HTTP API 參考
目錄
此 HTTP API 是一個 RESTful API,讓您可以與 text-generation-inference 元件互動。目前提供兩個端點:
- 文字生成推論 (Text Generation Inference) 自訂 API
- OpenAI 的 Messages API
文字生成推論 (Text Generation Inference) 自訂 API
請參閱 API 文件以了解更多關於如何與文字生成推論 (TGI) 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'串流 (Streaming)
您也可以使用 OpenAI 的 Python 客戶端程式庫來發送串流 (streaming) 請求。操作方式如下:
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)同步請求 (Synchronous)
如果您偏好發送同步請求,可以按照以下方式進行:
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 Inference Endpoints
Messages API 已與 Inference Endpoints 整合。任何使用帶有聊天模板之大型語言模型 (LLM) 的「文字生成推論」端點現在都可以使用。以下是使用 OpenAI Python 客戶端程式庫透過 Inference Endpoints (IE) 使用 TGI 的範例:
注意:請務必將
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 原生支援 Chat Completions API。
如需完整的部署與效能評測指南(包含 EC2),請參閱 在 AWS 上部署 (EC2 和 SageMaker)。
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.5"),
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?"},
]
}
)