Skip to content

Commit

Permalink
adds more fine-grained request error raising
Browse files Browse the repository at this point in the history
  • Loading branch information
lauraschauer committed Jul 2, 2024
1 parent 0f8dd3d commit a1a5f72
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 22 deletions.
28 changes: 21 additions & 7 deletions prospector/llm/models/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,29 @@ def _call(
],
}

response = requests.post(endpoint, headers=headers, json=data)

if not response.status_code == 200:
try:
response = requests.post(endpoint, headers=headers, json=data)
return self.parse(response.json())
except requests.exceptions.HTTPError as http_error:
logger.error(
f"Invalid response from AI Core API with error code {response.status_code}"
f"HTTP error occurred when sending a request through AI Core: {http_error}"
)
raise Exception("Invalid response from AI Core API.")

return self.parse(response.json())
raise
except requests.exceptions.Timeout as timeout_err:
logger.error(
f"Timeout error occured when sending a request through AI Core: {timeout_err}"
)
raise
except requests.exceptions.ConnectionError as conn_err:
logger.error(
f"Connection error occurred when sending a request through AI Core: {conn_err}"
)
raise
except requests.exceptions.RequestException as req_err:
logger.error(
f"A request error occured when sending a request through AI Core: {req_err}"
)
raise

def parse(self, message) -> str:
"""Parse the returned JSON object from OpenAI."""
Expand Down
28 changes: 21 additions & 7 deletions prospector/llm/models/mistral.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,29 @@ def _call(
"messages": [{"role": "user", "content": prompt}],
}

response = requests.post(endpoint, headers=headers, json=data)

if not response.status_code == 200:
try:
response = requests.post(endpoint, headers=headers, json=data)
return self.parse(response.json())
except requests.exceptions.HTTPError as http_error:
logger.error(
f"Invalid response from AI Core API with error code {response.status_code}"
f"HTTP error occurred when sending a request through AI Core: {http_error}"
)
raise Exception("Invalid response from AI Core API.")

return self.parse(response.json())
raise
except requests.exceptions.Timeout as timeout_err:
logger.error(
f"Timeout error occured when sending a request through AI Core: {timeout_err}"
)
raise
except requests.exceptions.ConnectionError as conn_err:
logger.error(
f"Connection error occurred when sending a request through AI Core: {conn_err}"
)
raise
except requests.exceptions.RequestException as req_err:
logger.error(
f"A request error occured when sending a request through AI Core: {req_err}"
)
raise

def parse(self, message) -> str:
"""Parse the returned JSON object from OpenAI."""
Expand Down
31 changes: 23 additions & 8 deletions prospector/llm/models/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def _identifying_params(self) -> Dict[str, Any]:
def _call(
self, prompt: str, stop: Optional[List[str]] = None, **kwargs: Any
) -> str:
endpoint = f"{self.deployment_url}/chat/completions?api-version=2023-05-15"
# endpoint = f"{self.deployment_url}/chat/completions?api-version=2023-05-15"
endpoint = f"{self.deployment_url}/chat/cpletions?api-version=2023-05-15"
headers = instantiation.get_headers(self.ai_core_sk_filepath)
data = {
"messages": [
Expand All @@ -42,15 +43,29 @@ def _call(
"temperature": self.temperature,
}

response = requests.post(endpoint, headers=headers, json=data)

if not response.status_code == 200:
try:
response = requests.post(endpoint, headers=headers, json=data)
return self.parse(response.json())
except requests.exceptions.HTTPError as http_error:
logger.error(
f"Invalid response from AI Core API with error code {response.status_code}"
f"HTTP error occurred when sending a request through AI Core: {http_error}"
)
raise Exception("Invalid response from AI Core API.")

return self.parse(response.json())
raise
except requests.exceptions.Timeout as timeout_err:
logger.error(
f"Timeout error occured when sending a request through AI Core: {timeout_err}"
)
raise
except requests.exceptions.ConnectionError as conn_err:
logger.error(
f"Connection error occurred when sending a request through AI Core: {conn_err}"
)
raise
except requests.exceptions.RequestException as req_err:
logger.error(
f"A request error occured when sending a request through AI Core: {req_err}"
)
raise

def parse(self, message) -> str:
"""Parse the returned JSON object from OpenAI."""
Expand Down

0 comments on commit a1a5f72

Please sign in to comment.