Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

minor fix for retry (#45)

Sri Vardhamanan4ee31749b84f
diff --git a/dsp/modules/cohere.py b/dsp/modules/cohere.py
index 13188b846..f30191195 100644
--- a/dsp/modules/cohere.py
+++ b/dsp/modules/cohere.py
@@ -9,6 +9,7 @@ try:
except ImportError:
print("Not loading Cohere because it is not installed.")
+
def backoff_hdlr(details):
"""Handler from https://pypi.org/project/backoff/"""
print(
@@ -16,7 +17,15 @@ def backoff_hdlr(details):
"calling function {target} with kwargs "
"{kwargs}".format(**details)
)
-
+
+
+def giveup_hdlr(details):
+ """wrapper function that decides when to give up on retry"""
+ if "rate limits" in details.message:
+ return False
+ return True
+
+
class Cohere(LM):
"""Wrapper around Cohere's API.
@@ -84,11 +93,12 @@ class Cohere(LM):
(cohere.CohereAPIError),
max_time=1000,
on_backoff=backoff_hdlr,
+ giveup=giveup_hdlr,
)
def request(self, prompt: str, **kwargs):
"""Handles retrieval of completions from Cohere whilst handling API errors"""
return self.basic_request(prompt, **kwargs)
-
+
def __call__(
self,
prompt: str,