From 9405af6919a2a04784f563e0207dcfacb8175a6c Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Wed, 8 Mar 2023 20:53:46 -0800 Subject: [PATCH] Harrison/hf inf error (#1543) Co-authored-by: Konstantin Hebenstreit <57603012+KonstantinHebenstreit@users.noreply.github.com> --- langchain/llms/huggingface_endpoint.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/langchain/llms/huggingface_endpoint.py b/langchain/llms/huggingface_endpoint.py index 1e776f7e..027ff917 100644 --- a/langchain/llms/huggingface_endpoint.py +++ b/langchain/llms/huggingface_endpoint.py @@ -121,12 +121,15 @@ class HuggingFaceEndpoint(LLM, BaseModel): ) except requests.exceptions.RequestException as e: # This is the correct syntax raise ValueError(f"Error raised by inference endpoint: {e}") + generated_text = response.json() + if "error" in generated_text: + raise ValueError( + f"Error raised by inference API: {generated_text['error']}" + ) if self.task == "text-generation": # Text generation return includes the starter text. - generated_text = response.json() text = generated_text[0]["generated_text"][len(prompt) :] elif self.task == "text2text-generation": - generated_text = response.json() text = generated_text[0]["generated_text"] else: raise ValueError(