-
Notifications
You must be signed in to change notification settings - Fork 136
[FIX]: Handle Error response from AI API #548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
a478a8c
2d59b18
65b0857
ddf4f5d
e89931a
967bb37
aa34a55
6c0bd76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
import requests | ||
from http import HTTPStatus | ||
from enum import Enum | ||
from typing import Dict, List | ||
from typing import Dict, List, Union | ||
|
||
|
||
class AIProvider(Enum): | ||
|
@@ -44,7 +44,32 @@ def __init__(self, llm: LLM, access_token: str): | |
def _get_message(self, message: str, role: str = "user"): | ||
return {"role": role, "content": message} | ||
|
||
def _handle_api_response( | ||
self, response | ||
) -> Union[Dict[str, str], Dict[str, Union[str, int]]]: | ||
""" | ||
Handles the API response, returning a success or error structure that the frontend can use. | ||
""" | ||
if response.status_code == HTTPStatus.OK: | ||
return { | ||
"status": "success", | ||
"data": response.json()["choices"][0]["message"]["content"], | ||
} | ||
elif response.status_code == HTTPStatus.UNAUTHORIZED: | ||
return { | ||
"status": "error", | ||
"message": "Unauthorized Access: Your access token is either missing, expired, or invalid. Please ensure that you are providing a valid token. ", | ||
} | ||
else: | ||
return { | ||
"status": "error", | ||
"message": f"Unexpected error: {response.text}", | ||
} | ||
|
||
def _open_ai_fetch_completion_open_ai(self, messages: List[Dict[str, str]]): | ||
""" | ||
Handles the request to OpenAI API for fetching completions. | ||
""" | ||
payload = { | ||
"model": self.LLM_NAME_TO_MODEL_MAP[self._llm], | ||
"temperature": 0.6, | ||
|
@@ -53,13 +78,12 @@ def _open_ai_fetch_completion_open_ai(self, messages: List[Dict[str, str]]): | |
api_url = "https://api.openai.com/v1/chat/completions" | ||
response = requests.post(api_url, headers=self._headers, json=payload) | ||
|
||
print(payload, api_url, response) | ||
if response.status_code != HTTPStatus.OK: | ||
raise Exception(response.json()) | ||
|
||
return response.json() | ||
return self._handle_api_response(response) | ||
|
||
def _fireworks_ai_fetch_completions(self, messages: List[Dict[str, str]]): | ||
""" | ||
Handles the request to Fireworks AI API for fetching completions. | ||
""" | ||
payload = { | ||
"model": self.LLM_NAME_TO_MODEL_MAP[self._llm], | ||
"temperature": 0.6, | ||
|
@@ -73,28 +97,28 @@ def _fireworks_ai_fetch_completions(self, messages: List[Dict[str, str]]): | |
api_url = "https://api.fireworks.ai/inference/v1/chat/completions" | ||
response = requests.post(api_url, headers=self._headers, json=payload) | ||
|
||
if response.status_code != HTTPStatus.OK: | ||
raise Exception(response.json()) | ||
|
||
return response.json() | ||
|
||
def _fetch_completion(self, messages: List[Dict[str, str]]): | ||
return self._handle_api_response(response) | ||
|
||
def _fetch_completion( | ||
self, messages: List[Dict[str, str]] | ||
) -> Union[Dict[str, str], Dict[str, Union[str, int]]]: | ||
""" | ||
Fetches the completion using the appropriate AI provider based on the LLM. | ||
""" | ||
if self._ai_provider == AIProvider.FIREWORKS_AI: | ||
return self._fireworks_ai_fetch_completions(messages)["choices"][0][ | ||
"message" | ||
]["content"] | ||
return self._fireworks_ai_fetch_completions(messages) | ||
|
||
if self._ai_provider == AIProvider.OPEN_AI: | ||
return self._open_ai_fetch_completion_open_ai(messages)["choices"][0][ | ||
"message" | ||
]["content"] | ||
return self._open_ai_fetch_completion_open_ai(messages) | ||
|
||
raise Exception(f"Invalid AI provider {self._ai_provider}") | ||
return { | ||
"status": "error", | ||
"message": f"Invalid AI provider {self._ai_provider}", | ||
} | ||
|
||
def get_dora_metrics_score( | ||
self, four_keys_data: Dict[str, float] | ||
) -> Dict[str, str]: | ||
) -> Union[Dict[str, str], Dict[str, Union[str, int]]]: | ||
|
||
""" | ||
Calculate the DORA metrics score using input data and an LLM (Language Learning Model). | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems over complicated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'll try to simplify it