Skip to content

Commit 7077cae

Browse files
Adds LanguageModelRateLimitingPlugin. Closes #1309 (#1324)
Co-authored-by: Copilot <[email protected]>
1 parent 0f949b0 commit 7077cae

File tree

5 files changed

+484
-4
lines changed

5 files changed

+484
-4
lines changed

DevProxy.Abstractions/LanguageModel/OpenAIModels.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace DevProxy.Abstractions.LanguageModel;
88

9-
public abstract class OpenAIRequest
9+
public class OpenAIRequest
1010
{
1111
[JsonPropertyName("frequency_penalty")]
1212
public long? FrequencyPenalty { get; set; }
@@ -22,7 +22,7 @@ public abstract class OpenAIRequest
2222
public double? TopP { get; set; }
2323
}
2424

25-
public abstract class OpenAIResponse : ILanguageModelCompletionResponse
25+
public class OpenAIResponse : ILanguageModelCompletionResponse
2626
{
2727
public long Created { get; set; }
2828
public OpenAIError? Error { get; set; }
@@ -37,12 +37,12 @@ public abstract class OpenAIResponse : ILanguageModelCompletionResponse
3737
public string? RequestUrl { get; set; }
3838

3939
public string? ErrorMessage => Error?.Message;
40-
public abstract string? Response { get; }
40+
public virtual string? Response { get; }
4141

4242
public OpenAIResponse ConvertToOpenAIResponse() => this;
4343
}
4444

45-
public abstract class OpenAIResponse<TChoice> : OpenAIResponse
45+
public class OpenAIResponse<TChoice> : OpenAIResponse
4646
{
4747
public IEnumerable<TChoice>? Choices { get; set; }
4848
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using DevProxy.Abstractions.Utils;
6+
using DevProxy.Abstractions.Proxy;
7+
using DevProxy.Abstractions.Plugins;
8+
using Microsoft.Extensions.Logging;
9+
using System.Text.Json;
10+
using DevProxy.Abstractions.Models;
11+
12+
namespace DevProxy.Plugins.Behavior;
13+
14+
internal sealed class LanguageModelRateLimitingCustomResponseLoader(
15+
HttpClient httpClient,
16+
ILogger<LanguageModelRateLimitingCustomResponseLoader> logger,
17+
LanguageModelRateLimitConfiguration configuration,
18+
IProxyConfiguration proxyConfiguration) :
19+
BaseLoader(httpClient, logger, proxyConfiguration)
20+
{
21+
private readonly LanguageModelRateLimitConfiguration _configuration = configuration;
22+
23+
protected override string FilePath => _configuration.CustomResponseFile;
24+
25+
protected override void LoadData(string fileContents)
26+
{
27+
try
28+
{
29+
var response = JsonSerializer.Deserialize<MockResponseResponse>(fileContents, ProxyUtils.JsonSerializerOptions);
30+
if (response is not null)
31+
{
32+
_configuration.CustomResponse = response;
33+
}
34+
}
35+
catch (Exception ex)
36+
{
37+
Logger.LogError(ex, "An error has occurred while reading {ConfigurationFile}:", _configuration.CustomResponseFile);
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)