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

Add retry logic for OpenAI API requests #1

Closed
wants to merge 1 commit into from

Conversation

lemillermicrosoft
Copy link
Owner

@lemillermicrosoft lemillermicrosoft commented Mar 3, 2023

Context and Motivation

This PR adds a retry mechanism for OpenAI API requests that encounter throttling or service errors, using the retry-after header if available. This improves the reliability and performance of the OpenAI client, especially when the request volume is high or unpredictable.

Description

The main changes in this PR are:

  • Add a new class RetryOnceWithDelay that implements IRetryMechanism and retries once after a delay, using the retry-after header if available.
  • Add a new class PassThroughWithoutRetry that implements IRetryMechanism and does not retry, for testing purposes.
  • Add unit tests for the new classes and the retry logic.
  • Modify the OpenAIClientAbstract class to check for the retry-after header in the response and throw an AIException with the retry-after value as an additional property.
  • Modify the OpenAIClientRetryHandler class to use the retry-after value as the delay before retrying, if available. Otherwise, it falls back to the exponential backoff strategy.

Contribution Checklist

dotnet/src/SemanticKernel/AI/AIException.cs Outdated Show resolved Hide resolved
dotnet/src/SemanticKernel/AI/AIException.cs Outdated Show resolved Hide resolved
dotnet/src/SemanticKernel/Configuration/KernelConfig.cs Outdated Show resolved Hide resolved
dotnet/src/SemanticKernel/Reliability/RetryWithDelay.cs Outdated Show resolved Hide resolved
dotnet/src/SemanticKernel/Reliability/RetryWithDelay.cs Outdated Show resolved Hide resolved
dotnet/src/SemanticKernel/Reliability/RetryWithDelay.cs Outdated Show resolved Hide resolved
dotnet/src/SemanticKernel/Reliability/RetryWithDelay.cs Outdated Show resolved Hide resolved
dotnet/src/SemanticKernel/Reliability/RetryWithDelay.cs Outdated Show resolved Hide resolved
HttpResponseMessage response = await this.HTTPClient.PostAsync(url, content);

HttpResponseMessage response =
await this._retryPolicy.ExecuteWithRetryAsync(async () => await this.HTTPClient.PostAsync(url, content, cancellationToken), this.Log,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Retries through a DelegatingHandler would be more elegant solution for the problem because would decouple the code from the retry logic which would make this class a little bit simpler.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, agreed. I think I'll go ahead and take a stab at doing it that way instead for compare/contrast purposes.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've taken a stab at this here: #2

The DelegatingHandler cannot be shared, so this also uses a Factory interface to aid with creation of DelegatingHandler instances.

… that implement it, allowing the caller to customize the retry logic for HTTP requests to AI services. The commit also modifies the OpenAI and AzureAI clients and services to accept and use the retry policy when making requests. Additionally, the commit adds a cancellation token parameter to the CompleteAsync method of the text completion clients and services, and passes it to the retry policy. The commit also updates the KernelConfig class, the kernel syntax examples, and the samples to use the new interface and classes. Finally, the commit adds unit tests and integration tests for the new functionality, and removes some unused or deprecated code.
@lemillermicrosoft
Copy link
Owner Author

Closing PR. Will be publishing #2 against the main repo.

dehoward pushed a commit that referenced this pull request Jun 1, 2023
Add Memory to the list and update diagrams with more recent information.
lemillermicrosoft pushed a commit that referenced this pull request Nov 13, 2023
…soft#3415)

### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants