Added ContentTransfer.

This commit is contained in:
Haiping Chen 2023-06-12 22:27:31 -05:00
parent 08b96c2475
commit 2dd93aef42
19 changed files with 198 additions and 32 deletions

View file

@ -31,7 +31,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.WeChat", "s
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{32FAFFFE-A4CB-4FEE-BF7C-84518BBC6DCC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTest", "tests\UnitTest\UnitTest.csproj", "{0B6E1D7F-ABDE-47F6-8B2D-4483C2CFF2D6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTest", "tests\UnitTest\UnitTest.csproj", "{0B6E1D7F-ABDE-47F6-8B2D-4483C2CFF2D6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

View file

@ -0,0 +1,12 @@
using BotSharp.Abstraction.Models;
namespace BotSharp.Abstraction.Infrastructures.ContentTransmitters;
public class ContentContainer
{
public string UserId { get; set; }
public string SessionId { get; set; }
public string AgentId { get; set; }
public List<RoleDialogModel> Conversations { get; set; }
public RoleDialogModel Output { get; set; }
}

View file

@ -0,0 +1,8 @@
using BotSharp.Abstraction.Infrastructures.ContentTransfers;
namespace BotSharp.Abstraction.Infrastructures.ContentTransmitters;
public interface IContentTransfer
{
Task<TransportResult> Transport(ContentContainer input);
}

View file

@ -0,0 +1,8 @@
using BotSharp.Abstraction.Infrastructures.ContentTransmitters;
namespace BotSharp.Abstraction.Infrastructures.ContentTransfers;
public interface IServiceZone
{
Task Serving(ContentContainer content);
}

View file

@ -0,0 +1,7 @@
namespace BotSharp.Abstraction.Infrastructures.ContentTransfers;
public class TransportResult
{
public bool IsSuccess { get; set; }
public List<string> Messages { get; set; }
}

View file

@ -0,0 +1,5 @@
namespace BotSharp.Abstraction.Knowledges;
public interface IKnowledgeBase
{
}

View file

@ -1,12 +0,0 @@
using BotSharp.Abstraction.Models;
namespace BotSharp.Abstraction.TextGeneratives;
public interface IChatCompletionProvider
{
string GetInstruction();
List<RoleDialogModel> GetChatSamples();
Task GetChatCompletionsAsync(List<RoleDialogModel> conversations,
Func<string, Task> onChunkReceived);
}

View file

@ -1,8 +1,10 @@
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Infrastructures.ContentTransmitters;
using BotSharp.Abstraction.Users;
using BotSharp.Core.Agents.Services;
using BotSharp.Core.Conversations.Services;
using BotSharp.Core.Infrastructures;
using BotSharp.Core.Users.Services;
using BotSharp.Plugins.LLamaSharp;
using Microsoft.AspNetCore.Builder;
@ -20,6 +22,8 @@ public static class BotSharpServiceCollectionExtensions
services.AddScoped<ISessionService, SessionService>();
services.AddScoped<IConversationService, ConversationService>();
services.AddScoped<IContentTransfer, ContentTransfer>();
RegisterRepository(services, config);
RegisterPlugins(services, config);
@ -78,6 +82,6 @@ public static class BotSharpServiceCollectionExtensions
return settings;
});
// services.AddSingleton<IChatCompletionProvider, ChatCompletionProvider>();
// services.AddScoped<IServiceZone, ChatCompletionProvider>();
}
}

View file

@ -1,5 +1,7 @@
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Infrastructures.ContentTransmitters;
using BotSharp.Abstraction.Models;
using BotSharp.Core.Conversations.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@ -31,4 +33,29 @@ public class ConversationController : ControllerBase, IApiAdapter
{
var service = _services.GetRequiredService<ISessionService>();
}
[HttpPost("/conversation/{sessionId}")]
public async Task<MessageResponseModel> SendMessage([FromBody] NewMessageModel input)
{
var transmitter = _services.GetRequiredService<IContentTransfer>();
var container = new ContentContainer
{
Conversations = new List<RoleDialogModel>
{
new RoleDialogModel
{
Role = "user",
Content = input.Content
}
}
};
var result = await transmitter.Transport(container);
return new MessageResponseModel
{
Content = container.Output.Content
};
}
}

View file

@ -0,0 +1,6 @@
namespace BotSharp.Core.Conversations.ViewModels;
public class MessageResponseModel
{
public string Content { get; set; }
}

View file

@ -0,0 +1,6 @@
namespace BotSharp.Core.Conversations.ViewModels;
public class NewMessageModel
{
public string Content { get; set; }
}

View file

@ -0,0 +1,47 @@
using BotSharp.Abstraction.Infrastructures.ContentTransfers;
using BotSharp.Abstraction.Infrastructures.ContentTransmitters;
using BotSharp.Abstraction.Users;
namespace BotSharp.Core.Infrastructures;
public class ContentTransfer : IContentTransfer
{
private readonly IServiceProvider _services;
private readonly ICurrentUser _user;
public ContentTransfer(IServiceProvider services, ICurrentUser user)
{
_services = services;
_user = user;
}
public async Task<TransportResult> Transport(ContentContainer input)
{
input.UserId = _user.Id;
var result = new TransportResult
{
IsSuccess = true,
Messages = new List<string>()
};
var zones = _services.GetServices<IServiceZone>();
foreach (var zone in zones)
{
input.Output = null;
try
{
await zone.Serving(input);
}
catch (Exception ex)
{
result.IsSuccess = false;
result.Messages.Add(ex.Message);
}
}
return result;
}
}

