Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

lint(dspy): refactor

nbqu73a9890e9984
diff --git a/dsp/modules/anthropic.py b/dsp/modules/anthropic.py
index 2c33dc727..e78e9d2d2 100644
--- a/dsp/modules/anthropic.py
+++ b/dsp/modules/anthropic.py
@@ -19,7 +19,7 @@ BASE_URL = "https://api.anthropic.com/v1/messages"
def backoff_hdlr(details):
- """Handler from https://pypi.org/project/backoff/"""
+ """Handler from https://pypi.org/project/backoff/."""
print(
"Backing off {wait:0.1f} seconds after {tries} tries "
"calling function {target} with kwargs "
@@ -28,7 +28,7 @@ def backoff_hdlr(details):
def giveup_hdlr(details):
- """wrapper function that decides when to give up on retry"""
+ """Wrapper function that decides when to give up on retry."""
if "rate limits" in details.message:
return False
return True
@@ -46,7 +46,7 @@ class Claude(LM):
super().__init__(model)
try:
- from anthropic import Anthropic, RateLimitError
+ from anthropic import Anthropic
except ImportError as err:
raise ImportError("Claude requires `pip install anthropic`.") from err
@@ -55,10 +55,10 @@ class Claude(LM):
self.api_base = BASE_URL if api_base is None else api_base
self.kwargs = {
- "temperature": 0.0 if "temperature" not in kwargs else kwargs["temperature"],
+ "temperature": kwargs.get("temperature", 0.0),
"max_tokens": min(kwargs.get("max_tokens", 4096), 4096),
- "top_p": 1.0 if "top_p" not in kwargs else kwargs["top_p"],
- "top_k": 1 if "top_k" not in kwargs else kwargs["top_k"],
+ "top_p": kwargs.get("top_p", 1.0),
+ "top_k": kwargs.get("top_k", 1),
"n": kwargs.pop("n", kwargs.pop("num_generations", 1)),
**kwargs,
}
@@ -101,7 +101,7 @@ class Claude(LM):
giveup=giveup_hdlr,
)
def request(self, prompt: str, **kwargs):
- """Handles retrieval of completions from Anthropic whilst handling API errors"""
+ """Handles retrieval of completions from Anthropic whilst handling API errors."""
return self.basic_request(prompt, **kwargs)
def __call__(self, prompt, only_completed=True, return_sorted=False, **kwargs):
@@ -123,12 +123,10 @@ class Claude(LM):
# per eg here: https://docs.anthropic.com/claude/reference/messages-examples
# max tokens can be used as a proxy to return smaller responses
# so this cannot be a proper indicator for incomplete response unless it isnt the user-intent.
- # if only_completed and response.stop_reason != "end_turn":
- # choices = []
n = kwargs.pop("n", 1)
completions = []
- for i in range(n):
+ for _ in range(n):
response = self.request(prompt, **kwargs)
# TODO: Log llm usage instead of hardcoded openai usage
# if dsp.settings.log_openai_usage: