Merge pull request #106 from hchen2020/master

Enhance Agent Rounter mechanism
This commit is contained in:
Haiping 2023-08-18 19:30:29 -05:00 committed by GitHub
commit c1da96021a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 127 additions and 70 deletions

3
.gitignore vendored
View file

@ -283,6 +283,7 @@ __pycache__/
*.btm.cs
*.odx.cs
*.xsd.cs
conversations
/docs/_build
*.bin
/src/WebStarter/data/conversations

View file

@ -43,6 +43,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.PaddleSharp
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.MetaMessenger", "src\Plugins\BotSharp.Plugin.MetaMessenger\BotSharp.Plugin.MetaMessenger.csproj", "{8300F66D-9EB8-438A-BF0F-70DFBE07D9DE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.OpenAPI", "src\Infrastructure\BotSharp.OpenAPI\BotSharp.OpenAPI.csproj", "{7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -147,6 +149,14 @@ Global
{8300F66D-9EB8-438A-BF0F-70DFBE07D9DE}.Release|Any CPU.Build.0 = Release|Any CPU
{8300F66D-9EB8-438A-BF0F-70DFBE07D9DE}.Release|x64.ActiveCfg = Release|Any CPU
{8300F66D-9EB8-438A-BF0F-70DFBE07D9DE}.Release|x64.Build.0 = Release|Any CPU
{7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7}.Debug|x64.ActiveCfg = Debug|Any CPU
{7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7}.Debug|x64.Build.0 = Debug|Any CPU
{7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7}.Release|Any CPU.Build.0 = Release|Any CPU
{7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7}.Release|x64.ActiveCfg = Release|Any CPU
{7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -168,6 +178,7 @@ Global
{B797EC3E-B5B9-4047-A6C8-6D330B7C6763} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C}
{0308FBFD-57EB-4709-9AE4-A80D516AD84D} = {B797EC3E-B5B9-4047-A6C8-6D330B7C6763}
{8300F66D-9EB8-438A-BF0F-70DFBE07D9DE} = {64264688-0F5C-4AB0-8F2B-B59B717CCE00}
{7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}

View file

@ -5,6 +5,6 @@ namespace BotSharp.Abstraction.Conversations;
public interface IConversationStorage
{
void InitStorage(string conversationId);
void Append(string conversationId, Agent agent, RoleDialogModel dialog);
void Append(string conversationId, string agentId, RoleDialogModel dialog);
List<RoleDialogModel> GetDialogs(string conversationId);
}

View file

@ -1,7 +1,6 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations.Models;
using Fluid;
using Microsoft.AspNetCore.Mvc;
namespace BotSharp.Core.Agents.Services;

View file

@ -40,20 +40,11 @@ public partial class AgentService
{
profile.Samples = File.ReadAllText(samplesFile);
}
else
{
_logger.LogWarning($"Can't find samples file from {samplesFile}");
}
var functionsFile = Path.Combine(dir, "functions.json");
if (File.Exists(functionsFile))
{
profile.Functions = File.ReadAllText(functionsFile);
}
else
{
_logger.LogInformation($"Can't find functions file from {functionsFile}");
}
return profile;
}

View file

@ -77,9 +77,7 @@
<PackageReference Include="EntityFrameworkCore.BootKit" Version="6.2.1" />
<PackageReference Include="Fluid.Core" Version="2.4.0" />
<PackageReference Include="LLamaSharp" Version="0.4.2-preview" />
<PackageReference Include="PdfPig" Version="0.1.8" />
<PackageReference Include="TensorFlow.Keras" Version="0.11.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
</ItemGroup>
<ItemGroup>

View file

@ -1,3 +1,5 @@
using BotSharp.Abstraction.Functions;
using BotSharp.Core.Functions;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
@ -35,6 +37,8 @@ public static class BotSharpServiceCollectionExtensions
services.AddScoped<IAgentRouting, AgentRouter>();
services.AddScoped<IFunctionCallback, GoToRouterFn>();
return services;
}

View file

