Skip to content

Commit d8ec3ee

Browse files
author
hchen2020
committed
Change ChatMessage to RoleDialogModel.
1 parent b250fc7 commit d8ec3ee

File tree

10 files changed

+34
-36
lines changed

10 files changed

+34
-36
lines changed

src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ public class RoleDialogModel
66
/// user, system, assistant
77
/// </summary>
88
public string Role { get; set; }
9-
public string Text { get; set; }
9+
public string Content { get; set; }
1010

1111
public RoleDialogModel(string role, string text)
1212
{
1313
Role = role;
14-
Text = text;
14+
Content = text;
1515
}
1616

1717
public override string ToString()
1818
{
19-
return $"{Role}: {Text}";
19+
return $"{Role}: {Content}";
2020
}
2121
}

src/Infrastructure/BotSharp.Abstraction/MLTasks/IChatCompletion.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ namespace BotSharp.Abstraction.MLTasks;
44

55
public interface IChatCompletion
66
{
7-
Task<string> GetChatCompletionsAsync(Agent agent, List<RoleDialogModel> conversations);
7+
string GetChatCompletions(Agent agent, List<RoleDialogModel> conversations);
8+
Task<string> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogModel> conversations);
89
}

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public async Task<string> SendMessage(string agentId, string conversationId, Lis
9292
agent.Knowledges = await knowledge.GetKnowledges(new KnowledgeRetrievalModel
9393
{
9494
AgentId = agentId,
95-
Question = string.Join("\n", wholeDialogs.Select(x => x.Text))
95+
Question = string.Join("\n", wholeDialogs.Select(x => x.Content))
9696
});
9797
}
9898

@@ -110,7 +110,7 @@ public async Task<string> SendMessage(string agentId, string conversationId, Lis
110110
.BeforeCompletion();
111111
});
112112

113-
var response = await chatCompletion.GetChatCompletionsAsync(agent, wholeDialogs);
113+
var response = await chatCompletion.GetChatCompletionsStreamingAsync(agent, wholeDialogs);
114114

115115
// After chat completion hook
116116
hooks.ForEach(async hook =>

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public ConversationStorage(IAgentService agent)
1414
public void Append(string agentId, string conversationId, RoleDialogModel dialog)
1515
{
1616
var conversationFile = GetStorageFile(agentId, conversationId);
17-
File.AppendAllText(conversationFile, $"{dialog.Role}: {dialog.Text}\n");
17+
File.AppendAllText(conversationFile, $"{dialog.Role}: {dialog.Content}\n");
1818
}
1919

2020
public List<RoleDialogModel> GetDialogs(string agentId, string conversationId)

src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/ChatCompletionProvider.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ public ChatCompletionProvider(IServiceProvider services)
1414
_services = services;
1515
}
1616

17-
public Task<string> GetChatCompletionsAsync(Agent agent, List<RoleDialogModel> conversations)
17+
public string GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
18+
{
19+
throw new NotImplementedException();
20+
}
21+
22+
public Task<string> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogModel> conversations)
1823
{
1924
string totalResponse = "";
20-
var content = string.Join("\n", conversations.Select(x => $"{x.Role}: {x.Text.Replace("user:", "")}")).Trim();
25+
var content = string.Join("\n", conversations.Select(x => $"{x.Role}: {x.Content.Replace("user:", "")}")).Trim();
2126
content += "\nassistant: ";
2227

2328
var llama = _services.GetRequiredService<LlamaAiModel>();

src/Plugins/BotSharp.Plugin.AzureOpenAI/BotSharp.Plugin.AzureOpenAI.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.5" />
11+
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.6" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using BotSharp.Plugin.AzureOpenAI.Settings;
77
using System;
88
using System.Collections.Generic;
9-
using System.IO;
109
using System.Threading.Tasks;
1110

1211
namespace BotSharp.Plugin.AzureOpenAI.Providers;
@@ -20,30 +19,25 @@ public ChatCompletionProvider(AzureOpenAiSettings settings)
2019
_settings = settings;
2120
}
2221

