Skip to content

Commit b96e199

Browse files
Merge pull request stanfordnlp#1268 from stanfordnlp/backoff_handler_configure
added configurable backoff max time
2 parents 110a282 + d22351e commit b96e199

19 files changed

+57
-24
lines changed

docs/docs/faqs.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,25 @@ Firstly, please refer to your LM/RM provider to ensure stable status or sufficie
137137

138138
Additionally, try reducing the number of threads you are testing on as the corresponding servers may get overloaded with requests and trigger a backoff + retry mechanism.
139139

140-
If all variables seem stable, you may be experiencing timeouts or backoff errors due to incorrect payload requests sent to the api providers. Please verify your arguments are compatible with the SDK you are interacting with. At times, DSPy may have hard-coded arguments that are not relevant for your compatible, in which case, please free to open a PR alerting this or comment out these default settings for your usage.
140+
If all variables seem stable, you may be experiencing timeouts or backoff errors due to incorrect payload requests sent to the api providers. Please verify your arguments are compatible with the SDK you are interacting with.
141+
142+
You can configure backoff times for your LM/RM provider by setting `dspy.settings.backoff_time` while configuring your DSPy workflow.
143+
144+
```python
145+
dspy.settings.configure(backoff_time = ...)
146+
```
147+
148+
Additionally, if you'd like to set individual backoff times for specific providers, you can do so through the DSPy context manager:
149+
150+
```python
151+
with dspy.context(backoff_time = ..):
152+
dspy.OpenAI(...) # example
153+
154+
with dspy.context(backoff_time = ..):
155+
dspy.AzureOpenAI(...) # example
156+
```
157+
158+
At times, DSPy may have hard-coded arguments that are not relevant for your compatible, in which case, please free to open a PR alerting this or comment out these default settings for your usage.
141159

142160
## Contributing
143161

dsp/modules/anthropic.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Any, Optional
44

55
import backoff
6+
from dsp.utils.settings import settings
67

78
from dsp.modules.lm import LM
89

