Skip to content

Commit 86f5470

Browse files
committed
fix(dspy): imports and minor bugs
1 parent 852e3bf commit 86f5470

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

dsp/modules/premai.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@
77

88
try:
99
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
1410

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
2116

2217

2318
def backoff_hdlr(details) -> None:
@@ -75,6 +70,10 @@ def __init__(
7570
Additional arguments to pass to the API provider
7671
"""
7772
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+
)
7877
self.kwargs = kwargs if kwargs == {} else self.kwargs
7978

8079
self.project_id = project_id
@@ -135,18 +134,28 @@ def basic_request(self, prompt, **kwargs) -> str:
135134
**all_kwargs,
136135
)
137136
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")
139138

140139
content = response.choices[0].message.content
140+
if not content:
141+
raise premai_api_error("ChatResponse is none")
142+
141143
output_text = content or ""
142144

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+
)
144153

145154
return output_text
146155

147156
@backoff.on_exception(
148157
backoff.expo,
149-
(ERROR),
158+
(premai_api_error),
150159
max_time=1000,
151160
on_backoff=backoff_hdlr,
152161
giveup=giveup_hdlr,

0 commit comments

Comments
 (0)