The `text-generation` library is the official Python client for interacting with the Hugging Face Text Generation Inference (TGI) backend, a highly optimized solution for deploying large language models. It provides synchronous and asynchronous APIs for text generation, including streaming capabilities. The current version is 0.7.0, and while the underlying TGI server has a rapid release cadence with frequent updates, the client library itself is updated less often, focusing on stability and compatibility with common TGI server versions.
pip install text-generationVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to instantiate a `Client` for synchronous text generation. It includes examples for both single-response generation and streaming token-by-token. Remember to set the `TGI_ENDPOINT` environment variable or directly provide the correct URL for your running Text Generation Inference server.
Always ensure your `text-generation` client version is compatible with your deployed `text-generation-inference` server version. Refer to the TGI server's documentation for API stability notes and recommended client versions.
Verify that the `base_url` provided to `Client()` or `AsyncClient()` points to a running and network-accessible TGI endpoint. Double-check port numbers, hostnames, and any necessary authentication if applicable.
Use `generate()` for single, complete outputs where latency isn't a critical concern for the first token, or for batching. Use `generate_stream()` for real-time applications where displaying tokens as they are produced enhances user experience.
Always wrap your client calls in a `try...except InferenceAPIError` block. Log the error message to understand the root cause of the server-side failure. Check your request parameters against the model's capabilities and server configuration.
Ensure your TGI server is running and accessible from where you are running the client. Verify the `base_url` (e.g., `http://127.0.0.1:8080`) matches the server's actual address and port.
Examine the error message within the `InferenceAPIError`. Common causes include: invalid model ID, insufficient GPU memory, unsupported generation parameters, or issues during model loading. Check server logs for more detailed diagnostics.
Always provide the URL of your Text Generation Inference server when creating a `Client` instance, e.g., `client = Client('http://your-tgi-server:8080')`.Ensure you have the latest `text-generation` library installed (`pip install --upgrade text-generation`). Verify your import statement is `from text_generation import Client` and check for any local files named `text_generation.py` that might be shadowing the installed library.