[HuggingFace Pipeline] add streaming support (#23852)

This commit is contained in:
Ethan Yang
2024-07-10 05:02:00 +08:00
committed by GitHub
parent 34a02efcf9
commit 13855ef0c3
5 changed files with 167 additions and 23 deletions

View File

@@ -0,0 +1,18 @@
from typing import Generator
from langchain_huggingface.llms import HuggingFacePipeline
def test_huggingface_pipeline_streaming() -> None:
"""Test streaming tokens from huggingface_pipeline."""
llm = HuggingFacePipeline.from_model_id(
model_id="gpt2", task="text-generation", pipeline_kwargs={"max_new_tokens": 10}
)
generator = llm.stream("Q: How do you say 'hello' in German? A:'", stop=["."])
stream_results_string = ""
assert isinstance(generator, Generator)
for chunk in generator:
assert isinstance(chunk, str)
stream_results_string = chunk
assert len(stream_results_string.strip()) > 1