23-
/*public async Task GetChatCompletionsAsync(List<RoleDialogModel> conversations,
24-
Func<string, Task> onChunkReceived)
22+
public string GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
2523
{
2624
var client = new OpenAIClient(new Uri(_settings.Endpoint), new AzureKeyCredential(_settings.ApiKey));
27-
var chatCompletionsOptions = PrepareOptions(conversations);
25+
var chatCompletionsOptions = PrepareOptions(agent, conversations);
2826

29-
var response = await client.GetChatCompletionsStreamingAsync(_settings.DeploymentModel.ChatCompletionModel, chatCompletionsOptions);
30-
using StreamingChatCompletions streaming = response.Value;
27+
var response = client.GetChatCompletions(_settings.DeploymentModel.ChatCompletionModel, chatCompletionsOptions);
3128

32-
string content = "";
33-
await foreach (var choice in streaming.GetChoicesStreaming())
29+
string output = "";
30+
foreach (var choice in response.Value.Choices)
3431
{
35-
await foreach (var message in choice.GetMessageStreaming())
36-
{
37-
if (message.Content == null)
38-
continue;
39-
Console.Write(message.Content);
40-
content += message.Content;
41-
await onChunkReceived(message.Content);
42-
}
32+
var message = choice.Message;
33+
if (message.Content == null)
34+
continue;
35+
Console.Write(message.Content);
36+
output += message.Content;
4337
}
4438

45-
Console.WriteLine();
46-
}*/
39+
return output.Trim();
40+
}
4741

4842
public List<RoleDialogModel> GetChatSamples(string sampleText)
4943
{
@@ -77,7 +71,7 @@ public List<RoleDialogModel> GetChatSamples(string sampleText)
7771
}
7872

7973

80-
public async Task<string> GetChatCompletionsAsync(Agent agent, List<RoleDialogModel> conversations)
74+
public async Task<string> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogModel> conversations)
8175
{
8276
var client = new OpenAIClient(new Uri(_settings.Endpoint), new AzureKeyCredential(_settings.ApiKey));
8377
var chatCompletionsOptions = PrepareOptions(agent, conversations);
@@ -117,12 +111,12 @@ private ChatCompletionsOptions PrepareOptions(Agent agent, List<RoleDialogModel>
117111
var samples = GetChatSamples(agent.Samples);
118112
foreach (var message in samples)
119113
{
120-
chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Text));
114+
chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content));
121115
}
122116

123117
foreach (var message in conversations)
124118
{
125-
chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Text));
119+
chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content));
126120
}
127121

128122
return chatCompletionsOptions;

src/Plugins/BotSharp.Plugin.ChatbotUI/BotSharp.Plugin.ChatbotUI.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.5" />
1211
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
1312
<PackageReference Include="System.Text.Json" Version="7.0.2" />
1413
</ItemGroup>

src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using System.Text;
1111
using System.Threading.Tasks;
1212
using System;
13-
using Azure.AI.OpenAI;
1413
using BotSharp.Abstraction.ApiAdapters;
1514
using BotSharp.Plugin.ChatbotUI.ViewModels;
1615
using Microsoft.Extensions.DependencyInjection;
@@ -94,7 +93,7 @@ private async Task OnChunkReceived(Stream outputStream, string content)
9493
{
9594
new OpenAiChoice
9695
{
97-
Delta = new ChatMessage(ChatRole.Assistant, content)
96+
Delta = new RoleDialogModel("assistant", content)
9897
}
9998
}
10099
};

src/Plugins/BotSharp.Plugin.ChatbotUI/ViewModels/OpenAiChoice.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Azure.AI.OpenAI;
1+
using BotSharp.Abstraction.Conversations.Models;
22
using Newtonsoft.Json;
33
using System.Text.Json.Serialization;
44

@@ -9,5 +9,5 @@ public class OpenAiChoice
99
[JsonPropertyName("finish_reason")]
1010
[JsonProperty("finish_reason")]
1111
public string FinishReason { get; set; }
12-
public ChatMessage Delta { get; set; }
12+
public RoleDialogModel Delta { get; set; }
1313
}

0 commit comments

Comments
 (0)