diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs index f8a3ecae..04f3e2e4 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs @@ -6,16 +6,16 @@ public class RoleDialogModel /// user, system, assistant /// public string Role { get; set; } - public string Text { get; set; } + public string Content { get; set; } public RoleDialogModel(string role, string text) { Role = role; - Text = text; + Content = text; } public override string ToString() { - return $"{Role}: {Text}"; + return $"{Role}: {Content}"; } } diff --git a/src/Infrastructure/BotSharp.Abstraction/MLTasks/IChatCompletion.cs b/src/Infrastructure/BotSharp.Abstraction/MLTasks/IChatCompletion.cs index 00ef6c7e..91dea670 100644 --- a/src/Infrastructure/BotSharp.Abstraction/MLTasks/IChatCompletion.cs +++ b/src/Infrastructure/BotSharp.Abstraction/MLTasks/IChatCompletion.cs @@ -4,5 +4,6 @@ namespace BotSharp.Abstraction.MLTasks; public interface IChatCompletion { - Task GetChatCompletionsAsync(Agent agent, List conversations); + string GetChatCompletions(Agent agent, List conversations); + Task GetChatCompletionsStreamingAsync(Agent agent, List conversations); } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs index 9f89a15f..b7edd207 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs @@ -92,7 +92,7 @@ public class ConversationService : IConversationService agent.Knowledges = await knowledge.GetKnowledges(new KnowledgeRetrievalModel { AgentId = agentId, - Question = string.Join("\n", wholeDialogs.Select(x => x.Text)) + Question = string.Join("\n", wholeDialogs.Select(x => x.Content)) }); } @@ -110,7 +110,7 @@ public class ConversationService : IConversationService .BeforeCompletion(); }); - var response = await chatCompletion.GetChatCompletionsAsync(agent, wholeDialogs); + var response = await chatCompletion.GetChatCompletionsStreamingAsync(agent, wholeDialogs); // After chat completion hook hooks.ForEach(async hook => diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs index 34d8f7a3..fc9564f9 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs @@ -14,7 +14,7 @@ public class ConversationStorage : IConversationStorage public void Append(string agentId, string conversationId, RoleDialogModel dialog) { var conversationFile = GetStorageFile(agentId, conversationId); - File.AppendAllText(conversationFile, $"{dialog.Role}: {dialog.Text}\n"); + File.AppendAllText(conversationFile, $"{dialog.Role}: {dialog.Content}\n"); } public List GetDialogs(string agentId, string conversationId) diff --git a/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/ChatCompletionProvider.cs b/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/ChatCompletionProvider.cs index edf9b217..35cdc80f 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/ChatCompletionProvider.cs +++ b/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/ChatCompletionProvider.cs @@ -14,10 +14,15 @@ public class ChatCompletionProvider : IChatCompletion _services = services; } - public Task GetChatCompletionsAsync(Agent agent, List conversations) + public string GetChatCompletions(Agent agent, List conversations) + { + throw new NotImplementedException(); + } + + public Task GetChatCompletionsStreamingAsync(Agent agent, List conversations) { string totalResponse = ""; - var content = string.Join("\n", conversations.Select(x => $"{x.Role}: {x.Text.Replace("user:", "")}")).Trim(); + var content = string.Join("\n", conversations.Select(x => $"{x.Role}: {x.Content.Replace("user:", "")}")).Trim(); content += "\nassistant: "; var llama = _services.GetRequiredService(); diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/BotSharp.Plugin.AzureOpenAI.csproj b/src/Plugins/BotSharp.Plugin.AzureOpenAI/BotSharp.Plugin.AzureOpenAI.csproj index 3ce7d561..651612a6 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/BotSharp.Plugin.AzureOpenAI.csproj +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/BotSharp.Plugin.AzureOpenAI.csproj @@ -8,7 +8,7 @@ - + diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs index 22b8c2fc..b2a47a67 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs @@ -6,7 +6,6 @@ using BotSharp.Abstraction.MLTasks; using BotSharp.Plugin.AzureOpenAI.Settings; using System; using System.Collections.Generic; -using System.IO; using System.Threading.Tasks; namespace BotSharp.Plugin.AzureOpenAI.Providers; @@ -20,30 +19,25 @@ public class ChatCompletionProvider : IChatCompletion _settings = settings; } - /*public async Task GetChatCompletionsAsync(List conversations, - Func onChunkReceived) + public string GetChatCompletions(Agent agent, List conversations) { var client = new OpenAIClient(new Uri(_settings.Endpoint), new AzureKeyCredential(_settings.ApiKey)); - var chatCompletionsOptions = PrepareOptions(conversations); + var chatCompletionsOptions = PrepareOptions(agent, conversations); - var response = await client.GetChatCompletionsStreamingAsync(_settings.DeploymentModel.ChatCompletionModel, chatCompletionsOptions); - using StreamingChatCompletions streaming = response.Value; + var response = client.GetChatCompletions(_settings.DeploymentModel.ChatCompletionModel, chatCompletionsOptions); - string content = ""; - await foreach (var choice in streaming.GetChoicesStreaming()) + string output = ""; + foreach (var choice in response.Value.Choices) { - await foreach (var message in choice.GetMessageStreaming()) - { - if (message.Content == null) - continue; - Console.Write(message.Content); - content += message.Content; - await onChunkReceived(message.Content); - } + var message = choice.Message; + if (message.Content == null) + continue; + Console.Write(message.Content); + output += message.Content; } - Console.WriteLine(); - }*/ + return output.Trim(); + } public List GetChatSamples(string sampleText) { @@ -77,7 +71,7 @@ public class ChatCompletionProvider : IChatCompletion } - public async Task GetChatCompletionsAsync(Agent agent, List conversations) + public async Task GetChatCompletionsStreamingAsync(Agent agent, List conversations) { var client = new OpenAIClient(new Uri(_settings.Endpoint), new AzureKeyCredential(_settings.ApiKey)); var chatCompletionsOptions = PrepareOptions(agent, conversations); @@ -117,12 +111,12 @@ public class ChatCompletionProvider : IChatCompletion var samples = GetChatSamples(agent.Samples); foreach (var message in samples) { - chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Text)); + chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content)); } foreach (var message in conversations) { - chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Text)); + chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content)); } return chatCompletionsOptions; diff --git a/src/Plugins/BotSharp.Plugin.ChatbotUI/BotSharp.Plugin.ChatbotUI.csproj b/src/Plugins/BotSharp.Plugin.ChatbotUI/BotSharp.Plugin.ChatbotUI.csproj index 065c06ae..75da1f2c 100644 --- a/src/Plugins/BotSharp.Plugin.ChatbotUI/BotSharp.Plugin.ChatbotUI.csproj +++ b/src/Plugins/BotSharp.Plugin.ChatbotUI/BotSharp.Plugin.ChatbotUI.csproj @@ -8,7 +8,6 @@ - diff --git a/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs b/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs index 0337c983..6e7ad937 100644 --- a/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs +++ b/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs @@ -10,7 +10,6 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System; -using Azure.AI.OpenAI; using BotSharp.Abstraction.ApiAdapters; using BotSharp.Plugin.ChatbotUI.ViewModels; using Microsoft.Extensions.DependencyInjection; @@ -94,7 +93,7 @@ public class ChatbotUiController : ControllerBase, IApiAdapter { new OpenAiChoice { - Delta = new ChatMessage(ChatRole.Assistant, content) + Delta = new RoleDialogModel("assistant", content) } } }; diff --git a/src/Plugins/BotSharp.Plugin.ChatbotUI/ViewModels/OpenAiChoice.cs b/src/Plugins/BotSharp.Plugin.ChatbotUI/ViewModels/OpenAiChoice.cs index a72510df..02bb4792 100644 --- a/src/Plugins/BotSharp.Plugin.ChatbotUI/ViewModels/OpenAiChoice.cs +++ b/src/Plugins/BotSharp.Plugin.ChatbotUI/ViewModels/OpenAiChoice.cs @@ -1,4 +1,4 @@ -using Azure.AI.OpenAI; +using BotSharp.Abstraction.Conversations.Models; using Newtonsoft.Json; using System.Text.Json.Serialization; @@ -9,5 +9,5 @@ public class OpenAiChoice [JsonPropertyName("finish_reason")] [JsonProperty("finish_reason")] public string FinishReason { get; set; } - public ChatMessage Delta { get; set; } + public RoleDialogModel Delta { get; set; } }