Registry / llm-agents / transformers-stream-generator

transformers-stream-generator

JSON →
library0.0.5pypypiunverified

This is a text generation method which returns a generator, streaming out each token in real-time during inference, based on Huggingface/Transformers. It provides a simple way to enable token-by-token streaming for Hugging Face `transformers` models, often used for large language models (LLMs). The library is currently at version 0.0.5 and appears to be in an early development stage with updates released as features or fixes are integrated.

pip install transformers-stream-generator
INSTALL
IMPORT
SIG · TRANSFORMERS-STREA
T
transformers-stream-generator
llm-agentspythonv0.0.5
Install
14.4s avg
Import
—
Disk
254MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.0.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 253.2MB
glibc
py 3.10–3.920 runs
installs and imports cleanly · install 14.4s · import 0.000s · 232MB
254MB installed
● package 254MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

init_stream_support
✓ from transformers_stream_generator import init_stream_support
This function patches the Hugging Face Transformers' generation logic to enable streaming.

This example demonstrates how to set up and use `transformers-stream-generator` with a Hugging Face model. First, `init_stream_support()` is called to patch the generation methods. Then, `model.generate()` is called with `do_stream=True` and `do_sample=True` (and usually `num_beams=1`) to get a generator that yields tokens in real-time.

from transformers import AutoModelForCausalLM, AutoTokenizer from transformers_stream_generator import init_stream_support import os # Initialize streaming support init_stream_support() # Load model and tokenizer (e.g., a small GPT-2 for demonstration) # Replace with your desired model model_name = os.environ.get('TRANSFORMERS_MODEL', 'gpt2') tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name) # Encode input input_text = "Hello, I am a language model and I can" input_ids = tokenizer.encode(input_text, return_tensors='pt') # Generate text with streaming enabled # do_stream=True requires do_sample=True and typically num_beams=1 print(f"Generating with {model_name} in streaming mode...") generator = model.generate( input_ids, max_new_tokens=50, do_stream=True, do_sample=True, # Required for do_stream=True temperature=0.7, top_k=50, top_p=0.95, num_beams=1 # Streaming generally works best with num_beams=1 ) # Iterate and print tokens as they are generated print(input_text, end="") for token_id in generator: word = tokenizer.decode(token_id, skip_special_tokens=True) print(word, end="", flush=True) print("\n\nGeneration complete.")
Debug
Known issues
deprecatedThe library modifies the pretrained model configuration directly to control generation, which Hugging Face Transformers considers a deprecated strategy. This approach may lead to breaking changes in future versions of the `transformers` library.
fix
Monitor the project's GitHub for updates or official guidance on adapting to future `transformers` API changes related to generation configuration files.
affects: <=0.0.5
gotchaFor `do_stream=True` to function correctly, `do_sample=True` must also be set in the `model.generate` function. Failing to do so can result in non-streaming output or unexpected behavior.
fix
Always include `do_sample=True` when `do_stream=True` in your `model.generate` calls.
affects: All versions
gotchaStreaming generation with `transformers-stream-generator` might not work as expected or at all if `num_beams` is set to a value greater than 1 (i.e., when using beam search).
fix
For reliable streaming, set `num_beams=1` when calling `model.generate` with `do_stream=True`.
affects: All versions
Upgrade
Version history
0.0.5latest on PyPI · released Mar 11, 2024
Audit
Dependencies
transformersrequiredCore functionality relies on Hugging Face Transformers library for model loading and generation.
Agent activity
44 hits · last 30 days
node
40
OpenAI (training)
1
Resources
transformers-stream-generator — pip install transformers-stream-generator · libregistry