Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Deploying LLMs with Text Generation Inference (TGI)

Welcome back! In our last lesson, you successfully deployed vLLM as a standalone inference server, moving from a library-based approach to a production-oriented service model. You saw how a local Docker deployment translates directly into a scalable Kubernetes architecture.

Today, we'll broaden your toolkit by exploring Text Generation Inference (TGI), Hugging Face's enterprise-focused serving framework. While vLLM is renowned for its raw performance, TGI is designed with production robustness, security, and tight integration with the Hugging Face ecosystem in mind. By deploying an LLM with TGI, you'll gain a comparative perspective on the leading tools in the LLM Ops landscape, a crucial asset for designing and managing AI infrastructure.

1. What is Text Generation Inference (TGI)?

TGI is a purpose-built solution for deploying Large Language Models in production. Developed by Hugging Face, it's engineered to deliver high throughput and low latency by incorporating several key optimizations out-of-the-box. For an SRE or Platform Engineer, TGI offers a pre-packaged, battle-tested system that addresses many common performance and reliability challenges.

How to Deploy Hugging Face Text Generation Inference Server on ...

To understand the value proposition of TGI, let's start with a brief overview from the OneUptime blog.

Read the section titled "Why TGI Over Basic Model Serving." As you read, notice how the listed features—continuous batching, token streaming, quantization, and Flash Attention—are the same class of optimizations we've discussed for vLLM. This highlights an industry consensus on what's required for high-performance inference.

At its core, TGI's architecture is designed for scalable, distributed serving.

This diagram illustrates TGI's high-level architecture. Incoming requests are received by a web server, which buffers and batches them. These batches are then sent via gRPC to one or more model shards running on GPUs. This sharding capability (tensor parallelism) allows TGI to serve models that are too large for a single GPU.

This architecture should look familiar from your work with distributed systems. It's a classic pattern: a front-end router/load balancer that feeds a pool of backend workers. TGI handles the complexity of batching requests and coordinating the distributed model workers, allowing you to focus on deployment and operations.

2. Hands-On: Deploying TGI with Docker

Just as we did with vLLM, the most direct way to get started with TGI is by using its official Docker container. This encapsulates the server, dependencies, and CUDA environment, providing a portable and consistent deployment unit. Your experience with Docker and Kubernetes will make this process feel very similar to the last lesson.

We will use a Docker command to launch the TGI server, specifying the model to serve and other configuration via arguments and environment variables.

TGI (Text Generation Inference) | Guides - Clore.ai

The documentation on Clore.ai provides an excellent, practical guide to launching TGI with Docker, including a clear breakdown of the most important configuration parameters.

First, review the "Quick Deploy on CLORE.AI" section to see the key environment variables. Then, look at the "Launch TGI with a Model" section to see example docker run commands. Finally, scan the table under "Key CLI Parameters" to familiarize yourself with the main configuration options, such as --model-id, --num-shard, and --quantize.

Your Turn: Launch the TGI Server

Now, let's bring up your own TGI instance. Ensure Docker and the NVIDIA Container Toolkit are ready on your machine.

  1. Open your terminal and execute the following docker run command. We'll deploy the same Mistral-7B model as in the last lesson for a direct comparison.

    docker run --rm --gpus all --shm-size 1g -p 8080:80 \
    -v $HOME/.cache/huggingface:/data \
    ghcr.io/huggingface/text-generation-inference:2.0 \
    --model-id mistralai/Mistral-7B-Instruct-v0.2
    

    Let's break down this command:

    • --gpus all: Gives the container access to your GPUs.
    • --shm-size 1g: Allocates 1GB of shared memory (/dev/shm), which TGI requires for inter-process communication. This is a common requirement for high-performance computing applications.
    • -p 8080:80: Maps port 8080 on your host to port 80 inside the container, where TGI listens by default.
    • -v $HOME/.cache/huggingface:/data: This is a crucial step for production. It mounts your local Hugging Face cache directory into the container. TGI will download models to this directory, ensuring they persist even if the container is restarted, avoiding lengthy re-downloads.
    • --model-id ...: Specifies the model to load from the Hugging Face Hub.

    The initial launch will take time as TGI downloads the model weights into the volume you mounted.

  2. Interact with the TGI API
    Once you see logs indicating the server is "Connected" and ready, open a new terminal. TGI provides multiple endpoints. Let's test two of them.

    A) The /generate endpoint (simple completion):

    curl http://localhost:8080/generate \
        -X POST \
        -H "Content-Type: application/json" \
        -d '{"inputs":"As an LLM Ops Engineer, what are the three most important metrics to monitor for an inference service?","parameters":{"max_new_tokens":256}}'
    

    B) The OpenAI-compatible endpoint:
    Just like vLLM, TGI provides an OpenAI-compatible API for seamless integration with existing tools. Note that the endpoint path is /v1/chat/completions, and it's served on the main port.

    curl http://localhost:8080/v1/chat/completions \
        -X POST \
        -H "Content-Type: application/json" \
        -d '{
            "model": "mistralai/Mistral-7B-Instruct-v0.2",
            "messages": [
                {"role": "user", "content": "What is the difference between a Kubernetes DaemonSet and a Deployment?"}
            ],
            "max_tokens": 256
        }'
    

    You have now successfully deployed and queried an LLM using TGI.

3. TGI Deployment in Kubernetes

Your hands-on experience with Docker provides the foundation for a production deployment on Kubernetes. The parameters from the docker run command map directly to a Kubernetes Deployment manifest. Given your background, translating this is a familiar task.

How to Deploy Hugging Face Text Generation Inference Server on ...

The OneUptime blog post provides a comprehensive Kubernetes Deployment YAML for TGI that incorporates several production best practices.

Please review the sections "Creating the Model Cache PVC," "Storing Hugging Face Credentials," and "Deploying TGI with Kubernetes." As you examine the tgi-deployment.yaml file, focus on how it formalizes our docker run command into a declarative, production-ready configuration.

Let's highlight the key elements from that Deployment manifest that build upon our local Docker setup:

  • PersistentVolumeClaim (PVC): The tgi-model-cache PVC provides a durable, network-attached volume for the model weights. This is the production equivalent of our -v Docker flag and is essential for ensuring fast pod startups and sharing models across replicas.
  • Secret: The huggingface-token secret securely manages the API token needed to download gated models, a standard practice you're well-versed in.
  • Container env and resources:
    • The model ID (MODEL_ID) is passed as an environment variable.
    • Performance tuning parameters like MAX_CONCURRENT_REQUESTS are also configured via environment variables.
    • The resources section explicitly requests nvidia.com/gpu: "1", ensuring the Kubernetes scheduler places the pod on a GPU-enabled node.
  • Health Checks (livenessProbe, readinessProbe): The manifest includes probes pointing to TGI's /health endpoint. This is a critical reliability pattern, allowing Kubernetes to automatically manage the lifecycle of the TGI pods, restarting them if they become unresponsive and only routing traffic to them when they are fully loaded and ready.

This Kubernetes manifest is a perfect example of how you would take a tool like TGI and integrate it into a managed, resilient, and scalable production environment.

Conclusion

In this lesson, you've added another powerful serving framework to your arsenal. By deploying an LLM with Text Generation Inference, you've seen an alternative to vLLM that is heavily integrated with the Hugging Face ecosystem and built with enterprise use cases in mind.

Here are the key takeaways from today:

  • TGI as an Enterprise Solution: TGI is a production-grade server that bundles critical optimizations like continuous batching and tensor parallelism, with strong backing from Hugging Face.
  • Docker-based Deployment: The standard way to run TGI is via its official Docker container, configuring it with command-line arguments and environment variables.
  • Production Patterns in Kubernetes: Deploying TGI on Kubernetes involves standard SRE practices like using PVCs for model caching, Secrets for credentials, and configuring health probes for reliability.
  • API Standardization: Like vLLM, TGI offers an OpenAI-compatible API, which simplifies client-side integration and allows for interchangeability between serving backends.

In our next lesson, we will dive deeper into the runtime configuration of these serving frameworks. You'll learn how to configure core serving parameters like max_tokens, temperature, concurrency limits, and batch size to tune the trade-offs between performance, cost, and output quality.

Can't find a good explanation? Sign up and we'll make it for you

Sign up