Skip to content
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

Fix config priority. #238

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -1,71 +1,63 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.MLTasks.Settings;

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);
Expand All @@ -80,20 +72,25 @@ public static IChatCompletion GetChatCompletion(IServiceProvider services, strin
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);
Expand Down
8 changes: 5 additions & 3 deletions src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public InstructService(IServiceProvider services, ILogger<InstructService> logge
_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);
Expand Down Expand Up @@ -48,7 +48,8 @@ public async Task<InstructResult> Execute(string agentId, RoleDialogModel messag
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
Expand All @@ -63,7 +64,8 @@ public async Task<InstructResult> Execute(string agentId, RoleDialogModel messag
var result = chatCompleter.GetChatCompletions(new Agent
{
Id = agentId,
Name = agent.Name
Name = agent.Name,
Instruction = instruction
}, new List<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, prompt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ public async Task<bool> InvokeAgent(string agentId, List<RoleDialogModel> dialog
}

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ public async Task<InstructResult> InstructCompletion([FromRoute] string agentId,
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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}