Adapt HuggingChat UI (WIP).

This commit is contained in:
Haiping Chen 2023-06-02 21:07:30 -05:00
parent 405a725dc3
commit 66bd6e53dd
22 changed files with 307 additions and 9 deletions

View file

@ -29,6 +29,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChatbotUI", "src\UiAdapters
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Platform.LlamaSharp", "src\Platforms\BotSharp.Platform.LlamaSharp\BotSharp.Platform.LlamaSharp.csproj", "{0D2CA3E8-11A1-4CA4-B07B-4EF01378866D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HuggingChatUI", "src\UiAdapters\HuggingChatUI\HuggingChatUI.csproj", "{1340286B-2E39-4CE6-BFD2-0B6B124641D0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -109,6 +111,14 @@ Global
{0D2CA3E8-11A1-4CA4-B07B-4EF01378866D}.Release|Any CPU.Build.0 = Release|Any CPU
{0D2CA3E8-11A1-4CA4-B07B-4EF01378866D}.Release|x64.ActiveCfg = Release|Any CPU
{0D2CA3E8-11A1-4CA4-B07B-4EF01378866D}.Release|x64.Build.0 = Release|Any CPU
{1340286B-2E39-4CE6-BFD2-0B6B124641D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1340286B-2E39-4CE6-BFD2-0B6B124641D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1340286B-2E39-4CE6-BFD2-0B6B124641D0}.Debug|x64.ActiveCfg = Debug|Any CPU
{1340286B-2E39-4CE6-BFD2-0B6B124641D0}.Debug|x64.Build.0 = Debug|Any CPU
{1340286B-2E39-4CE6-BFD2-0B6B124641D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1340286B-2E39-4CE6-BFD2-0B6B124641D0}.Release|Any CPU.Build.0 = Release|Any CPU
{1340286B-2E39-4CE6-BFD2-0B6B124641D0}.Release|x64.ActiveCfg = Release|Any CPU
{1340286B-2E39-4CE6-BFD2-0B6B124641D0}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -122,6 +132,7 @@ Global
{B874E946-AE39-4731-8DA5-16A99115D111} = {FFA662D3-B67B-478D-92FC-76DC5EB64412}
{223895FB-EE4C-4225-8042-292352B43973} = {42226933-0A0D-48E8-85EC-AFE773A1F841}
{0D2CA3E8-11A1-4CA4-B07B-4EF01378866D} = {FFA662D3-B67B-478D-92FC-76DC5EB64412}
{1340286B-2E39-4CE6-BFD2-0B6B124641D0} = {42226933-0A0D-48E8-85EC-AFE773A1F841}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}

View file

@ -0,0 +1,10 @@
using BotSharp.Abstraction.Models;
namespace BotSharp.Abstraction.Conversations;
public interface IConversationService
{
void AddDialog(RoleDialogModel dialog);
List<RoleDialogModel> GetDialogHistory(string sessionId);
void CleanHistory();
}

View file

@ -0,0 +1,10 @@
using BotSharp.Abstraction.Conversations.Models;
namespace BotSharp.Abstraction.Conversations;
public interface ISessionService
{
SessionModel NewSession(string userId);
List<string> GetAllSessions(string userId);
void DeleteSession(string sessionId);
}

View file

@ -0,0 +1,7 @@
namespace BotSharp.Abstraction.Conversations.Models;
public class SessionModel
{
public string UserId { get; set; } = string.Empty;
public string SessionId { get; set; } = string.Empty;
}

View file

@ -1,9 +1,11 @@
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Models;
namespace BotSharp.Abstraction;
public interface IPlatformMidware
{
ISessionService SessionService { get; }
Task GetChatCompletionsAsync(List<RoleDialogModel> conversations,
Func<string, Task> onChunkReceived);
}

View file

@ -1,9 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Abstraction;
public interface ITaskRouter
{
}

View file

@ -1,5 +1,10 @@
using BotSharp.Abstraction.Conversations;
using BotSharp.Core.Conversations;
using BotSharp.Core.Repository;
using BotSharp.Core.Services;
using EntityFrameworkCore.BootKit;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace BotSharp.Core;
@ -9,6 +14,8 @@ public static class BotSharpServiceCollectionExtensions
public static IServiceCollection AddBotSharp(this IServiceCollection services)
{
services.AddScoped<IPlatformMidware, PlatformMidware>();
services.AddSingleton<IConversationService, ConversationService>();
return services;
}
@ -25,4 +32,26 @@ public static class BotSharpServiceCollectionExtensions
return app;
}
public static void RegisterRepository(this IServiceCollection services, IConfiguration config)
{
var databaseSettings = new DatabaseSettings();
config.Bind("Database", databaseSettings);
services.AddSingleton((IServiceProvider x) =>
{
return databaseSettings;
});
var myDatabaseSettings = new MyDatabaseSettings();
config.Bind("Database", myDatabaseSettings);
services.AddSingleton((IServiceProvider x) =>
{
return databaseSettings;
});
services.AddScoped((IServiceProvider x) =>
{
return DataContextHelper.GetDbContext<MongoDbContext>(myDatabaseSettings, x);
});
}
}

