From ea88557abf101dd468808ed9fcf5532a0425ee5d Mon Sep 17 00:00:00 2001 From: Matt Seddon Date: Wed, 22 Jan 2025 10:05:31 +1100 Subject: [PATCH] catch (HfHub)HTTPError in hf-dataset-llm-eval example --- examples/llm_and_nlp/hf-dataset-llm-eval.py | 22 ++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/examples/llm_and_nlp/hf-dataset-llm-eval.py b/examples/llm_and_nlp/hf-dataset-llm-eval.py index 8b5ce3e5f..81c738757 100644 --- a/examples/llm_and_nlp/hf-dataset-llm-eval.py +++ b/examples/llm_and_nlp/hf-dataset-llm-eval.py @@ -1,4 +1,5 @@ from huggingface_hub import InferenceClient +from requests import HTTPError from datachain import C, DataChain, DataModel @@ -20,15 +21,18 @@ def eval_dialog( user_input: str, bot_response: str, ) -> DialogEval: - completion = client.chat_completion( - messages=[ - { - "role": "user", - "content": f"{PROMPT}\n\nUser: {user_input}\nBot: {bot_response}", - }, - ], - response_format={"type": "json", "value": DialogEval.model_json_schema()}, - ) + try: + completion = client.chat_completion( + messages=[ + { + "role": "user", + "content": f"{PROMPT}\n\nUser: {user_input}\nBot: {bot_response}", + }, + ], + response_format={"type": "json", "value": DialogEval.model_json_schema()}, + ) + except HTTPError: + return DialogEval(result="Error", reason="Huggingface API returned an error.") message = completion.choices[0].message try: