Enhance Agent Rounter mechanism

This commit is contained in:
hchen2020 2023-08-18 19:15:50 -05:00
parent 9bbbede619
commit d9c5f5a0ad
21 changed files with 51 additions and 437 deletions

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,40 +0,0 @@
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Core.Agents.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace BotSharp.Core.Agents;
[Authorize]
[ApiController]
public class AgentController : ControllerBase, IApiAdapter
{
private readonly IAgentService _agentService;
public AgentController(IAgentService agentService)
{
_agentService = agentService;
}
[HttpPost("/agent")]
public async Task<AgentViewModel> CreateAgent(AgentCreationModel agent)
{
var createdAgent = await _agentService.CreateAgent(agent.ToAgent());
return AgentViewModel.FromAgent(createdAgent);
}
[HttpPut("/agent/{agentId}")]
public async Task UpdateAgent([FromRoute] string agentId,
[FromBody] AgentUpdateModel agent)
{
var model = agent.ToAgent();
model.Id = agentId;
await _agentService.UpdateAgent(model);
}
[HttpGet("/agents")]
public async Task<List<AgentViewModel>> GetAgents()
{
var agents = await _agentService.GetAgents();
return agents.Select(x => AgentViewModel.FromAgent(x)).ToList();
}
}

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

@ -1,18 +0,0 @@
using BotSharp.Abstraction.Agents.Models;
namespace BotSharp.Core.Agents.ViewModels;
public class AgentCreationModel
{
public string Name { get; set; }
public string Description { get; set; }
public Agent ToAgent()
{
return new Agent
{
Name = Name,
Description = Description
};
}
}

View file

@ -1,46 +0,0 @@
using BotSharp.Abstraction.Agents.Models;
namespace BotSharp.Core.Agents.ViewModels;
public class AgentUpdateModel
{
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
/// <summary>
/// Instruction
/// </summary>
public string? Instruction { get; set; }
/// <summary>
/// Samples
/// </summary>
public string? Samples { get; set; }
/// <summary>
/// Functions
/// </summary>
public string? Functions { get; set; }
public Agent ToAgent()
{
var agent = new Agent
{
Name = Name
};
if (Description != null)
agent.Description = Description;
if (Instruction != null)
agent.Instruction = Instruction;
if (Samples != null)
agent.Samples = Samples;
if (Functions != null)
agent.Functions = Functions;
return agent;
}
}

View file

@ -1,22 +0,0 @@
using BotSharp.Abstraction.Agents.Models;
namespace BotSharp.Core.Agents.ViewModels;
public class AgentViewModel
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime UpdatedDateTime { get; set; }
public static AgentViewModel FromAgent(Agent agent)
{
return new AgentViewModel
{
Id = agent.Id,
Name = agent.Name,
Description = agent.Description,
UpdatedDateTime = agent.UpdatedDateTime
};
}
}

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,62 +0,0 @@
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Core.Conversations.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace BotSharp.Core.Conversations;
[Authorize]
[ApiController]
public class ConversationController : ControllerBase, IApiAdapter
{
private readonly IServiceProvider _services;
private readonly IUserIdentity _user;
public ConversationController(IServiceProvider services,
IUserIdentity user)
{
_services = services;
_user = user;
}
[HttpPost("/conversation/{agentId}")]
public async Task<ConversationViewModel> NewConversation([FromRoute] string agentId)
{
var service = _services.GetRequiredService<IConversationService>();
var sess = new Conversation
{
UserId = _user.Id,
AgentId = agentId
};
sess = await service.NewConversation(sess);
return ConversationViewModel.FromSession(sess);
}
[HttpDelete("/conversation/{agentId}/{conversationId}")]
public async Task DeleteConversation([FromRoute] string agentId, [FromRoute] string conversationId)
{
var service = _services.GetRequiredService<IConversationService>();
}
[HttpPost("/conversation/{agentId}/{conversationId}")]
public async Task<MessageResponseModel> SendMessage([FromRoute] string agentId,
[FromRoute] string conversationId,
[FromBody] NewMessageModel input)
{
var conv = _services.GetRequiredService<IConversationService>();
var response = new MessageResponseModel();
var stackMsg = new List<RoleDialogModel>();
await conv.SendMessage(agentId, conversationId,
new RoleDialogModel("user", input.Text),
async msg =>
stackMsg.Add(msg),
async fn
=> await Task.CompletedTask);
response.Text = string.Join("\r\n", stackMsg.Select(x => x.Content));
return response;
}
}

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

