From d7e12750df1b696793097ca049dccbebf1fc3f3c Mon Sep 17 00:00:00 2001 From: Alexander Dicke <119596967+AIexanderDicke@users.noreply.github.com> Date: Tue, 30 Apr 2024 05:09:55 +0200 Subject: [PATCH] community[patch]: allows using `text-generation-inference` /generate route with `HuggingFaceEndpoint` (#20100) - **Description:** allows to use the /generate route of `text-generation-inference` with the `HuggingFaceEndpoint` --- .../langchain_community/llms/huggingface_endpoint.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/libs/community/langchain_community/llms/huggingface_endpoint.py b/libs/community/langchain_community/llms/huggingface_endpoint.py index 3239414bc41..e7155c7a293 100644 --- a/libs/community/langchain_community/llms/huggingface_endpoint.py +++ b/libs/community/langchain_community/llms/huggingface_endpoint.py @@ -258,7 +258,10 @@ class HuggingFaceEndpoint(LLM): stream=False, task=self.task, ) - response_text = json.loads(response.decode())[0]["generated_text"] + try: + response_text = json.loads(response.decode())[0]["generated_text"] + except KeyError: + response_text = json.loads(response.decode())["generated_text"] # Maybe the generation has stopped at one of the stop sequences: # then we remove this stop sequence from the end of the generated text @@ -289,7 +292,10 @@ class HuggingFaceEndpoint(LLM): stream=False, task=self.task, ) - response_text = json.loads(response.decode())[0]["generated_text"] + try: + response_text = json.loads(response.decode())[0]["generated_text"] + except KeyError: + response_text = json.loads(response.decode())["generated_text"] # Maybe the generation has stopped at one of the stop sequences: # then remove this stop sequence from the end of the generated text