Skip to content

Commit

Permalink
fix: fix langchain error handling to avoid crash on openai>1.0.0 (#2307)
Browse files Browse the repository at this point in the history
* make langchain error handling support openai>1.0

* fix lint

---------

Co-authored-by: cruise <[email protected]>
  • Loading branch information
lhrotk and mslhrotk authored Nov 12, 2024
1 parent 2758261 commit f68c133
Showing 1 changed file with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit f68c133

Please sign in to comment.