Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Support retry policies when calling completion() / text_completion() without requiring Router #6623

Open
dbczumar opened this issue Nov 6, 2024 · 6 comments · May be fixed by #6916
Labels
enhancement New feature or request

Comments

@dbczumar
Copy link
Contributor

dbczumar commented Nov 6, 2024

The Feature

Support retry policies when calling completion() / text_completion() without requiring Router. Example:

import litellm
from litellm import RetryPolicy

retry_policy = RetryPolicy(
    TimeoutErrorRetries=num_retries,
    RateLimitErrorRetries=num_retries,
    InternalServerErrorRetries=num_retries,
    # We don't retry on errors that are unlikely to be transient
    # (e.g. bad request, invalid auth credentials)
    BadRequestErrorRetries=0,
    AuthenticationErrorRetries=0,
    ContentPolicyViolationErrorRetries=0,
)

litellm.completion(
    model="openai/gpt-4o-mini",
    messages=[{"role": "user", "content": "Example content"}],
    retry_strategy="exponential_backoff_retry",
    retry_policy=retry_policy,
)

Motivation, pitch

The DSPy library (https://github.com/stanfordnlp/dspy) depends on LiteLLM for issuing LLM calls. When these calls fail due to transient network errors or rate limiting, we want to retry with exponential backoff. However, when these calls fail due to user error (e.g. bad API keys, malformed requests), we want to fail fast.

DSPy users configure LLM keys and parameters using constructor arguments to the dspy.LM class (and optionally be setting environment variables like `OPENAI_API_KEY'), for example:

llm = dspy.LM(model="openai/gpt-4o-mini", api_key="<my key>", model_type="chat")
llm("Who invented deep learing?")

# Env var alternative
os.environ["OPENAI_API_KEY"] = "<my_key>"
llm = dspy.LM(model="openai/gpt-4o-mini", model_type="chat")
llm("Who invented deep learnng?")

DSPy currently wraps litellm.completion() and litellm.text_completion() to implement this interface. See https://github.com/stanfordnlp/dspy/blob/8bc3439052eb80ba4e5ba340c348a6e3b2c94d7c/dspy/clients/lm.py#L78-L87 / https://github.com/stanfordnlp/dspy/blob/8bc3439052eb80ba4e5ba340c348a6e3b2c94d7c/dspy/clients/lm.py#L166-L216. Currently, these interfaces don't support specifying a retry policy.

We've attempted to work around this by constructing a Router internally, but Router construction requires us to fetch the api key and base and pass them to a model_list (due to OpenAI / Azure OpenAI initialization -

litellm/litellm/router.py

Lines 3999 to 4001 in 45ff74a

InitalizeOpenAISDKClient.set_client(
litellm_router_instance=self, model=deployment.to_json(exclude_none=True)
)
), which is difficult if those keys are stored in environment variables.

Twitter / LinkedIn details

No response

@dbczumar dbczumar added the enhancement New feature or request label Nov 6, 2024
@dbczumar
Copy link
Contributor Author

dbczumar commented Nov 6, 2024

Hi @krrishdholakia, can you advise regarding how to support configuring retry policies without Router? We're happy to contribute the change if it's fairly straightforward :D (though any bandwidth you have on your side to support it would be massively appreciated).

cc @okhat

@krrishdholakia
Copy link
Contributor

@ishaan-jaff do we still need openai/azure client init on router?

iirc you implemented some sort of client caching logic on the .completion call already, right?

@krrishdholakia
Copy link
Contributor

i wonder how hard it would be to just move the async_function_with_retries outside the router, and use that inside the wrapper_async / wrapper functions

@ishaan-jaff
Copy link
Contributor

do we still need openai/azure client init on router?

Nope, we don't it's probably better to have this on completion level

@dbczumar
Copy link
Contributor Author

Hi @krrishdholakia @ishaan-jaff , thank you so much for the ideation here! What's best regarding next steps for getting this implemented?

@dbczumar dbczumar linked a pull request Nov 26, 2024 that will close this issue
@dbczumar
Copy link
Contributor Author

@krrishdholakia @ishaan-jaff I gave this a try based on your suggestion. Let me know how it looks - #6916. Happy to split this up for reviewability / adjust course as needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants