|
7 | 7 |
|
8 | 8 | try: |
9 | 9 | import premai |
10 | | -except ImportError as err: |
11 | | - raise ImportError( |
12 | | - "Not loading Prem AI because it is not installed. Install it with `pip install -U premai`.", |
13 | | - ) from err |
14 | 10 |
|
15 | | - |
16 | | -class ChatPremAPIError(Exception): |
17 | | - """Error with the `PremAI` API.""" |
18 | | - |
19 | | - |
20 | | -ERROR = ChatPremAPIError |
| 11 | + premai_error = premai.errors.UnexpectedStatus |
| 12 | +except ImportError: |
| 13 | + premai_api_error = Exception |
| 14 | +except AttributeError: |
| 15 | + premai_api_error = Exception |
21 | 16 |
|
22 | 17 |
|
23 | 18 | def backoff_hdlr(details) -> None: |
@@ -75,6 +70,10 @@ def __init__( |
75 | 70 | Additional arguments to pass to the API provider |
76 | 71 | """ |
77 | 72 | super().__init__(model) |
| 73 | + if premai_api_error == Exception: |
| 74 | + raise ImportError( |
| 75 | + "Not loading Prem AI because it is not installed. Install it with `pip install premai`.", |
| 76 | + ) |
78 | 77 | self.kwargs = kwargs if kwargs == {} else self.kwargs |
79 | 78 |
|
80 | 79 | self.project_id = project_id |
@@ -135,18 +134,28 @@ def basic_request(self, prompt, **kwargs) -> str: |
135 | 134 | **all_kwargs, |
136 | 135 | ) |
137 | 136 | if not response.choices: |
138 | | - raise ChatPremAPIError("ChatResponse must have at least one candidate") |
| 137 | + raise premai_api_error("ChatResponse must have at least one candidate") |
139 | 138 |
|
140 | 139 | content = response.choices[0].message.content |
| 140 | + if not content: |
| 141 | + raise premai_api_error("ChatResponse is none") |
| 142 | + |
141 | 143 | output_text = content or "" |
142 | 144 |
|
143 | | - self.history.append({"prompt": prompt, "response": content, "kwargs": all_kwargs, "raw_kwargs": kwargs}) |
| 145 | + self.history.append( |
| 146 | + { |
| 147 | + "prompt": prompt, |
| 148 | + "response": content, |
| 149 | + "kwargs": all_kwargs, |
| 150 | + "raw_kwargs": kwargs, |
| 151 | + }, |
| 152 | + ) |
144 | 153 |
|
145 | 154 | return output_text |
146 | 155 |
|
147 | 156 | @backoff.on_exception( |
148 | 157 | backoff.expo, |
149 | | - (ERROR), |
| 158 | + (premai_api_error), |
150 | 159 | max_time=1000, |
151 | 160 | on_backoff=backoff_hdlr, |
152 | 161 | giveup=giveup_hdlr, |
|
0 commit comments