From 3c21203b79b60e26a0fbca49b41a69f2480ddd00 Mon Sep 17 00:00:00 2001 From: cruise Date: Tue, 5 Nov 2024 15:13:10 +0800 Subject: [PATCH 1/2] make langchain error handling support openai>1.0 --- .../services/langchain/LangchainTransform.py | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/cognitive/src/main/python/synapse/ml/services/langchain/LangchainTransform.py b/cognitive/src/main/python/synapse/ml/services/langchain/LangchainTransform.py index 1a2b6467db..898b624bb6 100644 --- a/cognitive/src/main/python/synapse/ml/services/langchain/LangchainTransform.py +++ b/cognitive/src/main/python/synapse/ml/services/langchain/LangchainTransform.py @@ -233,6 +233,7 @@ def _transform(self, dataset): @udf(schema) def udfFunction(x): import openai + from packaging import version if self.running_on_synapse_internal and not self.isSet(self.url): from synapse.ml.fabric.prerun.openai_prerun import OpenAIPrerun @@ -244,15 +245,21 @@ def udfFunction(x): openai.api_base = self.getUrl() openai.api_version = self.getApiVersion() - error_messages = { - openai.error.Timeout: "OpenAI API request timed out, please retry your request after a brief wait and contact us if the issue persists: {}", - openai.error.APIError: "OpenAI API returned an API Error: {}", - openai.error.APIConnectionError: "OpenAI API request failed to connect, check your network settings, proxy configuration, SSL certificates, or firewall rules: {}", - openai.error.InvalidRequestError: "OpenAI API request was invalid: {}", - openai.error.AuthenticationError: "OpenAI API request was not authorized, please check your API key or token and make sure it is correct and active. You may need to generate a new one from your account dashboard: {}", - openai.error.PermissionError: "OpenAI API request was not permitted, make sure your API key has the appropriate permissions for the action or model accessed: {}", - openai.error.RateLimitError: "OpenAI API request exceeded rate limit: {}", - } + error_messages = {} + if version.parse(openai.__version__) < version.parse('1.0.0'): + error_messages = { + openai.error.Timeout: "OpenAI API request timed out, please retry your request after a brief wait and contact us if the issue persists: {}", + openai.error.APIError: "OpenAI API returned an API Error: {}", + openai.error.APIConnectionError: "OpenAI API request failed to connect, check your network settings, proxy configuration, SSL certificates, or firewall rules: {}", + openai.error.InvalidRequestError: "OpenAI API request was invalid: {}", + openai.error.AuthenticationError: "OpenAI API request was not authorized, please check your API key or token and make sure it is correct and active. You may need to generate a new one from your account dashboard: {}", + openai.error.PermissionError: "OpenAI API request was not permitted, make sure your API key has the appropriate permissions for the action or model accessed: {}", + openai.error.RateLimitError: "OpenAI API request exceeded rate limit: {}", + } + else: + { + openai.OpenAIError: "OpenAI API returned an API Error: {}", + } try: result = self.getChain().run(x) From da95b6d43631e3af0d8e2e5f65e06fb78f5b9935 Mon Sep 17 00:00:00 2001 From: cruise Date: Tue, 5 Nov 2024 16:05:48 +0800 Subject: [PATCH 2/2] fix lint --- .../python/synapse/ml/services/langchain/LangchainTransform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cognitive/src/main/python/synapse/ml/services/langchain/LangchainTransform.py b/cognitive/src/main/python/synapse/ml/services/langchain/LangchainTransform.py index 898b624bb6..03c69a10e2 100644 --- a/cognitive/src/main/python/synapse/ml/services/langchain/LangchainTransform.py +++ b/cognitive/src/main/python/synapse/ml/services/langchain/LangchainTransform.py @@ -246,7 +246,7 @@ def udfFunction(x): openai.api_version = self.getApiVersion() error_messages = {} - if version.parse(openai.__version__) < version.parse('1.0.0'): + if version.parse(openai.__version__) < version.parse("1.0.0"): error_messages = { openai.error.Timeout: "OpenAI API request timed out, please retry your request after a brief wait and contact us if the issue persists: {}", openai.error.APIError: "OpenAI API returned an API Error: {}",