From 15d11f40fd3b78b06782bb0d794a0a74a9000595 Mon Sep 17 00:00:00 2001 From: harini-venkataraman Date: Thu, 23 May 2024 18:16:20 +0530 Subject: [PATCH 1/4] Exception handling for Prompt Service --- src/unstract/sdk/__init__.py | 2 +- src/unstract/sdk/prompt.py | 46 ++++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/unstract/sdk/__init__.py b/src/unstract/sdk/__init__.py index a0ae66de..610a2805 100644 --- a/src/unstract/sdk/__init__.py +++ b/src/unstract/sdk/__init__.py @@ -1,4 +1,4 @@ -__version__ = "0.27.0" +__version__ = "0.28.0" def get_sdk_version(): diff --git a/src/unstract/sdk/prompt.py b/src/unstract/sdk/prompt.py index a03d8915..1cf3e04b 100644 --- a/src/unstract/sdk/prompt.py +++ b/src/unstract/sdk/prompt.py @@ -1,7 +1,14 @@ from typing import Any, Optional import requests -from requests import RequestException, Response +from requests import ( + ConnectionError, + HTTPError, + RequestException, + Response, + Timeout, + TooManyRedirects, +) from unstract.sdk.constants import LogLevel, PromptStudioKeys, ToolEnv from unstract.sdk.helper import SdkHelper @@ -25,9 +32,7 @@ def __init__( """ self.tool = tool - self.base_url = SdkHelper.get_platform_base_url( - prompt_host, prompt_port - ) + self.base_url = SdkHelper.get_platform_base_url(prompt_host, prompt_port) self.bearer_token = tool.get_env_or_die(ToolEnv.PLATFORM_API_KEY) def answer_prompt(self, payload: dict[str, Any]) -> dict[str, Any]: @@ -36,9 +41,7 @@ def answer_prompt(self, payload: dict[str, Any]) -> dict[str, Any]: def single_pass_extraction(self, payload: dict[str, Any]) -> dict[str, Any]: return self._post_call("single-pass-extraction", payload) - def _post_call( - self, url_path: str, payload: dict[str, Any] - ) -> dict[str, Any]: + def _post_call(self, url_path: str, payload: dict[str, Any]) -> dict[str, Any]: """Invokes and communicates to prompt service to fetch response for the prompt. @@ -63,17 +66,26 @@ def _post_call( "structure_output": "", } url: str = f"{self.base_url}/{url_path}" - headers: dict[str, str] = { - "Authorization": f"Bearer {self.bearer_token}" - } + headers: dict[str, str] = {"Authorization": f"Bearer {self.bearer_token}"} + response: Response = Response() try: # TODO: Review timeout value - response: Response = requests.post( - url, json=payload, headers=headers, timeout=600 - ) + response = requests.post(url, json=payload, headers=headers, timeout=600) response.raise_for_status() result["status"] = "OK" result["structure_output"] = response.text + except ConnectionError as connect_err: + msg = "Unable to connect to prompt service. Please contact admin." + result["error"] = self._stringify_and_stream_err(connect_err, msg) + except Timeout as time_out: + msg = "Request to run prompt has timed out" + result["error"] = self._stringify_and_stream_err(time_out, msg) + except TooManyRedirects as too_many_redirects: + msg = "Too many redirects while connecting to prompt service." + result["error"] = self._stringify_and_stream_err(too_many_redirects, msg) + except HTTPError as http_err: + msg = "Error while fetching prompt response." + result["error"] = self._stringify_and_stream_err(http_err, msg) except RequestException as e: # Extract error information from the response if available error_message = str(e) @@ -91,6 +103,14 @@ def _post_call( ) return result + def _stringify_and_stream_err(self, err: RequestException, msg: str) -> str: + error_message = str(err) + self.tool.stream_log( + f"{msg}: {error_message}", + level=LogLevel.ERROR, + ) + return error_message + @staticmethod def get_exported_tool( tool: BaseTool, prompt_registry_id: str From ab3300648d0aa7fdab7fbe8e9db2cb0ffc3e9dba Mon Sep 17 00:00:00 2001 From: harini-venkataraman Date: Sun, 9 Jun 2024 10:47:14 +0530 Subject: [PATCH 2/4] Fixing timeout --- src/unstract/sdk/__init__.py | 2 +- src/unstract/sdk/prompt.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/unstract/sdk/__init__.py b/src/unstract/sdk/__init__.py index e297481a..8aab40ce 100644 --- a/src/unstract/sdk/__init__.py +++ b/src/unstract/sdk/__init__.py @@ -1,4 +1,4 @@ -__version__ = "0.31.0" +__version__ = "0.31.1" def get_sdk_version(): diff --git a/src/unstract/sdk/prompt.py b/src/unstract/sdk/prompt.py index fa5fce5d..675af188 100644 --- a/src/unstract/sdk/prompt.py +++ b/src/unstract/sdk/prompt.py @@ -68,7 +68,7 @@ def _post_call(self, url_path: str, payload: dict[str, Any]) -> dict[str, Any]: headers: dict[str, str] = {"Authorization": f"Bearer {self.bearer_token}"} response: Response = Response() try: - response = requests.post(url, json=payload, headers=headers, timeout=600) + response = requests.post(url, json=payload, headers=headers) response.raise_for_status() result["status"] = "OK" result["structure_output"] = response.text From ad16003487b6927d86bc16c169c8dcea3fdde89b Mon Sep 17 00:00:00 2001 From: harini-venkataraman Date: Mon, 10 Jun 2024 13:55:15 +0530 Subject: [PATCH 3/4] Fixing timeout --- src/unstract/sdk/prompt.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/unstract/sdk/prompt.py b/src/unstract/sdk/prompt.py index 675af188..194e5dbd 100644 --- a/src/unstract/sdk/prompt.py +++ b/src/unstract/sdk/prompt.py @@ -2,7 +2,7 @@ from typing import Any, Optional import requests -from requests import ConnectionError, RequestException, Response, Timeout +from requests import ConnectionError, RequestException, Response from unstract.sdk.constants import LogLevel, PromptStudioKeys, ToolEnv from unstract.sdk.helper import SdkHelper @@ -76,13 +76,6 @@ def _post_call(self, url_path: str, payload: dict[str, Any]) -> dict[str, Any]: msg = "Unable to connect to prompt service. Please contact admin." self._stringify_and_stream_err(connect_err, msg) result["error"] = msg - except Timeout as time_out: - msg = ( - "Request to run prompt has timed out. " - "Probable causes might be connectivity issues in LLMs." - ) - self._stringify_and_stream_err(time_out, msg) - result["error"] = msg except RequestException as e: # Extract error information from the response if available error_message = str(e) From 53d2435427c927817280dc0e799ce5676a735416 Mon Sep 17 00:00:00 2001 From: harini-venkataraman Date: Tue, 11 Jun 2024 18:56:45 +0530 Subject: [PATCH 4/4] Fixing typo --- src/unstract/sdk/index.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/unstract/sdk/index.py b/src/unstract/sdk/index.py index 0e90ca2c..3c160a6f 100644 --- a/src/unstract/sdk/index.py +++ b/src/unstract/sdk/index.py @@ -401,7 +401,7 @@ def index_file( ) @deprecated( - "Deprecated class and method. Use Index and query_texy_from_index() instead" + "Deprecated class and method. Use Index and query_index() instead" ) def get_text_from_index( self, embedding_type: str, vector_db: str, doc_id: str