text-generation-inference 文件
HTTP API 參考
並獲得增強的文件體驗
開始使用
HTTP API 參考
目錄
HTTP API 是一個 RESTful API,允許您與 text-generation-inference 元件進行互動。有兩個可用的端點:
- 文字生成推理 自定義 API
- OpenAI 的 Messages API
文字生成推理自定義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?"}
]
})