add processor

This commit is contained in:
Jicheng Lu 2024-10-10 12:55:44 -05:00
parent b81e162bf4
commit 76aa397e99
10 changed files with 87 additions and 12 deletions

View file

@ -40,7 +40,8 @@ public interface IKnowledgeService
/// <param name="contents"></param>
/// <param name="refData"></param>
/// <returns></returns>
Task<bool> ImportDocumentContentToKnowledge(string collectionName, string fileName, string fileSource, IEnumerable<string> contents, DocMetaRefData? refData = null);
Task<bool> ImportDocumentContentToKnowledge(string collectionName, string fileName, string fileSource, IEnumerable<string> contents,
DocMetaRefData? refData = null, Dictionary<string, object>? payload = null);
/// <summary>
/// Delete one document and its related knowledge in the collection
/// </summary>

View file

@ -0,0 +1,12 @@
using BotSharp.Abstraction.Processors.Models;
namespace BotSharp.Abstraction.Processors;
public interface IBaseProcessor<TInput, TOutput> where TInput : LlmBaseRequest where TOutput : class
{
string Provider { get; }
string Name => string.Empty;
int Order { get; }
Task<TOutput> Execute(TInput input);
}

View file

@ -0,0 +1,9 @@
namespace BotSharp.Abstraction.Processors.Models;
public class LlmBaseRequest
{
public string Provider { get; set; }
public string Model { get; set; }
public string? AgentId { get; set; }
public string? TemplateName { get; set; }
}

View file

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(TargetFramework)</TargetFramework>

View file

@ -8,6 +8,7 @@ using BotSharp.Abstraction.Messaging.JsonConverters;
using BotSharp.Abstraction.Users.Settings;
using BotSharp.Abstraction.Interpreters.Settings;
using BotSharp.Abstraction.Infrastructures;
using BotSharp.Core.Processors;
namespace BotSharp.Core;
@ -23,6 +24,7 @@ public static class BotSharpCoreExtensions
services.AddScoped<ISettingService, SettingService>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<ProcessorFactory>();
services.AddSingleton<DistributedLocker>();

View file

@ -46,6 +46,7 @@ public partial class ConversationService
}
// Before chat completion hook
hooks = ReOrderConversationHooks(hooks);
foreach (var hook in hooks)
{
hook.SetAgent(agent)
@ -173,4 +174,18 @@ public partial class ConversationService
// Add to dialog history
_storage.Append(_conversationId, response);
}
private List<IConversationHook> ReOrderConversationHooks(List<IConversationHook> hooks)
{
var target = "ChatHubConversationHook";
var chathub = hooks.FirstOrDefault(x => x.GetType().Name == target);
var otherHooks = hooks.Where(x => x.GetType().Name != target).ToList();
if (chathub != null)
{
var newHooks = new List<IConversationHook> { chathub }.Concat(otherHooks);
return newHooks.ToList();
}
return hooks;
}
}

View file

@ -0,0 +1,29 @@
using BotSharp.Abstraction.Processors;
using BotSharp.Abstraction.Processors.Models;
namespace BotSharp.Core.Processors;
public class ProcessorFactory
{
private readonly IServiceProvider _services;
public ProcessorFactory(IServiceProvider services)
{
_services = services;
}
public IEnumerable<IBaseProcessor<TInput, TOutput>> Create<TInput, TOutput>(string provider)
where TInput : LlmBaseRequest where TOutput : class
{
var processors = _services.GetServices<IBaseProcessor<TInput, TOutput>>();
processors = processors.Where(x => x.Provider == provider);
return processors.OrderBy(x => x.Order);
}
public IBaseProcessor<TInput, TOutput>? Create<TInput, TOutput>(string provider, string name)
where TInput : LlmBaseRequest where TOutput : class
{
var processors = _services.GetServices<IBaseProcessor<TInput, TOutput>>();
return processors.FirstOrDefault(x => x.Provider == provider && x.Name == name);
}
}

View file

@ -54,6 +54,7 @@ public class ChatHubConversationHook : ConversationHookBase
{
ConversationId = conv.ConversationId,
MessageId = message.MessageId,
Payload = message.Payload,
Text = !string.IsNullOrEmpty(message.SecondaryContent) ? message.SecondaryContent : message.Content,
Sender = UserViewModel.FromUser(sender)
};

View file

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(TargetFramework)</TargetFramework>

View file

@ -112,7 +112,7 @@ public partial class KnowledgeService
public async Task<bool> ImportDocumentContentToKnowledge(string collectionName, string fileName, string fileSource,
IEnumerable<string> contents, DocMetaRefData? refData = null)
IEnumerable<string> contents, DocMetaRefData? refData = null, Dictionary<string, object>? payload = null)
{
if (string.IsNullOrWhiteSpace(collectionName)
|| string.IsNullOrWhiteSpace(fileName)
@ -132,20 +132,26 @@ public partial class KnowledgeService
var fileId = Guid.NewGuid();
var contentType = FileUtility.GetFileContentType(fileName);
var payload = new Dictionary<string, object>()
var innerPayload = new Dictionary<string, object>();
if (payload != null)
{
{ KnowledgePayloadName.DataSource, VectorDataSource.File },
{ KnowledgePayloadName.FileId, fileId.ToString() },
{ KnowledgePayloadName.FileName, fileName },
{ KnowledgePayloadName.FileSource, fileSource }
};
foreach (var item in payload)
{
innerPayload[item.Key] = item.Value;
}
}
innerPayload[KnowledgePayloadName.DataSource] = VectorDataSource.File;
innerPayload[KnowledgePayloadName.FileId] = fileId.ToString();
innerPayload[KnowledgePayloadName.FileName] = fileName;
innerPayload[KnowledgePayloadName.FileSource] = fileSource;
if (!string.IsNullOrWhiteSpace(refData?.Url))
{
payload[KnowledgePayloadName.FileUrl] = refData.Url;
innerPayload[KnowledgePayloadName.FileUrl] = refData.Url;
}
var dataIds = await SaveToVectorDb(collectionName, contents, payload);
var dataIds = await SaveToVectorDb(collectionName, contents, innerPayload);
db.SaveKnolwedgeBaseFileMeta(new KnowledgeDocMetaData
{
Collection = collectionName,