Skip to content

Commit 12e7d32

Browse files
author
hchen2020
committed
Update IConversationCompletionHook.
1 parent 6b2d1fa commit 12e7d32

File tree

7 files changed

+63
-9
lines changed

7 files changed

+63
-9
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using BotSharp.Abstraction.Conversations.Models;
2+
3+
namespace BotSharp.Abstraction.Conversations;
4+
5+
public abstract class ConversationCompletionHookBase
6+
{
7+
protected Agent _agent;
8+
public Agent Agent => _agent;
9+
10+
protected Conversation _conversation;
11+
public Conversation Conversation => _conversation;
12+
13+
protected List<RoleDialogModel> _dialogs;
14+
public List<RoleDialogModel> Dialogs => _dialogs;
15+
16+
public IConversationCompletionHook SetContexts(Agent agent, Conversation conversation, List<RoleDialogModel> dialogs)
17+
{
18+
_agent = agent;
19+
_conversation = conversation;
20+
_dialogs = dialogs;
21+
return this as IConversationCompletionHook;
22+
}
23+
}

src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationCompletionHook.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ namespace BotSharp.Abstraction.Conversations;
44

55
public interface IConversationCompletionHook
66
{
7-
Task BeforeCompletion(Agent agent, List<RoleDialogModel> conversations);
8-
Task<string> AfterCompletion(Agent agent, string response);
7+
Agent Agent { get; }
8+
Conversation Conversation { get; }
9+
List<RoleDialogModel> Dialogs { get; }
10+
IConversationCompletionHook SetContexts(Agent agent, Conversation conversation, List<RoleDialogModel> dialogs);
11+
Task BeforeCompletion();
12+
Task<string> AfterCompletion(string response);
913
}

src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace BotSharp.Abstraction.Conversations;
55
public interface IConversationService
66
{
77
Task<Conversation> NewConversation(Conversation conversation);
8+
Task<Conversation> GetConversation(string id);
89
Task<List<Conversation>> GetConversations();
910
Task DeleteConversation(string id);
1011
Task<string> SendMessage(string agentId, string conversationId, RoleDialogModel lastDalog);

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ public Task DeleteConversation(string id)
2828
throw new NotImplementedException();
2929
}
3030

31+
public async Task<Conversation> GetConversation(string id)
32+
{
33+
var db = _services.GetRequiredService<AgentDbContext>();
34+
var query = from sess in db.Conversation
35+
where sess.Id == id
36+
orderby sess.CreatedTime descending
37+
select sess.ToConversation();
38+
return query.FirstOrDefault();
39+
}
40+
3141
public async Task<List<Conversation>> GetConversations()
3242
{
3343
var db = _services.GetRequiredService<AgentDbContext>();
@@ -77,6 +87,7 @@ public async Task<string> SendMessage(string agentId, string conversationId, Rol
7787
public async Task<string> SendMessage(string agentId, string conversationId, List<RoleDialogModel> wholeDialogs)
7888
{
7989
var agent = await _services.GetRequiredService<IAgentService>().GetAgent(agentId);
90+
var converation = await GetConversation(conversationId);
8091

8192
// Get relevant domain knowledge
8293
if (_settings.EnableKnowledgeBase)
@@ -94,14 +105,18 @@ public async Task<string> SendMessage(string agentId, string conversationId, Lis
94105
// Before chat completion hook
95106
var hooks = _services.GetServices<IConversationCompletionHook>().ToList();
96107

97-
hooks.ForEach(hook => hook.BeforeCompletion(agent, wholeDialogs));
108+
hooks.ForEach(hook =>
109+
{
110+
hook.SetContexts(agent, converation, wholeDialogs)
111+
.BeforeCompletion();
112+
});
98113

99114
var response = await chatCompletion.GetChatCompletionsAsync(agent, wholeDialogs);
100115

101116
// After chat completion hook
102117
hooks.ForEach(async hook =>
103118
{
104-
response = await hook.AfterCompletion(agent, response);
119+
response = await hook.AfterCompletion(response);
105120
});
106121

107122
return response;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ public List<RoleDialogModel> GetChatSamples(string sampleText)
5151
if (!string.IsNullOrEmpty(sampleText))
5252
{
5353
var lines = sampleText.Split('\n');
54-
for (int i = 0; i < lines.Length; i++)
54+
for (int i = 0; i < lines.Length; i += 3)
5555
{
5656
var line = lines[i];
57-
var role = line.Substring(0, line.IndexOf(' ') - 1);
58-
var content = line.Substring(line.IndexOf(' ') + 1);
57+
var role = line.Substring(0, line.IndexOf(' ') - 1).Trim();
58+
var content = line.Substring(line.IndexOf(' ') + 1).Trim();
5959

6060
samples.Add(new RoleDialogModel
6161
{

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,23 @@ public async Task SendMessage([FromBody] OpenAiMessageInput input)
5959
Response.Headers.Add(HeaderNames.Connection, "keep-alive");
6060
var outputStream = Response.Body;
6161

62-
var conversations = input.Messages.Skip(1).Select(x => new RoleDialogModel
62+
var conversations = input.Messages.Select(x => new RoleDialogModel
6363
{
6464
Role = x.Role,
6565
Text = x.Content
6666
}).ToList();
6767

6868
var conv = _services.GetRequiredService<IConversationService>();
6969

70-
var result = await conv.SendMessage("", "", conversations.Last());
70+
// Check if this conversation exists
71+
var converation = await conv.GetConversation(input.ConversationId);
72+
var sess = new Conversation
73+
{
74+
AgentId = input.AgentId
75+
};
76+
sess = await conv.NewConversation(sess);
77+
78+
var result = await conv.SendMessage(input.AgentId, input.ConversationId, conversations);
7179

7280
await OnChunkReceived(outputStream, result);
7381
await OnEventCompleted(outputStream);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ namespace BotSharp.Plugin.ChatbotUI.ViewModels;
66

77
public class OpenAiMessageInput
88
{
9+
public string AgentId { get; set; }
10+
public string ConversationId { get; set; }
911
public string Model { get; set; } = string.Empty;
1012
public List<OpenAiMessageBody> Messages { get; set; } = new List<OpenAiMessageBody>();
1113
[JsonPropertyName("max_tokens")]
1214
public int MaxTokens { get; set; } = 4000;
1315
public bool Stream { get; set; } = true;
16+
public string? SystemPrompt { get; set; }
1417
public float Temperature { get; set; } = 0.9f;
1518

1619
public override string ToString()

0 commit comments

Comments
 (0)