Skip to content

Commit 15bbd4b

Browse files
authored
Merge pull request #896 from iceljc/master
add instruction page
2 parents 8a18780 + ed8b3aa commit 15bbd4b

File tree

12 files changed

+103
-11
lines changed

12 files changed

+103
-11
lines changed

src/Infrastructure/BotSharp.Abstraction/MLTasks/ILlmProviderService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ public interface ILlmProviderService
88
List<string> GetProviders();
99
LlmModelSetting GetProviderModel(string provider, string id, bool? multiModal = null, bool? realTime = false, bool imageGenerate = false);
1010
List<LlmModelSetting> GetProviderModels(string provider);
11+
List<LlmProviderSetting> GetLlmConfigs(LlmConfigOptions? options = null);
1112
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace BotSharp.Abstraction.MLTasks.Settings;
2+
3+
public class LlmConfigOptions
4+
{
5+
public LlmModelType? Type { get; set; }
6+
public bool? MultiModal { get; set; }
7+
public bool? RealTime { get; set; }
8+
public bool? ImageGeneration { get; set; }
9+
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ namespace BotSharp.Abstraction.MLTasks.Settings;
22

33
public class LlmProviderSetting
44
{
5-
public string Provider { get; set; }
6-
= "azure-openai";
5+
public string Provider { get; set; } = "azure-openai";
76

8-
public List<LlmModelSetting> Models { get; set; }
9-
= new List<LlmModelSetting>();
7+
public List<LlmModelSetting> Models { get; set; } = [];
108

119
public override string ToString()
1210
{

src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>$(TargetFramework)</TargetFramework>

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,50 @@ public LlmModelSetting GetProviderModel(string provider, string id, bool? multiM
103103

104104
return modelSetting;
105105
}
106+
107+
108+
public List<LlmProviderSetting> GetLlmConfigs(LlmConfigOptions? options = null)
109+
{
110+
var settingService = _services.GetRequiredService<ISettingService>();
111+
var providers = settingService.Bind<List<LlmProviderSetting>>($"LlmProviders");
112+
var configs = new List<LlmProviderSetting>();
113+
114+
if (providers.IsNullOrEmpty()) return configs;
115+
116+
if (options == null) return providers ?? [];
117+
118+
foreach (var provider in providers)
119+
{
120+
var models = provider.Models ?? [];
121+
if (options.Type.HasValue)
122+
{
123+
models = models.Where(x => x.Type == options.Type.Value).ToList();
124+
}
125+
126+
if (options.MultiModal.HasValue)
127+
{
128+
models = models.Where(x => x.MultiModal == options.MultiModal.Value).ToList();
129+
}
130+
131+
if (options.ImageGeneration.HasValue)
132+
{
133+
models = models.Where(x => x.ImageGeneration == options.ImageGeneration.Value).ToList();
134+
}
135+
136+
if (options.RealTime.HasValue)
137+
{
138+
models = models.Where(x => x.RealTime == options.RealTime.Value).ToList();
139+
}
140+
141+
if (models.IsNullOrEmpty())
142+
{
143+
continue;
144+
}
145+
146+
provider.Models = models;
147+
configs.Add(provider);
148+
}
149+
150+
return configs;
151+
}
106152
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using BotSharp.Abstraction.Plugins.Models;
2+
using Microsoft.Extensions.Configuration;
3+
4+
namespace BotSharp.Core.Instructs;
5+
6+
public class InsturctionPlugin : IBotSharpPlugin
7+
{
8+
public string Id => "8189e133-819c-4505-9f82-84f793bc1be0";
9+
public string Name => "Instruction";
10+
public string Description => "Handle agent instruction request";
11+
12+
public void RegisterDI(IServiceCollection services, IConfiguration config)
13+
{
14+
15+
}
16+
17+
public bool AttachMenu(List<PluginMenuDef> menu)
18+
{
19+
var section = menu.First(x => x.Label == "Apps");
20+
menu.Add(new PluginMenuDef("Instruction", link: "page/instruction", icon: "bx bx-book-content", weight: section.Weight + 5));
21+
return true;
22+
}
23+
}

src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public async Task<InstructResult> InstructCompletion([FromRoute] string agentId,
4444
}
4545

4646
[HttpPost("/instruct/text-completion")]
47-
public async Task<string> TextCompletion([FromBody] IncomingMessageModel input)
47+
public async Task<string> TextCompletion([FromBody] IncomingInstructRequest input)
4848
{
4949
var state = _services.GetRequiredService<IConversationStateService>();
5050
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
@@ -53,23 +53,24 @@ public async Task<string> TextCompletion([FromBody] IncomingMessageModel input)
5353
.SetState("model_id", input.ModelId, source: StateSource.External);
5454

5555
var textCompletion = CompletionProvider.GetTextCompletion(_services);
56-
return await textCompletion.GetCompletion(input.Text, Guid.Empty.ToString(), Guid.NewGuid().ToString());
56+
return await textCompletion.GetCompletion(input.Text, input.AgentId ?? Guid.Empty.ToString(), Guid.NewGuid().ToString());
5757
}
5858

5959
#region Chat
6060
[HttpPost("/instruct/chat-completion")]
61-
public async Task<string> ChatCompletion([FromBody] IncomingMessageModel input)
61+
public async Task<string> ChatCompletion([FromBody] IncomingInstructRequest input)
6262
{
6363
var state = _services.GetRequiredService<IConversationStateService>();
6464
input.States.ForEach(x => state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
6565
state.SetState("provider", input.Provider, source: StateSource.External)
6666
.SetState("model", input.Model, source: StateSource.External)
6767
.SetState("model_id", input.ModelId, source: StateSource.External);
6868

69-
var textCompletion = CompletionProvider.GetChatCompletion(_services);
70-
var message = await textCompletion.GetChatCompletions(new Agent()
69+
var completion = CompletionProvider.GetChatCompletion(_services);
70+
var message = await completion.GetChatCompletions(new Agent()
7171
{
72-
Id = Guid.Empty.ToString(),
72+
Id = input.AgentId ?? Guid.Empty.ToString(),
73+
Instruction = input.Instruction
7374
}, new List<RoleDialogModel>
7475
{
7576
new RoleDialogModel(AgentRole.User, input.Text)

src/Infrastructure/BotSharp.OpenAPI/Controllers/LlmProviderController.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,11 @@ public IEnumerable<LlmModelSetting> GetLlmProviderModels([FromRoute] string prov
2727
var list = _llmProvider.GetProviderModels(provider);
2828
return list.Where(x => x.Type == LlmModelType.Chat);
2929
}
30+
31+
[HttpGet("/llm-configs")]
32+
public List<LlmProviderSetting> GetLlmConfigs([FromQuery] LlmConfigOptions options)
33+
{
34+
var configs = _llmProvider.GetLlmConfigs(options);
35+
return configs;
36+
}
3037
}

src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructMessageModel.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ public class InstructMessageModel : IncomingMessageModel
99
public override string Channel { get; set; } = ConversationChannel.OpenAPI;
1010
public string? Template { get; set; }
1111
}
12+
13+
14+
public class IncomingInstructRequest : IncomingMessageModel
15+
{
16+
public string? AgentId { get; set; }
17+
public string? Instruction { get; set; }
18+
}

0 commit comments

Comments
 (0)