Skip to content

Commit 3dad385

Browse files
committed
LLM Load balance #252
1 parent 844ed86 commit 3dad385

File tree

5 files changed

+71
-11
lines changed

5 files changed

+71
-11
lines changed

docs/llm/provider.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
# LLM Provider
22

3-
`BotSharp` can support multiple LLM providers through plug-ins.
3+
`BotSharp` can support multiple LLM providers through plug-ins, one `provider` could contain several `model` settings.
4+
```json
5+
[{
6+
"Provider": "azure-openai",
7+
"Models": [
8+
{
9+
"Id": "",
10+
"Name": "gpt-35-turbo",
11+
"Group": "",
12+
"ApiKey": "",
13+
"Endpoint": "https://gpt-35-turbo.openai.azure.com/",
14+
"Type": "chat",
15+
"PromptCost": 0.0015,
16+
"CompletionCost": 0.002
17+
},
18+
{
19+
"Name": "gpt-35-turbo-instruct",
20+
"ApiKey": "",
21+
"Endpoint": "https://gpt-35-turbo-instruct.openai.azure.com/",
22+
"Type": "text",
23+
"PromptCost": 0.0015,
24+
"CompletionCost": 0.002
25+
}
26+
]
27+
}]
28+
```
429

530
You can set the names of `Provider` and `Model` in each round of dialogue to control the LLM that should be used in the current dialogue, or you can also specify the LLM used in subsequent dialogues once during dialogue initialization.
631

@@ -10,4 +35,8 @@ You can set the names of `Provider` and `Model` in each round of dialogue to con
1035
"provider": "google-ai",
1136
"model": "palm2"
1237
}
13-
```
38+
```
39+
40+
## Load balancing
41+
42+
If you have deployed models with the same functions in multiple regions and want to establish a load balance among these regions to reduce the resource constraints of large model providers, you need to set a consistent Group value in the model configuration.

src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424

2525
<ItemGroup>
2626
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
27-
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
27+
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
2828
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
2929
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
3030
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
31-
<PackageReference Include="System.Text.Json" Version="8.0.0" />
31+
<PackageReference Include="System.Text.Json" Version="8.0.1" />
3232
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
3333
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
3434
</ItemGroup>

src/Infrastructure/BotSharp.Abstraction/MLTasks/Settings/LlmModelSetting.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,22 @@ namespace BotSharp.Abstraction.MLTasks.Settings;
22

33
public class LlmModelSetting
44
{
5-
public string Id { get; set; }
5+
/// <summary>
6+
/// Model Id, like "gpt-3.5" and "gpt-4".
7+
/// </summary>
8+
public string? Id { get; set; }
9+
10+
/// <summary>
11+
/// Deployment model name
12+
/// </summary>
613
public string Name { get; set; }
14+
15+
/// <summary>
16+
/// Deployment same functional model in a group.
17+
/// It can be used to deploy same model in different regions.
18+
/// </summary>
19+
public string? Group { get; set; }
20+
721
public string ApiKey { get; set; }
822
public string Endpoint { get; set; }
923
public LlmModelType Type { get; set; } = LlmModelType.Chat;

src/Infrastructure/BotSharp.Core/Infrastructures/LlmProviderService.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,37 @@ public List<LlmModelSetting> GetProviderModels(string provider)
4141
public LlmModelSetting? GetSetting(string provider, string model)
4242
{
4343
var settings = _services.GetRequiredService<List<LlmProviderSetting>>();
44-
var providerSetting = settings.FirstOrDefault(p => p.Provider.Equals(provider, StringComparison.CurrentCultureIgnoreCase));
44+
var providerSetting = settings.FirstOrDefault(p =>
45+
p.Provider.Equals(provider, StringComparison.CurrentCultureIgnoreCase));
4546
if (providerSetting == null)
4647
{
4748
_logger.LogError($"Can't find provider settings for {provider}");
4849
return null;
49-
}
50+
}
5051

51-
var modelSetting = providerSetting.Models.FirstOrDefault(m => m.Name.Equals(model, StringComparison.CurrentCultureIgnoreCase));
52+
var modelSetting = providerSetting.Models.FirstOrDefault(m =>
53+
m.Name.Equals(model, StringComparison.CurrentCultureIgnoreCase));
5254
if (modelSetting == null)
5355
{
5456
_logger.LogError($"Can't find model settings for {provider}.{model}");
5557
return null;
5658
}
5759

60+
// load balancing
61+
if (!string.IsNullOrEmpty(modelSetting.Group))
62+
{
63+
// find the models in the same group
64+
var models = providerSetting.Models
65+
.Where(m => !string.IsNullOrEmpty(m.Group) &&
66+
m.Group.Equals(modelSetting.Group, StringComparison.CurrentCultureIgnoreCase))
67+
.ToList();
68+
69+
// pick one model randomly
70+
var random = new Random();
71+
var index = random.Next(0, models.Count());
72+
modelSetting = models.ElementAt(index);
73+
}
74+
5875
return modelSetting;
5976
}
6077
}

tests/BotSharp.Plugin.PizzaBot/data/users/456e35c5-caf0-4d45-9084-b44a8ca717e4/user.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"userName": "haiping",
3-
"firstName": "Haiping",
4-
"lastName": "Chen",
2+
"userName": "admin",
3+
"firstName": "Administrator",
4+
"lastName": "",
55
"email": "[email protected]",
66
"salt": "55f8fafcf829479ca97e635937440fee",
77
"password": "3b459ae9988628d41f0362c499a768dd",

0 commit comments

Comments
 (0)