Skip to content

Commit

Permalink
Add RouterController.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oceania2018 committed Dec 30, 2023
1 parent ccca262 commit 627f7fa
Show file tree
Hide file tree
Showing 24 changed files with 154 additions and 42 deletions.
Binary file removed docs/assets/BotSharp.min.psd
Binary file not shown.
Binary file removed docs/assets/BotSharp.psd
Binary file not shown.
Binary file removed docs/assets/front-cover.psd
Binary file not shown.
Binary file removed docs/static/logos/BotSharp.min.png
Binary file not shown.
Binary file removed docs/static/logos/BotSharp.png
Binary file not shown.
Binary file removed docs/static/logos/favicon.ico
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace BotSharp.Abstraction.Knowledges;
public interface IKnowledgeService
{
Task Feed(KnowledgeFeedModel knowledge);
Task EmbedKnowledge(KnowledgeCreationModel knowledge);
Task<string> GetKnowledges(KnowledgeRetrievalModel retrievalModel);
Task<List<RetrievedResult>> GetAnswer(KnowledgeRetrievalModel retrievalModel);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace BotSharp.Abstraction.Knowledges
{
public interface IPdf2TextConverter
{
Task<string> ConvertPdfToText(IFormFile formFile, int? startPageNum, int? endPageNum);
Task<string> ConvertPdfToText(string filePath, int? startPageNum, int? endPageNum);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BotSharp.Abstraction.Knowledges.Models;

public class KnowledgeCreationModel
{
public string Content { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public class AgentFilter
public bool? AllowRouting { get; set; }
public bool? IsPublic { get; set; }
public bool? IsRouter { get; set; }
public bool? IsEvaluator { get; set; }
public List<string>? AgentIds { get; set; }
}
9 changes: 9 additions & 0 deletions src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Repositories.Models;
using BotSharp.Abstraction.Routing.Settings;
using BotSharp.Abstraction.Evaluations.Settings;

namespace BotSharp.Core.Repository;

Expand Down Expand Up @@ -552,6 +553,14 @@ public List<Agent> GetAgents(AgentFilter filter)
query.Where(x => x.Id != route.AgentId);
}

if (filter.IsEvaluator.HasValue)
{
var evaluate = _services.GetRequiredService<EvaluatorSetting>();
query = filter.IsEvaluator.Value ?
query.Where(x => x.Id == evaluate.AgentId) :
query.Where(x => x.Id != evaluate.AgentId);
}

if (filter.AgentIds != null)
{
query = query.Where(x => filter.AgentIds.Contains(x.Id));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using BotSharp.Abstraction.Routing.Settings;

namespace BotSharp.OpenAPI.Controllers;

[Authorize]
Expand All @@ -13,6 +15,13 @@ public AgentController(IAgentService agentService, IServiceProvider services)
_services = services;
}

[HttpGet("/agent/settings")]
public AgentSettings GetSettings()
{
var settings = _services.GetRequiredService<AgentSettings>();
return settings;
}

[HttpGet("/agent/{id}")]
public async Task<AgentViewModel> GetAgent([FromRoute] string id)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.Extensions.Hosting;

namespace BotSharp.OpenAPI.Controllers;

[Authorize]
[ApiController]
public class ApplicationController : ControllerBase
{
private readonly IServiceProvider _services;
public ApplicationController(IServiceProvider services)
{
_services = services;
}

[HttpGet("/app/shutdown")]
public IActionResult Restart()
{
var app = _services.GetRequiredService<IHostApplicationLifetime>();
app.StopApplication();
return Ok();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ namespace BotSharp.OpenAPI.Controllers;

[Authorize]
[ApiController]
public class EvaluationController : ControllerBase
public class EvaluatorController : ControllerBase
{
private readonly IServiceProvider _services;
public EvaluationController(IServiceProvider services)
public EvaluatorController(IServiceProvider services)
{
_services = services;
}

[HttpPost("/evaluation/execute/{task}")]
[HttpPost("/evaluator/execute/{task}")]
public async Task<Conversation> Execute([FromRoute] string task, [FromBody] EvaluationRequest request)
{
var eval = _services.GetRequiredService<IEvaluatingService>();
return await eval.Execute(task, request);
}

[HttpPost("/evaluation/review/{conversationId}")]
[HttpPost("/evaluator/review/{conversationId}")]
public async Task<EvaluationResult> Review([FromRoute] string conversationId, [FromBody] EvaluationRequest request)
{
var eval = _services.GetRequiredService<IEvaluatingService>();
return await eval.Review(conversationId, request);
}

[HttpPost("/evaluation/evaluate/{conversationId}")]
[HttpPost("/evaluator/evaluate/{conversationId}")]
public async Task<EvaluationResult> Evaluate([FromRoute] string conversationId, [FromBody] EvaluationRequest request)
{
var eval = _services.GetRequiredService<IEvaluatingService>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
using BotSharp.Abstraction.Knowledges.Models;
using BotSharp.Abstraction.Knowledges.Settings;
using Microsoft.AspNetCore.Http;

namespace BotSharp.OpenAPI.Controllers;

[Authorize]
[ApiController]
public class KnowledgeController : ControllerBase
public class KnowledgeBaseController : ControllerBase
{
private readonly IKnowledgeService _knowledgeService;
private readonly IServiceProvider _services;

public KnowledgeController(IKnowledgeService knowledgeService, IServiceProvider services)
public KnowledgeBaseController(IKnowledgeService knowledgeService, IServiceProvider services)
{
_knowledgeService = knowledgeService;
_services = services;
}

[HttpGet("/knowledge/{agentId}")]
public async Task<List<RetrievedResult>> RetrieveKnowledge([FromRoute] string agentId, [FromQuery(Name = "q")] string question)
{
Expand All @@ -25,6 +27,33 @@ public async Task<List<RetrievedResult>> RetrieveKnowledge([FromRoute] string ag
});
}

[HttpPost("/knowledge-base/upload")]
public async Task<IActionResult> UploadKnowledge(IFormFile file, [FromQuery] int? startPageNum, [FromQuery] int? endPageNum)
{
var setttings = _services.GetRequiredService<KnowledgeBaseSettings>();
var textConverter = _services.GetServices<IPdf2TextConverter>()
.First(x => x.GetType().FullName.EndsWith(setttings.Pdf2TextConverter));

var filePath = Path.GetTempFileName();
using (var stream = System.IO.File.Create(filePath))
{
await file.CopyToAsync(stream);
}

var content = await textConverter.ConvertPdfToText(filePath, startPageNum, endPageNum);

// Process uploaded files
// Don't rely on or trust the FileName property without validation.

// Add FeedWithMetaData
await _knowledgeService.EmbedKnowledge(new KnowledgeCreationModel
{
Content = content
});

return Ok(new { count = 1, file.Length });
}

[HttpPost("/knowledge/{agentId}")]
public async Task<IActionResult> FeedKnowledge([FromRoute] string agentId, List<IFormFile> files, [FromQuery] int? startPageNum, [FromQuery] int? endPageNum, [FromQuery] bool? paddleModel)
{
Expand All @@ -34,9 +63,11 @@ public async Task<IActionResult> FeedKnowledge([FromRoute] string agentId, List<

foreach (var formFile in files)
{
var content = "";
var filePath = Path.GetTempFileName();
using var stream = System.IO.File.Create(filePath);
await formFile.CopyToAsync(stream);

content = await textConverter.ConvertPdfToText(formFile, startPageNum, endPageNum);
var content = await textConverter.ConvertPdfToText(filePath, startPageNum, endPageNum);

// Process uploaded files
// Don't rely on or trust the FileName property without validation.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using BotSharp.Abstraction.Routing.Settings;

namespace BotSharp.OpenAPI.Controllers;

[Authorize]
[ApiController]
public class RouterController : ControllerBase
{
private readonly IServiceProvider _services;
public RouterController(IServiceProvider services)
{
_services = services;
}

[HttpGet("/router/settings")]
public RoutingSettings GetSettings()
{
var settings = _services.GetRequiredService<RoutingSettings>();
return settings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,29 @@ public KnowledgeService(IServiceProvider services,
_textChopper = textChopper;
}

public async Task EmbedKnowledge(KnowledgeCreationModel knowledge)
{
var idStart = 0;
var lines = _textChopper.Chop(knowledge.Content, new ChunkOption
{
Size = 1024,
Conjunction = 32,
SplitByWord = true,
});

var db = GetVectorDb();
var textEmbedding = GetTextEmbedding();

await db.CreateCollection("shared", textEmbedding.Dimension);
foreach (var line in lines)
{
var vec = await textEmbedding.GetVectorAsync(line);
await db.Upsert("shared", idStart, vec, line);
idStart++;
Console.WriteLine($"Saved vector {idStart}/{lines.Count}: {line}\n");
}
}

public async Task Feed(KnowledgeFeedModel knowledge)
{
var idStart = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
using Microsoft.AspNetCore.Http;
using UglyToad.PdfPig;
using UglyToad.PdfPig.Content;

namespace BotSharp.Plugin.KnowledgeBase.Services;

public class PigPdf2TextConverter : IPdf2TextConverter
{
public async Task<string> ConvertPdfToText(IFormFile formFile, int? startPageNum, int? endPageNum)
public async Task<string> ConvertPdfToText(string filePath, int? startPageNum, int? endPageNum)
{
return await OpenPdfDocumentAsync(formFile, startPageNum, endPageNum);
return await OpenPdfDocumentAsync(filePath, startPageNum, endPageNum);
}

private async Task<string> OpenPdfDocumentAsync(IFormFile formFile, int? startPageNum, int? endPageNum)
private async Task<string> OpenPdfDocumentAsync(string filePath, int? startPageNum, int? endPageNum)
{
if (formFile.Length <= 0)
{
return await Task.FromResult(string.Empty);
}

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())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Evaluations.Settings;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Repositories.Models;
Expand Down Expand Up @@ -470,6 +471,14 @@ public List<Agent> GetAgents(AgentFilter filter)
query.Where(x => x.Id != route.AgentId);
}

if (filter.IsEvaluator.HasValue)
{
var evaluate = _services.GetRequiredService<EvaluatorSetting>();
query = filter.IsEvaluator.Value ?
query.Where(x => x.Id == evaluate.AgentId) :
query.Where(x => x.Id != evaluate.AgentId);
}

if (filter.AgentIds != null)
{
query = query.Where(x => filter.AgentIds.Contains(x.Id));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
using System.Threading.Tasks;
using BotSharp.Abstraction.Knowledges;
using System.Linq;
using Docnet;
using Docnet.Core.Models;
using Docnet.Core;
using Docnet.Core.Converters;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
Expand All @@ -33,9 +31,9 @@ public Pdf2TextConverter(PaddleSharpSettings paddleSharpSettings)
_paddleSharpSettings = paddleSharpSettings;
}

public async Task<string> ConvertPdfToText(IFormFile formFile, int? startPageNum, int? endPageNum)
public async Task<string> ConvertPdfToText(string filePath, int? startPageNum, int? endPageNum)
{
await ConvertPdfToLocalImagesAsync(formFile, startPageNum, endPageNum);
await ConvertPdfToLocalImagesAsync(filePath, startPageNum, endPageNum);
return await LocalImageToTextsAsync();
}

Expand Down Expand Up @@ -115,17 +113,10 @@ public void DocnetConverter(string filePath, int width = 1080, int height = 1920
};
}

private async Task ConvertPdfToLocalImagesAsync(IFormFile formFile, int? startPageNum, int? endPageNum)
private async Task ConvertPdfToLocalImagesAsync(string filePath, int? startPageNum, int? endPageNum)
{
string rootFileName;

var filePath = Path.GetTempFileName();

using (var stream = System.IO.File.Create(filePath))
{
await formFile.CopyToAsync(stream);
}

using var images = new MagickImageCollection();
// _magicReadSettings.Density = new Density((double)300);
/*
Expand Down
3 changes: 2 additions & 1 deletion src/WebStarter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
builder.Services.AddCors(options =>
{
options.AddPolicy("MyCorsPolicy",
builder => builder.WithOrigins("http://localhost:5015")
builder => builder.WithOrigins("http://localhost:5015",
"https://botsharp.scisharpstack.org")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
Expand Down
4 changes: 2 additions & 2 deletions src/WebStarter/WebStarter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="LLamaSharp.Backend.Cuda11" Version="0.6.0" />
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.11.4" />
<PackageReference Include="LLamaSharp.Backend.Cuda11" Version="0.8.1" />
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.16.0" />
</ItemGroup>

<ItemGroup Condition="$(SolutionName)==BotSharp">
Expand Down
3 changes: 2 additions & 1 deletion src/WebStarter/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@
// "TextEmbedding": "LLamaSharp.TextEmbeddingProvider",
"TextCompletion": "AzureOpenAI.Providers.TextCompletionProvider",
// "TextCompletion": "LLamaSharp.TextCompletionProvider",
"Pdf2TextConverter": "PaddleSharp.Providers.Pdf2TextConverter"
// "Pdf2TextConverter": "PaddleSharp.Providers.Pdf2TextConverter"
"Pdf2TextConverter": "PigPdf2TextConverter"
},

"PluginLoader": {
Expand Down
Binary file removed tests/Dishwasher-Whirlpool.pdf
Binary file not shown.

0 comments on commit 627f7fa

Please sign in to comment.