DSPy: The framework for programming—not prompting—language models
Python
update import
Arnav Singhvi567cca881cdc
diff --git a/docs/docs/faqs.md b/docs/docs/faqs.mdindex 80cc21b9f..4bc9b3d9e 100644--- a/docs/docs/faqs.md+++ b/docs/docs/faqs.md@@ -137,7 +137,27 @@ Firstly, please refer to your LM/RM provider to ensure stable status or sufficieAdditionally, 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.-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.+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.++You can configure backoff times for your LM/RM provider by setting `dspy.settings.backoff_time` while configuring your DSPy workflow.++```python+dspy.settings.configure(backoff_time = ...)+```++Additionally, if you'd like to set individual backoff times for specific providers, you can do through the DSPy context manager:++```python+with dspy.context(backoff_time = ..):+ dspy.OpenAI(...) # example++with dspy.context(backoff_time = ..):+ dspy.AzureOpenAI(...) # example+```++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.++Note that## Contributingdiff --git a/dsp/modules/anthropic.py b/dsp/modules/anthropic.pyindex 013988281..cd3f2d4b7 100644--- a/dsp/modules/anthropic.py+++ b/dsp/modules/anthropic.py@@ -3,7 +3,7 @@ import osfrom typing import Any, Optionalimport backoff-import dspy+from dsp.utils.settings import settingsfrom dsp.modules.lm import LM@@ -91,7 +91,7 @@ class Claude(LM):@backoff.on_exception(backoff.expo,(anthropic_rate_limit),- max_time=dspy.settings.backoff_time,+ max_time=settings.backoff_time,max_tries=8,on_backoff=backoff_hdlr,giveup=giveup_hdlr,diff --git a/dsp/modules/aws_providers.py b/dsp/modules/aws_providers.pyindex e350c8898..38ed91102 100644--- a/dsp/modules/aws_providers.py+++ b/dsp/modules/aws_providers.py@@ -4,7 +4,7 @@ from abc import ABC, abstractmethodfrom typing import Any, Optionalimport backoff-import dspy+from dsp.utils.settings import settingstry:import boto3@@ -81,7 +81,7 @@ class AWSProvider(ABC):@backoff.on_exception(backoff.expo,ERRORS,- max_time=dspy.settings.backoff_time,+ max_time=settings.backoff_time,max_tries=8,on_backoff=backoff_hdlr,giveup=giveup_hdlr,@@ -156,7 +156,7 @@ class Sagemaker(AWSProvider):@backoff.on_exception(backoff.expo,ERRORS,- max_time=dspy.settings.backoff_time,+ max_time=settings.backoff_time,max_tries=8,on_backoff=backoff_hdlr,giveup=giveup_hdlr,diff --git a/dsp/modules/azure_openai.py b/dsp/modules/azure_openai.pyindex 45ca89bfd..e85b2c69f 100644--- a/dsp/modules/azure_openai.py+++ b/dsp/modules/azure_openai.py@@ -8,7 +8,8 @@ import openaifrom dsp.modules.cache_utils import CacheMemory, NotebookCacheMemory, cache_turn_onfrom dsp.modules.lm import LM-import dspy+from dsp.utils.settings import settings+try:OPENAI_LEGACY = int(openai.version.__version__[0]) == 0@@ -167,7 +168,7 @@ class AzureOpenAI(LM):@backoff.on_exception(backoff.expo,ERRORS,- max_time=dspy.settings.backoff_time,+ max_time=settings.backoff_time,on_backoff=backoff_hdlr,)def request(self, prompt: str, **kwargs):diff --git a/dsp/modules/cloudflare.py b/dsp/modules/cloudflare.pyindex abb3e5179..6abbe653b 100644--- a/dsp/modules/cloudflare.py+++ b/dsp/modules/cloudflare.py@@ -7,7 +7,7 @@ import requestsfrom pydantic import BaseModel, ValidationErrorfrom dsp.modules.lm import LM-import dspy+from dsp.utils.settings import settingsdef backoff_hdlr(details) -> None:@@ -70,7 +70,7 @@ class CloudflareAI(LM):@backoff.on_exception(backoff.expo,requests.exceptions.RequestException,- max_time=dspy.settings.backoff_time,+ max_time=settings.backoff_time,on_backoff=backoff_hdlr,on_giveup=giveup_hdlr,)diff --git a/dsp/modules/cohere.py b/dsp/modules/cohere.pyindex 67882b027..c7fb026bc 100644--- a/dsp/modules/cohere.py+++ b/dsp/modules/cohere.py@@ -3,7 +3,7 @@ from typing import Any, Optionalimport backofffrom dsp.modules.lm import LM-import dspy+from dsp.utils.settings import settingstry:import cohere@@ -102,7 +102,7 @@ class Cohere(LM):@backoff.on_exception(backoff.expo,(cohere_api_error),- max_time=dspy.settings.backoff_time,+ max_time=settings.backoff_time,on_backoff=backoff_hdlr,giveup=giveup_hdlr,)diff --git a/dsp/modules/google.py b/dsp/modules/google.pyindex 07e9922e7..e80799402 100644--- a/dsp/modules/google.py+++ b/dsp/modules/google.py@@ -5,7 +5,7 @@ from typing import Any, Optionalimport backofffrom dsp.modules.lm import LM-import dspy+from dsp.utils.settings import settingstry:import google.generativeai as genai@@ -134,7 +134,7 @@ class Google(LM):@backoff.on_exception(backoff.expo,(google_api_error),- max_time=dspy.settings.backoff_time,+ max_time=settings.backoff_time,max_tries=8,on_backoff=backoff_hdlr,giveup=giveup_hdlr,diff --git a/dsp/modules/googlevertexai.py b/dsp/modules/googlevertexai.pyindex 5db14ebe3..62b8635d5 100644--- a/dsp/modules/googlevertexai.py+++ b/dsp/modules/googlevertexai.py@@ -6,7 +6,7 @@ import backofffrom pydantic_core import PydanticCustomErrorfrom dsp.modules.lm import LM-import dspy+from dsp.utils.settings import settingstry:import vertexai # type: ignore[import-untyped]@@ -185,7 +185,7 @@ class GoogleVertexAI(LM):@backoff.on_exception(backoff.expo,(Exception),- max_time=dspy.settings.backoff_time,+ max_time=settings.backoff_time,on_backoff=backoff_hdlr,giveup=giveup_hdlr,)diff --git a/dsp/modules/gpt3.py b/dsp/modules/gpt3.pyindex c779e4359..37ee12693 100644--- a/dsp/modules/gpt3.py+++ b/dsp/modules/gpt3.py@@ -8,7 +8,7 @@ import openaifrom dsp.modules.cache_utils import CacheMemory, NotebookCacheMemory, cache_turn_onfrom dsp.modules.lm import LM-import dspy+from dsp.utils.settings import settingstry:OPENAI_LEGACY = int(openai.version.__version__[0]) == 0@@ -134,7 +134,7 @@ class GPT3(LM):@backoff.on_exception(backoff.expo,ERRORS,- max_time=dspy.settings.backoff_time,+ max_time=settings.backoff_time,on_backoff=backoff_hdlr,)def request(self, prompt: str, **kwargs):diff --git a/dsp/modules/groq_client.py b/dsp/modules/groq_client.pyindex c62e3a3cd..e22c505ac 100644--- a/dsp/modules/groq_client.py+++ b/dsp/modules/groq_client.py@@ -13,7 +13,7 @@ except ImportError:from dsp.modules.lm import LM-import dspy+from dsp.utils.settings import settingsdef backoff_hdlr(details):@@ -92,7 +92,7 @@ class GroqLM(LM):@backoff.on_exception(backoff.expo,groq_api_error,- max_time=dspy.settings.backoff_time,+ max_time=settings.backoff_time,on_backoff=backoff_hdlr,)def request(self, prompt: str, **kwargs):diff --git a/dsp/modules/hf_client.py b/dsp/modules/hf_client.pyindex f11a613e9..d0c71469f 100644--- a/dsp/modules/hf_client.py+++ b/dsp/modules/hf_client.py@@ -11,7 +11,7 @@ import requestsfrom dsp.modules.cache_utils import CacheMemory, NotebookCacheMemoryfrom dsp.modules.hf import HFModel, openai_to_hf-import dspy+from dsp.utils.settings import settingsERRORS = (Exception)@@ -341,7 +341,7 @@ class Together(HFModel):@backoff.on_exception(backoff.expo,ERRORS,- max_time=dspy.settings.backoff_time,+ max_time=settings.backoff_time,on_backoff=backoff_hdlr,)def _generate(self, prompt, use_chat_api=False, **kwargs):diff --git a/dsp/modules/mistral.py b/dsp/modules/mistral.pyindex ecb79070e..33a065ed9 100644--- a/dsp/modules/mistral.py+++ b/dsp/modules/mistral.py@@ -3,7 +3,7 @@ from typing import Any, Optionalimport backofffrom dsp.modules.lm import LM-import dspy+from dsp.utils.settings import settingstry:import mistralai@@ -100,7 +100,7 @@ class Mistral(LM):@backoff.on_exception(backoff.expo,(mistralai_api_error),- max_time=dspy.settings.backoff_time,+ max_time=settings.backoff_time,on_backoff=backoff_hdlr,giveup=giveup_hdlr,)diff --git a/dsp/modules/premai.py b/dsp/modules/premai.pyindex 166fc72d5..17e9d151f 100644--- a/dsp/modules/premai.py+++ b/dsp/modules/premai.py@@ -5,7 +5,7 @@ from typing import Any, Optionalimport backofffrom dsp.modules.lm import LM-import dspy+from dsp.utils.settings import settingstry:import premai@@ -180,7 +180,7 @@ class PremAI(LM):@backoff.on_exception(backoff.expo,(premai_api_error),- max_time=dspy.settings.backoff_time,+ max_time=settings.backoff_time,on_backoff=backoff_hdlr,giveup=giveup_hdlr,)diff --git a/dsp/modules/snowflake.py b/dsp/modules/snowflake.pyindex ad308abc0..51b6a2312 100644--- a/dsp/modules/snowflake.py+++ b/dsp/modules/snowflake.py@@ -6,7 +6,7 @@ import backofffrom pydantic_core import PydanticCustomErrorfrom dsp.modules.lm import LM-import dspy+from dsp.utils.settings import settingstry:from snowflake.snowpark import Session@@ -147,7 +147,7 @@ class Snowflake(LM):@backoff.on_exception(backoff.expo,(Exception),- max_time=dspy.settings.backoff_time,+ max_time=settings.backoff_time,on_backoff=backoff_hdlr,giveup=giveup_hdlr,)diff --git a/dspy/retrieve/chromadb_rm.py b/dspy/retrieve/chromadb_rm.pyindex 97a9bba8d..cd5c86fad 100644--- a/dspy/retrieve/chromadb_rm.py+++ b/dspy/retrieve/chromadb_rm.py@@ -7,7 +7,7 @@ from typing import List, Optional, Unionimport backoffimport openai-import dspy+from dsp.utils.settings import settingsfrom dsp.utils import dotdicttry:@@ -124,7 +124,7 @@ class ChromadbRM(dspy.Retrieve):@backoff.on_exception(backoff.expo,ERRORS,- max_time=dspy.settings.backoff_time,+ max_time=settings.backoff_time,)def _get_embeddings(self, queries: List[str]) -> List[List[float]]:"""Return query vector after creating embedding using OpenAIdiff --git a/dspy/retrieve/mongodb_atlas_rm.py b/dspy/retrieve/mongodb_atlas_rm.pyindex 8b2c76c54..41542a507 100644--- a/dspy/retrieve/mongodb_atlas_rm.py+++ b/dspy/retrieve/mongodb_atlas_rm.py@@ -10,7 +10,7 @@ from openai import (UnprocessableEntityError,)-import dspy+from dsp.utils.settings import settingstry:from pymongo import MongoClient@@ -61,7 +61,7 @@ class Embedder:RateLimitError,UnprocessableEntityError,),- max_time=dspy.settings.backoff_time,+ max_time=settings.backoff_time,)def __call__(self, queries) -> Any:embedding = self.client.embeddings.create(input=queries, model=self.model)diff --git a/dspy/retrieve/neo4j_rm.py b/dspy/retrieve/neo4j_rm.pyindex d8c071277..43c9024da 100644--- a/dspy/retrieve/neo4j_rm.py+++ b/dspy/retrieve/neo4j_rm.py@@ -10,7 +10,7 @@ from openai import (UnprocessableEntityError,)-import dspy+from dsp.utils.settings import settingsfrom dsp.utils import dotdicttry:@@ -42,7 +42,7 @@ class Embedder:RateLimitError,UnprocessableEntityError,),- max_time=dspy.settings.backoff_time,+ max_time=settings.backoff_time,)def __call__(self, queries) -> Any:embedding = self.client.embeddings.create(input=queries, model=self.model)diff --git a/dspy/retrieve/pinecone_rm.py b/dspy/retrieve/pinecone_rm.pyindex 5d527f8dd..fe65bf23a 100644--- a/dspy/retrieve/pinecone_rm.py+++ b/dspy/retrieve/pinecone_rm.py@@ -7,7 +7,7 @@ from typing import List, Optional, Unionimport backoff-import dspy+from dsp.utils.settings import settingsfrom dsp.utils import dotdicttry:@@ -178,7 +178,7 @@ class PineconeRM(dspy.Retrieve):@backoff.on_exception(backoff.expo,ERRORS,- max_time=dspy.settings.backoff_time,+ max_time=settings.backoff_time,)def _get_embeddings(self,