# Điểm cuối suy luận

Inference Endpoints cung cấp giải pháp production an toàn để triển khai dễ dàng bất kỳ mô hình `transformers`, `sentence-transformers` và `diffusers` trên hạ tầng chuyên dụng và tự động mở rộng do Hugging Face quản lý. Một Inference Endpoint được xây dựng từ một mô hình trên [Hub](https://huggingface.co/models). Trong hướng dẫn này, chúng ta sẽ học cách quản lý Inference Endpoints theo cách lập trình bằng `huggingface_hub`. Để biết thêm thông tin về sản phẩm Inference Endpoints, hãy xem [tài liệu chính thức](https://huggingface.co/docs/inference-endpoints/index) của nó.

Hướng dẫn này giả định rằng `huggingface_hub` đã được cài đặt đúng cách và máy của bạn đã đăng nhập. Nếu chưa, hãy xem [hướng dẫn Bắt đầu nhanh](https://huggingface.co/docs/huggingface_hub/quick-start#quickstart). Phiên bản tối thiểu hỗ trợ API Inference Endpoints là `v0.19.0`.

> [!TIP]
> **Mới:** giờ đây bạn có thể triển khai một Inference Endpoint từ [HF model catalog](https://endpoints.huggingface.co/catalog) bằng một lời gọi API đơn giản. Catalog là danh sách các model được tuyển chọn kỹ lưỡng, có thể triển khai với các cài đặt tối ưu. Bạn không cần cấu hình gì cả, chúng tôi sẽ lo toàn bộ phần phức tạp! Tất cả các model và cài đặt đều được đảm bảo đã kiểm tra để cung cấp sự cân bằng tốt nhất giữa chi phí và hiệu năng. [create_inference_endpoint_from_catalog()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.create_inference_endpoint_from_catalog) hoạt động giống như [create_inference_endpoint()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.create_inference_endpoint), nhưng với ít tham số cần truyền hơn. Bạn có thể sử dụng [list_inference_catalog()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.list_inference_catalog) để lấy catalog một cách tự động.
>
> Lưu ý rằng đây vẫn là một tính năng thử nghiệm. Hãy cho chúng tôi biết ý kiến của bạn nếu bạn sử dụng nó!

Mỗi mô hình trong danh mục được triển khai thông qua một *công thức*: một tổ hợp phần cứng và engine đã được kiểm tra cho mô hình đó. [list_inference_catalog()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.list_inference_catalog) trả về các mô hình cùng với công thức của chúng, và nhận các bộ lọc phía máy chủ (`accelerator`, `engine`, `license`, `task`, `search`, `limit`):

```py
>>> from huggingface_hub import list_inference_catalog
>>> catalog = list_inference_catalog(task="text-generation", accelerator="neuron")
>>> catalog[0]
InferenceCatalogModel(repo_id='meta-llama/Llama-3.1-8B-Instruct', name='Llama-3.1-8B-Instruct', ...)
>>> catalog[0].recipes
[InferenceCatalogRecipe(id='sizzling-biryani-g4xsi1ac', accelerator='gpu', engine='vllm', gguf_file=None, revision=None),
 InferenceCatalogRecipe(id='artisanal-quinoa-yz9ynamx', accelerator='neuron', engine='vllmNeuron', gguf_file=None, revision=None)]
```

Truyền `repo_id` vào [create_inference_endpoint_from_catalog()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.create_inference_endpoint_from_catalog) để triển khai công thức mặc định của một mô hình, có thể thu hẹp phạm vi bằng `accelerator` (`"cpu"`, `"gpu"` hoặc `"neuron"`) và `gguf_file`, hoặc truyền `recipe_id` để triển khai một công thức cụ thể:

```py
>>> from huggingface_hub import create_inference_endpoint_from_catalog
>>> endpoint = create_inference_endpoint_from_catalog(repo_id="openai/gpt-oss-120b")
>>> endpoint = create_inference_endpoint_from_catalog(recipe_id="artisanal-quinoa-yz9ynamx")
```

## Tìm phần cứng để triển khai

Các giá trị `accelerator`, `vendor`, `region`, `instance_type` và `instance_size` được chấp nhận bởi [create_inference_endpoint()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.create_inference_endpoint) có mối quan hệ phụ thuộc lẫn nhau. Hãy sử dụng [list_inference_endpoints_hardware()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.list_inference_endpoints_hardware) để liệt kê các tổ hợp hợp lệ, cùng với giá mỗi giờ và hạn mức accelerator của namespace của bạn:

```py
>>> from huggingface_hub import list_inference_endpoints_hardware

>>> for hw in list_inference_endpoints_hardware():
...     if hw.accelerator == "gpu" and hw.status == "available":
...         print(hw.vendor, hw.region, hw.instance_type, hw.instance_size, hw.price_per_hour)
aws us-east-1 nvidia-l4 x1 0.8
aws us-east-1 nvidia-l4 x4 3.8
...
```

Quota được tính theo namespace, vì vậy hãy truyền `namespace` mà bạn sẽ triển khai vào (mặc định là tài khoản của bạn):

```py
>>> list_inference_endpoints_hardware(namespace="my-org")
```

Hoặc qua CLI:

```bash
hf endpoints hardware --accelerator gpu
```

## Tạo một Inference Endpoint

Bước đầu tiên là tạo một Inference Endpoint bằng cách sử dụng [create_inference_endpoint()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.create_inference_endpoint):

```py
>>> from huggingface_hub import create_inference_endpoint

>>> endpoint = create_inference_endpoint(
...     "my-endpoint-name",
...     repository="gpt2",
...     framework="pytorch",
...     task="text-generation",
...     accelerator="cpu",
...     vendor="aws",
...     region="us-east-1",
...     type="authenticated",
...     instance_size="x2",
...     instance_type="intel-icl"
... )
```

Hoặc qua CLI:

```bash
hf endpoints deploy my-endpoint-name --repo gpt2 --framework pytorch --accelerator cpu --vendor aws --region us-east-1 --instance-size x2 --instance-type intel-icl --task text-generation

# Deploy from the catalog with a single command
hf endpoints catalog deploy --repo openai/gpt-oss-120b

# Deploy from the catalog with a specific accelerator
hf endpoints catalog deploy --repo openai/gpt-oss-120b --accelerator gpu

# Deploy an exact catalog recipe, as listed by 'hf endpoints catalog ls'
hf endpoints catalog deploy --recipe artisanal-quinoa-yz9ynamx
```

Trong ví dụ này, chúng ta đã tạo một Inference Endpoint `authenticated` có tên là `"my-endpoint-name"`, để phục vụ [gpt2](https://huggingface.co/gpt2) cho `text-generation`. Inference Endpoint `authenticated` có nghĩa là bạn cần token để truy cập API. Chúng ta cũng cần cung cấp thêm thông tin để cấu hình các yêu cầu phần cứng, chẳng hạn như nhà cung cấp, khu vực, bộ tăng tốc, loại instance và kích thước. Bạn có thể xem danh sách các tài nguyên khả dụng [tại đây](https://api.endpoints.huggingface.cloud/#/v2%3A%3Aprovider/list_vendors). Ngoài ra, bạn có thể tạo Inference Endpoint thủ công bằng [giao diện Web](https://ui.endpoints.huggingface.co) cho tiện lợi. Tham khảo [hướng dẫn](https://huggingface.co/docs/inference-endpoints/guides/advanced) này để biết chi tiết về các cài đặt nâng cao và cách sử dụng chúng.

Giá trị được trả về bởi [create_inference_endpoint()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.create_inference_endpoint) là một đối tượng [InferenceEndpoint](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpoint):

```py
>>> endpoint
InferenceEndpoint(name='my-endpoint-name', namespace='Wauplin', repository='gpt2', status='pending', url=None)
```

Hoặc qua CLI:

```bash
hf endpoints describe my-endpoint-name
```

Đây là một dataclass chứa thông tin về endpoint. Bạn có thể truy cập các thuộc tính quan trọng như `name`, `repository`, `status`, `task`, `created_at`, `updated_at`, v.v. Nếu cần, bạn cũng có thể truy cập phản hồi thô từ máy chủ bằng `endpoint.raw`.

Sau khi Inference Endpoint của bạn được tạo, bạn có thể tìm thấy nó trên [dashboard cá nhân](https://ui.endpoints.huggingface.co/) của mình.

![](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/huggingface_hub/inference_endpoints_created.png)

#### Sử dụng hình ảnh tùy chỉnh

Mặc định, Inference Endpoint được xây dựng từ một docker image do Hugging Face cung cấp. Tuy nhiên, bạn có thể chỉ định bất kỳ docker image nào bằng tham số `custom_image`. Một trường hợp sử dụng phổ biến là chạy LLM bằng framework [text-generation-inference](https://github.com/huggingface/text-generation-inference). Bạn có thể thực hiện như sau:

```python
# Start an Inference Endpoint running Zephyr-7b-beta on TGI
>>> from huggingface_hub import create_inference_endpoint
>>> endpoint = create_inference_endpoint(
...     "aws-zephyr-7b-beta-0486",
...     repository="HuggingFaceH4/zephyr-7b-beta",
...     framework="pytorch",
...     task="text-generation",
...     accelerator="gpu",
...     vendor="aws",
...     region="us-east-1",
...     type="authenticated",
...     instance_size="x1",
...     instance_type="nvidia-a10g",
...     custom_image={
...         "healthRoute": "/health",
...         "env": {
...             "MAX_BATCH_PREFILL_TOKENS": "2048",
...             "MAX_INPUT_LENGTH": "1024",
...             "MAX_TOTAL_TOKENS": "1512",
...             "MODEL_ID": "/repository"
...         },
...         "url": "ghcr.io/huggingface/text-generation-inference:1.1.0",
...     },
... )
```

Giá trị cần truyền vào `custom_image` là một từ điển chứa url của docker container và cấu hình để chạy nó. Để biết thêm chi tiết, hãy xem [tài liệu Swagger](https://api.endpoints.huggingface.cloud/#/v2%3A%3Aendpoint/create_endpoint).

Đối với một hình ảnh được lưu trữ trong một registry container riêng, hãy truyền `container_registry_username` và, khi registry yêu cầu, `container_registry_password`. Thông tin xác thực chỉ được hỗ trợ cho một container tùy chỉnh đơn giản:

```python
>>> endpoint = create_inference_endpoint(
...     # Other endpoint and hardware arguments omitted for brevity.
...     custom_image={"url": "private.registry.example/my-image:latest", "port": 8080},
...     container_registry_username="user",
...     container_registry_password="password",
... )
```

Các tùy chọn CLI tương đương là `--container-registry-username` và `--container-registry-password`:

```bash
hf endpoints deploy my-endpoint ... --custom-image private.registry.example/my-image:latest \
  --container-registry-username user --container-registry-password password
```

`custom_image` cũng chấp nhận các kiểu container riêng cho từng engine được hỗ trợ bởi API, bằng cách dùng tên engine (`vLLM`, `vLLMNeuron`, `sGLang`, `tgi`, `tgiNeuron`, `tei`, `llamacpp`, `hfServe`, ...) làm khóa cho từ điển, thay vì để phẳng. Mỗi engine nhận các trường container thông thường (`url`, `port`, `healthRoute`) cùng các tùy chọn tinh chỉnh riêng. Bất kỳ dict nào không có `url` ở cấp cao nhất sẽ được chuyển nguyên vẹn đến API, do đó các engine được thêm vào API sau này sẽ hoạt động mà không cần nâng cấp `huggingface_hub`:

```python
# Start an Inference Endpoint running the vLLM engine image
>>> endpoint = create_inference_endpoint(
...     "llama-3-1-8b-instruct-vllm",
...     repository="meta-llama/Llama-3.1-8B-Instruct",
...     framework="pytorch",
...     task="text-generation",
...     accelerator="gpu",
...     vendor="aws",
...     region="us-east-1",
...     instance_size="x1",
...     instance_type="nvidia-l4",
...     custom_image={
...         "vLLM": {
...             "url": "vllm/vllm-openai:v0.23.0",
...             "healthRoute": "/health",
...             "port": 8000,
...             "tensorParallelSize": 1,
...         }
...     },
... )
```

Nếu không có engine key, cùng một dictionary đó sẽ được gửi dưới dạng một custom container thông thường, do đó các tùy chọn cụ thể của engine (ở đây là `tensorParallelSize`) sẽ bị loại bỏ.

Đối với các container cần entrypoint tùy chỉnh hoặc các cờ runtime, hãy truyền `container_command` và/hoặc `container_args` (mỗi cái là một danh sách các token). Chúng được ánh xạ đến `model.command` và `model.args` trong API payload. Chúng không bị ràng buộc với các image tùy chỉnh: các image engine được quản lý (ví dụ: vLLM, SGLang) cũng chấp nhận các cờ engine thông qua `container_args`. Tính năng tương tự cũng có sẵn từ CLI thông qua `hf endpoints deploy ... --container-command "..." --container-args "..."`.

#### Song song trên các instance đa bộ tăng tốc

vLLM và SGLang mặc định sử dụng một bộ tăng tốc trong khi endpoint được cấp phát mọi bộ tăng tốc của instance, do đó nếu không đặt song song hóa, mô hình sẽ được nạp lên một bộ tăng tốc và các bộ còn lại sẽ nằm im trong khi vẫn báo trạng thái khỏe mạnh, khiến bạn phải trả tiền cho tất cả nhưng chỉ đạt thông lượng của một. Đó là lý do API hiện từ chối các triển khai như vậy. (TGI suy ra số shard từ instance và không bị ảnh hưởng.) Đặt `tensorParallelSize` để chia một bản sao mô hình trên các bộ tăng tốc, hoặc `dataParallelSize` (chỉ vLLM) để chạy một bản sao trên mỗi bộ tăng tốc:

```py
>>> endpoint = create_inference_endpoint(
...     "gpt-oss-120b-vllm",
...     repository="openai/gpt-oss-120b",
...     framework="custom",
...     accelerator="gpu",
...     instance_size="x8",
...     instance_type="nvidia-h200",
...     region="us-east-1",
...     vendor="aws",
...     custom_image={"vLLM": {"url": "vllm/vllm-openai:v0.23.0", "tensorParallelSize": 8}},
... )
```

Từ CLI, `--engine` chọn ảnh engine được quản lý và hai cờ được ghi vào cấu hình của nó:

```bash
hf endpoints deploy gpt-oss-120b-vllm --repo openai/gpt-oss-120b --framework custom \
  --accelerator gpu --instance-size x8 --instance-type nvidia-h200 --region us-east-1 --vendor aws \
  --engine vllm --custom-image vllm/vllm-openai:v0.23.0 --tensor-parallel-size 8
```

Để tinh chỉnh lại một endpoint đã triển khai, chỉ cần truyền `tensor_parallel_size` vào [update()](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpoint.update) hoặc `hf endpoints update`. `model.image` được gửi như một khối hoàn chỉnh và yêu cầu `url`, do đó hình ảnh hiện tại của endpoint sẽ được lấy về và cập nhật tại chỗ:

```py
>>> endpoint.update(tensor_parallel_size=4)
```

`container_args` (`--tp 8`) không tương đương: nó được truyền đến engine dưới dạng cờ dòng lệnh, chứ không phải cấu hình `model.image` mà API kiểm tra đối chiếu với số lượng bộ tăng tốc của instance. Hãy dùng nó cho các cờ engine không có trường image, và cho các container tùy chỉnh thông thường, vốn không có các trường song song hóa.

### Lấy hoặc liệt kê các Inference Endpoints hiện có

Trong một số trường hợp, bạn có thể cần quản lý các Inference Endpoints đã tạo trước đó. Nếu bạn biết tên, bạn có thể lấy nó bằng cách sử dụng [get_inference_endpoint()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.get_inference_endpoint), phương thức này trả về một đối tượng [InferenceEndpoint](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpoint). Ngoài ra, bạn có thể sử dụng [list_inference_endpoints()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.list_inference_endpoints) để lấy danh sách tất cả các Inference Endpoints. Cả hai phương thức đều nhận một tham số tùy chọn `namespace`. Bạn có thể đặt `namespace` thành bất kỳ tổ chức nào mà bạn là thành viên. Nếu không, giá trị mặc định sẽ là tên người dùng của bạn.

```py
>>> from huggingface_hub import get_inference_endpoint, list_inference_endpoints

# Get one
>>> get_inference_endpoint("my-endpoint-name")
InferenceEndpoint(name='my-endpoint-name', namespace='Wauplin', repository='gpt2', status='pending', url=None)

# List all endpoints from an organization
>>> list_inference_endpoints(namespace="huggingface")
[InferenceEndpoint(name='aws-starchat-beta', namespace='huggingface', repository='HuggingFaceH4/starchat-beta', status='paused', url=None), ...]

# List all endpoints from all organizations the user belongs to
>>> list_inference_endpoints(namespace="*")
[InferenceEndpoint(name='aws-starchat-beta', namespace='huggingface', repository='HuggingFaceH4/starchat-beta', status='paused', url=None), ...]
```

Hoặc qua CLI:

```bash
hf endpoints describe my-endpoint-name
hf endpoints ls --namespace huggingface
hf endpoints ls --namespace '*'
```

## Kiểm tra trạng thái triển khai

Trong phần còn lại của hướng dẫn này, chúng ta sẽ giả định rằng có một đối tượng [InferenceEndpoint](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpoint) tên là `endpoint`. Bạn có thể đã nhận thấy rằng endpoint có thuộc tính `status` có kiểu [InferenceEndpointStatus](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpointStatus). Khi Inference Endpoint được triển khai và có thể truy cập, trạng thái phải là `"running"` và thuộc tính `url` được đặt:

```py
>>> endpoint
InferenceEndpoint(name='my-endpoint-name', namespace='Wauplin', repository='gpt2', status='running', url='https://jpj7k2q4j805b727.us-east-1.aws.endpoints.huggingface.cloud')
```

Trước khi đạt trạng thái `"running"`, Inference Endpoint thường trải qua giai đoạn `"initializing"` hoặc `"pending"`. Bạn có thể lấy trạng thái mới của endpoint bằng cách chạy [fetch()](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpoint.fetch). Giống như mọi phương thức khác của [InferenceEndpoint](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpoint) thực hiện yêu cầu đến máy chủ, các thuộc tính bên trong của `endpoint` sẽ bị thay đổi tại chỗ:

```py
>>> endpoint.fetch()
InferenceEndpoint(name='my-endpoint-name', namespace='Wauplin', repository='gpt2', status='pending', url=None)
```

Hoặc qua CLI:

```bash
hf endpoints describe my-endpoint-name
```

Thay vì lấy trạng thái của Inference Endpoint trong khi chờ nó chạy, bạn có thể gọi trực tiếp [wait()](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpoint.wait). Hàm trợ giúp này nhận vào một tham số `timeout` và một tham số `fetch_every` (tính bằng giây) và sẽ chặn luồng cho đến khi Inference Endpoint được triển khai. Giá trị mặc định lần lượt là `None` (không có timeout) và `5` giây.

```py
# Pending endpoint
>>> endpoint
InferenceEndpoint(name='my-endpoint-name', namespace='Wauplin', repository='gpt2', status='pending', url=None)

# Wait 10s => raises a InferenceEndpointTimeoutError
>>> endpoint.wait(timeout=10)
    raise InferenceEndpointTimeoutError("Timeout while waiting for Inference Endpoint to be deployed.")
huggingface_hub._inference_endpoints.InferenceEndpointTimeoutError: Timeout while waiting for Inference Endpoint to be deployed.

# Wait more
>>> endpoint.wait()
InferenceEndpoint(name='my-endpoint-name', namespace='Wauplin', repository='gpt2', status='running', url='https://jpj7k2q4j805b727.us-east-1.aws.endpoints.huggingface.cloud')
```

Nếu `timeout` được đặt và Inference Endpoint mất quá nhiều thời gian để tải, một lỗi timeout `InferenceEndpointTimeoutError` sẽ được ném ra.

## Chạy suy luận

Khi Inference Endpoint của bạn đã sẵn sàng và hoạt động, bạn có thể chạy suy luận trên nó!

[InferenceEndpoint](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpoint) có hai thuộc tính `client` và `async_client` lần lượt trả về một đối tượng [InferenceClient](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_client#huggingface_hub.InferenceClient) và một đối tượng [AsyncInferenceClient](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_client#huggingface_hub.AsyncInferenceClient).

```py
# Run text_generation task:
>>> endpoint.client.text_generation("I am")
' not a fan of the idea of a "big-budget" movie. I think it\'s a'

# Or in an asyncio context:
>>> await endpoint.async_client.text_generation("I am")
```

Nếu Inference Endpoint không đang chạy, một ngoại lệ [InferenceEndpointError](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpointError) sẽ được ném ra:

```py
>>> endpoint.client
huggingface_hub._inference_endpoints.InferenceEndpointError: Cannot create a client for this Inference Endpoint as it is not yet deployed. Please wait for the Inference Endpoint to be deployed using `endpoint.wait()` and try again.
```

Để biết thêm chi tiết về cách sử dụng [InferenceClient](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_client#huggingface_hub.InferenceClient), hãy xem [hướng dẫn suy luận](../guides/inference).

## Quản lý vòng đời

Giờ đây, sau khi đã xem cách tạo một Inference Endpoint và chạy suy luận trên đó, hãy cùng tìm hiểu cách quản lý vòng đời của nó.

> [!TIP]
> Trong phần này, chúng ta sẽ xem xét các phương thức như [pause()](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpoint.pause), [resume()](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpoint.resume), [scale_to_zero()](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpoint.scale_to_zero), [update()](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpoint.update) và [delete()](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpoint.delete). Tất cả những phương thức này đều là các alias được thêm vào [InferenceEndpoint](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpoint) để tiện lợi. Nếu bạn muốn, bạn cũng có thể sử dụng các phương thức tổng quát được định nghĩa trong `HfApi`: [pause_inference_endpoint()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.pause_inference_endpoint), [resume_inference_endpoint()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.resume_inference_endpoint), [scale_to_zero_inference_endpoint()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.scale_to_zero_inference_endpoint), [update_inference_endpoint()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.update_inference_endpoint) và [delete_inference_endpoint()](/docs/huggingface_hub/v2.4.0/vi/package_reference/hf_api#huggingface_hub.HfApi.delete_inference_endpoint).

### Tạm dừng hoặc thu về mức không

Để giảm chi phí khi Inference Endpoint của bạn không được sử dụng, bạn có thể chọn tạm dừng nó bằng [pause()](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpoint.pause) hoặc thu nhỏ về không bằng [scale_to_zero()](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpoint.scale_to_zero).

> [!TIP]
> Inference Endpoint ở trạng thái *tạm dừng* hoặc *thu về không* sẽ không phát sinh chi phí. Sự khác nhau giữa hai trạng thái này là endpoint *tạm dừng* cần được *khôi phục* một cách rõ ràng bằng [resume()](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpoint.resume). Ngược lại, endpoint *thu về không* sẽ tự động khởi động khi có một cuộc gọi suy luận được gửi đến, kèm theo độ trễ khởi động lạnh bổ sung. Inference Endpoint cũng có thể được cấu hình để tự động thu về không sau một khoảng thời gian không hoạt động nhất định.

```py
# Pause and resume endpoint
>>> endpoint.pause()
InferenceEndpoint(name='my-endpoint-name', namespace='Wauplin', repository='gpt2', status='paused', url=None)
>>> endpoint.resume()
InferenceEndpoint(name='my-endpoint-name', namespace='Wauplin', repository='gpt2', status='pending', url=None)
>>> endpoint.wait().client.text_generation(...)
...

# Scale to zero
>>> endpoint.scale_to_zero()
InferenceEndpoint(name='my-endpoint-name', namespace='Wauplin', repository='gpt2', status='scaledToZero', url='https://jpj7k2q4j805b727.us-east-1.aws.endpoints.huggingface.cloud')
# Endpoint is not 'running' but still has a URL and will restart on first call.
```

Hoặc qua CLI:

```bash
hf endpoints pause my-endpoint-name
hf endpoints resume my-endpoint-name
hf endpoints scale-to-zero my-endpoint-name
```

### Cập nhật yêu cầu về mô hình hoặc phần cứng

Trong một số trường hợp, bạn có thể muốn cập nhật Inference Endpoint của mình mà không cần tạo một endpoint mới. Bạn có thể cập nhật mô hình được lưu trữ hoặc các yêu cầu phần cứng để chạy mô hình. Bạn có thể thực hiện điều này bằng cách sử dụng [update()](/docs/huggingface_hub/v2.4.0/vi/package_reference/inference_endpoints#huggingface_hub.InferenceEndpoint.update):

```py
# Change target model
>>> endpoint.update(repository="gpt2-large")
InferenceEndpoint(name='my-endpoint-name', namespace='Wauplin', repository='gpt2-large', status='pending', url=None)

# Update number of replicas
>>> endpoint.update(min_replica=2, max_replica=6)
InferenceEndpoint(name='my-endpoint-name', namespace='Wauplin', repository='gpt2-large', status='pending', url=None)

# Update to larger instance
>>> endpoint.update(accelerator="cpu", instance_size="x4", instance_type="intel-icl")
InferenceEndpoint(name='my-endpoint-name', namespace='Wauplin', repository='gpt2-large', status='pending', url=None)

# Update the container arguments, e.g. engine flags on an endpoint running vLLM or SGLang.
# Replaces the current value, it is not appended.
>>> endpoint.update(container_args=["--enable-auto-tool-choice", "--tool-call-parser", "lfm2"])
InferenceEndpoint(name='my-endpoint-name', namespace='Wauplin', repository='gpt2-large', status='pending', url=None)
```

Hoặc qua CLI:

```bash
hf endpoints update my-endpoint-name --repo gpt2-large
hf endpoints update my-endpoint-name --min-replica 2 --max-replica 6
hf endpoints update my-endpoint-name --accelerator cpu --instance-size x4 --instance-type intel-icl
hf endpoints update my-endpoint-name --container-args "--enable-auto-tool-choice --tool-call-parser lfm2"
# Merged into the image currently configured on the endpoint.
hf endpoints update my-endpoint-name --tensor-parallel-size 8
# Or replace the image entirely, in which case pass back the settings you want to keep.
hf endpoints update my-endpoint-name --engine vllm --custom-image vllm/vllm-openai:v0.23.0 --port 8000
```

### Xóa điểm cuối

Cuối cùng, nếu bạn không còn sử dụng Inference Endpoint nữa, bạn chỉ cần gọi `~InferenceEndpoint.delete()`.

> [!WARNING]
> Đây là một thao tác không thể hoàn tác, sẽ xóa hoàn toàn endpoint, bao gồm cấu hình, nhật ký và các chỉ số sử dụng của nó. Bạn không thể khôi phục một Inference Endpoint đã bị xóa.

## Một ví dụ đầu-cuối

Một trường hợp sử dụng điển hình của Inference Endpoints là xử lý một lô công việc cùng lúc để hạn chế chi phí hạ tầng. Bạn có thể tự động hóa quy trình này bằng cách sử dụng những gì chúng ta đã xem trong hướng dẫn này:

```py
>>> import asyncio
>>> from huggingface_hub import create_inference_endpoint

# Start endpoint + wait until initialized
>>> endpoint = create_inference_endpoint(name="batch-endpoint",...).wait()

# Run inference
>>> client = endpoint.client
>>> results = [client.text_generation(...) for job in jobs]

# Or with asyncio
>>> async_client = endpoint.async_client
>>> results = await asyncio.gather(*[async_client.text_generation(...) for job in jobs])

# Pause endpoint
>>> endpoint.pause()
```

Hoặc nếu Inference Endpoint của bạn đã tồn tại và đang ở trạng thái tạm dừng:

```py
>>> import asyncio
>>> from huggingface_hub import get_inference_endpoint

# Get endpoint + wait until initialized
>>> endpoint = get_inference_endpoint("batch-endpoint").resume().wait()

# Run inference
>>> async_client = endpoint.async_client
>>> results = await asyncio.gather(*[async_client.text_generation(...) for job in jobs])

# Pause endpoint
>>> endpoint.pause()
```

