Add Meta AI plugin.
This commit is contained in:
parent
5189158d3f
commit
dcffdd076d
11
BotSharp.sln
11
BotSharp.sln
|
|
@ -33,6 +33,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{32FAFFFE
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTest", "tests\UnitTest\UnitTest.csproj", "{0B6E1D7F-ABDE-47F6-8B2D-4483C2CFF2D6}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.MetaAI", "src\Plugins\BotSharp.Plugin.MetaAI\BotSharp.Plugin.MetaAI.csproj", "{6D8D18A9-86D7-455E-81EC-9682C30AB7E7}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
|
@ -113,6 +115,14 @@ Global
|
|||
{0B6E1D7F-ABDE-47F6-8B2D-4483C2CFF2D6}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{0B6E1D7F-ABDE-47F6-8B2D-4483C2CFF2D6}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{0B6E1D7F-ABDE-47F6-8B2D-4483C2CFF2D6}.Release|x64.Build.0 = Release|Any CPU
|
||||
{6D8D18A9-86D7-455E-81EC-9682C30AB7E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{6D8D18A9-86D7-455E-81EC-9682C30AB7E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{6D8D18A9-86D7-455E-81EC-9682C30AB7E7}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{6D8D18A9-86D7-455E-81EC-9682C30AB7E7}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{6D8D18A9-86D7-455E-81EC-9682C30AB7E7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{6D8D18A9-86D7-455E-81EC-9682C30AB7E7}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{6D8D18A9-86D7-455E-81EC-9682C30AB7E7}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{6D8D18A9-86D7-455E-81EC-9682C30AB7E7}.Release|x64.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
@ -129,6 +139,7 @@ Global
|
|||
{0149F6C2-4CF8-4904-A545-54F3278BEA74} = {64264688-0F5C-4AB0-8F2B-B59B717CCE00}
|
||||
{2323A7A3-E938-488D-A57E-638638054BC4} = {64264688-0F5C-4AB0-8F2B-B59B717CCE00}
|
||||
{0B6E1D7F-ABDE-47F6-8B2D-4483C2CFF2D6} = {32FAFFFE-A4CB-4FEE-BF7C-84518BBC6DCC}
|
||||
{6D8D18A9-86D7-455E-81EC-9682C30AB7E7} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}
|
||||
|
|
|
|||
|
|
@ -70,7 +70,6 @@
|
|||
<PackageReference Include="Qdrant.Client" Version="0.1.0" />
|
||||
<PackageReference Include="TensorFlow.Keras" Version="0.10.5" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
|
||||
<PackageReference Include="FastText.NetWrapper" Version="1.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -1,23 +0,0 @@
|
|||
using BotSharp.Abstraction.MLTasks;
|
||||
using FastText.NetWrapper;
|
||||
|
||||
namespace BotSharp.Core.Plugins.fastText;
|
||||
|
||||
public class fastTextEmbeddingProvider : ITextEmbedding
|
||||
{
|
||||
FastTextWrapper fastText;
|
||||
public fastTextEmbeddingProvider()
|
||||
{
|
||||
fastText = new FastTextWrapper();
|
||||
|
||||
if (!fastText.IsModelReady())
|
||||
{
|
||||
fastText.LoadModel(@"D:\Service Mesh\prediction\WebStarter\tmp_data\models\crawl-300d-2M-subword.bin");
|
||||
}
|
||||
}
|
||||
|
||||
public float[] GetVector(string text)
|
||||
{
|
||||
return fastText.GetSentenceVector(text);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
using BotSharp.Abstraction.MLTasks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace BotSharp.Core.Plugins.fastText;
|
||||
|
||||
public class fastTextPlugin : IBotSharpPlugin
|
||||
{
|
||||
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
services.AddSingleton<ITextEmbedding, fastTextEmbeddingProvider>();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,7 @@
|
|||
using BotSharp.Abstraction.Infrastructures.ContentTransfers;
|
||||
using BotSharp.Abstraction.MLTasks;
|
||||
using BotSharp.Abstraction.Plugins;
|
||||
using BotSharp.Plugin.AzureOpenAI.TextGeneratives;
|
||||
using BotSharp.Plugin.AzureOpenAI.TextTasks;
|
||||
using BotSharp.Plugin.AzureOpenAI.Providers;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,25 +0,0 @@
|
|||
using BotSharp.Abstraction.Infrastructures.ContentTransfers;
|
||||
using BotSharp.Platform.AzureAi;
|
||||
using BotSharp.Plugin.AzureOpenAI.TextGeneratives;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace BotSharp.Core;
|
||||
|
||||
public static class AzureOpenAiServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddAzureOpenAiPlatform(this IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
var settings = new AzureOpenAiSettings();
|
||||
config.Bind("AzureOpenAi", settings);
|
||||
|
||||
services.AddSingleton(x =>
|
||||
{
|
||||
return settings;
|
||||
});
|
||||
|
||||
services.AddScoped<IServiceZone, ChatCompletionProvider>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
|
@ -9,7 +9,7 @@ using System.Collections.Generic;
|
|||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BotSharp.Plugin.AzureOpenAI.TextGeneratives;
|
||||
namespace BotSharp.Plugin.AzureOpenAI.Providers;
|
||||
|
||||
public class ChatCompletionProvider : IServiceZone
|
||||
{
|
||||
|
|
@ -5,7 +5,7 @@ using BotSharp.Platform.AzureAi;
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BotSharp.Plugin.AzureOpenAI.TextTasks;
|
||||
namespace BotSharp.Plugin.AzureOpenAI.Providers;
|
||||
|
||||
public class TextCompletionProvider : ITextCompletion
|
||||
{
|
||||
|
|
@ -1,130 +0,0 @@
|
|||
using Azure;
|
||||
using Azure.AI.OpenAI;
|
||||
using BotSharp.Abstraction.Infrastructures.ContentTransfers;
|
||||
using BotSharp.Abstraction.Infrastructures.ContentTransmitters;
|
||||
using BotSharp.Abstraction.Models;
|
||||
using BotSharp.Platform.AzureAi;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BotSharp.Plugin.AzureOpenAI.TextGeneratives;
|
||||
|
||||
public class ChatCompletionProvider : IServiceZone
|
||||
{
|
||||
private readonly AzureOpenAiSettings _settings;
|
||||
|
||||
public ChatCompletionProvider(AzureOpenAiSettings settings)
|
||||
{
|
||||
_settings = settings;
|
||||
}
|
||||
|
||||
public async Task GetChatCompletionsAsync(List<RoleDialogModel> conversations,
|
||||
Func<string, Task> onChunkReceived)
|
||||
{
|
||||
var client = new OpenAIClient(new Uri(_settings.Endpoint), new AzureKeyCredential(_settings.ApiKey));
|
||||
var chatCompletionsOptions = PrepareOptions(conversations);
|
||||
|
||||
var response = await client.GetChatCompletionsStreamingAsync(_settings.DeploymentModel, chatCompletionsOptions);
|
||||
using StreamingChatCompletions streaming = response.Value;
|
||||
|
||||
string content = "";
|
||||
await foreach (var choice in streaming.GetChoicesStreaming())
|
||||
{
|
||||
await foreach (var message in choice.GetMessageStreaming())
|
||||
{
|
||||
if (message.Content == null)
|
||||
continue;
|
||||
Console.Write(message.Content);
|
||||
content += message.Content;
|
||||
await onChunkReceived(message.Content);
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
public List<RoleDialogModel> GetChatSamples()
|
||||
{
|
||||
var samples = new List<RoleDialogModel>();
|
||||
if (!string.IsNullOrEmpty(_settings.ChatSampleFile))
|
||||
{
|
||||
var lines = File.ReadAllLines(_settings.ChatSampleFile);
|
||||
for (int i = 0; i < lines.Length; i++)
|
||||
{
|
||||
var line = lines[i];
|
||||
var role = line.Substring(0, line.IndexOf(' ') - 1);
|
||||
var content = line.Substring(line.IndexOf(' ') + 1);
|
||||
|
||||
samples.Add(new RoleDialogModel
|
||||
{
|
||||
Role = role,
|
||||
Content = content
|
||||
});
|
||||
}
|
||||
}
|
||||
return samples;
|
||||
}
|
||||
|
||||
public string GetInstruction()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_settings.InstructionFile))
|
||||
{
|
||||
return File.ReadAllText(_settings.InstructionFile);
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public async Task Serving(ContentContainer content)
|
||||
{
|
||||
var client = new OpenAIClient(new Uri(_settings.Endpoint), new AzureKeyCredential(_settings.ApiKey));
|
||||
var chatCompletionsOptions = PrepareOptions(content.Conversations);
|
||||
|
||||
var response = await client.GetChatCompletionsStreamingAsync(_settings.DeploymentModel, chatCompletionsOptions);
|
||||
using StreamingChatCompletions streaming = response.Value;
|
||||
|
||||
string output = "";
|
||||
await foreach (var choice in streaming.GetChoicesStreaming())
|
||||
{
|
||||
await foreach (var message in choice.GetMessageStreaming())
|
||||
{
|
||||
if (message.Content == null)
|
||||
continue;
|
||||
Console.Write(message.Content);
|
||||
output += message.Content;
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine();
|
||||
content.Output = new RoleDialogModel
|
||||
{
|
||||
Role = ChatRole.Assistant.ToString(),
|
||||
Content = output
|
||||
};
|
||||
}
|
||||
|
||||
private ChatCompletionsOptions PrepareOptions(List<RoleDialogModel> conversations)
|
||||
{
|
||||
var prompt = GetInstruction();
|
||||
var chatCompletionsOptions = new ChatCompletionsOptions()
|
||||
{
|
||||
Messages =
|
||||
{
|
||||
new ChatMessage(ChatRole.System, prompt)
|
||||
}
|
||||
};
|
||||
|
||||
foreach (var message in GetChatSamples())
|
||||
{
|
||||
chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content));
|
||||
}
|
||||
|
||||
foreach (var message in conversations)
|
||||
{
|
||||
chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content));
|
||||
}
|
||||
|
||||
return chatCompletionsOptions;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>10</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="C:\Users\haipi\.nuget\packages\fasttext.netwrapper\1.3.0\contentFiles\any\any\fasttext.dll" />
|
||||
<None Remove="C:\Users\haipi\.nuget\packages\fasttext.netwrapper\1.3.0\contentFiles\any\any\libfasttext.dylib" />
|
||||
<None Remove="C:\Users\haipi\.nuget\packages\fasttext.netwrapper\1.3.0\contentFiles\any\any\libfasttext.so" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
|
||||
<PackageReference Include="FastText.NetWrapper" Version="1.3.0" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
22
src/Plugins/BotSharp.Plugin.MetaAI/MetaAiPlugin.cs
Normal file
22
src/Plugins/BotSharp.Plugin.MetaAI/MetaAiPlugin.cs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
using BotSharp.Abstraction.MLTasks;
|
||||
using BotSharp.Abstraction.Plugins;
|
||||
using BotSharp.Plugin.MetaAI.Providers;
|
||||
using BotSharp.Plugin.MetaAI.Settings;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
|
||||
namespace BotSharp.Plugin.MetaAI;
|
||||
|
||||
public class MetaAiPlugin : IBotSharpPlugin
|
||||
{
|
||||
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
var settings = new MetaAiSettings();
|
||||
config.Bind("MetaAi", settings);
|
||||
services.AddSingleton(x => settings);
|
||||
services.AddSingleton(x => settings.fastText);
|
||||
|
||||
services.AddSingleton<ITextEmbedding, fastTextEmbeddingProvider>();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
using BotSharp.Abstraction.MLTasks;
|
||||
using BotSharp.Plugin.MetaAI.Settings;
|
||||
using FastText.NetWrapper;
|
||||
|
||||
namespace BotSharp.Plugin.MetaAI.Providers;
|
||||
|
||||
public class fastTextEmbeddingProvider : ITextEmbedding
|
||||
{
|
||||
private FastTextWrapper _fastText;
|
||||
private readonly fastTextSetting _settings;
|
||||
|
||||
public fastTextEmbeddingProvider(fastTextSetting settings)
|
||||
{
|
||||
_settings = settings;
|
||||
_fastText = new FastTextWrapper();
|
||||
|
||||
if (!_fastText.IsModelReady())
|
||||
{
|
||||
_fastText.LoadModel(settings.ModelPath);
|
||||
}
|
||||
}
|
||||
|
||||
public float[] GetVector(string text)
|
||||
{
|
||||
return _fastText.GetSentenceVector(text);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
namespace BotSharp.Plugin.MetaAI.Settings;
|
||||
|
||||
public class MetaAiSettings
|
||||
{
|
||||
public fastTextSetting fastText { get; set; }
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
namespace BotSharp.Plugin.MetaAI.Settings;
|
||||
|
||||
public class fastTextSetting
|
||||
{
|
||||
public string ModelPath { get; set; }
|
||||
}
|
||||
|
|
@ -31,6 +31,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.MetaAI\BotSharp.Plugin.MetaAI.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -30,6 +30,12 @@
|
|||
"DeploymentModel": ""
|
||||
},
|
||||
|
||||
"MetaAi": {
|
||||
"fastText": {
|
||||
"ModelPath": "crawl-300d-2M-subword.bin"
|
||||
}
|
||||
},
|
||||
|
||||
"Database": {
|
||||
"MongoDb": {
|
||||
"Master": "mongodb://localhost:27017/chat-ui"
|
||||
|
|
@ -45,7 +51,8 @@
|
|||
"PluginLoader": {
|
||||
"Assemblies": [
|
||||
"BotSharp.Core",
|
||||
"BotSharp.Plugin.AzureOpenAI"
|
||||
"BotSharp.Plugin.AzureOpenAI",
|
||||
"BotSharp.Plugin.MetaAI"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue