Skip to content

Commit dc1ed55

Browse files
authored
Merge pull request #679 from iceljc/features/add-knowledge-processpr
add processor
2 parents b81e162 + 76aa397 commit dc1ed55

File tree

10 files changed

+87
-12
lines changed

10 files changed

+87
-12
lines changed

src/Infrastructure/BotSharp.Abstraction/Knowledges/IKnowledgeService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public interface IKnowledgeService
4040
/// <param name="contents"></param>
4141
/// <param name="refData"></param>
4242
/// <returns></returns>
43-
Task<bool> ImportDocumentContentToKnowledge(string collectionName, string fileName, string fileSource, IEnumerable<string> contents, DocMetaRefData? refData = null);
43+
Task<bool> ImportDocumentContentToKnowledge(string collectionName, string fileName, string fileSource, IEnumerable<string> contents,
44+
DocMetaRefData? refData = null, Dictionary<string, object>? payload = null);
4445
/// <summary>
4546
/// Delete one document and its related knowledge in the collection
4647
/// </summary>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using BotSharp.Abstraction.Processors.Models;
2+
3+
namespace BotSharp.Abstraction.Processors;
4+
5+
public interface IBaseProcessor<TInput, TOutput> where TInput : LlmBaseRequest where TOutput : class
6+
{
7+
string Provider { get; }
8+
string Name => string.Empty;
9+
int Order { get; }
10+
11+
Task<TOutput> Execute(TInput input);
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace BotSharp.Abstraction.Processors.Models;
2+
3+
public class LlmBaseRequest
4+
{
5+
public string Provider { get; set; }
6+
public string Model { get; set; }
7+
public string? AgentId { get; set; }
8+
public string? TemplateName { get; set; }
9+
}

src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>$(TargetFramework)</TargetFramework>

src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using BotSharp.Abstraction.Users.Settings;
99
using BotSharp.Abstraction.Interpreters.Settings;
1010
using BotSharp.Abstraction.Infrastructures;
11+
using BotSharp.Core.Processors;
1112

1213
namespace BotSharp.Core;
1314

@@ -23,6 +24,7 @@ public static IServiceCollection AddBotSharpCore(this IServiceCollection service
2324

2425
services.AddScoped<ISettingService, SettingService>();
2526
services.AddScoped<IUserService, UserService>();
27+
services.AddScoped<ProcessorFactory>();
2628

2729
services.AddSingleton<DistributedLocker>();
2830

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public async Task<bool> SendMessage(string agentId,
4646
}
4747

4848
// Before chat completion hook
49+
hooks = ReOrderConversationHooks(hooks);
4950
foreach (var hook in hooks)
5051
{
5152
hook.SetAgent(agent)
@@ -173,4 +174,18 @@ await hook.OnResponseGenerated(response)
173174
// Add to dialog history
174175
_storage.Append(_conversationId, response);
175176
}
177+
178+
private List<IConversationHook> ReOrderConversationHooks(List<IConversationHook> hooks)
179+
{
180+
var target = "ChatHubConversationHook";
181+
var chathub = hooks.FirstOrDefault(x => x.GetType().Name == target);
182+
var otherHooks = hooks.Where(x => x.GetType().Name != target).ToList();
183+
184+
if (chathub != null)
185+
{
186+
var newHooks = new List<IConversationHook> { chathub }.Concat(otherHooks);
187+
return newHooks.ToList();
188+
}
189+
return hooks;
190+
}
176191
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using BotSharp.Abstraction.Processors;
2+
using BotSharp.Abstraction.Processors.Models;
3+
4+
namespace BotSharp.Core.Processors;
5+
6+
public class ProcessorFactory
7+
{
8+
private readonly IServiceProvider _services;
9+
10+
public ProcessorFactory(IServiceProvider services)
11+
{
12+
_services = services;
13+
}
14+
15+
public IEnumerable<IBaseProcessor<TInput, TOutput>> Create<TInput, TOutput>(string provider)
16+
where TInput : LlmBaseRequest where TOutput : class
17+
{
18+
var processors = _services.GetServices<IBaseProcessor<TInput, TOutput>>();
19+
processors = processors.Where(x => x.Provider == provider);
20+
return processors.OrderBy(x => x.Order);
21+
}
22+
23+
public IBaseProcessor<TInput, TOutput>? Create<TInput, TOutput>(string provider, string name)
24+
where TInput : LlmBaseRequest where TOutput : class
25+
{
26+
var processors = _services.GetServices<IBaseProcessor<TInput, TOutput>>();
27+
return processors.FirstOrDefault(x => x.Provider == provider && x.Name == name);
28+
}
29+
}

src/Plugins/BotSharp.Plugin.ChatHub/Hooks/ChatHubConversationHook.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public override async Task OnMessageReceived(RoleDialogModel message)
5454
{
5555
ConversationId = conv.ConversationId,
5656
MessageId = message.MessageId,
57+
Payload = message.Payload,
5758
Text = !string.IsNullOrEmpty(message.SecondaryContent) ? message.SecondaryContent : message.Content,
5859
Sender = UserViewModel.FromUser(sender)
5960
};

src/Plugins/BotSharp.Plugin.KnowledgeBase/BotSharp.Plugin.KnowledgeBase.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>$(TargetFramework)</TargetFramework>

src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Document.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public async Task<UploadKnowledgeResponse> UploadDocumentsToKnowledge(string col
112112

113113

114114
public async Task<bool> ImportDocumentContentToKnowledge(string collectionName, string fileName, string fileSource,
115-
IEnumerable<string> contents, DocMetaRefData? refData = null)
115+
IEnumerable<string> contents, DocMetaRefData? refData = null, Dictionary<string, object>? payload = null)
116116
{
117117
if (string.IsNullOrWhiteSpace(collectionName)
118118
|| string.IsNullOrWhiteSpace(fileName)
@@ -132,20 +132,26 @@ public async Task<bool> ImportDocumentContentToKnowledge(string collectionName,
132132
var fileId = Guid.NewGuid();
133133
var contentType = FileUtility.GetFileContentType(fileName);
134134

135-
var payload = new Dictionary<string, object>()
135+
var innerPayload = new Dictionary<string, object>();
136+
if (payload != null)
136137
{
137-
{ KnowledgePayloadName.DataSource, VectorDataSource.File },
138-
{ KnowledgePayloadName.FileId, fileId.ToString() },
139-
{ KnowledgePayloadName.FileName, fileName },
140-
{ KnowledgePayloadName.FileSource, fileSource }
141-
};
138+
foreach (var item in payload)
139+
{
140+
innerPayload[item.Key] = item.Value;
141+
}
142+
}
143+
144+
innerPayload[KnowledgePayloadName.DataSource] = VectorDataSource.File;
145+
innerPayload[KnowledgePayloadName.FileId] = fileId.ToString();
146+
innerPayload[KnowledgePayloadName.FileName] = fileName;
147+
innerPayload[KnowledgePayloadName.FileSource] = fileSource;
142148

143149
if (!string.IsNullOrWhiteSpace(refData?.Url))
144150
{
145-
payload[KnowledgePayloadName.FileUrl] = refData.Url;
151+
innerPayload[KnowledgePayloadName.FileUrl] = refData.Url;
146152
}
147153

148-
var dataIds = await SaveToVectorDb(collectionName, contents, payload);
154+
var dataIds = await SaveToVectorDb(collectionName, contents, innerPayload);
149155
db.SaveKnolwedgeBaseFileMeta(new KnowledgeDocMetaData
150156
{
151157
Collection = collectionName,

0 commit comments

Comments
 (0)