@ -1,5 +1,3 @@
using Amazon.SecurityToken.Model.Internal.MarshallTransformations;
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Functions;
@ -98,8 +96,10 @@ public class ConversationService : IConversationService
var router = _services.GetRequiredService<IAgentRouting>();
var agent = await router.LoadCurrentAgent();
_logger.LogInformation($"[{agent.Name}] {lastDialog.Role}: {lastDialog.Content}");
lastDialog.CurrentAgentId = agent.Id;
_storage.Append(conversationId, agent, lastDialog);
_storage.Append(conversationId, agent.Id, lastDialog);
var wholeDialogs = GetDialogHistory(conversationId);
@ -149,28 +149,29 @@ public class ConversationService : IConversationService
await HandleAssistantMessage(msg, onMessageReceived);
// Add to dialog history
_storage.Append(conversationId, agent, msg);
_storage.Append(conversationId, agent.Id, msg);
}, async fn =>
{
var preAgentId = agent.Id;
await HandleFunctionMessage(fn, onFunctionExecuting);
fn.Content = fn.ExecutionResult;
// Agent has been transferred
if (fn.CurrentAgentId != preAgentId)
{
var agentSettings = _services.GetRequiredService<AgentSettings>();
var agentService = _services.GetRequiredService<IAgentService>();
agent = await agentService.LoadAgent(fn.CurrentAgentId);
// Set state to make next conversation will go to this agent directly
var state = _services.GetRequiredService<IConversationStateService>();
state.SetState("agentId", fn.CurrentAgentId);
// var state = _services.GetRequiredService<IConversationStateService>();
// state.SetState("agentId", fn.CurrentAgentId);
}
fn.Content = fn.ExecutionResult;
// Add to dialog history
_storage.Append(conversationId, agent, fn);
_storage.Append(conversationId, preAgentId, fn);
// After function is executed, pass the result to LLM to get a natural response
wholeDialogs.Add(fn);

View file

@ -1,5 +1,4 @@
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations.Models;
using System.IO;
@ -8,12 +7,14 @@ namespace BotSharp.Core.Conversations.Services;
public class ConversationStorage : IConversationStorage
{
private readonly MyDatabaseSettings _dbSettings;
public ConversationStorage(MyDatabaseSettings dbSettings)
private readonly IServiceProvider _services;
public ConversationStorage(MyDatabaseSettings dbSettings, IServiceProvider services)
{
_dbSettings = dbSettings;
_services = services;
}
public void Append(string conversationId, Agent agent, RoleDialogModel dialog)
public void Append(string conversationId, string agentId, RoleDialogModel dialog)
{
var conversationFile = GetStorageFile(conversationId);
var sb = new StringBuilder();
@ -22,7 +23,7 @@ public class ConversationStorage : IConversationStorage
{
var args = dialog.FunctionArgs.Replace("\r", " ").Replace("\n", " ").Trim();
sb.AppendLine($"{dialog.CreatedAt}|{dialog.Role}|{agent.Name}|{dialog.FunctionName}|{args}");
sb.AppendLine($"{dialog.CreatedAt}|{dialog.Role}|{agentId}|{dialog.FunctionName}|{args}");
var content = dialog.ExecutionResult.Replace("\r", " ").Replace("\n", " ").Trim();
if (string.IsNullOrEmpty(content))
@ -31,19 +32,12 @@ public class ConversationStorage : IConversationStorage
}
sb.AppendLine($" - {content}");
}
else if (dialog.Role == AgentRole.Assistant)
{
sb.AppendLine($"{dialog.CreatedAt}|{dialog.Role}|{agent.Name}||");
var content = dialog.Content.Replace("\r", " ").Replace("\n", " ").Trim();
if (string.IsNullOrEmpty(content))
{
return;
}
sb.AppendLine($" - {content}");
}
else
{
sb.AppendLine($"{dialog.CreatedAt}|{dialog.Role}|{agent.Name}||");
var db = _services.GetRequiredService<IBotSharpRepository>();
var agent = db.Agent.First(x => x.Id == agentId);
sb.AppendLine($"{dialog.CreatedAt}|{dialog.Role}|{agentId}|{agent.Name}|");
var content = dialog.Content.Replace("\r", " ").Replace("\n", " ").Trim();
if (string.IsNullOrEmpty(content))
{

View file

@ -1,6 +0,0 @@
namespace BotSharp.Core.Conversations.ViewModels;
public class ConversationCreationModel
{
}

View file

@ -0,0 +1,27 @@
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Functions;
using BotSharp.Abstraction.Functions.Models;
namespace BotSharp.Core.Functions;
public class GoToRouterFn : IFunctionCallback
{
public string Name => "go_to_router";
private readonly IServiceProvider _services;
public GoToRouterFn(IServiceProvider services)
{
_services = services;
}
public async Task<bool> Execute(RoleDialogModel message)
{
var settings = _services.GetRequiredService<AgentSettings>();
message.CurrentAgentId = settings.RouterId;
var result = new FunctionExecutionValidationResult("true");
message.ExecutionResult = JsonSerializer.Serialize(result);
return true;
}
}

View file

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<VersionPrefix>0.8.0</VersionPrefix>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="PdfPig" Version="0.1.8" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
</ItemGroup>
</Project>

View file

@ -1,9 +1,7 @@
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Core.Agents.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using BotSharp.OpenAPI.ViewModels.Agents;
namespace BotSharp.Core.Agents;
namespace BotSharp.OpenAPI.Controllers;
[Authorize]
[ApiController]

View file

@ -1,10 +1,10 @@
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Core.Conversations.ViewModels;
using BotSharp.OpenAPI.ViewModels.Conversations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace BotSharp.Core.Conversations;
namespace BotSharp.OpenAPI.Controllers;
[Authorize]
[ApiController]

View file

@ -1,13 +1,10 @@
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Knowledges.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using UglyToad.PdfPig.Content;
using UglyToad.PdfPig;
namespace BotSharp.Core.Plugins.Knowledges;
namespace BotSharp.OpenAPI.Controllers;
[Authorize]
[ApiController]

View file

@ -1,10 +1,8 @@
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Users.Models;
using BotSharp.Core.Users.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using BotSharp.OpenAPI.ViewModels.Users;
namespace BotSharp.Core.Users;
namespace BotSharp.OpenAPI.Controllers;
[Authorize]
[ApiController]

View file

@ -0,0 +1,18 @@
global using System;
global using System.Collections.Generic;
global using System.Text;
global using System.Threading.Tasks;
global using System.Linq;
global using System.Text.Json;
global using Microsoft.AspNetCore.Authorization;
global using Microsoft.AspNetCore.Mvc;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Logging;
global using BotSharp.Abstraction.Plugins;
global using BotSharp.Abstraction.Agents;
global using BotSharp.Abstraction.Conversations;
global using BotSharp.Abstraction.Knowledges;
global using BotSharp.Abstraction.Users;
global using BotSharp.Abstraction.Utilities;
global using BotSharp.Abstraction.Agents.Settings;
global using BotSharp.Abstraction.Conversations.Settings;

View file

@ -1,6 +1,6 @@
using BotSharp.Abstraction.Agents.Models;
namespace BotSharp.Core.Agents.ViewModels;
namespace BotSharp.OpenAPI.ViewModels.Agents;
public class AgentCreationModel
{
@ -9,10 +9,10 @@ public class AgentCreationModel
public Agent ToAgent()
{
return new Agent
{
Name = Name,
Description = Description
return new Agent
{
Name = Name,
Description = Description
};
}
}

View file

@ -1,6 +1,6 @@
using BotSharp.Abstraction.Agents.Models;
namespace BotSharp.Core.Agents.ViewModels;
namespace BotSharp.OpenAPI.ViewModels.Agents;
public class AgentUpdateModel
{

View file

@ -1,6 +1,6 @@
using BotSharp.Abstraction.Agents.Models;
namespace BotSharp.Core.Agents.ViewModels;
namespace BotSharp.OpenAPI.ViewModels.Agents;
public class AgentViewModel
{

View file

@ -0,0 +1,6 @@
namespace BotSharp.OpenAPI.ViewModels.Conversations;
public class ConversationCreationModel
{
}

View file

@ -1,6 +1,6 @@
using BotSharp.Abstraction.Conversations.Models;
namespace BotSharp.Core.Conversations.ViewModels;
namespace BotSharp.OpenAPI.ViewModels.Conversations;
public class ConversationViewModel
{

View file

@ -1,4 +1,4 @@
namespace BotSharp.Core.Conversations.ViewModels;
namespace BotSharp.OpenAPI.ViewModels.Conversations;
public class MessageResponseModel
{

View file

@ -1,4 +1,4 @@
namespace BotSharp.Core.Conversations.ViewModels;
namespace BotSharp.OpenAPI.ViewModels.Conversations;
public class NewMessageModel
{

View file

@ -1,6 +1,6 @@
using BotSharp.Abstraction.Users.Models;
namespace BotSharp.Core.Users.ViewModels;
namespace BotSharp.OpenAPI.ViewModels.Users;
public class UserCreationModel
{

View file

@ -1,6 +1,6 @@
using BotSharp.Abstraction.Users.Models;
namespace BotSharp.Core.Users.ViewModels;
namespace BotSharp.OpenAPI.ViewModels.Users;
public class UserViewModel
{

View file

@ -35,6 +35,7 @@
<ItemGroup>
<ProjectReference Include="..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
<ProjectReference Include="..\Infrastructure\BotSharp.OpenAPI\BotSharp.OpenAPI.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.AzureOpenAI\BotSharp.Plugin.AzureOpenAI.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.ChatbotUI\BotSharp.Plugin.ChatbotUI.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.HuggingFace\BotSharp.Plugin.HuggingFace.csproj" />