-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
dotnet/src/SemanticKernel/AI/OpenAI/Clients/OpenAIClientAbstract.cs
Outdated
Show resolved
Hide resolved
samples/dotnet/kernel-syntax-examples/Reliability/RetryThreeTimesWithBackoff.cs
Show resolved
Hide resolved
e8f71ac
to
99db183
Compare
4d7ee16
to
ca50027
Compare
dotnet/src/SemanticKernel/Reliability/DefaultHttpRetryPolicy.cs
Outdated
Show resolved
Hide resolved
c52796c
to
807f7d4
Compare
dotnet/src/SemanticKernel/AI/OpenAI/Clients/OpenAIClientAbstract.cs
Outdated
Show resolved
Hide resolved
samples/dotnet/kernel-syntax-examples/Reliability/RetryThreeTimesWithRetryAfterBackoff.cs
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, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
99b9f79
to
deef524
Compare
… 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.
deef524
to
cff73ce
Compare
Closing PR. Will be publishing #2 against the main repo. |
Add Memory to the list and update diagrams with more recent information.
…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 😄
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:
Contribution Checklist
dotnet format