View file

@ -0,0 +1,7 @@
using BotSharp.Abstraction.Knowledges;
namespace BotSharp.Core.Knowledges;
public class KnowledgeBase : IKnowledgeBase
{
}

View file

@ -1,11 +1,12 @@
using BotSharp.Abstraction.Infrastructures.ContentTransfers;
using BotSharp.Abstraction.Infrastructures.ContentTransmitters;
using BotSharp.Abstraction.Models;
using BotSharp.Abstraction.TextGeneratives;
using LLama;
using System.IO;
namespace BotSharp.Plugins.LLamaSharp;
public class ChatCompletionProvider : IChatCompletionProvider, IBotSharpPlugin
public class ChatCompletionProvider : IBotSharpPlugin, IServiceZone
{
private readonly IChatModel _model;
private readonly LlamaSharpSettings _settings;
@ -80,4 +81,9 @@ public class ChatCompletionProvider : IChatCompletionProvider, IBotSharpPlugin
return instruction;
}
public async Task Serving(ContentContainer content)
{
}
}

View file

@ -1,4 +1,4 @@
using BotSharp.Abstraction.TextGeneratives;
using BotSharp.Abstraction.Infrastructures.ContentTransfers;
using BotSharp.Platform.AzureAi;
using BotSharp.Plugin.AzureOpenAI.TextGeneratives;
using Microsoft.Extensions.Configuration;
@ -18,7 +18,7 @@ public static class AzureOpenAiServiceCollectionExtensions
return settings;
});
services.AddScoped<IChatCompletionProvider, ChatCompletionProvider>();
services.AddScoped<IServiceZone, ChatCompletionProvider>();
return services;
}

View file

@ -1,7 +1,8 @@
using Azure;
using Azure.AI.OpenAI;
using BotSharp.Abstraction.Infrastructures.ContentTransfers;
using BotSharp.Abstraction.Infrastructures.ContentTransmitters;
using BotSharp.Abstraction.Models;
using BotSharp.Abstraction.TextGeneratives;
using BotSharp.Platform.AzureAi;
using System;
using System.Collections.Generic;
@ -10,7 +11,7 @@ using System.Threading.Tasks;
namespace BotSharp.Plugin.AzureOpenAI.TextGeneratives;
public class ChatCompletionProvider : IChatCompletionProvider
public class ChatCompletionProvider : IServiceZone
{
private readonly AzureOpenAiSettings _settings;
@ -75,6 +76,34 @@ public class ChatCompletionProvider : IChatCompletionProvider
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();

View file

@ -13,21 +13,21 @@ using System;
using Azure.AI.OpenAI;
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Plugin.ChatbotUI.ViewModels;
using BotSharp.Abstraction.TextGeneratives;
using BotSharp.Abstraction.Infrastructures.ContentTransmitters;
using Microsoft.Extensions.DependencyInjection;
namespace BotSharp.Plugin.ChatbotUI.Controllers;
[ApiController]
public class ChatbotUiController : ControllerBase, IApiAdapter
{
private readonly IServiceProvider _services;
private readonly ILogger<ChatbotUiController> _logger;
private readonly IChatCompletionProvider _chatCompletionProvider;
public ChatbotUiController(ILogger<ChatbotUiController> logger,
IChatCompletionProvider chatCompletionProvider)
public ChatbotUiController(ILogger<ChatbotUiController> logger, IServiceProvider services)
{
_logger = logger;
_chatCompletionProvider = chatCompletionProvider;
_services = services;
}
[HttpGet("/v1/models")]
@ -64,12 +64,22 @@ public class ChatbotUiController : ControllerBase, IApiAdapter
Content = x.Content
}).ToList();
await _chatCompletionProvider.GetChatCompletionsAsync(conversations,
/*await _chatCompletionProvider.GetChatCompletionsAsync(conversations,
async content =>
{
await OnChunkReceived(outputStream, content);
});
});*/
var transmitter = _services.GetRequiredService<IContentTransfer>();
var container = new ContentContainer
{
Conversations = conversations
};
var result = await transmitter.Transport(container);
await OnChunkReceived(outputStream, container.Output.Content);
await OnEventCompleted(outputStream);
}

View file

@ -1,4 +1,3 @@
using BotSharp.Abstraction;
using BotSharp.Abstraction.ApiAdapters;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
@ -10,16 +9,13 @@ 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 IChatCompletionProvider _chatCompletionProvider;
public HuggingChatController(IChatCompletionProvider chatCompletionProvider)
public HuggingChatController()
{
_chatCompletionProvider = chatCompletionProvider;
}
/*[HttpPost("/conversation")]