Add LLamaSharp.
This commit is contained in:
parent
96e64e60fb
commit
4d5ac2111d
42
BotSharp.sln
42
BotSharp.sln
|
|
@ -23,11 +23,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebStarter", "src\WebStarte
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Platform.AzureAi", "src\Platforms\BotSharp.Platform.AureAi\BotSharp.Platform.AzureAi.csproj", "{B874E946-AE39-4731-8DA5-16A99115D111}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Platform.AzureAi", "src\Platforms\BotSharp.Platform.AureAi\BotSharp.Platform.AzureAi.csproj", "{B874E946-AE39-4731-8DA5-16A99115D111}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BotUIs", "BotUIs", "{42226933-0A0D-48E8-85EC-AFE773A1F841}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "UiAdapters", "UiAdapters", "{42226933-0A0D-48E8-85EC-AFE773A1F841}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatbotUI", "src\BotUIs\ChatbotUI\ChatbotUI.csproj", "{E33CC7F2-225E-42A9-8B4A-EAA97930D820}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChatbotUI", "src\UiAdapters\ChatbotUI\ChatbotUI.csproj", "{223895FB-EE4C-4225-8042-292352B43973}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Platform.Local", "src\Platforms\BotSharp.Platform.Local\BotSharp.Platform.Local.csproj", "{7ECA0231-ECE5-416B-AC61-9B1A8DACCD39}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Platform.LlamaSharp", "src\Platforms\BotSharp.Platform.LlamaSharp\BotSharp.Platform.LlamaSharp.csproj", "{0D2CA3E8-11A1-4CA4-B07B-4EF01378866D}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
|
@ -93,22 +93,22 @@ Global
|
||||||
{B874E946-AE39-4731-8DA5-16A99115D111}.Release|Any CPU.Build.0 = Release|Any CPU
|
{B874E946-AE39-4731-8DA5-16A99115D111}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{B874E946-AE39-4731-8DA5-16A99115D111}.Release|x64.ActiveCfg = Release|Any CPU
|
{B874E946-AE39-4731-8DA5-16A99115D111}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
{B874E946-AE39-4731-8DA5-16A99115D111}.Release|x64.Build.0 = Release|Any CPU
|
{B874E946-AE39-4731-8DA5-16A99115D111}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{E33CC7F2-225E-42A9-8B4A-EAA97930D820}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{223895FB-EE4C-4225-8042-292352B43973}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{E33CC7F2-225E-42A9-8B4A-EAA97930D820}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{223895FB-EE4C-4225-8042-292352B43973}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{E33CC7F2-225E-42A9-8B4A-EAA97930D820}.Debug|x64.ActiveCfg = Debug|Any CPU
|
{223895FB-EE4C-4225-8042-292352B43973}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
{E33CC7F2-225E-42A9-8B4A-EAA97930D820}.Debug|x64.Build.0 = Debug|Any CPU
|
{223895FB-EE4C-4225-8042-292352B43973}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
{E33CC7F2-225E-42A9-8B4A-EAA97930D820}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{223895FB-EE4C-4225-8042-292352B43973}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{E33CC7F2-225E-42A9-8B4A-EAA97930D820}.Release|Any CPU.Build.0 = Release|Any CPU
|
{223895FB-EE4C-4225-8042-292352B43973}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{E33CC7F2-225E-42A9-8B4A-EAA97930D820}.Release|x64.ActiveCfg = Release|Any CPU
|
{223895FB-EE4C-4225-8042-292352B43973}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
{E33CC7F2-225E-42A9-8B4A-EAA97930D820}.Release|x64.Build.0 = Release|Any CPU
|
{223895FB-EE4C-4225-8042-292352B43973}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{7ECA0231-ECE5-416B-AC61-9B1A8DACCD39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{0D2CA3E8-11A1-4CA4-B07B-4EF01378866D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{7ECA0231-ECE5-416B-AC61-9B1A8DACCD39}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{0D2CA3E8-11A1-4CA4-B07B-4EF01378866D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{7ECA0231-ECE5-416B-AC61-9B1A8DACCD39}.Debug|x64.ActiveCfg = Debug|Any CPU
|
{0D2CA3E8-11A1-4CA4-B07B-4EF01378866D}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
{7ECA0231-ECE5-416B-AC61-9B1A8DACCD39}.Debug|x64.Build.0 = Debug|Any CPU
|
{0D2CA3E8-11A1-4CA4-B07B-4EF01378866D}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
{7ECA0231-ECE5-416B-AC61-9B1A8DACCD39}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{0D2CA3E8-11A1-4CA4-B07B-4EF01378866D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{7ECA0231-ECE5-416B-AC61-9B1A8DACCD39}.Release|Any CPU.Build.0 = Release|Any CPU
|
{0D2CA3E8-11A1-4CA4-B07B-4EF01378866D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{7ECA0231-ECE5-416B-AC61-9B1A8DACCD39}.Release|x64.ActiveCfg = Release|Any CPU
|
{0D2CA3E8-11A1-4CA4-B07B-4EF01378866D}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
{7ECA0231-ECE5-416B-AC61-9B1A8DACCD39}.Release|x64.Build.0 = Release|Any CPU
|
{0D2CA3E8-11A1-4CA4-B07B-4EF01378866D}.Release|x64.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|
@ -120,8 +120,8 @@ Global
|
||||||
{36F5CEBD-31A8-4BEF-8BAA-BAC4E63E4815} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
|
{36F5CEBD-31A8-4BEF-8BAA-BAC4E63E4815} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
|
||||||
{286C7DE5-1EBC-4D28-AB25-4FAAD4C31D2A} = {FFA662D3-B67B-478D-92FC-76DC5EB64412}
|
{286C7DE5-1EBC-4D28-AB25-4FAAD4C31D2A} = {FFA662D3-B67B-478D-92FC-76DC5EB64412}
|
||||||
{B874E946-AE39-4731-8DA5-16A99115D111} = {FFA662D3-B67B-478D-92FC-76DC5EB64412}
|
{B874E946-AE39-4731-8DA5-16A99115D111} = {FFA662D3-B67B-478D-92FC-76DC5EB64412}
|
||||||
{E33CC7F2-225E-42A9-8B4A-EAA97930D820} = {42226933-0A0D-48E8-85EC-AFE773A1F841}
|
{223895FB-EE4C-4225-8042-292352B43973} = {42226933-0A0D-48E8-85EC-AFE773A1F841}
|
||||||
{7ECA0231-ECE5-416B-AC61-9B1A8DACCD39} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
|
{0D2CA3E8-11A1-4CA4-B07B-4EF01378866D} = {FFA662D3-B67B-478D-92FC-76DC5EB64412}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}
|
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
# The Open Source AI Chatbot Platform Builder
|
# Chatbot Platform Builder
|
||||||
|
## Powered by LLM
|
||||||
|
|
||||||
[](https://discord.gg/qRVm82fKTS)
|
[](https://discord.gg/qRVm82fKTS)
|
||||||
[](http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=sN9VVMwbWjs5L0ATpizKKxOcZdEPMrp8&authKey=RLDw41bLTrEyEgZZi%2FzT4pYk%2BwmEFgFcrhs8ZbkiVY7a4JFckzJefaYNW6Lk4yPX&noverify=0&group_code=985366726)
|
[](http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=sN9VVMwbWjs5L0ATpizKKxOcZdEPMrp8&authKey=RLDw41bLTrEyEgZZi%2FzT4pYk%2BwmEFgFcrhs8ZbkiVY7a4JFckzJefaYNW6Lk4yPX&noverify=0&group_code=985366726)
|
||||||
|
|
|
||||||
|
|
@ -1,39 +0,0 @@
|
||||||
using BotSharp.Abstraction;
|
|
||||||
using BotSharp.Abstraction.Models;
|
|
||||||
using ChatbotUI.ViewModels;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace BotSharp.Platform.AzureAi.Controllers;
|
|
||||||
|
|
||||||
[ApiController]
|
|
||||||
public class ChatbotUiController : ControllerBase, IBotUiAdapter
|
|
||||||
{
|
|
||||||
private readonly ILogger<ChatbotUiController> _logger;
|
|
||||||
|
|
||||||
public ChatbotUiController(ILogger<ChatbotUiController> logger)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet("/v1/models")]
|
|
||||||
[HttpGet("/openai/deployments")]
|
|
||||||
public OpenAiModels GetOpenAiModels()
|
|
||||||
{
|
|
||||||
return new OpenAiModels
|
|
||||||
{
|
|
||||||
Data = new List<AiModel>
|
|
||||||
{
|
|
||||||
new AiModel
|
|
||||||
{
|
|
||||||
Id = "gpt-3.5-turbo",
|
|
||||||
Model = "gpt-3.5-turbo",
|
|
||||||
Name = "Default (GPT-3.5)",
|
|
||||||
MaxLength = 4000,
|
|
||||||
TokenLimit = 4000
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -6,4 +6,10 @@
|
||||||
<LangVersion>10.0</LangVersion>
|
<LangVersion>10.0</LangVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
4
src/Infrastructure/BotSharp.Abstraction/GlobalUsing.cs
Normal file
4
src/Infrastructure/BotSharp.Abstraction/GlobalUsing.cs
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
global using System;
|
||||||
|
global using System.Collections.Generic;
|
||||||
|
global using System.Text;
|
||||||
|
global using System.Threading.Tasks;
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace BotSharp.Abstraction;
|
||||||
|
|
||||||
|
public interface IChatCompletionHandler
|
||||||
|
{
|
||||||
|
Task GetChatCompletionsAsync(string text, Func<string, bool, Task> onChunkReceived);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
namespace BotSharp.Abstraction;
|
||||||
|
|
||||||
|
public interface IPlatformMidware
|
||||||
|
{
|
||||||
|
Task GetChatCompletionsAsync(string text, Func<string, bool, Task> onChunkReceived);
|
||||||
|
}
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.1</TargetFramework>
|
<TargetFramework>netstandard2.1</TargetFramework>
|
||||||
<LangVersion>10.0</LangVersion>
|
<LangVersion>10.0</LangVersion>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
|
||||||
<Version>0.4.0</Version>
|
<Version>0.4.0</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|
@ -56,8 +56,6 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CherubNLP" Version="0.5.0" />
|
<PackageReference Include="CherubNLP" Version="0.5.0" />
|
||||||
<PackageReference Include="Colorful.Console" Version="1.2.15" />
|
<PackageReference Include="Colorful.Console" Version="1.2.15" />
|
||||||
<PackageReference Include="Ding.Core" Version="1.1.8" />
|
|
||||||
<PackageReference Include="DotNetToolkit" Version="1.7.0" />
|
|
||||||
<PackageReference Include="EntityFrameworkCore.BootKit" Version="6.2.0" />
|
<PackageReference Include="EntityFrameworkCore.BootKit" Version="6.2.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
using BotSharp.Core.Services;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace BotSharp.Core;
|
||||||
|
|
||||||
|
public static class BotSharpServiceCollectionExtensions
|
||||||
|
{
|
||||||
|
public static IServiceCollection AddBotSharp(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddScoped<IPlatformMidware, PlatformMidware>();
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void ConfigureBotSharp(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IApplicationBuilder UseBotSharp(this IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
if (app == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(app));
|
||||||
|
}
|
||||||
|
|
||||||
|
return app;
|
||||||
|
}
|
||||||
|
}
|
||||||
6
src/Infrastructure/BotSharp.Core/GlobalUsing.cs
Normal file
6
src/Infrastructure/BotSharp.Core/GlobalUsing.cs
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
global using System;
|
||||||
|
global using System.Collections.Generic;
|
||||||
|
global using System.Text;
|
||||||
|
global using System.Threading.Tasks;
|
||||||
|
global using BotSharp.Abstraction;
|
||||||
|
global using System.Linq;
|
||||||
22
src/Infrastructure/BotSharp.Core/Services/PlatformMidware.cs
Normal file
22
src/Infrastructure/BotSharp.Core/Services/PlatformMidware.cs
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace BotSharp.Core.Services;
|
||||||
|
|
||||||
|
public class PlatformMidware : IPlatformMidware
|
||||||
|
{
|
||||||
|
private readonly IServiceProvider _services;
|
||||||
|
public PlatformMidware(IServiceProvider services)
|
||||||
|
{
|
||||||
|
_services = services;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task GetChatCompletionsAsync(string text, Func<string, bool, Task> onChunkReceived)
|
||||||
|
{
|
||||||
|
var handlers = _services.GetServices<IChatCompletionHandler>().ToList();
|
||||||
|
for (int i = 0; i < handlers.Count(); i++)
|
||||||
|
{
|
||||||
|
var handler = handlers[i];
|
||||||
|
await handler.GetChatCompletionsAsync(text, onChunkReceived);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.1</TargetFramework>
|
<TargetFramework>netstandard2.1</TargetFramework>
|
||||||
|
|
@ -7,7 +7,11 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
|
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.5" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netstandard2.1</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<LangVersion>10</LangVersion>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="LLamaSharp" Version="0.3.0" />
|
||||||
|
<PackageReference Include="LLamaSharp.Backend.Cpu" Version="0.3.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
using BotSharp.Abstraction;
|
||||||
|
using BotSharp.Platform.LlamaSharp;
|
||||||
|
using LLama;
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace BotSharp.Platform.Local.Handlers;
|
||||||
|
|
||||||
|
public class ChatCompletionHandler : IChatCompletionHandler
|
||||||
|
{
|
||||||
|
private readonly IChatModel _model;
|
||||||
|
private readonly LlamaSharpSettings _settings;
|
||||||
|
|
||||||
|
public ChatCompletionHandler(LlamaSharpSettings settings)
|
||||||
|
{
|
||||||
|
_settings = settings;
|
||||||
|
_model = new LLamaModel(new LLamaParams(model: _settings.ModelPath,
|
||||||
|
n_ctx: 512,
|
||||||
|
interactive: true,
|
||||||
|
repeat_penalty: 1.0f,
|
||||||
|
verbose_prompt: false));
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(settings.InstructionFile))
|
||||||
|
{
|
||||||
|
var prompt = File.ReadAllText(settings.InstructionFile);
|
||||||
|
_model.InitChatPrompt(prompt, "UTF-8");
|
||||||
|
}
|
||||||
|
|
||||||
|
_model.InitChatAntiprompt(new string[] { "User:" });
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task GetChatCompletionsAsync(string text, Func<string, bool, Task> onChunkReceived)
|
||||||
|
{
|
||||||
|
string totalResponse = "";
|
||||||
|
foreach (var response in _model.Chat(text, "", "UTF-8"))
|
||||||
|
{
|
||||||
|
Console.WriteLine(response);
|
||||||
|
totalResponse += response;
|
||||||
|
onChunkReceived(response, false);
|
||||||
|
}
|
||||||
|
onChunkReceived("", true);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
namespace BotSharp.Platform.LlamaSharp;
|
||||||
|
|
||||||
|
public class LlamaSharpSettings
|
||||||
|
{
|
||||||
|
public string ModelPath { get; set; }
|
||||||
|
public string InstructionFile { get; set; }
|
||||||
|
public int MaxContextLength { get; set; } = 512;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
using BotSharp.Abstraction;
|
||||||
|
using BotSharp.Platform.LlamaSharp;
|
||||||
|
using BotSharp.Platform.Local.Handlers;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace BotSharp.Core;
|
||||||
|
|
||||||
|
public static class LlamaSharplServiceCollectionExtensions
|
||||||
|
{
|
||||||
|
public static IServiceCollection AddLlamaSharp(this IServiceCollection services, IConfiguration config)
|
||||||
|
{
|
||||||
|
services.AddSingleton(x =>
|
||||||
|
{
|
||||||
|
var settings = new LlamaSharpSettings();
|
||||||
|
config.Bind("LlamaSharp", settings);
|
||||||
|
return settings;
|
||||||
|
});
|
||||||
|
services.AddScoped<IChatCompletionHandler, ChatCompletionHandler>();
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFramework>netstandard2.1</TargetFramework>
|
|
||||||
<Nullable>enable</Nullable>
|
|
||||||
<LangVersion>10</LangVersion>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.1</TargetFramework>
|
<TargetFramework>netstandard2.1</TargetFramework>
|
||||||
|
|
@ -6,8 +6,4 @@
|
||||||
<LangVersion>10</LangVersion>
|
<LangVersion>10</LangVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,9 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.5" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.0" />
|
||||||
|
<PackageReference Include="System.Text.Json" Version="7.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
117
src/UiAdapters/ChatbotUI/ChatbotUiController.cs
Normal file
117
src/UiAdapters/ChatbotUI/ChatbotUiController.cs
Normal file
|
|
@ -0,0 +1,117 @@
|
||||||
|
using BotSharp.Abstraction;
|
||||||
|
using BotSharp.Abstraction.Models;
|
||||||
|
using ChatbotUI.ViewModels;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.Net.Http.Headers;
|
||||||
|
using Newtonsoft.Json.Serialization;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System;
|
||||||
|
using Azure.AI.OpenAI;
|
||||||
|
|
||||||
|
namespace BotSharp.Platform.AzureAi.Controllers;
|
||||||
|
|
||||||
|
[ApiController]
|
||||||
|
public class ChatbotUiController : ControllerBase, IBotUiAdapter
|
||||||
|
{
|
||||||
|
private readonly ILogger<ChatbotUiController> _logger;
|
||||||
|
private readonly IPlatformMidware _platform;
|
||||||
|
|
||||||
|
public ChatbotUiController(ILogger<ChatbotUiController> logger,
|
||||||
|
IPlatformMidware platform)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_platform = platform;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/v1/models")]
|
||||||
|
[HttpGet("/openai/deployments")]
|
||||||
|
public OpenAiModels GetOpenAiModels()
|
||||||
|
{
|
||||||
|
return new OpenAiModels
|
||||||
|
{
|
||||||
|
Data = new List<AiModel>
|
||||||
|
{
|
||||||
|
new AiModel
|
||||||
|
{
|
||||||
|
Id = "gpt-3.5-turbo",
|
||||||
|
Model = "gpt-3.5-turbo",
|
||||||
|
Name = "Default (GPT-3.5)",
|
||||||
|
MaxLength = 4000,
|
||||||
|
TokenLimit = 4000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/v1/chat/completions")]
|
||||||
|
[HttpPost("/openai/deployments/one/chat/completions")]
|
||||||
|
public async Task SendMessage([FromBody] OpenAiMessageInput input, [FromQuery(Name = "api-version")] string apiVersion = "2023-03-15-preview")
|
||||||
|
{
|
||||||
|
Response.StatusCode = 200;
|
||||||
|
Response.Headers.Add(HeaderNames.ContentType, "text/event-stream");
|
||||||
|
Response.Headers.Add(HeaderNames.CacheControl, "no-cache");
|
||||||
|
Response.Headers.Add(HeaderNames.Connection, "keep-alive");
|
||||||
|
var outputStream = Response.Body;
|
||||||
|
|
||||||
|
await _platform.GetChatCompletionsAsync(input.Messages.Last().Content, async (content, end) =>
|
||||||
|
{
|
||||||
|
if (end)
|
||||||
|
{
|
||||||
|
if (content.Length > 0)
|
||||||
|
{
|
||||||
|
await OnChunkReceived(outputStream, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
await OnEventCompleted(outputStream);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await OnChunkReceived(outputStream, content);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task OnChunkReceived(Stream outputStream, string content)
|
||||||
|
{
|
||||||
|
|
||||||
|
var response = new OpenAiChatOutput
|
||||||
|
{
|
||||||
|
Choices = new List<OpenAiChoice>
|
||||||
|
{
|
||||||
|
new OpenAiChoice
|
||||||
|
{
|
||||||
|
Delta = new ChatMessage(ChatRole.Assistant, content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var json = JsonConvert.SerializeObject(response, new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
Formatting = Formatting.None,
|
||||||
|
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||||
|
});
|
||||||
|
|
||||||
|
var buffer = Encoding.UTF8.GetBytes($"data:{json}\n");
|
||||||
|
await outputStream.WriteAsync(buffer, 0, buffer.Length);
|
||||||
|
await Task.Delay(100);
|
||||||
|
|
||||||
|
buffer = Encoding.UTF8.GetBytes("\n");
|
||||||
|
await outputStream.WriteAsync(buffer, 0, buffer.Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task OnEventCompleted(Stream outputStream)
|
||||||
|
{
|
||||||
|
var buffer = Encoding.UTF8.GetBytes("data:[DONE]\n");
|
||||||
|
await outputStream.WriteAsync(buffer, 0, buffer.Length);
|
||||||
|
|
||||||
|
buffer = Encoding.UTF8.GetBytes("\n");
|
||||||
|
await outputStream.WriteAsync(buffer, 0, buffer.Length);
|
||||||
|
|
||||||
|
await outputStream.FlushAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
8
src/UiAdapters/ChatbotUI/ViewModels/OpenAiChatOutput.cs
Normal file
8
src/UiAdapters/ChatbotUI/ViewModels/OpenAiChatOutput.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace ChatbotUI.ViewModels;
|
||||||
|
|
||||||
|
public class OpenAiChatOutput
|
||||||
|
{
|
||||||
|
public List<OpenAiChoice> Choices { get; set; }
|
||||||
|
}
|
||||||
11
src/UiAdapters/ChatbotUI/ViewModels/OpenAiChoice.cs
Normal file
11
src/UiAdapters/ChatbotUI/ViewModels/OpenAiChoice.cs
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
using Azure.AI.OpenAI;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace ChatbotUI.ViewModels;
|
||||||
|
|
||||||
|
public class OpenAiChoice
|
||||||
|
{
|
||||||
|
[JsonPropertyName("finish_reason")]
|
||||||
|
public string FinishReason { get; set; }
|
||||||
|
public ChatMessage Delta { get; set; }
|
||||||
|
}
|
||||||
7
src/UiAdapters/ChatbotUI/ViewModels/OpenAiMessageBody.cs
Normal file
7
src/UiAdapters/ChatbotUI/ViewModels/OpenAiMessageBody.cs
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
namespace ChatbotUI.ViewModels;
|
||||||
|
|
||||||
|
public class OpenAiMessageBody
|
||||||
|
{
|
||||||
|
public string Role { get; set; }
|
||||||
|
public string Content { get; set; }
|
||||||
|
}
|
||||||
20
src/UiAdapters/ChatbotUI/ViewModels/OpenAiMessageInput.cs
Normal file
20
src/UiAdapters/ChatbotUI/ViewModels/OpenAiMessageInput.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace ChatbotUI.ViewModels;
|
||||||
|
|
||||||
|
public class OpenAiMessageInput
|
||||||
|
{
|
||||||
|
public string Model { get; set; } = string.Empty;
|
||||||
|
public List<OpenAiMessageBody> Messages { get; set; } = new List<OpenAiMessageBody>();
|
||||||
|
[JsonPropertyName("max_tokens")]
|
||||||
|
public int MaxTokens { get; set; } = 4000;
|
||||||
|
public bool Stream { get; set; } = true;
|
||||||
|
public float Temperature { get; set; } = 0.9f;
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return string.Join("\n", Messages.Select(x => x.Role + ": " + x.Content));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,16 +0,0 @@
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
|
|
||||||
namespace WebStarter.Controllers
|
|
||||||
{
|
|
||||||
[ApiController]
|
|
||||||
[Route("[controller]")]
|
|
||||||
public class LlmAgentController : ControllerBase
|
|
||||||
{
|
|
||||||
private readonly ILogger<LlmAgentController> _logger;
|
|
||||||
|
|
||||||
public LlmAgentController(ILogger<LlmAgentController> logger)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
using BotSharp.Core;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
|
|
@ -8,9 +10,9 @@ builder.Services.AddSwaggerGen();
|
||||||
|
|
||||||
builder.Services.AddHttpContextAccessor();
|
builder.Services.AddHttpContextAccessor();
|
||||||
|
|
||||||
// Streaming response to Redis
|
// Add BotSharp
|
||||||
// var multiplexer = ConnectionMultiplexer.Connect("192.168.0.26:6379");
|
builder.Services.AddBotSharp();
|
||||||
// builder.Services.AddSingleton<IConnectionMultiplexer>(multiplexer);
|
builder.Services.AddLlamaSharp(builder.Configuration);
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
|
@ -25,4 +27,7 @@ app.UseAuthorization();
|
||||||
|
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
|
|
||||||
|
// Use BotSharp
|
||||||
|
app.UseBotSharp();
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|
|
||||||
7
src/WebStarter/Prompts/chat-with-bob.txt
Normal file
7
src/WebStarter/Prompts/chat-with-bob.txt
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
Transcript of a dialog, where the User interacts with an Assistant named Bob. Bob is helpful, kind, honest, good at writing, and never fails to answer the User's requests immediately and with precision.
|
||||||
|
|
||||||
|
User: Hello, Bob.
|
||||||
|
Bob: Hello. How may I help you today?
|
||||||
|
User: Please tell me the largest city in Europe.
|
||||||
|
Bob: Sure. The largest city in Europe is Moscow, the capital of Russia.
|
||||||
|
User:
|
||||||
|
|
@ -7,15 +7,24 @@
|
||||||
<UserSecretsId>4fb8c9df-7975-4926-ba73-46c8ca440691</UserSecretsId>
|
<UserSecretsId>4fb8c9df-7975-4926-ba73-46c8ca440691</UserSecretsId>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Remove="Prompts\chat-with-bob.txt" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Content Include="Prompts\chat-with-bob.txt">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\BotUIs\ChatbotUI\ChatbotUI.csproj" />
|
<ProjectReference Include="..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
|
||||||
<ProjectReference Include="..\Channels\BotSharp.Channel.FacebookMessenger\BotSharp.Channel.FacebookMessenger.csproj" />
|
<ProjectReference Include="..\Platforms\BotSharp.Platform.LlamaSharp\BotSharp.Platform.LlamaSharp.csproj" />
|
||||||
<ProjectReference Include="..\Channels\BotSharp.Channel.Weixin\BotSharp.Channel.Weixin.csproj" />
|
<ProjectReference Include="..\UiAdapters\ChatbotUI\ChatbotUI.csproj" />
|
||||||
<ProjectReference Include="..\Platforms\BotSharp.Platform.OpenAi\BotSharp.Platform.OpenAi.csproj" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -10,5 +10,11 @@
|
||||||
"OpenAi": {
|
"OpenAi": {
|
||||||
"userName": "",
|
"userName": "",
|
||||||
"password": ""
|
"password": ""
|
||||||
|
},
|
||||||
|
|
||||||
|
"LlamaSharp": {
|
||||||
|
"ModelPath": "C:\\Users\\haipi\\Downloads\\ggml-vic13b-q5_1.bin",
|
||||||
|
"InstructionFile": "Prompts\\chat-with-bob.txt",
|
||||||
|
"MaxContextLength": 512
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue