Simulate HuggingFace inference API.

This commit is contained in:
Haiping Chen 2023-06-07 22:43:16 -05:00
parent 1a96803f14
commit 295744958f
26 changed files with 194 additions and 130 deletions

View file

@ -19,15 +19,15 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Messages", "Messages", "{64
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Platforms", "Platforms", "{D5293208-2BEF-42FC-A64C-5954F61720BA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.AzureOpenAI", "src\Plugins\Platforms\BotSharp.Plugin.AureOpenAI\BotSharp.Plugin.AzureOpenAI.csproj", "{A8DF9393-9D67-460C-86F3-1D3C65A80D0A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.FacebookMessenger", "src\Plugins\BotSharp.Channel.FacebookMessenger\BotSharp.Plugin.FacebookMessenger.csproj", "{FF9B429F-972E-47CE-80A6-5339E6119EE6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.FacebookMessenger", "src\Plugins\Messages\BotSharp.Channel.FacebookMessenger\BotSharp.Plugin.FacebookMessenger.csproj", "{ACAB7D42-CCB3-4559-9DB7-B969C52A833D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.WeChat", "src\Plugins\BotSharp.Channel.WeChat\BotSharp.Plugin.WeChat.csproj", "{8014739C-1B56-4859-A5A5-CA2EF9C58833}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.Weixin", "src\Plugins\Messages\BotSharp.Channel.Weixin\BotSharp.Plugin.Weixin.csproj", "{4DA3E063-9F5A-4361-8EFE-8727409B238D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.AzureOpenAI", "src\Plugins\BotSharp.Plugin.AzureOpenAI\BotSharp.Plugin.AzureOpenAI.csproj", "{3EAB9CF3-0F47-4BFB-8BAC-8ADFF24AD899}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.ChatbotUI", "src\Plugins\UIs\BotSharp.Plugin.ChatbotUI\BotSharp.Plugin.ChatbotUI.csproj", "{BDCE7250-A5B0-4AA6-BEB6-B799C12990EF}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.HuggingFace", "src\Plugins\BotSharp.Plugin.HuggingFace\BotSharp.Plugin.HuggingFace.csproj", "{57806BAF-7736-425A-B499-13A2A2DF1E63}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.HuggingFace", "src\Plugins\Platforms\BotSharp.Plugin.HuggingFace\BotSharp.Plugin.HuggingFace.csproj", "{48889657-1BFF-4FDD-B702-CFA5F866BC20}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.ChatbotUI", "src\Plugins\BotSharp.Plugin.ChatbotUI\BotSharp.Plugin.ChatbotUI.csproj", "{68C7C9E9-496B-4004-A1F8-75FFB8C06C76}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -61,46 +61,46 @@ Global
{07AD18C5-CE7B-495A-815F-170E93CCC42A}.Release|Any CPU.Build.0 = Release|Any CPU
{07AD18C5-CE7B-495A-815F-170E93CCC42A}.Release|x64.ActiveCfg = Release|Any CPU
{07AD18C5-CE7B-495A-815F-170E93CCC42A}.Release|x64.Build.0 = Release|Any CPU
{A8DF9393-9D67-460C-86F3-1D3C65A80D0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A8DF9393-9D67-460C-86F3-1D3C65A80D0A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A8DF9393-9D67-460C-86F3-1D3C65A80D0A}.Debug|x64.ActiveCfg = Debug|Any CPU
{A8DF9393-9D67-460C-86F3-1D3C65A80D0A}.Debug|x64.Build.0 = Debug|Any CPU
{A8DF9393-9D67-460C-86F3-1D3C65A80D0A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A8DF9393-9D67-460C-86F3-1D3C65A80D0A}.Release|Any CPU.Build.0 = Release|Any CPU
{A8DF9393-9D67-460C-86F3-1D3C65A80D0A}.Release|x64.ActiveCfg = Release|Any CPU
{A8DF9393-9D67-460C-86F3-1D3C65A80D0A}.Release|x64.Build.0 = Release|Any CPU
{ACAB7D42-CCB3-4559-9DB7-B969C52A833D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ACAB7D42-CCB3-4559-9DB7-B969C52A833D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ACAB7D42-CCB3-4559-9DB7-B969C52A833D}.Debug|x64.ActiveCfg = Debug|Any CPU
{ACAB7D42-CCB3-4559-9DB7-B969C52A833D}.Debug|x64.Build.0 = Debug|Any CPU
{ACAB7D42-CCB3-4559-9DB7-B969C52A833D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ACAB7D42-CCB3-4559-9DB7-B969C52A833D}.Release|Any CPU.Build.0 = Release|Any CPU
{ACAB7D42-CCB3-4559-9DB7-B969C52A833D}.Release|x64.ActiveCfg = Release|Any CPU
{ACAB7D42-CCB3-4559-9DB7-B969C52A833D}.Release|x64.Build.0 = Release|Any CPU
{4DA3E063-9F5A-4361-8EFE-8727409B238D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4DA3E063-9F5A-4361-8EFE-8727409B238D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4DA3E063-9F5A-4361-8EFE-8727409B238D}.Debug|x64.ActiveCfg = Debug|Any CPU
{4DA3E063-9F5A-4361-8EFE-8727409B238D}.Debug|x64.Build.0 = Debug|Any CPU
{4DA3E063-9F5A-4361-8EFE-8727409B238D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4DA3E063-9F5A-4361-8EFE-8727409B238D}.Release|Any CPU.Build.0 = Release|Any CPU
{4DA3E063-9F5A-4361-8EFE-8727409B238D}.Release|x64.ActiveCfg = Release|Any CPU
{4DA3E063-9F5A-4361-8EFE-8727409B238D}.Release|x64.Build.0 = Release|Any CPU
{BDCE7250-A5B0-4AA6-BEB6-B799C12990EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BDCE7250-A5B0-4AA6-BEB6-B799C12990EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BDCE7250-A5B0-4AA6-BEB6-B799C12990EF}.Debug|x64.ActiveCfg = Debug|Any CPU
{BDCE7250-A5B0-4AA6-BEB6-B799C12990EF}.Debug|x64.Build.0 = Debug|Any CPU
{BDCE7250-A5B0-4AA6-BEB6-B799C12990EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BDCE7250-A5B0-4AA6-BEB6-B799C12990EF}.Release|Any CPU.Build.0 = Release|Any CPU
{BDCE7250-A5B0-4AA6-BEB6-B799C12990EF}.Release|x64.ActiveCfg = Release|Any CPU
{BDCE7250-A5B0-4AA6-BEB6-B799C12990EF}.Release|x64.Build.0 = Release|Any CPU
{48889657-1BFF-4FDD-B702-CFA5F866BC20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{48889657-1BFF-4FDD-B702-CFA5F866BC20}.Debug|Any CPU.Build.0 = Debug|Any CPU
{48889657-1BFF-4FDD-B702-CFA5F866BC20}.Debug|x64.ActiveCfg = Debug|Any CPU
{48889657-1BFF-4FDD-B702-CFA5F866BC20}.Debug|x64.Build.0 = Debug|Any CPU
{48889657-1BFF-4FDD-B702-CFA5F866BC20}.Release|Any CPU.ActiveCfg = Release|Any CPU
{48889657-1BFF-4FDD-B702-CFA5F866BC20}.Release|Any CPU.Build.0 = Release|Any CPU
{48889657-1BFF-4FDD-B702-CFA5F866BC20}.Release|x64.ActiveCfg = Release|Any CPU
{48889657-1BFF-4FDD-B702-CFA5F866BC20}.Release|x64.Build.0 = Release|Any CPU
{FF9B429F-972E-47CE-80A6-5339E6119EE6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FF9B429F-972E-47CE-80A6-5339E6119EE6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FF9B429F-972E-47CE-80A6-5339E6119EE6}.Debug|x64.ActiveCfg = Debug|Any CPU
{FF9B429F-972E-47CE-80A6-5339E6119EE6}.Debug|x64.Build.0 = Debug|Any CPU
{FF9B429F-972E-47CE-80A6-5339E6119EE6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FF9B429F-972E-47CE-80A6-5339E6119EE6}.Release|Any CPU.Build.0 = Release|Any CPU
{FF9B429F-972E-47CE-80A6-5339E6119EE6}.Release|x64.ActiveCfg = Release|Any CPU
{FF9B429F-972E-47CE-80A6-5339E6119EE6}.Release|x64.Build.0 = Release|Any CPU
{8014739C-1B56-4859-A5A5-CA2EF9C58833}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8014739C-1B56-4859-A5A5-CA2EF9C58833}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8014739C-1B56-4859-A5A5-CA2EF9C58833}.Debug|x64.ActiveCfg = Debug|Any CPU
{8014739C-1B56-4859-A5A5-CA2EF9C58833}.Debug|x64.Build.0 = Debug|Any CPU
{8014739C-1B56-4859-A5A5-CA2EF9C58833}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8014739C-1B56-4859-A5A5-CA2EF9C58833}.Release|Any CPU.Build.0 = Release|Any CPU
{8014739C-1B56-4859-A5A5-CA2EF9C58833}.Release|x64.ActiveCfg = Release|Any CPU
{8014739C-1B56-4859-A5A5-CA2EF9C58833}.Release|x64.Build.0 = Release|Any CPU
{3EAB9CF3-0F47-4BFB-8BAC-8ADFF24AD899}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3EAB9CF3-0F47-4BFB-8BAC-8ADFF24AD899}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3EAB9CF3-0F47-4BFB-8BAC-8ADFF24AD899}.Debug|x64.ActiveCfg = Debug|Any CPU
{3EAB9CF3-0F47-4BFB-8BAC-8ADFF24AD899}.Debug|x64.Build.0 = Debug|Any CPU
{3EAB9CF3-0F47-4BFB-8BAC-8ADFF24AD899}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3EAB9CF3-0F47-4BFB-8BAC-8ADFF24AD899}.Release|Any CPU.Build.0 = Release|Any CPU
{3EAB9CF3-0F47-4BFB-8BAC-8ADFF24AD899}.Release|x64.ActiveCfg = Release|Any CPU
{3EAB9CF3-0F47-4BFB-8BAC-8ADFF24AD899}.Release|x64.Build.0 = Release|Any CPU
{57806BAF-7736-425A-B499-13A2A2DF1E63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{57806BAF-7736-425A-B499-13A2A2DF1E63}.Debug|Any CPU.Build.0 = Debug|Any CPU
{57806BAF-7736-425A-B499-13A2A2DF1E63}.Debug|x64.ActiveCfg = Debug|Any CPU
{57806BAF-7736-425A-B499-13A2A2DF1E63}.Debug|x64.Build.0 = Debug|Any CPU
{57806BAF-7736-425A-B499-13A2A2DF1E63}.Release|Any CPU.ActiveCfg = Release|Any CPU
{57806BAF-7736-425A-B499-13A2A2DF1E63}.Release|Any CPU.Build.0 = Release|Any CPU
{57806BAF-7736-425A-B499-13A2A2DF1E63}.Release|x64.ActiveCfg = Release|Any CPU
{57806BAF-7736-425A-B499-13A2A2DF1E63}.Release|x64.Build.0 = Release|Any CPU
{68C7C9E9-496B-4004-A1F8-75FFB8C06C76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{68C7C9E9-496B-4004-A1F8-75FFB8C06C76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{68C7C9E9-496B-4004-A1F8-75FFB8C06C76}.Debug|x64.ActiveCfg = Debug|Any CPU
{68C7C9E9-496B-4004-A1F8-75FFB8C06C76}.Debug|x64.Build.0 = Debug|Any CPU
{68C7C9E9-496B-4004-A1F8-75FFB8C06C76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{68C7C9E9-496B-4004-A1F8-75FFB8C06C76}.Release|Any CPU.Build.0 = Release|Any CPU
{68C7C9E9-496B-4004-A1F8-75FFB8C06C76}.Release|x64.ActiveCfg = Release|Any CPU
{68C7C9E9-496B-4004-A1F8-75FFB8C06C76}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -111,11 +111,11 @@ Global
{42226933-0A0D-48E8-85EC-AFE773A1F841} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C}
{64264688-0F5C-4AB0-8F2B-B59B717CCE00} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C}
{D5293208-2BEF-42FC-A64C-5954F61720BA} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C}
{A8DF9393-9D67-460C-86F3-1D3C65A80D0A} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
{ACAB7D42-CCB3-4559-9DB7-B969C52A833D} = {64264688-0F5C-4AB0-8F2B-B59B717CCE00}
{4DA3E063-9F5A-4361-8EFE-8727409B238D} = {64264688-0F5C-4AB0-8F2B-B59B717CCE00}
{BDCE7250-A5B0-4AA6-BEB6-B799C12990EF} = {42226933-0A0D-48E8-85EC-AFE773A1F841}
{48889657-1BFF-4FDD-B702-CFA5F866BC20} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
{FF9B429F-972E-47CE-80A6-5339E6119EE6} = {64264688-0F5C-4AB0-8F2B-B59B717CCE00}
{8014739C-1B56-4859-A5A5-CA2EF9C58833} = {64264688-0F5C-4AB0-8F2B-B59B717CCE00}
{3EAB9CF3-0F47-4BFB-8BAC-8ADFF24AD899} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
{57806BAF-7736-425A-B499-13A2A2DF1E63} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
{68C7C9E9-496B-4004-A1F8-75FFB8C06C76} = {42226933-0A0D-48E8-85EC-AFE773A1F841}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}

View file

@ -24,7 +24,7 @@ BotSharp is in accordance with components principle strictly, decouples every pa
* Built-in multi-agents and conversation management.
* Support multiple LLM platforms.
* Support export/ import agent from other bot platforms directly.
* Support different open source UI [Chatbot UI](src\BotUIs\ChatbotUI\Chatbot-UI.md).
* Support different open source UI [Chatbot UI](src\Plugins\BotSharp.Plugin.ChatbotUI\Chatbot-UI.md), [HuggingChat UI](src\Plugins\BotSharp.Plugin.HuggingFace\HuggingChat-UI.md).
* Integrate with popular message channels like Facebook Messenger, Slack and Telegram.
### Quick Started

View file

@ -4,8 +4,4 @@
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
</ItemGroup>
</Project>

View file

@ -23,8 +23,4 @@
<PackageReference Include="Senparc.Weixin.MP.MVC" Version="7.12.5.7" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
</ItemGroup>
</Project>

View file

@ -11,7 +11,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
</ItemGroup>
</Project>

View file

@ -9,11 +9,11 @@
<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.5" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.0" />
<PackageReference Include="System.Text.Json" Version="7.0.0" />
<PackageReference Include="System.Text.Json" Version="7.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
</ItemGroup>
</Project>

View file

@ -1,4 +1,5 @@
using Azure.AI.OpenAI;
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace BotSharp.Plugin.ChatbotUI.ViewModels;
@ -6,6 +7,7 @@ namespace BotSharp.Plugin.ChatbotUI.ViewModels;
public class OpenAiChoice
{
[JsonPropertyName("finish_reason")]
[JsonProperty("finish_reason")]
public string FinishReason { get; set; }
public ChatMessage Delta { get; set; }
}

View file

@ -8,11 +8,11 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.0" />
<PackageReference Include="System.Text.Json" Version="7.0.0" />
<PackageReference Include="System.Text.Json" Version="7.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,130 @@
using BotSharp.Abstraction;
using BotSharp.Abstraction.ApiAdapters;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using System;
using System.Text;
using System.Threading.Tasks;
using BotSharp.Plugin.HuggingFace.HuggingChat.ViewModels;
using BotSharp.Abstraction.TextGeneratives;
using System.Collections.Generic;
using System.Threading;
namespace BotSharp.Plugin.HuggingFace.HuggingChat;
public class HuggingChatController : ControllerBase, IApiAdapter
{
private readonly IPlatformMidware _platform;
public HuggingChatController(IPlatformMidware platform)
{
_platform = platform;
}
[HttpPost("/conversation")]
public async Task<ConversationViewModel> NewSession([FromBody] ConversationCreationModel conversationCreationModel)
{
var session = await _platform.SessionService.NewSession("anonymous");
return new ConversationViewModel
{
ConversationId = session.SessionId
};
}
[HttpPost("/conversation/{id}/summarize")]
public string SummarizeTitle([FromRoute] string id)
{
return "SummarizeTitle";
}
[HttpPost("/models/OpenAssistant/{model}")]
public async Task SendMessage([FromRoute] string model, [FromBody] ChatInput message)
{
if (message.Stream)
{
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;
var response = new ChatResponse
{
Token = new TextToken
{
Id = 12092,
Text = "Hello"
}
};
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);
response = new ChatResponse
{
Token = new TextToken
{
Text = "<|endoftext|>",
Special = true
},
GeneratedText = "Hello there! How can I assist you today?"
};
json = JsonConvert.SerializeObject(response, new JsonSerializerSettings
{
Formatting = Formatting.None,
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
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);
}
else
{
Response.StatusCode = 200;
Response.Headers.Add(HeaderNames.ContentType, "application/json");
Response.Headers.Add(HeaderNames.CacheControl, "no-cache");
Response.Headers.Add(HeaderNames.Connection, "keep-alive");
var outputStream = Response.Body;
var response = new ChatResponse
{
Token = new TextToken
{
Text = "<|endoftext|>",
Special = true
},
GeneratedText = "Hello there! How can I assist you today?"
};
var json = JsonConvert.SerializeObject(new List<ChatResponse>
{
response
}, new JsonSerializerSettings
{
Formatting = Formatting.None,
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
var buffer = Encoding.UTF8.GetBytes(json);
await outputStream.WriteAsync(buffer, 0, buffer.Length);
}
}
}

View file

@ -3,4 +3,5 @@ namespace BotSharp.Plugin.HuggingFace.HuggingChat.ViewModels;
public class ChatInput
{
public string Inputs { get; set; }
public bool Stream { get; set; }
}

View file

@ -1,4 +1,5 @@
using BotSharp.Abstraction.TextGeneratives;
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace BotSharp.Plugin.HuggingFace.HuggingChat.ViewModels;
@ -7,6 +8,7 @@ public class ChatResponse
{
public TextToken Token { get; set; }
[JsonPropertyName("generated_text")]
[JsonProperty("generated_text")]
public string GeneratedText { get; set; }
public string Details { get; set; }
}

View file

@ -1,61 +0,0 @@
using BotSharp.Abstraction;
using BotSharp.Abstraction.ApiAdapters;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using System;
using System.Text;
using System.Threading.Tasks;
using BotSharp.Plugin.HuggingFace.HuggingChat.ViewModels;
namespace BotSharp.Plugin.HuggingFace.HuggingChat;
public class HuggingChatController : ControllerBase, IApiAdapter
{
private readonly IPlatformMidware _platform;
public HuggingChatController(IPlatformMidware platform)
{
_platform = platform;
}
[HttpPost("/conversation")]
public async Task<ConversationViewModel> NewSession([FromBody] ConversationCreationModel conversationCreationModel)
{
var session = await _platform.SessionService.NewSession("anonymous");
return new ConversationViewModel
{
ConversationId = session.SessionId
};
}
[HttpPost("/conversation/{id}/summarize")]
public string SummarizeTitle([FromRoute] string id)
{
return "SummarizeTitle";
}
[HttpPost("/models/OpenAssistant/{model}")]
public async Task SendMessage([FromRoute] string model, [FromBody] ChatInput message)
{
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;
var response = new ChatResponse
{
};
var json = JsonConvert.SerializeObject(response, new JsonSerializerSettings
{
Formatting = Formatting.None,
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
var buffer = Encoding.UTF8.GetBytes(json);
await outputStream.WriteAsync(buffer, 0, buffer.Length);
}
}

View file

@ -27,11 +27,9 @@
<ItemGroup>
<ProjectReference Include="..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
<ProjectReference Include="..\Plugins\Messages\BotSharp.Channel.FacebookMessenger\BotSharp.Plugin.FacebookMessenger.csproj" />
<ProjectReference Include="..\Plugins\Messages\BotSharp.Channel.Weixin\BotSharp.Plugin.Weixin.csproj" />
<ProjectReference Include="..\Plugins\Platforms\BotSharp.Plugin.AureOpenAI\BotSharp.Plugin.AzureOpenAI.csproj" />
<ProjectReference Include="..\Plugins\Platforms\BotSharp.Plugin.HuggingFace\BotSharp.Plugin.HuggingFace.csproj" />
<ProjectReference Include="..\Plugins\UIs\BotSharp.Plugin.ChatbotUI\BotSharp.Plugin.ChatbotUI.csproj" />
<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" />
</ItemGroup>
</Project>