View file

@ -0,0 +1,52 @@
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Models;
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Core.Conversations;
public class ConversationService : IConversationService
{
Dictionary<string, List<RoleDialogModel>> _history;
public ConversationService()
{
_history = new Dictionary<string, List<RoleDialogModel>>();
}
public void AddDialog(RoleDialogModel dialog)
{
_history[Guid.Empty.ToString()].Add(dialog);
}
public void CleanHistory()
{
throw new NotImplementedException();
}
public void DeleteSession()
{
throw new NotImplementedException();
}
public List<string> GetAllSessions()
{
throw new NotImplementedException();
}
public List<RoleDialogModel> GetDialogHistory()
{
return _history[Guid.Empty.ToString()];
}
public List<RoleDialogModel> GetDialogHistory(string sessionId)
{
throw new NotImplementedException();
}
public string NewSession()
{
throw new NotImplementedException();
}
}

View file

@ -0,0 +1,32 @@
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Conversations.Models;
namespace BotSharp.Core.Conversations;
public class SessionService : ISessionService
{
Dictionary<string, List<string>> _sessions;
public SessionService()
{
_sessions = new Dictionary<string, List<string>>();
}
public void DeleteSession(string sessionId)
{
throw new NotImplementedException();
}
public List<string> GetAllSessions(string userId)
{
throw new NotImplementedException();
}
public SessionModel NewSession(string userId)
{
return new SessionModel
{
SessionId = Guid.NewGuid().ToString()
};
}
}

View file

@ -0,0 +1,28 @@
using EntityFrameworkCore.BootKit;
using Microsoft.Data.SqlClient;
namespace BotSharp.Core.Repository;
public static class DataContextHelper
{
public static T GetDbContext<T>(MyDatabaseSettings settings, IServiceProvider serviceProvider)
where T : Database, new()
{
if (settings.Assemblies == null)
throw new Exception("Please set assemblies.");
AppDomain.CurrentDomain.SetData("Assemblies", settings.Assemblies);
var dc = new T();
if (typeof(T) == typeof(DbContext4MongoDb))
{
dc.BindDbContext<IMongoDbCollection, DbContext4MongoDb>(new DatabaseBind
{
ServiceProvider = serviceProvider,
MasterConnection = new MongoDbConnection("mongodb://user:password@localhost:27017"),
CreateDbIfNotExist = true
});
}
return dc;
}
}

View file

@ -0,0 +1,5 @@
namespace BotSharp.Core.Repository;
public interface IMongoDbCollection
{
}

View file

@ -0,0 +1,7 @@
using EntityFrameworkCore.BootKit;
namespace BotSharp.Core.Repository;
public class MongoDbContext : Database
{
}

View file

