Merge branch 'SciSharp:master' into master
This commit is contained in:
commit
1d8c765948
|
|
@ -6,16 +6,22 @@ public class AgentKnowledgeBase
|
|||
public string Type { get; set; }
|
||||
public bool Disabled { get; set; }
|
||||
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public decimal? Confidence { get; set; }
|
||||
|
||||
public AgentKnowledgeBase()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public AgentKnowledgeBase(string name, string type, bool enabled)
|
||||
public AgentKnowledgeBase(
|
||||
string name, string type,
|
||||
bool enabled, decimal? confidence = null)
|
||||
{
|
||||
Name = name;
|
||||
Type = type;
|
||||
Disabled = enabled;
|
||||
Confidence = confidence;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
|
|
|
|||
105
src/Plugins/BotSharp.Plugin.KnowledgeBase/Hooks/KnowledgeHook.cs
Normal file
105
src/Plugins/BotSharp.Plugin.KnowledgeBase/Hooks/KnowledgeHook.cs
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
namespace BotSharp.Plugin.KnowledgeBase.Hooks;
|
||||
|
||||
public class KnowledgeHook : IKnowledgeHook
|
||||
{
|
||||
private readonly IKnowledgeService _knowledgeService;
|
||||
private readonly ITextEmbedding _textEmbedding;
|
||||
private readonly IServiceProvider _services;
|
||||
|
||||
public KnowledgeHook(
|
||||
IKnowledgeService knowledgeService,
|
||||
ITextEmbedding textEmbedding,
|
||||
IServiceProvider services)
|
||||
{
|
||||
_knowledgeService = knowledgeService;
|
||||
_textEmbedding = textEmbedding;
|
||||
_services = services;
|
||||
}
|
||||
|
||||
private async Task<List<AgentKnowledgeBase>> GetKnowledgeBaseNameByAgentIdAsync(string agentId)
|
||||
{
|
||||
var agentService = _services.GetRequiredService<IAgentService>();
|
||||
var agent = await agentService.GetAgent(agentId);
|
||||
return agent.KnowledgeBases;
|
||||
}
|
||||
|
||||
public async Task<List<string>> GetDomainKnowledges(RoleDialogModel message, string text)
|
||||
{
|
||||
// Get agent Id by knowledge base name
|
||||
var knowledgeBases = await GetKnowledgeBaseNameByAgentIdAsync(message.CurrentAgentId);
|
||||
var results = new List<string>();
|
||||
|
||||
foreach (var knowledgeBase in knowledgeBases)
|
||||
{
|
||||
if (knowledgeBase.Type == "relationships")
|
||||
{
|
||||
var options = new GraphSearchOptions
|
||||
{
|
||||
Method = "local"
|
||||
};
|
||||
var result = await _knowledgeService.SearchGraphKnowledge(text, options);
|
||||
results.Add(result.Result);
|
||||
}
|
||||
else
|
||||
{
|
||||
var options = new VectorSearchOptions
|
||||
{
|
||||
Fields = null,
|
||||
Limit = 5,
|
||||
Confidence = 0.5f,
|
||||
WithVector = true
|
||||
};
|
||||
var result = await _knowledgeService.SearchVectorKnowledge(text, knowledgeBase.Name, options);
|
||||
results.AddRange(result.Where(x => x.Data != null && (x.Data.ContainsKey("text") || x.Data.ContainsKey("answer")))
|
||||
.Select(x => x.Data.ContainsKey("answer") ? x.Data["text"].ToString() + "\r\n\r\n" + x.Data["answer"].ToString() : x.Data["text"].ToString())
|
||||
.Where(x => x != null)!);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public async Task<List<string>> GetGlobalKnowledges(RoleDialogModel message)
|
||||
{
|
||||
var text = message.Content;
|
||||
var results = new List<string>();
|
||||
|
||||
// Get all knowledge bases
|
||||
var knowledgeBases = await _knowledgeService.GetVectorCollections();
|
||||
|
||||
foreach (var knowledgeBase in knowledgeBases)
|
||||
{
|
||||
if (knowledgeBase.Type == "relationships")
|
||||
{
|
||||
var options = new GraphSearchOptions
|
||||
{
|
||||
Method = "local"
|
||||
};
|
||||
var result = await _knowledgeService.SearchGraphKnowledge(text, options);
|
||||
results.Add(result.Result);
|
||||
}
|
||||
else
|
||||
{
|
||||
var options = new VectorSearchOptions
|
||||
{
|
||||
Fields = null,
|
||||
Limit = 5,
|
||||
Confidence = 0.5f,
|
||||
WithVector = true
|
||||
};
|
||||
var result = await _knowledgeService.SearchVectorKnowledge(text, knowledgeBase.Name, options);
|
||||
results.AddRange(result.Where(x => x.Data != null && (x.Data.ContainsKey("text") || x.Data.ContainsKey("answer")))
|
||||
.Select(x => x.Data.ContainsKey("answer") ? x.Data["text"].ToString() + "\r\n\r\n" + x.Data["answer"].ToString() : x.Data["text"].ToString())
|
||||
.Where(x => x != null)!);
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public async Task<List<KnowledgeChunk>> CollectChunkedKnowledge()
|
||||
{
|
||||
return new List<KnowledgeChunk>();
|
||||
}
|
||||
}
|
||||
|
|
@ -26,6 +26,8 @@ public class KnowledgeBasePlugin : IBotSharpPlugin
|
|||
services.AddSingleton<IPdf2TextConverter, PigPdf2TextConverter>();
|
||||
services.AddScoped<IAgentUtilityHook, KnowledgeBaseUtilityHook>();
|
||||
services.AddScoped<IKnowledgeService, KnowledgeService>();
|
||||
services.AddScoped<IKnowledgeHook, KnowledgeHook>();
|
||||
|
||||
}
|
||||
|
||||
public bool AttachMenu(List<PluginMenuDef> menu)
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ public class ConversationDialogDocument : MongoBase
|
|||
{
|
||||
public string ConversationId { get; set; } = default!;
|
||||
public string AgentId { get; set; } = default!;
|
||||
public string UserId { get; set; } = default!;
|
||||
public DateTime UpdatedTime { get; set; }
|
||||
public List<DialogMongoElement> Dialogs { get; set; } = [];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ public class ConversationStateDocument : MongoBase
|
|||
{
|
||||
public string ConversationId { get; set; } = default!;
|
||||
public string AgentId { get; set; } = default!;
|
||||
public string UserId { get; set; } = default!;
|
||||
public DateTime UpdatedTime { get; set; }
|
||||
public List<StateMongoElement> States { get; set; } = [];
|
||||
public List<BreakpointMongoElement> Breakpoints { get; set; } = [];
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ public class AgentKnowledgeBaseMongoElement
|
|||
public string Name { get; set; } = default!;
|
||||
public string Type { get; set; } = default!;
|
||||
public bool Disabled { get; set; }
|
||||
public decimal? Confidence { get; set; }
|
||||
|
||||
public static AgentKnowledgeBaseMongoElement ToMongoElement(AgentKnowledgeBase knowledgeBase)
|
||||
{
|
||||
|
|
@ -15,7 +16,8 @@ public class AgentKnowledgeBaseMongoElement
|
|||
{
|
||||
Name = knowledgeBase.Name,
|
||||
Type = knowledgeBase.Type,
|
||||
Disabled = knowledgeBase.Disabled
|
||||
Disabled = knowledgeBase.Disabled,
|
||||
Confidence = knowledgeBase.Confidence
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -25,7 +27,8 @@ public class AgentKnowledgeBaseMongoElement
|
|||
{
|
||||
Name = knowledgeBase.Name,
|
||||
Type = knowledgeBase.Type,
|
||||
Disabled = knowledgeBase.Disabled
|
||||
Disabled = knowledgeBase.Disabled,
|
||||
Confidence = knowledgeBase.Confidence
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,11 +10,12 @@ public partial class MongoRepository
|
|||
if (conversation == null) return;
|
||||
|
||||
var utcNow = DateTime.UtcNow;
|
||||
var userId = !string.IsNullOrEmpty(conversation.UserId) ? conversation.UserId : string.Empty;
|
||||
var convDoc = new ConversationDocument
|
||||
{
|
||||
Id = !string.IsNullOrEmpty(conversation.Id) ? conversation.Id : Guid.NewGuid().ToString(),
|
||||
AgentId = conversation.AgentId,
|
||||
UserId = !string.IsNullOrEmpty(conversation.UserId) ? conversation.UserId : string.Empty,
|
||||
UserId = userId,
|
||||
Title = conversation.Title,
|
||||
Channel = conversation.Channel,
|
||||
ChannelId = conversation.ChannelId,
|
||||
|
|
@ -30,6 +31,7 @@ public partial class MongoRepository
|
|||
Id = Guid.NewGuid().ToString(),
|
||||
ConversationId = convDoc.Id,
|
||||
AgentId = conversation.AgentId,
|
||||
UserId = userId,
|
||||
Dialogs = [],
|
||||
UpdatedTime = utcNow
|
||||
};
|
||||
|
|
@ -39,6 +41,7 @@ public partial class MongoRepository
|
|||
Id = Guid.NewGuid().ToString(),
|
||||
ConversationId = convDoc.Id,
|
||||
AgentId = conversation.AgentId,
|
||||
UserId = userId,
|
||||
States = [],
|
||||
Breakpoints = [],
|
||||
UpdatedTime = utcNow
|
||||
|
|
|
|||
Loading…
Reference in a new issue