@ -1,24 +0,0 @@
using BotSharp.Abstraction.Conversations.Models;
namespace BotSharp.Core.Conversations.ViewModels;
public class ConversationViewModel
{
public string Id { get; set; }
public string AgentId { get; set; }
public string Title { get; set; } = string.Empty;
public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
public static ConversationViewModel FromSession(Conversation sess)
{
return new ConversationViewModel
{
Id = sess.Id,
AgentId = sess.AgentId,
Title = sess.Title,
CreatedTime = sess.CreatedTime,
UpdatedTime = sess.UpdatedTime
};
}
}

View file

@ -1,6 +0,0 @@
namespace BotSharp.Core.Conversations.ViewModels;
public class MessageResponseModel
{
public string Text { get; set; }
}

View file

@ -1,6 +0,0 @@
namespace BotSharp.Core.Conversations.ViewModels;
public class NewMessageModel
{
public string Text { get; set; }
}

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

@ -1,80 +0,0 @@
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;
[Authorize]
[ApiController]
public class KnowledgeController : ControllerBase, IApiAdapter
{
private readonly IKnowledgeService _knowledgeService;
public KnowledgeController(IKnowledgeService knowledgeService)
{
_knowledgeService = knowledgeService;
}
[HttpGet("/knowledge/{agentId}")]
public async Task<List<RetrievedResult>> RetrieveKnowledge([FromRoute] string agentId, [FromQuery(Name = "q")] string question)
{
return await _knowledgeService.GetAnswer(new KnowledgeRetrievalModel
{
AgentId = agentId,
Question = question
});
}
[HttpPost("/knowledge/{agentId}")]
public async Task<IActionResult> FeedKnowledge([FromRoute] string agentId, List<IFormFile> files, [FromQuery] int? startPageNum, [FromQuery] int? endPageNum)
{
long size = files.Sum(f => f.Length);
foreach (var formFile in files)
{
if (formFile.Length <= 0)
{
continue;
}
var filePath = Path.GetTempFileName();
using (var stream = System.IO.File.Create(filePath))
{
await formFile.CopyToAsync(stream);
}
var document = PdfDocument.Open(filePath);
var content = "";
foreach (Page page in document.GetPages())
{
if (startPageNum.HasValue && page.Number < startPageNum.Value)
{
continue;
}
if (endPageNum.HasValue && page.Number > endPageNum.Value)
{
continue;
}
content += page.Text;
}
// Process uploaded files
// Don't rely on or trust the FileName property without validation.
await _knowledgeService.Feed(new KnowledgeFeedModel
{
AgentId = agentId,
Content = content
});
}
return Ok(new { count = files.Count, size });
}
}

View file

@ -1,46 +0,0 @@
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Users.Models;
using BotSharp.Core.Users.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace BotSharp.Core.Users;
[Authorize]
[ApiController]
public class UserController : ControllerBase, IApiAdapter
{
private readonly IUserService _userService;
public UserController(IUserService userService)
{
_userService = userService;
}
[AllowAnonymous]
[HttpPost("/token")]
public async Task<ActionResult<Token>> GetToken()
{
var authcode = Request.Headers["Authorization"].ToString();
var token = await _userService.GetToken(authcode.Split(' ')[1]);
if (token == null)
{
return Unauthorized();
}
return Ok(token);
}
[AllowAnonymous]
[HttpPost("/user")]
public async Task<UserViewModel> CreateUser(UserCreationModel user)
{
var createdUser = await _userService.CreateUser(user.ToUser());
return UserViewModel.FromUser(createdUser);
}
[HttpGet("/user/my")]
public async Task<UserViewModel> GetMyUserProfile()
{
var user = await _userService.GetMyProfile();
return UserViewModel.FromUser(user);
}
}

View file

@ -1,22 +0,0 @@
using BotSharp.Abstraction.Users.Models;
namespace BotSharp.Core.Users.ViewModels;
public class UserCreationModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public User ToUser()
{
return new User
{
FirstName = FirstName,
LastName = LastName,
Email = Email,
Password = Password
};
}
}

View file

@ -1,22 +0,0 @@
using BotSharp.Abstraction.Users.Models;
namespace BotSharp.Core.Users.ViewModels;
public class UserViewModel
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public static UserViewModel FromUser(User user)
{
return new UserViewModel
{
Id = user.Id,
FirstName = user.FirstName,
LastName = user.LastName,
Email = user.Email
};
}
}