@ -0,0 +1,9 @@
using EntityFrameworkCore.BootKit;
namespace BotSharp.Core.Repository;
public class MyDatabaseSettings : DatabaseSettings
{
public string[] Assemblies { get; set; }
public DbConnectionSetting MongoDb { get; set; }
}

View file

@ -1,3 +1,4 @@
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Models;
using Microsoft.Extensions.DependencyInjection;
@ -6,11 +7,16 @@ namespace BotSharp.Core.Services;
public class PlatformMidware : IPlatformMidware
{
private readonly IServiceProvider _services;
private readonly ISessionService _sessionService;
public PlatformMidware(IServiceProvider services)
{
_services = services;
_sessionService = services.GetRequiredService<ISessionService>();
}
public ISessionService SessionService => _sessionService;
public async Task GetChatCompletionsAsync(List<RoleDialogModel> conversations,
Func<string, Task> onChunkReceived)
{

View file

@ -0,0 +1,16 @@
# Chatbot UI
[HuggingChat UI](https://github.com/huggingface/chat-ui) is A chat interface using open source models, eg OpenAssistant..
```shell
git clone https://github.com/huggingface/chat-ui
npm i
npm run dev
```
* Rename `.env.local.example` to `.env.local`
```shell
# Change host to BotSharp service
OPENAI_API_HOST=http://localhost:5500
```

View file

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,31 @@
using BotSharp.Abstraction;
using HuggingChatUI.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System;
namespace HuggingChatUI;
public class HuggingChatUiController : ControllerBase, IBotUiAdapter
{
private readonly IPlatformMidware _platform;
public HuggingChatUiController(IPlatformMidware platform)
{
_platform = platform;
}
[HttpPost("conversation")]
public ConversationViewModel NewSession([FromBody] ConversationCreationModel conversationCreationModel)
{
var session = _platform.SessionService.NewSession("");
return new ConversationViewModel
{
ConversationId = session.SessionId.Replace("-", "").Substring(0, 24)
};
}
[HttpPost("conversation/{id}/summarize")]
public string SummarizeTitle([FromRoute] string id)
{
return "SummarizeTitle";
}
}

View file

@ -0,0 +1,6 @@
namespace HuggingChatUI.ViewModels;
public class ConversationCreationModel
{
public string Model { get; set; }
}

View file

@ -0,0 +1,6 @@
namespace HuggingChatUI.ViewModels;
public class ConversationViewModel
{
public string ConversationId { get; set; }
}

View file

@ -15,6 +15,17 @@ builder.Services.AddBotSharp();
builder.Services.AddLlamaSharp(builder.Configuration);
// builder.Services.AddAzureOpenAi(builder.Configuration);
builder.Services.AddCors(options =>
{
options.AddPolicy("MyCorsPolicy",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
@ -31,4 +42,8 @@ app.MapControllers();
// Use BotSharp
app.UseBotSharp();
#if DEBUG
app.UseCors("MyCorsPolicy");
#endif
app.Run();

View file

@ -31,6 +31,7 @@
<ProjectReference Include="..\Platforms\BotSharp.Platform.LlamaSharp\BotSharp.Platform.LlamaSharp.csproj" />
<ProjectReference Include="..\Platforms\BotSharp.Platform.OpenAi\BotSharp.Platform.OpenAi.csproj" />
<ProjectReference Include="..\UiAdapters\ChatbotUI\ChatbotUI.csproj" />
<ProjectReference Include="..\UiAdapters\HuggingChatUI\HuggingChatUI.csproj" />
</ItemGroup>
</Project>

View file

@ -26,5 +26,12 @@
"InstructionFile": "Prompts\\chat-with-bob.txt",
"ChatSampleFile": "Prompts\\chat-samples.txt",
"DeploymentModel": ""
},
"Database": {
"MongoDb": {
"Master": "mongodb://localhost:27017"
},
"Assemblies": [ "BotSharp.Core" ]
}
}