add enums

This commit is contained in:
Jicheng Lu 2024-08-06 14:01:26 -05:00
parent ea441c75e8
commit 7e51ea6da2
13 changed files with 43 additions and 43 deletions

View file

@ -0,0 +1,6 @@
namespace BotSharp.Abstraction.Knowledges.Enums;
public static class KnowledgeCollectionName
{
public static string BotSharp = nameof(BotSharp);
}

View file

@ -0,0 +1,10 @@
namespace BotSharp.Abstraction.Knowledges.Enums;
public static class KnowledgePayloadName
{
public static string Text = "text";
public static string Question = "question";
public static string Answer = "answer";
public static string Request = "request";
public static string Response = "response";
}

View file

@ -1,9 +1,3 @@
using BotSharp.Abstraction.Functions;
using BotSharp.Abstraction.Messaging.Enums;
using BotSharp.Abstraction.Messaging.Models.RichContent.Template;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using BotSharp.Abstraction.Messaging;
namespace BotSharp.Plugin.KnowledgeBase.Functions;
public class ConfirmKnowledgePersistenceFn : IFunctionCallback

View file

@ -1,6 +1,3 @@
using BotSharp.Abstraction.Functions;
using BotSharp.Core.Infrastructures;
namespace BotSharp.Plugin.KnowledgeBase.Functions;
public class KnowledgeRetrievalFn : IFunctionCallback
@ -23,15 +20,9 @@ public class KnowledgeRetrievalFn : IFunctionCallback
var embedding = _services.GetServices<ITextEmbedding>()
.FirstOrDefault(x => x.GetType().FullName.EndsWith(_settings.TextEmbedding));
var vector = await embedding.GetVectorsAsync(new List<string>
{
args.Question
});
var vector = await embedding.GetVectorAsync(args.Question);
var vectorDb = _services.GetRequiredService<IVectorDb>();
var id = Utilities.HashTextMd5(args.Question);
var knowledges = await vectorDb.Search("lessen", vector[0], "answer");
var knowledges = await vectorDb.Search(KnowledgeCollectionName.BotSharp, vector, KnowledgePayloadName.Answer);
if (knowledges.Count > 0)
{

View file

@ -1,4 +1,3 @@
using BotSharp.Abstraction.Functions;
using BotSharp.Core.Infrastructures;
namespace BotSharp.Plugin.KnowledgeBase.Functions;
@ -30,10 +29,10 @@ public class MemorizeKnowledgeFn : IFunctionCallback
var vectorDb = _services.GetRequiredService<IVectorDb>();
await vectorDb.CreateCollection("lessen", vector[0].Length);
await vectorDb.CreateCollection(KnowledgeCollectionName.BotSharp, vector[0].Length);
var id = Utilities.HashTextMd5(args.Question);
var result = await vectorDb.Upsert("lessen", id, vector[0],
var result = await vectorDb.Upsert(KnowledgeCollectionName.BotSharp, id, vector[0],
args.Question,
new Dictionary<string, string>
{

View file

@ -1,9 +1,3 @@
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Plugin.KnowledgeBase.Enum;
namespace BotSharp.Plugin.KnowledgeBase.Hooks;
public class KnowledgeBaseAgentHook : AgentHookBase, IAgentHook

View file

@ -1,5 +1,3 @@
using BotSharp.Plugin.KnowledgeBase.Enum;
namespace BotSharp.Plugin.KnowledgeBase.Hooks;
public class KnowledgeBaseUtilityHook : IAgentUtilityHook

View file

@ -28,11 +28,11 @@ public partial class KnowledgeService : IKnowledgeService
var db = GetVectorDb();
var textEmbedding = GetTextEmbedding();
await db.CreateCollection("shared", textEmbedding.Dimension);
await db.CreateCollection(KnowledgeCollectionName.BotSharp, textEmbedding.Dimension);
foreach (var line in lines)
{
var vec = await textEmbedding.GetVectorAsync(line);
await db.Upsert("shared", idStart.ToString(), vec, line);
await db.Upsert(KnowledgeCollectionName.BotSharp, idStart.ToString(), vec, line);
idStart++;
Console.WriteLine($"Saved vector {idStart}/{lines.Count}: {line}\n");
}
@ -68,7 +68,7 @@ public partial class KnowledgeService : IKnowledgeService
// Vector search
var db = GetVectorDb();
var result = await db.Search("shared", vector, "answer", limit: 10);
var result = await db.Search(KnowledgeCollectionName.BotSharp, vector, KnowledgePayloadName.Answer, limit: 10);
// Restore
return string.Join("\n\n", result.Select((x, i) => $"### Paragraph {i + 1} ###\n{x.Trim()}"));

View file

@ -16,7 +16,7 @@ public class TextChopperService : ITextChopper
var chunks = new List<string>();
var words = content.Split(' ')
.Where(x => !string.IsNullOrEmpty(x))
.Where(x => !string.IsNullOrWhiteSpace(x))
.ToList();
var chunk = "";

View file

@ -17,7 +17,18 @@ global using BotSharp.Abstraction.Conversations.Models;
global using BotSharp.Abstraction.Agents.Settings;
global using BotSharp.Abstraction.Conversations.Settings;
global using BotSharp.Abstraction.Knowledges.Settings;
global using BotSharp.Abstraction.Knowledges.Enums;
global using BotSharp.Abstraction.VectorStorage;
global using BotSharp.Abstraction.Knowledges.Models;
global using BotSharp.Abstraction.MLTasks;
global using BotSharp.Plugin.KnowledgeBase.Services;
global using BotSharp.Abstraction.Functions;
global using BotSharp.Abstraction.Messaging.Enums;
global using BotSharp.Abstraction.Messaging.Models.RichContent.Template;
global using BotSharp.Abstraction.Messaging.Models.RichContent;
global using BotSharp.Abstraction.Messaging;
global using BotSharp.Abstraction.Agents.Enums;
global using BotSharp.Abstraction.Agents.Models;
global using BotSharp.Abstraction.Functions.Models;
global using BotSharp.Abstraction.Repositories;
global using BotSharp.Plugin.KnowledgeBase.Services;
global using BotSharp.Plugin.KnowledgeBase.Enum;

View file

@ -1,13 +1,5 @@
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.VectorStorage;
using Microsoft.Extensions.DependencyInjection;
using Qdrant.Client;
using Qdrant.Client.Grpc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace BotSharp.Plugin.Qdrant;
@ -80,7 +72,7 @@ public class QdrantDb : IVectorDb
Payload =
{
{ "text", text }
{ KnowledgePayloadName.Text, text }
}
};

View file

@ -1,6 +1,5 @@
using BotSharp.Abstraction.Plugins;
using BotSharp.Abstraction.Settings;
using BotSharp.Abstraction.VectorStorage;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

View file

@ -0,0 +1,6 @@
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Threading.Tasks;
global using BotSharp.Abstraction.VectorStorage;
global using BotSharp.Abstraction.Knowledges.Enums;