From 93caa568f91cd65fca5883bc3e14905cd011a898 Mon Sep 17 00:00:00 2001 From: balloonio Date: Fri, 12 Apr 2024 15:16:34 -0400 Subject: [PATCH] community[patch]: Invoke callback prior to yielding token fix for HuggingFaceEndpoint (#20366) - [x] **PR title**: community[patch]: Invoke callback prior to yielding token fix for HuggingFaceEndpoint - [x] **PR message**: - **Description:** Invoke callback prior to yielding token in stream method in community HuggingFaceEndpoint - **Issue:** https://github.com/langchain-ai/langchain/issues/16913 - **Dependencies:** None - **Twitter handle:** @bolun_zhang If no one reviews your PR within a few days, please @-mention one of baskaryan, efriis, eyurtsev, hwchase17. --------- Co-authored-by: Chester Curme --- .../langchain_community/llms/huggingface_endpoint.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libs/community/langchain_community/llms/huggingface_endpoint.py b/libs/community/langchain_community/llms/huggingface_endpoint.py index b4bbf96d820..290b2da9633 100644 --- a/libs/community/langchain_community/llms/huggingface_endpoint.py +++ b/libs/community/langchain_community/llms/huggingface_endpoint.py @@ -326,9 +326,10 @@ class HuggingFaceEndpoint(LLM): # yield text, if any if text: chunk = GenerationChunk(text=text) - yield chunk + if run_manager: run_manager.on_llm_new_token(chunk.text) + yield chunk # break if stop sequence found if stop_seq_found: @@ -361,9 +362,10 @@ class HuggingFaceEndpoint(LLM): # yield text, if any if text: chunk = GenerationChunk(text=text) - yield chunk + if run_manager: await run_manager.on_llm_new_token(chunk.text) + yield chunk # break if stop sequence found if stop_seq_found: