Change ChatMessage to RoleDialogModel.

This commit is contained in:
hchen2020 2023-07-21 15:15:09 -05:00
parent b250fc730d
commit d8ec3ee198
10 changed files with 34 additions and 36 deletions

View file

@ -6,16 +6,16 @@ public class RoleDialogModel
/// user, system, assistant
/// </summary>
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}";
}
}

View file

@ -4,5 +4,6 @@ namespace BotSharp.Abstraction.MLTasks;
public interface IChatCompletion
{
Task<string> GetChatCompletionsAsync(Agent agent, List<RoleDialogModel> conversations);
string GetChatCompletions(Agent agent, List<RoleDialogModel> conversations);
Task<string> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogModel> conversations);
}

View file

@ -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 =>

View file

@ -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<RoleDialogModel> GetDialogs(string agentId, string conversationId)

View file

@ -14,10 +14,15 @@ public class ChatCompletionProvider : IChatCompletion
_services = services;
}
public Task<string> GetChatCompletionsAsync(Agent agent, List<RoleDialogModel> conversations)
public string GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
{
throw new NotImplementedException();
}
public Task<string> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogModel> 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<LlamaAiModel>();

View file

@ -8,7 +8,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.5" />
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.6" />
</ItemGroup>
<ItemGroup>

View file

@ -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<RoleDialogModel> conversations,
Func<string, Task> onChunkReceived)
public string GetChatCompletions(Agent agent, List<RoleDialogModel> 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<RoleDialogModel> GetChatSamples(string sampleText)
{
@ -77,7 +71,7 @@ public class ChatCompletionProvider : IChatCompletion
}
public async Task<string> GetChatCompletionsAsync(Agent agent, List<RoleDialogModel> conversations)
public async Task<string> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogModel> 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;

View file

@ -8,7 +8,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.5" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="System.Text.Json" Version="7.0.2" />
</ItemGroup>

View file

@ -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)
}
}
};

View file

@ -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; }
}