@@ -90,7 +91,7 @@ def basic_request(self, prompt: str, **kwargs):
9091
@backoff.on_exception(
9192
backoff.expo,
9293
(anthropic_rate_limit),
93-
max_time=1000,
94+
max_time=settings.backoff_time,
9495
max_tries=8,
9596
on_backoff=backoff_hdlr,
9697
giveup=giveup_hdlr,

dsp/modules/aws_providers.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Any, Optional
55

66
import backoff
7+
from dsp.utils.settings import settings
78

89
try:
910
import boto3
@@ -80,7 +81,7 @@ def get_provider_name(self) -> str:
8081
@backoff.on_exception(
8182
backoff.expo,
8283
ERRORS,
83-
max_time=1000,
84+
max_time=settings.backoff_time,
8485
max_tries=8,
8586
on_backoff=backoff_hdlr,
8687
giveup=giveup_hdlr,
@@ -155,7 +156,7 @@ def __init__(
155156
@backoff.on_exception(
156157
backoff.expo,
157158
ERRORS,
158-
max_time=1000,
159+
max_time=settings.backoff_time,
159160
max_tries=8,
160161
on_backoff=backoff_hdlr,
161162
giveup=giveup_hdlr,

dsp/modules/azure_openai.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
from dsp.modules.cache_utils import CacheMemory, NotebookCacheMemory, cache_turn_on
1010
from dsp.modules.lm import LM
11+
from dsp.utils.settings import settings
12+
1113

1214
try:
1315
OPENAI_LEGACY = int(openai.version.__version__[0]) == 0
@@ -166,7 +168,7 @@ def basic_request(self, prompt: str, **kwargs):
166168
@backoff.on_exception(
167169
backoff.expo,
168170
ERRORS,
169-
max_time=1000,
171+
max_time=settings.backoff_time,
170172
on_backoff=backoff_hdlr,
171173
)
172174
def request(self, prompt: str, **kwargs):

dsp/modules/cloudflare.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pydantic import BaseModel, ValidationError
88

99
from dsp.modules.lm import LM
10+
from dsp.utils.settings import settings
1011

1112

1213
def backoff_hdlr(details) -> None:
@@ -69,7 +70,7 @@ def __init__(
6970
@backoff.on_exception(
7071
backoff.expo,
7172
requests.exceptions.RequestException,
72-
max_time=1000,
73+
max_time=settings.backoff_time,
7374
on_backoff=backoff_hdlr,
7475
on_giveup=giveup_hdlr,
7576
)

dsp/modules/cohere.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import backoff
44

55
from dsp.modules.lm import LM
6+
from dsp.utils.settings import settings
67

78
try:
89
import cohere
@@ -101,7 +102,7 @@ def basic_request(self, prompt: str, **kwargs):
101102
@backoff.on_exception(
102103
backoff.expo,
103104
(cohere_api_error),
104-
max_time=1000,
105+
max_time=settings.backoff_time,
105106
on_backoff=backoff_hdlr,
106107
giveup=giveup_hdlr,
107108
)

dsp/modules/google.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import backoff
66

77
from dsp.modules.lm import LM
8+
from dsp.utils.settings import settings
89

910
try:
1011
import google.generativeai as genai
@@ -133,7 +134,7 @@ def basic_request(self, prompt: str, **kwargs):
133134
@backoff.on_exception(
134135
backoff.expo,
135136
(google_api_error),
136-
max_time=1000,
137+
max_time=settings.backoff_time,
137138
max_tries=8,
138139
on_backoff=backoff_hdlr,
139140
giveup=giveup_hdlr,

dsp/modules/googlevertexai.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pydantic_core import PydanticCustomError
77

88
from dsp.modules.lm import LM
9+
from dsp.utils.settings import settings
910

1011
try:
1112
import vertexai # type: ignore[import-untyped]
@@ -184,7 +185,7 @@ def basic_request(self, prompt: str, **kwargs):
184185
@backoff.on_exception(
185186
backoff.expo,
186187
(Exception),
187-
max_time=1000,
188+
max_time=settings.backoff_time,
188189
on_backoff=backoff_hdlr,
189190
giveup=giveup_hdlr,
190191
)

dsp/modules/gpt3.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from dsp.modules.cache_utils import CacheMemory, NotebookCacheMemory, cache_turn_on
1010
from dsp.modules.lm import LM
11+
from dsp.utils.settings import settings
1112

1213
try:
1314
OPENAI_LEGACY = int(openai.version.__version__[0]) == 0
@@ -133,11 +134,11 @@ def basic_request(self, prompt: str, **kwargs):
133134
@backoff.on_exception(
134135
backoff.expo,
135136
ERRORS,
136-
max_time=1000,
137+
max_time=settings.backoff_time,
137138
on_backoff=backoff_hdlr,
138139
)
139140
def request(self, prompt: str, **kwargs):
140-
"""Handles retreival of GPT-3 completions whilst handling rate limiting and caching."""
141+
"""Handles retrieval of GPT-3 completions whilst handling rate limiting and caching."""
141142
if "model_type" in kwargs:
142143
del kwargs["model_type"]
143144

dsp/modules/groq_client.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414

1515
from dsp.modules.lm import LM
16+
from dsp.utils.settings import settings
1617

1718

1819
def backoff_hdlr(details):
@@ -91,7 +92,7 @@ def basic_request(self, prompt: str, **kwargs):
9192
@backoff.on_exception(
9293
backoff.expo,
9394
groq_api_error,
94-
max_time=1000,
95+
max_time=settings.backoff_time,
9596
on_backoff=backoff_hdlr,
9697
)
9798
def request(self, prompt: str, **kwargs):

dsp/modules/hf_client.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from dsp.modules.cache_utils import CacheMemory, NotebookCacheMemory
1313
from dsp.modules.hf import HFModel, openai_to_hf
14+
from dsp.utils.settings import settings
1415

1516
ERRORS = (Exception)
1617

@@ -340,7 +341,7 @@ def __init__(self, model, api_base="https://api.together.xyz/v1", api_key=None,
340341
@backoff.on_exception(
341342
backoff.expo,
342343
ERRORS,
343-
max_time=1000,
344+
max_time=settings.backoff_time,
344345
on_backoff=backoff_hdlr,
345346
)
346347
def _generate(self, prompt, use_chat_api=False, **kwargs):

dsp/modules/mistral.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import backoff
44

55
from dsp.modules.lm import LM
6+
from dsp.utils.settings import settings
67

78
try:
89
import mistralai
@@ -99,7 +100,7 @@ def basic_request(self, prompt: str, **kwargs):
99100
@backoff.on_exception(
100101
backoff.expo,
101102
(mistralai_api_error),
102-
max_time=1000,
103+
max_time=settings.backoff_time,
103104
on_backoff=backoff_hdlr,
104105
giveup=giveup_hdlr,
105106
)

dsp/modules/premai.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import backoff
66

77
from dsp.modules.lm import LM
8+
from dsp.utils.settings import settings
89

910
try:
1011
import premai
@@ -179,7 +180,7 @@ def basic_request(self, prompt, **kwargs) -> list[str]:
179180
@backoff.on_exception(
180181
backoff.expo,
181182
(premai_api_error),
182-
max_time=1000,
183+
max_time=settings.backoff_time,
183184
on_backoff=backoff_hdlr,
184185
giveup=giveup_hdlr,
185186
)

dsp/modules/snowflake.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pydantic_core import PydanticCustomError
77

88
from dsp.modules.lm import LM
9+
from dsp.utils.settings import settings
910

1011
try:
1112
from snowflake.snowpark import Session
@@ -146,7 +147,7 @@ def basic_request(self, prompt: str, **kwargs) -> list:
146147
@backoff.on_exception(
147148
backoff.expo,
148149
(Exception),
149-
max_time=1000,
150+
max_time=settings.backoff_time,
150151
on_backoff=backoff_hdlr,
151152
giveup=giveup_hdlr,
152153
)

dsp/utils/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def __new__(cls):
4242
suggest_failures=0,
4343
langchain_history=[],
4444
experimental=False,
45+
backoff_time = 10
4546
)
4647
cls._instance.__append(config)
4748

dspy/retrieve/chromadb_rm.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import backoff
88
import openai
99

10-
import dspy
10+
from dsp.utils.settings import settings
1111
from dsp.utils import dotdict
1212

1313
try:
@@ -124,7 +124,7 @@ def _init_chromadb(
124124
@backoff.on_exception(
125125
backoff.expo,
126126
ERRORS,
127-
max_time=15,
127+
max_time=settings.backoff_time,
128128
)
129129
def _get_embeddings(self, queries: List[str]) -> List[List[float]]:
130130
"""Return query vector after creating embedding using OpenAI

dspy/retrieve/mongodb_atlas_rm.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
UnprocessableEntityError,
1111
)
1212

13-
import dspy
13+
from dsp.utils.settings import settings
1414

1515
try:
1616
from pymongo import MongoClient
@@ -61,7 +61,7 @@ def __init__(self, provider: str, model: str):
6161
RateLimitError,
6262
UnprocessableEntityError,
6363
),
64-
max_time=15,
64+
max_time=settings.backoff_time,
6565
)
6666
def __call__(self, queries) -> Any:
6767
embedding = self.client.embeddings.create(input=queries, model=self.model)

dspy/retrieve/neo4j_rm.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
UnprocessableEntityError,
1111
)
1212

13-
import dspy
13+
from dsp.utils.settings import settings
1414
from dsp.utils import dotdict
1515

1616
try:
@@ -42,7 +42,7 @@ def __init__(self, provider: str, model: str):
4242
RateLimitError,
4343
UnprocessableEntityError,
4444
),
45-
max_time=15,
45+
max_time=settings.backoff_time,
4646
)
4747
def __call__(self, queries) -> Any:
4848
embedding = self.client.embeddings.create(input=queries, model=self.model)

dspy/retrieve/pinecone_rm.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import backoff
99

10-
import dspy
10+
from dsp.utils.settings import settings
1111
from dsp.utils import dotdict
1212

1313
try:
@@ -178,7 +178,7 @@ def _mean_pooling(
178178
@backoff.on_exception(
179179
backoff.expo,
180180
ERRORS,
181-
max_time=15,
181+
max_time=settings.backoff_time,
182182
)
183183
def _get_embeddings(
184184
self,

0 commit comments

Comments
 (0)