Hugging Face Text Generation Python Client

0.7.0 · active · verified Fri Apr 17

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

import os
from text_generation import Client, InferenceAPIError

# Ensure the TGI server is running and accessible at this URL
# For example, a local server might be 'http://127.0.0.1:8080'
# or a deployed endpoint 'https://your-tgi-endpoint.huggingface.cloud'
TGI_ENDPOINT = os.environ.get('TGI_ENDPOINT', 'http://127.0.0.1:8080')

try:
    client = Client(TGI_ENDPOINT)
    
    # Generate a single response
    response = client.generate(
        "What is the capital of France?", 
        max_new_tokens=20,
        repetition_penalty=1.05
    )
    print(f"Generated Text: {response.generated_text}")

    print("\n--- Streaming Example ---")
    # Stream tokens
    for response in client.generate_stream(
        "Write a short poem about a cat.", 
        max_new_tokens=50
    ):
        if not response.token.special:
            print(response.token.text, end="", flush=True)
    print("\n")

except InferenceAPIError as e:
    print(f"Error from TGI server: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →