Text Generation Inference 文件
使用文字生成推論 (Text Generation Inference)
並獲得增強的文件體驗
開始使用
使用文字生成推論 (Text Generation Inference)
在您的應用程式中,有許多方式可以呼叫文字生成推論 (TGI) 伺服器。啟動伺服器後,您可以使用 Messages API 的 /v1/chat/completions 路由並發送 POST 請求來獲取伺服器的結果。如果您希望 TGI 返回 Token 流,也可以在呼叫中傳入 "stream": true。
有關 API 的更多資訊,請查閱 text-generation-inference 的 OpenAPI 文件,連結請見此處。
您可以使用任何偏好的工具來發送請求,例如 curl、Python 或 TypeScript。為了提供端到端的體驗,我們開源了 ChatUI,這是一個專為開放存取模型設計的聊天介面。
curl
伺服器成功啟動後,您可以使用 v1/chat/completions 路由來查詢模型,以獲得符合 OpenAI Chat Completion 規範的回應。
curl localhost:8080/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'對於非聊天類的使用場景,您也可以使用 /generate 和 /generate_stream 路由。
curl 127.0.0.1:8080/generate \
-X POST \
-d '{
"inputs":"What is Deep Learning?",
"parameters":{
"max_new_tokens":20
}
}' \
-H 'Content-Type: application/json'Python
Inference Client
huggingface_hub 是一個用於與 Hugging Face Hub(包含其端點)互動的 Python 函式庫。它提供了一個高階類別 huggingface_hub.InferenceClient,可以輕鬆呼叫 TGI 的 Messages API。InferenceClient 同時也會處理參數驗證,並提供一個易於使用的介面。
請透過 pip 安裝 huggingface_hub 套件。
pip install huggingface_hub
現在,您使用 InferenceClient 的方式將與在 Python 中使用 OpenAI 客戶端完全相同。
from huggingface_hub import InferenceClient
client = InferenceClient(
base_url="https://:8080/v1/",
)
output = client.chat.completions.create(
model="tgi",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Count to 10"},
],
stream=True,
max_tokens=1024,
)
for chunk in output:
print(chunk.choices[0].delta.content)您可以點擊此處查看關於 OpenAI 相容性的更多細節。
此外還有一個基於 asyncio 和 aiohttp 的非同步客戶端版本:AsyncInferenceClient。您可以點擊此處查看相關文件。
OpenAI Client
您可以直接使用 OpenAI 的 Python 或 JS 客戶端來與 TGI 互動。
請透過 pip 安裝 OpenAI Python 套件。
pip install openai
from openai import OpenAI
# init the client but point it to TGI
client = OpenAI(
base_url="https://:8080/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)UI
Gradio
Gradio 是一個 Python 函式庫,可協助您透過幾行程式碼為機器學習模型建構網頁應用程式。它有一個 ChatInterface 封裝器,可以為聊天機器人建立簡潔的介面。讓我們來看看如何使用 TGI 和 Gradio 以串流模式建立聊天機器人。首先,讓我們安裝 Gradio 和 Hub Python 函式庫。
pip install huggingface-hub gradio
假設您正在 port 8080 上提供模型服務,我們將透過 InferenceClient 進行查詢。
import gradio as gr
from huggingface_hub import InferenceClient
client = InferenceClient(base_url="http://127.0.0.1:8080")
def inference(message, history):
partial_message = ""
output = client.chat.completions.create(
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": message},
],
stream=True,
max_tokens=1024,
)
for chunk in output:
partial_message += chunk.choices[0].delta.content
yield partial_message
gr.ChatInterface(
inference,
type="messages",
description="This is the demo for Gradio UI consuming TGI endpoint.",
title="Gradio 🤝 TGI",
examples=["Are tomatoes vegetables?"],
).queue().launch()您可以直接點擊此處查看 UI 並試用 Demo 👇
您可以在此處閱讀更多關於如何自訂 ChatInterface 的內容。
ChatUI
ChatUI 是一個專為呼叫大型語言模型 (LLM) 而建構的開源介面。它提供了許多自訂選項,例如使用 SERP API 進行網頁搜尋等。ChatUI 可以自動調用 TGI 伺服器,甚至提供了切換不同 TGI 端點的選項。您可以在 Hugging Chat 試用它,或使用 ChatUI Docker Space 將您自己的 Hugging Chat 部署到 Spaces。
若要將 ChatUI 和 TGI 部署在同一個環境,只需在 chat-ui 儲存庫內的 .env.local 檔案中,將您自己的端點添加到 MODELS 變數即可。請確保端點指向 TGI 伺服器所在的位址。
{
// rest of the model config here
"endpoints": [{"url": "https://HOST:PORT/generate_stream"}]
}