Merge pull request #143 from hchen2020/master

Set default ModelName as null.
This commit is contained in:
Haiping 2023-09-14 20:21:02 -05:00 committed by GitHub
commit 3347d27a3b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 45 additions and 28 deletions

View file

@ -8,7 +8,7 @@ public interface IConversationStateService
ConversationState Load(string conversationId);
string GetState(string name, string defaultValue = "");
ConversationState GetStates();
void SetState(string name, string value);
IConversationStateService SetState(string name, string value);
void CleanState();
void Save();
}

View file

@ -12,7 +12,7 @@ public class IncomingMessageModel
/// Model name
/// </summary>
[JsonPropertyName("model")]
public virtual string ModelName { get; set; } = "gpt-3.5-turbo";
public virtual string? ModelName { get; set; } = null;
/// <summary>
/// The sampling temperature to use that controls the apparent creativity of generated completions.

View file

@ -1,6 +1,5 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Repositories;
using System.IO;
namespace BotSharp.Core.Agents.Services;
@ -17,6 +16,9 @@ public partial class AgentService
return query.ToList();
}
#if !DEBUG
[MemoryCache(10 * 60)]
#endif
public async Task<Agent> GetAgent(string id)
{
var db = _services.GetRequiredService<IBotSharpRepository>();

View file

@ -29,7 +29,7 @@ public class ConversationStateService : IConversationStateService, IDisposable
_states = new ConversationState();
}
public void SetState(string name, string value)
public IConversationStateService SetState(string name, string value)
{
var hooks = _services.GetServices<IConversationHook>();
string preValue = _states.ContainsKey(name) ? _states[name] : "";
@ -43,6 +43,8 @@ public class ConversationStateService : IConversationStateService, IDisposable
hook.OnStateChanged(name, preValue, currentValue).Wait();
}
}
return this;
}
public ConversationState Load(string conversationId)
@ -118,6 +120,12 @@ public class ConversationStateService : IConversationStateService, IDisposable
{
_states[name] = defaultValue ?? "";
}
if (_states[name] == null)
{
return defaultValue;
}
return _states[name];
}

View file

@ -4,13 +4,16 @@ namespace BotSharp.Core.Infrastructures;
public class CompletionProvider
{
public static IChatCompletion GetChatCompletion(IServiceProvider services)
public static IChatCompletion GetChatCompletion(IServiceProvider services, string? model = null)
{
var completions = services.GetServices<IChatCompletion>();
// var settings = services.GetRequiredService<ConversationSetting>();
// completions.FirstOrDefault(x => x.GetType().FullName.EndsWith(settings.ChatCompletion));
var state = services.GetRequiredService<IConversationStateService>();
var model = state.GetState("model", "gpt-3.5-turbo");
if (model == null)
{
model = state.GetState("model", "gpt-3.5-turbo");
}
return completions.FirstOrDefault(x => x.ModelName == model);
}
}

View file

@ -96,7 +96,7 @@ public class RouteToAgentFn : IFunctionCallback
{
// Add field to args
message.FunctionArgs = AppendPropertyToArgs(message.FunctionArgs, "missing_fields", missingFields);
message.ExecutionResult = $"missing some information";
message.ExecutionResult = $"missing some information: [{string.Join(',', missingFields)}]";
// Handle redirect
if (!string.IsNullOrEmpty(routingRule.RedirectTo))
@ -107,6 +107,11 @@ public class RouteToAgentFn : IFunctionCallback
// Add redirected agent
message.FunctionArgs = AppendPropertyToArgs(message.FunctionArgs, "redirect_to", agent.Name);
}
else
{
// back to router
agentId = message.CurrentAgentId;
}
}
return missingFields.Any();

View file

@ -1,4 +1,3 @@
using Aspects.Cache;
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Routing.Models;
@ -29,7 +28,9 @@ public class Router : IAgentRouting
return await agentService.LoadAgent(AgentId);
}
#if !DEBUG
[MemoryCache(10 * 60)]
#endif
public RoutingItem[] GetRoutingRecords()
{
var db = _services.GetRequiredService<IBotSharpRepository>();
@ -53,11 +54,11 @@ public class Router : IAgentRouting
public RoutingItem GetRecordByName(string name)
{
return GetRoutingRecords().First(x => x.Name.ToLower() == name.ToLower());
return GetRoutingRecords().FirstOrDefault(x => x.Name.ToLower() == name.ToLower());
}
public RoutingItem GetRecordByAgentId(string id)
{
return GetRoutingRecords().First(x => x.AgentId == id);
return GetRoutingRecords().FirstOrDefault(x => x.AgentId == id);
}
}

View file

@ -134,6 +134,11 @@ public class Simulator
private void SaveStateByArgs(JsonDocument args)
{
if (args == null)
{
return;
}
var stateService = _services.GetRequiredService<IConversationStateService>();
if (args.RootElement is JsonElement root)
{

View file

@ -44,10 +44,10 @@ public class ConversationController : ControllerBase, IApiAdapter
{
var conv = _services.GetRequiredService<IConversationService>();
conv.SetConversationId(conversationId, input.States);
conv.States.SetState("channel", input.Channel);
conv.States.SetState("model", input.ModelName);
conv.States.SetState("temperature", input.Temperature.ToString());
conv.States.SetState("sampling_factor", input.SamplingFactor.ToString());
conv.States.SetState("channel", input.Channel)
.SetState("model", input.ModelName)
.SetState("temperature", input.Temperature.ToString())
.SetState("sampling_factor", input.SamplingFactor.ToString());
var response = new MessageResponseModel();
var stackMsg = new List<RoleDialogModel>();

View file

@ -20,9 +20,9 @@ namespace BotSharp.Plugin.AzureOpenAI.Providers;
public class ChatCompletionProvider : IChatCompletion
{
private readonly AzureOpenAiSettings _settings;
private readonly IServiceProvider _services;
private readonly ILogger _logger;
protected readonly AzureOpenAiSettings _settings;
protected readonly IServiceProvider _services;
protected readonly ILogger _logger;
public virtual string ModelName => "gpt-3.5-turbo";

View file

@ -8,19 +8,12 @@ namespace BotSharp.Plugin.AzureOpenAI.Providers;
public class GPT4CompletionProvider : ChatCompletionProvider
{
private readonly AzureOpenAiSettings _settings;
private readonly IServiceProvider _services;
private readonly ILogger _logger;
public override string ModelName => "gpt-4";
public GPT4CompletionProvider(AzureOpenAiSettings settings,
ILogger<GPT4CompletionProvider> logger,
IServiceProvider services) : base(settings, logger, services)
{
_settings = settings;
_logger = logger;
_services = services;
}
protected override (OpenAIClient, string) GetClient()

View file

@ -78,8 +78,8 @@ public class ChatbotUiController : ControllerBase, IApiAdapter
conv.SetConversationId(input.ConversationId, input.States);
conv.States.SetState("model", input.ModelName);
conv.States.SetState("channel", "webchat");
conv.States.SetState("temperature", "0.5");
conv.States.SetState("sampling_factor", "0.5");
conv.States.SetState("temperature", input.Temperature.ToString());
conv.States.SetState("sampling_factor", input.SamplingFactor.ToString());
var result = await conv.SendMessage(input.AgentId,
message,