Add RouterController.
This commit is contained in:
parent
ccca262c6d
commit
627f7fa771
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
docs/static/logos/BotSharp.min.png
vendored
BIN
docs/static/logos/BotSharp.min.png
vendored
Binary file not shown.
|
Before Width: | Height: | Size: 6.3 KiB |
BIN
docs/static/logos/BotSharp.png
vendored
BIN
docs/static/logos/BotSharp.png
vendored
Binary file not shown.
|
Before Width: | Height: | Size: 12 KiB |
BIN
docs/static/logos/favicon.ico
vendored
BIN
docs/static/logos/favicon.ico
vendored
Binary file not shown.
|
Before Width: | Height: | Size: 4.2 KiB |
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
namespace BotSharp.Abstraction.Knowledges.Models;
|
||||
|
||||
public class KnowledgeCreationModel
|
||||
{
|
||||
public string Content { get; set; } = string.Empty;
|
||||
}
|
||||
|
|
@ -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; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ using BotSharp.Abstraction.Routing.Models;
|
|||
using BotSharp.Abstraction.Repositories.Filters;
|
||||
using BotSharp.Abstraction.Repositories.Models;
|
||||
using BotSharp.Abstraction.Routing.Settings;
|
||||
using BotSharp.Abstraction.Evaluations.Settings;
|
||||
|
||||
namespace BotSharp.Core.Repository;
|
||||
|
||||
|
|
@ -552,6 +553,14 @@ public class FileRepository : IBotSharpRepository
|
|||
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));
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
using BotSharp.Abstraction.Routing.Settings;
|
||||
|
||||
namespace BotSharp.OpenAPI.Controllers;
|
||||
|
||||
[Authorize]
|
||||
|
|
@ -13,6 +15,13 @@ public class AgentController : ControllerBase
|
|||
_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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -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>();
|
||||
|
|
@ -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)
|
||||
{
|
||||
|
|
@ -25,6 +27,33 @@ public class KnowledgeController : ControllerBase
|
|||
});
|
||||
}
|
||||
|
||||
[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)
|
||||
{
|
||||
|
|
@ -34,9 +63,11 @@ public class KnowledgeController : ControllerBase
|
|||
|
||||
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.
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -15,6 +15,29 @@ public class KnowledgeService : IKnowledgeService
|
|||
_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;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
using Microsoft.AspNetCore.Http;
|
||||
using UglyToad.PdfPig;
|
||||
using UglyToad.PdfPig.Content;
|
||||
|
||||
|
|
@ -6,25 +5,13 @@ 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())
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
@ -470,6 +471,14 @@ public class MongoRepository : IBotSharpRepository
|
|||
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));
|
||||
|
|
|
|||
|
|
@ -12,10 +12,8 @@ using Sdcb.PaddleOCR;
|
|||
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;
|
||||
|
|
@ -33,9 +31,9 @@ public class Pdf2TextConverter : IPdf2TextConverter
|
|||
_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();
|
||||
}
|
||||
|
||||
|
|
@ -115,17 +113,10 @@ public class Pdf2TextConverter : IPdf2TextConverter
|
|||
};
|
||||
}
|
||||
|
||||
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);
|
||||
/*
|
||||
|
|
|
|||
|
|
@ -39,7 +39,8 @@ builder.Services.AddBotSharpLogger(builder.Configuration);
|
|||
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());
|
||||
|
|
|
|||
|
|
@ -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">
|
||||
|
|
|
|||
|
|
@ -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": {
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Reference in a new issue