Separate KnoledgeBase as standalone plugin.
This commit is contained in:
parent
05b8db8547
commit
9f74b04fd7
16
BotSharp.sln
16
BotSharp.sln
|
|
@ -51,7 +51,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.PizzaBot",
|
|||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{51AFE054-AE99-497D-A593-69BAEFB5106F}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.RoutingSpeeder", "src\Plugins\BotSharp.Plugin.RoutingSpeeder\BotSharp.Plugin.RoutingSpeeder.csproj", "{631D9C12-86C4-44F0-99C3-D32C0754BF37}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.RoutingSpeeder", "src\Plugins\BotSharp.Plugin.RoutingSpeeder\BotSharp.Plugin.RoutingSpeeder.csproj", "{631D9C12-86C4-44F0-99C3-D32C0754BF37}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "RAGs", "RAGs", "{4F346DCE-087F-4368-AF88-EE9C720D0E69}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.KnowledgeBase", "src\Plugins\BotSharp.Plugin.KnowledgeBase\BotSharp.Plugin.KnowledgeBase.csproj", "{298AC787-A104-414C-B114-82BE764FBD9C}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
|
|
@ -189,6 +193,14 @@ Global
|
|||
{631D9C12-86C4-44F0-99C3-D32C0754BF37}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{631D9C12-86C4-44F0-99C3-D32C0754BF37}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{631D9C12-86C4-44F0-99C3-D32C0754BF37}.Release|x64.Build.0 = Release|Any CPU
|
||||
{298AC787-A104-414C-B114-82BE764FBD9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{298AC787-A104-414C-B114-82BE764FBD9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{298AC787-A104-414C-B114-82BE764FBD9C}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{298AC787-A104-414C-B114-82BE764FBD9C}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{298AC787-A104-414C-B114-82BE764FBD9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{298AC787-A104-414C-B114-82BE764FBD9C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{298AC787-A104-414C-B114-82BE764FBD9C}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{298AC787-A104-414C-B114-82BE764FBD9C}.Release|x64.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
@ -215,6 +227,8 @@ Global
|
|||
{A1118A2C-C6D7-4E22-9462-964AEC7CC46E} = {32FAFFFE-A4CB-4FEE-BF7C-84518BBC6DCC}
|
||||
{51AFE054-AE99-497D-A593-69BAEFB5106F} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C}
|
||||
{631D9C12-86C4-44F0-99C3-D32C0754BF37} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
|
||||
{4F346DCE-087F-4368-AF88-EE9C720D0E69} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C}
|
||||
{298AC787-A104-414C-B114-82BE764FBD9C} = {4F346DCE-087F-4368-AF88-EE9C720D0E69}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}
|
||||
|
|
|
|||
8
Directory.Build.props
Normal file
8
Directory.Build.props
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<Project>
|
||||
<PropertyGroup>
|
||||
<LangVersion>10.0</LangVersion>
|
||||
<OutputPath>..\..\..\packages</OutputPath>
|
||||
<PackageVersion>0.11.0</PackageVersion>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
using BotSharp.Abstraction.Agents.Models;
|
||||
using BotSharp.Abstraction.Agents.Settings;
|
||||
|
||||
namespace BotSharp.Core.Agents.Services;
|
||||
namespace BotSharp.Abstraction.Agents;
|
||||
|
||||
public abstract class AgentHookBase : IAgentHook
|
||||
{
|
||||
|
|
@ -28,6 +28,9 @@ public abstract class AgentHookBase : IAgentHook
|
|||
|
||||
public virtual bool OnInstructionLoaded(string template, Dictionary<string, object> dict)
|
||||
{
|
||||
dict["current_date"] = $"{DateTime.Now:MMM dd, yyyy}";
|
||||
dict["current_time"] = $"{DateTime.Now:hh:mm tt}";
|
||||
dict["current_weekday"] = $"{DateTime.Now:dddd}";
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -2,6 +2,6 @@ namespace BotSharp.Abstraction.Agents.Settings;
|
|||
|
||||
public class AgentSettings
|
||||
{
|
||||
public string DataDir { get; set; }
|
||||
public string TemplateFormat { get; set; }
|
||||
public string DataDir { get; set; } = string.Empty;
|
||||
public string TemplateFormat { get; set; } = "liquid";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,9 +3,10 @@
|
|||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>10.0</LangVersion>
|
||||
<VersionPrefix>0.10.1</VersionPrefix>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(PackageVersion)</VersionPrefix>
|
||||
<PackageIcon>Icon.png</PackageIcon>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -2,5 +2,5 @@ namespace BotSharp.Abstraction.Plugins;
|
|||
|
||||
public class PluginLoaderSettings
|
||||
{
|
||||
public string[] Assemblies { get; set; }
|
||||
public string[] Assemblies { get; set; } = new string[0];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,10 +5,10 @@ public class RoutingSettings
|
|||
/// <summary>
|
||||
/// Router Agent Id
|
||||
/// </summary>
|
||||
public string RouterId { get; set; }
|
||||
public string RouterId { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Reasoner Agent Id
|
||||
/// </summary>
|
||||
public string ReasonerId { get; set; }
|
||||
public string ReasonerId { get; set; } = string.Empty;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<LangVersion>10.0</LangVersion>
|
||||
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
|
||||
<VersionPrefix>0.10.4</VersionPrefix>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(PackageVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Label="Globals">
|
||||
|
|
@ -76,7 +76,6 @@
|
|||
<PackageReference Include="EntityFrameworkCore.BootKit" Version="6.2.1" />
|
||||
<PackageReference Include="Fluid.Core" Version="2.4.0" />
|
||||
<PackageReference Include="TensorFlow.Keras" Version="0.11.2" />
|
||||
<PackageReference Include="PdfPig" Version="0.1.9-alpha-20230806-4a480" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -1,10 +1,8 @@
|
|||
using BotSharp.Abstraction.Agents.Models;
|
||||
using BotSharp.Abstraction.Functions;
|
||||
using BotSharp.Core.Functions;
|
||||
using BotSharp.Core.Hooks;
|
||||
using BotSharp.Core.Routing;
|
||||
using BotSharp.Core.Templating;
|
||||
using BotSharp.Core.Plugins.Knowledges.Services;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using BotSharp.Abstraction.Routing.Settings;
|
||||
|
|
@ -113,7 +111,5 @@ public static class BotSharpServiceCollectionExtensions
|
|||
loader.Load();
|
||||
|
||||
services.AddSingleton(loader);
|
||||
|
||||
services.AddSingleton<IPdf2TextConverter, PigPdf2TextConverter>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,17 +28,6 @@ public partial class ConversationService
|
|||
|
||||
_storage.Append(_conversationId, agent.Id, lastDialog);
|
||||
|
||||
// Get relevant domain knowledge
|
||||
/*if (_settings.EnableKnowledgeBase)
|
||||
{
|
||||
var knowledge = _services.GetRequiredService<IKnowledgeService>();
|
||||
agent.Knowledges = await knowledge.GetKnowledges(new KnowledgeRetrievalModel
|
||||
{
|
||||
AgentId = agentId,
|
||||
Question = string.Join("\n", wholeDialogs.Select(x => x.Content))
|
||||
});
|
||||
}*/
|
||||
|
||||
var hooks = _services.GetServices<IConversationHook>().ToList();
|
||||
|
||||
// Before chat completion hook
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ using System.Drawing;
|
|||
using System.IO;
|
||||
using System.Reflection;
|
||||
|
||||
namespace BotSharp.Core.Plugins;
|
||||
namespace BotSharp.Core.Infrastructures;
|
||||
|
||||
public class PluginLoader
|
||||
{
|
||||
|
|
@ -22,5 +22,4 @@ global using BotSharp.Core.Repository.DbTables;
|
|||
global using BotSharp.Core.Agents.Services;
|
||||
global using BotSharp.Core.Conversations.Services;
|
||||
global using BotSharp.Core.Infrastructures;
|
||||
global using BotSharp.Core.Plugins;
|
||||
global using BotSharp.Core.Users.Services;
|
||||
|
|
@ -4,7 +4,9 @@
|
|||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<VersionPrefix>0.10.1</VersionPrefix>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(PackageVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -1,9 +1,6 @@
|
|||
using BotSharp.Abstraction.ApiAdapters;
|
||||
using BotSharp.Abstraction.Knowledges.Models;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using UglyToad.PdfPig.Content;
|
||||
using UglyToad.PdfPig;
|
||||
using BotSharp.Core.Plugins.Knowledges;
|
||||
using BotSharp.Abstraction.Knowledges.Settings;
|
||||
|
||||
namespace BotSharp.OpenAPI.Controllers;
|
||||
|
|
|
|||
|
|
@ -4,10 +4,10 @@ namespace BotSharp.OpenAPI.ViewModels.Users;
|
|||
|
||||
public class UserCreationModel
|
||||
{
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string Password { get; set; }
|
||||
public string FirstName { get; set; } = string.Empty;
|
||||
public string LastName { get; set; } = string.Empty;
|
||||
public string Email { get; set; } = string.Empty;
|
||||
public string Password { get; set; } = string.Empty;
|
||||
|
||||
public User ToUser()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -3,8 +3,9 @@
|
|||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>10</LangVersion>
|
||||
<VersionPrefix>0.10.1</VersionPrefix>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(PackageVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -3,8 +3,9 @@
|
|||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>10</LangVersion>
|
||||
<VersionPrefix>0.10.1</VersionPrefix>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(PackageVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>10</LangVersion>
|
||||
<VersionPrefix>0.9.0</VersionPrefix>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(PackageVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(PackageVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="PdfPig" Version="0.1.9-alpha-20230827-ee756" />
|
||||
<PackageReference Include="TensorFlow.Keras" Version="0.11.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
using System;
|
||||
namespace BotSharp.Plugin.KnowledgeBase;
|
||||
|
||||
public class KnowledgeBaseAgentHook : AgentHookBase
|
||||
{
|
||||
public KnowledgeBaseAgentHook(IServiceProvider services, AgentSettings settings)
|
||||
: base(services, settings)
|
||||
{
|
||||
}
|
||||
|
||||
public override bool OnInstructionLoaded(string template, Dictionary<string, object> dict)
|
||||
{
|
||||
// Get relevant domain knowledge
|
||||
/*if (_settings.EnableKnowledgeBase)
|
||||
{
|
||||
var knowledge = _services.GetRequiredService<IKnowledgeService>();
|
||||
agent.Knowledges = await knowledge.GetKnowledges(new KnowledgeRetrievalModel
|
||||
{
|
||||
AgentId = agentId,
|
||||
Question = string.Join("\n", wholeDialogs.Select(x => x.Content))
|
||||
});
|
||||
}*/
|
||||
|
||||
return base.OnInstructionLoaded(template, dict);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,6 @@
|
|||
using BotSharp.Abstraction.Knowledges.Settings;
|
||||
using BotSharp.Core.Plugins.Knowledges.Services;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace BotSharp.Core.Plugins.Knowledges;
|
||||
namespace BotSharp.Plugin.KnowledgeBase;
|
||||
|
||||
public class KnowledgeBasePlugin : IBotSharpPlugin
|
||||
{
|
||||
|
|
@ -14,5 +12,6 @@ public class KnowledgeBasePlugin : IBotSharpPlugin
|
|||
|
||||
services.AddScoped<ITextChopper, TextChopperService>();
|
||||
services.AddScoped<IKnowledgeService, KnowledgeService>();
|
||||
services.AddSingleton<IPdf2TextConverter, PigPdf2TextConverter>();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
using BotSharp.Abstraction.VectorStorage;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace BotSharp.Core.Plugins.MemVecDb;
|
||||
namespace BotSharp.Plugin.KnowledgeBase.MemVecDb;
|
||||
|
||||
public class MemVecDbPlugin : IBotSharpPlugin
|
||||
{
|
||||
|
|
@ -1,9 +1,7 @@
|
|||
using BotSharp.Abstraction.VectorStorage;
|
||||
using Tensorflow;
|
||||
using Tensorflow.NumPy;
|
||||
using static Tensorflow.Binding;
|
||||
|
||||
namespace BotSharp.Core.Plugins.MemVecDb;
|
||||
namespace BotSharp.Plugin.KnowledgeBase.MemVecDb;
|
||||
|
||||
public class MemVectorDatabase : IVectorDb
|
||||
{
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
namespace BotSharp.Core.Plugins.MemVecDb;
|
||||
namespace BotSharp.Plugin.KnowledgeBase.MemVecDb;
|
||||
|
||||
public class VecRecord
|
||||
{
|
||||
|
|
@ -1,9 +1,4 @@
|
|||
using BotSharp.Abstraction.Knowledges.Models;
|
||||
using BotSharp.Abstraction.Knowledges.Settings;
|
||||
using BotSharp.Abstraction.MLTasks;
|
||||
using BotSharp.Abstraction.VectorStorage;
|
||||
|
||||
namespace BotSharp.Core.Plugins.Knowledges.Services;
|
||||
namespace BotSharp.Plugin.KnowledgeBase.Services;
|
||||
|
||||
public class KnowledgeService : IKnowledgeService
|
||||
{
|
||||
|
|
@ -1,12 +1,8 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using UglyToad.PdfPig;
|
||||
using UglyToad.PdfPig.Content;
|
||||
|
||||
namespace BotSharp.Core.Plugins.Knowledges.Services;
|
||||
namespace BotSharp.Plugin.KnowledgeBase.Services;
|
||||
|
||||
public class PigPdf2TextConverter : IPdf2TextConverter
|
||||
{
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
using BotSharp.Abstraction.Knowledges.Models;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace BotSharp.Core.Plugins.Knowledges.Services;
|
||||
namespace BotSharp.Plugin.KnowledgeBase.Services;
|
||||
|
||||
public class TextChopperService : ITextChopper
|
||||
{
|
||||
23
src/Plugins/BotSharp.Plugin.KnowledgeBase/Using.cs
Normal file
23
src/Plugins/BotSharp.Plugin.KnowledgeBase/Using.cs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
global using System;
|
||||
global using System.IO;
|
||||
global using System.Collections.Generic;
|
||||
global using System.Text;
|
||||
global using System.Threading.Tasks;
|
||||
global using System.Linq;
|
||||
global using System.Text.Json;
|
||||
global using Microsoft.Extensions.DependencyInjection;
|
||||
global using Microsoft.Extensions.Logging;
|
||||
global using BotSharp.Abstraction.Plugins;
|
||||
global using BotSharp.Abstraction.Agents;
|
||||
global using BotSharp.Abstraction.Conversations;
|
||||
global using BotSharp.Abstraction.Knowledges;
|
||||
global using BotSharp.Abstraction.Users;
|
||||
global using BotSharp.Abstraction.Utilities;
|
||||
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.VectorStorage;
|
||||
global using BotSharp.Abstraction.Knowledges.Models;
|
||||
global using BotSharp.Abstraction.MLTasks;
|
||||
global using BotSharp.Plugin.KnowledgeBase.Services;
|
||||
|
|
@ -3,12 +3,13 @@
|
|||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>10</LangVersion>
|
||||
<VersionPrefix>0.9.0</VersionPrefix>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(PackageVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="LLamaSharp" Version="0.4.2-preview" />
|
||||
<PackageReference Include="LLamaSharp" Version="0.5.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using BotSharp.Plugin.LLamaSharp.Settings;
|
||||
using LLama;
|
||||
using LLama.Abstractions;
|
||||
using LLama.Common;
|
||||
|
||||
namespace BotSharp.Plugins.LLamaSharp;
|
||||
|
|
@ -9,13 +10,25 @@ public class LlamaAiModel
|
|||
private readonly LlamaSharpSettings _settings;
|
||||
public LlamaSharpSettings Settings => _settings;
|
||||
|
||||
LLamaModel _model;
|
||||
LLamaWeights _model;
|
||||
|
||||
public LLamaModel Model => _model;
|
||||
public LLamaWeights Model => _model;
|
||||
|
||||
ModelParams _params;
|
||||
public ModelParams Params => _params;
|
||||
|
||||
private ILLamaExecutor _statelessExecutor;
|
||||
|
||||
public LlamaAiModel(LlamaSharpSettings settings)
|
||||
{
|
||||
_settings = settings;
|
||||
|
||||
_params = new ModelParams(_settings.ModelPath)
|
||||
{
|
||||
ContextSize = _settings.MaxContextLength,
|
||||
Seed = 1337,
|
||||
GpuLayerCount = _settings.NumberOfGpuLayer
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -26,8 +39,15 @@ public class LlamaAiModel
|
|||
return;
|
||||
}
|
||||
|
||||
_model = new LLamaModel(new ModelParams(_settings.ModelPath,
|
||||
contextSize: _settings.MaxContextLength,
|
||||
gpuLayerCount: _settings.NumberOfGpuLayer));
|
||||
_model = LLamaWeights.LoadFromFile(_params);
|
||||
}
|
||||
|
||||
public ILLamaExecutor GetStatelessExecutor()
|
||||
{
|
||||
if (_statelessExecutor == null)
|
||||
{
|
||||
_statelessExecutor = new StatelessExecutor(_model, _params);
|
||||
}
|
||||
return _statelessExecutor;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,13 @@
|
|||
using BotSharp.Abstraction.Agents.Enums;
|
||||
using BotSharp.Abstraction.Agents.Models;
|
||||
using BotSharp.Abstraction.Conversations.Models;
|
||||
using BotSharp.Abstraction.Conversations.Settings;
|
||||
using BotSharp.Abstraction.MLTasks;
|
||||
using BotSharp.Plugins.LLamaSharp;
|
||||
using LLama;
|
||||
using LLama.Common;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
|
@ -15,9 +18,13 @@ namespace BotSharp.Plugin.LLamaSharp.Providers;
|
|||
public class ChatCompletionProvider : IChatCompletion
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
public ChatCompletionProvider(IServiceProvider services)
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public ChatCompletionProvider(IServiceProvider services,
|
||||
ILogger<ChatCompletionProvider> logger)
|
||||
{
|
||||
_services = services;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string GetChatCompletions(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived)
|
||||
|
|
@ -30,29 +37,42 @@ public class ChatCompletionProvider : IChatCompletion
|
|||
Func<RoleDialogModel, Task> onMessageReceived,
|
||||
Func<RoleDialogModel, Task> onFunctionExecuting)
|
||||
{
|
||||
var content = string.Join("\n", conversations.Select(x => $"{x.Role}: {x.Content.Replace("user:", "User:")}")).Trim();
|
||||
content += "\nBob: ";
|
||||
var content = string.Join("\n", conversations.Select(x => $"{x.Role}: {x.Content}")).Trim();
|
||||
content += $"\n{AgentRole.Assistant}: ";
|
||||
|
||||
var llama = _services.GetRequiredService<LlamaAiModel>();
|
||||
llama.LoadModel();
|
||||
var executor = new StatelessExecutor(llama.Model);
|
||||
var executor = llama.GetStatelessExecutor();
|
||||
|
||||
var inferenceParams = new InferenceParams()
|
||||
{
|
||||
Temperature = 1.0f,
|
||||
AntiPrompts = new List<string> { "User:" },
|
||||
AntiPrompts = new List<string> { $"{AgentRole.User}:", "\n", "?" },
|
||||
MaxTokens = 256
|
||||
};
|
||||
|
||||
string totalResponse = "";
|
||||
|
||||
var prompt = agent.Instruction + content;
|
||||
await foreach (var response in executor.InferAsync(prompt, inferenceParams))
|
||||
|
||||
var convSetting = _services.GetRequiredService<ConversationSetting>();
|
||||
if (convSetting.ShowVerboseLog)
|
||||
{
|
||||
_logger.LogInformation(prompt);
|
||||
}
|
||||
|
||||
foreach (var response in executor.Infer(prompt, inferenceParams))
|
||||
{
|
||||
Console.Write(response);
|
||||
totalResponse += response;
|
||||
}
|
||||
|
||||
await onMessageReceived(new RoleDialogModel("assistant", totalResponse));
|
||||
foreach (var anti in inferenceParams.AntiPrompts)
|
||||
{
|
||||
totalResponse = totalResponse.Replace(anti, "").Trim();
|
||||
}
|
||||
|
||||
await onMessageReceived(new RoleDialogModel(AgentRole.Assistant, totalResponse));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
@ -60,13 +80,19 @@ public class ChatCompletionProvider : IChatCompletion
|
|||
public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived)
|
||||
{
|
||||
string totalResponse = "";
|
||||
var content = string.Join("\n", conversations.Select(x => $"{x.Role}: {x.Content.Replace("user:", "")}")).Trim();
|
||||
content += "\nassistant: ";
|
||||
var content = string.Join("\n", conversations.Select(x => $"{x.Role}: {x.Content}")).Trim();
|
||||
content += $"\n{AgentRole.Assistant}: ";
|
||||
|
||||
var llama = _services.GetRequiredService<LlamaAiModel>();
|
||||
llama.LoadModel();
|
||||
var executor = new StatelessExecutor(llama.Model);
|
||||
var inferenceParams = new InferenceParams() { Temperature = 1.0f, AntiPrompts = new List<string> { "user:" }, MaxTokens = 64 };
|
||||
var executor = new StatelessExecutor(llama.Model, llama.Params);
|
||||
var inferenceParams = new InferenceParams() { Temperature = 1.0f, AntiPrompts = new List<string> { $"{AgentRole.User}:" }, MaxTokens = 64 };
|
||||
|
||||
var convSetting = _services.GetRequiredService<ConversationSetting>();
|
||||
if (convSetting.ShowVerboseLog)
|
||||
{
|
||||
_logger.LogInformation(agent.Instruction);
|
||||
}
|
||||
|
||||
foreach (var response in executor.Infer(agent.Instruction, inferenceParams))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ public class TextCompletionProvider : ITextCompletion
|
|||
var llama = _services.GetRequiredService<LlamaAiModel>();
|
||||
llama.LoadModel();
|
||||
|
||||
var executor = new InstructExecutor(llama.Model);
|
||||
var executor = new InstructExecutor(llama.Model.CreateContext(llama.Params));
|
||||
var inferenceParams = new InferenceParams() { Temperature = 0.5f, MaxTokens = 128 };
|
||||
|
||||
string totalResponse = "";
|
||||
|
|
|
|||
|
|
@ -3,8 +3,9 @@
|
|||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>10</LangVersion>
|
||||
<VersionPrefix>0.10.1</VersionPrefix>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(PackageVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -2,8 +2,9 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<LangVersion>10</LangVersion>
|
||||
<VersionPrefix>0.10.1</VersionPrefix>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(PackageVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -3,8 +3,9 @@
|
|||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>10</LangVersion>
|
||||
<VersionPrefix>0.9.0</VersionPrefix>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(PackageVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -3,8 +3,9 @@
|
|||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>10</LangVersion>
|
||||
<VersionPrefix>0.9.0</VersionPrefix>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(PackageVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -3,8 +3,9 @@
|
|||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>10</LangVersion>
|
||||
<VersionPrefix>0.10.2</VersionPrefix>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(PackageVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(PackageVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
|
|
@ -12,7 +14,7 @@
|
|||
<PackageProjectUrl>https://github.com/Oceania2018/botsharp-channel-weixin</PackageProjectUrl>
|
||||
<Copyright>Apache 2.0</Copyright>
|
||||
<PackageTags>botsharp, wechat, wexin, chatbot</PackageTags>
|
||||
<VersionPrefix>0.9.0</VersionPrefix>
|
||||
<VersionPrefix>$(PackageVersion)</VersionPrefix>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
|
|
@ -27,7 +27,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="LLamaSharp.Backend.Cuda11" Version="0.4.2-preview" />
|
||||
<PackageReference Include="LLamaSharp.Backend.Cuda11" Version="0.5.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.16" />
|
||||
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.11.4" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||
|
|
@ -40,6 +40,7 @@
|
|||
<ProjectReference Include="..\Plugins\BotSharp.Plugin.AzureOpenAI\BotSharp.Plugin.AzureOpenAI.csproj" />
|
||||
<ProjectReference Include="..\Plugins\BotSharp.Plugin.ChatbotUI\BotSharp.Plugin.ChatbotUI.csproj" />
|
||||
<ProjectReference Include="..\Plugins\BotSharp.Plugin.HuggingFace\BotSharp.Plugin.HuggingFace.csproj" />
|
||||
<ProjectReference Include="..\Plugins\BotSharp.Plugin.KnowledgeBase\BotSharp.Plugin.KnowledgeBase.csproj" />
|
||||
<ProjectReference Include="..\Plugins\BotSharp.Plugin.LLamaSharp\BotSharp.Plugin.LLamaSharp.csproj" />
|
||||
<ProjectReference Include="..\Plugins\BotSharp.Plugin.MetaAI\BotSharp.Plugin.MetaAI.csproj" />
|
||||
<ProjectReference Include="..\Plugins\BotSharp.Plugin.PaddleSharp\BotSharp.Plugin.PaddleSharp.csproj" />
|
||||
|
|
|
|||
|
|
@ -33,9 +33,9 @@
|
|||
|
||||
"LlamaSharp": {
|
||||
"Interactive": true,
|
||||
"ModelPath": "C:/Users/haipi/Downloads/llama-2-7b-chat.ggmlv3.q3_K_S.bin",
|
||||
"ModelPath": "C:/Users/haipi/Downloads/llama-2-7b-guanaco-qlora.Q4_K_S.gguf",
|
||||
"MaxContextLength": 1024,
|
||||
"NumberOfGpuLayer": 10
|
||||
"NumberOfGpuLayer": 15
|
||||
},
|
||||
|
||||
"AzureOpenAi": {
|
||||
|
|
@ -105,10 +105,12 @@
|
|||
"BotSharp.Core",
|
||||
"BotSharp.Plugin.AzureOpenAI",
|
||||
"BotSharp.Plugin.MetaAI",
|
||||
"BotSharp.Plugin.LLamaSharp",
|
||||
"BotSharp.Plugin.KnowledgeBase",
|
||||
"BotSharp.Plugin.Qdrant",
|
||||
"BotSharp.Plugin.PaddleSharp",
|
||||
"BotSharp.Plugin.WeChat",
|
||||
"BotSharp.Plugin.RoutingSpeeder",
|
||||
// "BotSharp.Plugin.RoutingSpeeder",
|
||||
"BotSharp.Plugin.PizzaBot"
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,8 @@
|
|||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>10</LangVersion>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<OutputPath>..\..\packages</OutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -1,17 +1,16 @@
|
|||
using BotSharp.Abstraction.Agents;
|
||||
|
||||
namespace BotSharp.Plugin.PizzaBot.Hooks;
|
||||
|
||||
public class PizzaBotAgentHook : AgentHookBase
|
||||
{
|
||||
public PizzaBotAgentHook(IServiceProvider services, AgentSettings settings)
|
||||
: base(services, settings)
|
||||
: base(services, settings)
|
||||
{
|
||||
}
|
||||
|
||||
public override bool OnInstructionLoaded(string template, Dictionary<string, object> dict)
|
||||
{
|
||||
dict["current_date"] = $"{DateTime.Now:MMM dd, yyyy}";
|
||||
dict["current_time"] = $"{DateTime.Now:hh:mm tt}";
|
||||
dict["current_weekday"] = $"{DateTime.Now:dddd}";
|
||||
return true;
|
||||
return base.OnInstructionLoaded(template, dict);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue