Fix config priority.

This commit is contained in:
hchen 2023-12-15 11:54:44 -06:00
parent a7d346c1e9
commit 1c03789a38
6 changed files with 45 additions and 42 deletions

View file

@ -4,5 +4,5 @@ namespace BotSharp.Abstraction.Instructs;
public interface IInstructService
{
Task<InstructResult> Execute(string agentId, RoleDialogModel message, string? templateName = null);
Task<InstructResult> Execute(string agentId, RoleDialogModel message, string? templateName = null, string? instruction = null);
}

View file

@ -1,3 +1,4 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.MLTasks.Settings;
@ -5,67 +6,58 @@ namespace BotSharp.Core.Infrastructures;
public class CompletionProvider
{
public static object GetCompletion(IServiceProvider services, string? provider = null, string? model = null)
public static object GetCompletion(IServiceProvider services,
string? provider = null,
string? model = null,
AgentLlmConfig? agentConfig = null)
{
var state = services.GetRequiredService<IConversationStateService>();
var agentSetting = services.GetRequiredService<AgentSettings>();
if (string.IsNullOrEmpty(provider))
{
provider = state.GetState("provider", "azure-openai");
provider = agentConfig?.Provider ?? agentSetting.LlmConfig?.Provider;
provider = state.GetState("provider", provider ?? "azure-openai");
}
if (string.IsNullOrEmpty(model))
{
model = state.GetState("model", "gpt-35-turbo-instruct");
model = agentConfig?.Model ?? agentSetting.LlmConfig?.Model;
model = state.GetState("model", model ?? "gpt-35-turbo-4k");
}
var settingsService = services.GetRequiredService<ILlmProviderSettingService>();
var settings = settingsService.GetSetting(provider, model);
if(settings.Type == LlmModelType.Text)
if (settings.Type == LlmModelType.Text)
{
var completions = services.GetServices<ITextCompletion>();
var completer = completions.FirstOrDefault(x => x.Provider == provider);
if (completer == null)
{
var logger = services.GetRequiredService<ILogger<CompletionProvider>>();
logger.LogError($"Can't resolve text completion provider by {provider}");
}
completer.SetModelName(model);
return completer;
return GetTextCompletion(services, provider: provider, model: model);
}
else
{
var completions = services.GetServices<IChatCompletion>();
var completer = completions.FirstOrDefault(x => x.Provider == provider);
if (completer == null)
{
var logger = services.GetRequiredService<ILogger<CompletionProvider>>();
logger.LogError($"Can't resolve chat completion provider by {provider}");
}
completer.SetModelName(model);
return completer;
return GetChatCompletion(services, provider: provider, model: model);
}
}
public static IChatCompletion GetChatCompletion(IServiceProvider services, string? provider = null, string? model = null)
public static IChatCompletion GetChatCompletion(IServiceProvider services,
string? provider = null,
string? model = null,
AgentLlmConfig? agentConfig = null)
{
var completions = services.GetServices<IChatCompletion>();
var agentSetting = services.GetRequiredService<AgentSettings>();
var state = services.GetRequiredService<IConversationStateService>();
if (string.IsNullOrEmpty(provider))
{
provider = state.GetState("provider", "azure-openai");
provider = agentConfig?.Provider ?? agentSetting.LlmConfig?.Provider;
provider = state.GetState("provider", provider ?? "azure-openai");
}
if (string.IsNullOrEmpty(model))
{
model = state.GetState("model", "gpt-35-turbo-4k");
model = agentConfig?.Model ?? agentSetting.LlmConfig?.Model;
model = state.GetState("model", model ?? "gpt-35-turbo-4k");
}
var completer = completions.FirstOrDefault(x => x.Provider == provider);
@ -80,20 +72,25 @@ public class CompletionProvider
return completer;
}
public static ITextCompletion GetTextCompletion(IServiceProvider services, string? provider = null, string? model = null)
public static ITextCompletion GetTextCompletion(IServiceProvider services,
string? provider = null,
string? model = null,
AgentLlmConfig? agentConfig = null)
{
var completions = services.GetServices<ITextCompletion>();
var agentSetting = services.GetRequiredService<AgentSettings>();
var state = services.GetRequiredService<IConversationStateService>();
if (string.IsNullOrEmpty(provider))
{
provider = state.GetState("provider", "azure-openai");
provider = agentConfig?.Provider ?? agentSetting.LlmConfig?.Provider;
provider = state.GetState("provider", provider ?? "azure-openai");
}
if (string.IsNullOrEmpty(model))
{
model = state.GetState("model", "gpt-35-turbo-instruct");
model = agentConfig?.Model ?? agentSetting.LlmConfig?.Model;
model = state.GetState("model", model ?? "gpt-35-turbo-instruct");
}
var completer = completions.FirstOrDefault(x => x.Provider == provider);

View file

@ -16,7 +16,7 @@ public partial class InstructService : IInstructService
_logger = logger;
}
public async Task<InstructResult> Execute(string agentId, RoleDialogModel message, string? templateName = null)
public async Task<InstructResult> Execute(string agentId, RoleDialogModel message, string? templateName = null, string? instruction = null)
{
var agentService = _services.GetRequiredService<IAgentService>();
Agent agent = await agentService.LoadAgent(agentId);
@ -48,7 +48,8 @@ public partial class InstructService : IInstructService
agentService.RenderedInstruction(agent) :
agentService.RenderedTemplate(agent, templateName);
var completer = CompletionProvider.GetCompletion(_services);
var completer = CompletionProvider.GetCompletion(_services,
agentConfig: agent.LlmConfig);
var response = new InstructResult
{
MessageId = message.MessageId
@ -63,7 +64,8 @@ public partial class InstructService : IInstructService
var result = chatCompleter.GetChatCompletions(new Agent
{
Id = agentId,
Name = agent.Name
Name = agent.Name,
Instruction = instruction
}, new List<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, prompt)

View file

@ -19,12 +19,10 @@ public partial class RoutingService
}
var agentService = _services.GetRequiredService<IAgentService>();
var agentSetting = _services.GetRequiredService<AgentSettings>();
var agent = await agentService.LoadAgent(agentId);
var chatCompletion = CompletionProvider.GetChatCompletion(_services,
provider: agent?.LlmConfig?.Provider ?? agentSetting.LlmConfig.Provider,
model: agent.LlmConfig?.Model ?? agentSetting.LlmConfig.Model);
agentConfig: agent.LlmConfig);
var message = dialogs.Last();
var response = chatCompletion.GetChatCompletions(agent, dialogs);

View file

@ -25,12 +25,14 @@ public class InstructModeController : ControllerBase, IApiAdapter
input.States.ForEach(x => state.SetState(x.Split('=')[0], x.Split('=')[1]));
state.SetState("provider", input.Provider)
.SetState("model", input.Model)
.SetState("instruction", input.Instruction)
.SetState("input_text", input.Text);
var instructor = _services.GetRequiredService<IInstructService>();
var result = await instructor.Execute(agentId,
new RoleDialogModel(AgentRole.User, input.Text),
templateName: input.Template);
templateName: input.Template,
instruction: input.Instruction);
result.States = state.GetStates();

View file

@ -4,6 +4,10 @@ namespace BotSharp.OpenAPI.ViewModels.Instructs;
public class InstructMessageModel : IncomingMessageModel
{
/// <summary>
/// System prompt
/// </summary>
public string? Instruction { get; set; }
public override string Channel { get; set; } = ConversationChannel.OpenAPI;
public string? Template { get; set; }
}