Skip to content

Commit 1ecb441

Browse files
authored
Merge pull request #85 from hchen2020/master
Fix IConversationCompletionHook
2 parents 6b2d1fa + b250fc7 commit 1ecb441

File tree

18 files changed

+179
-56
lines changed

18 files changed

+179
-56
lines changed

src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
<PackageIcon>Icon.png</PackageIcon>
99
</PropertyGroup>
1010

11+
<ItemGroup>
12+
<Compile Remove="Infrastructures\**" />
13+
<EmbeddedResource Remove="Infrastructures\**" />
14+
<None Remove="Infrastructures\**" />
15+
</ItemGroup>
16+
1117
<ItemGroup>
1218
<None Include="..\..\..\arts\Icon.png">
1319
<Pack>True</Pack>
@@ -22,8 +28,4 @@
2228
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
2329
</ItemGroup>
2430

25-
<ItemGroup>
26-
<Folder Include="Infrastructures\" />
27-
</ItemGroup>
28-
2931
</Project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using BotSharp.Abstraction.Conversations.Models;
2+
using BotSharp.Abstraction.MLTasks;
3+
4+
namespace BotSharp.Abstraction.Conversations;
5+
6+
public abstract class ConversationCompletionHookBase : IConversationCompletionHook
7+
{
8+
protected Agent _agent;
9+
public Agent Agent => _agent;
10+
11+
protected Conversation _conversation;
12+
public Conversation Conversation => _conversation;
13+
14+
protected List<RoleDialogModel> _dialogs;
15+
public List<RoleDialogModel> Dialogs => _dialogs;
16+
17+
protected IChatCompletion _chatCompletion;
18+
public IChatCompletion ChatCompletion => _chatCompletion;
19+
20+
public IConversationCompletionHook SetAgent(Agent agent)
21+
{
22+
_agent = agent;
23+
return this;
24+
}
25+
26+
public IConversationCompletionHook SetConversation(Conversation conversation)
27+
{
28+
_conversation = conversation;
29+
return this;
30+
}
31+
32+
public IConversationCompletionHook SetDialogs(List<RoleDialogModel> dialogs)
33+
{
34+
_dialogs = dialogs;
35+
return this;
36+
}
37+
38+
public IConversationCompletionHook SetChatCompletion(IChatCompletion chatCompletion)
39+
{
40+
_chatCompletion = chatCompletion;
41+
return this;
42+
}
43+
44+
public virtual Task BeforeCompletion()
45+
{
46+
return Task.CompletedTask;
47+
}
48+
49+
public virtual Task<string> AfterCompletion(string response)
50+
{
51+
return Task.FromResult(response);
52+
}
53+
}
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
using BotSharp.Abstraction.Conversations.Models;
2+
using BotSharp.Abstraction.MLTasks;
23

34
namespace BotSharp.Abstraction.Conversations;
45

56
public interface IConversationCompletionHook
67
{
7-
Task BeforeCompletion(Agent agent, List<RoleDialogModel> conversations);
8-
Task<string> AfterCompletion(Agent agent, string response);
8+
Agent Agent { get; }
9+
IConversationCompletionHook SetAgent(Agent agent);
10+
11+
Conversation Conversation { get; }
12+
IConversationCompletionHook SetConversation(Conversation conversation);
13+
14+
List<RoleDialogModel> Dialogs { get; }
15+
IConversationCompletionHook SetDialogs(List<RoleDialogModel> dialogs);
16+
17+
IChatCompletion ChatCompletion { get; }
18+
IConversationCompletionHook SetChatCompletion(IChatCompletion chatCompletion);
19+
20+
Task BeforeCompletion();
21+
Task<string> AfterCompletion(string response);
922
}

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.Abstraction/Conversations/Models/RoleDialogModel.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ public class RoleDialogModel
88
public string Role { get; set; }
99
public string Text { get; set; }
1010

11+
public RoleDialogModel(string role, string text)
12+
{
13+
Role = role;
14+
Text = text;
15+
}
16+
1117
public override string ToString()
1218
{
1319
return $"{Role}: {Text}";
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace BotSharp.Abstraction.Utilities;
2+
3+
public static class StringExtensions
4+
{
5+
public static string IfNullOrEmptyAs(this string str, string defaultValue)
6+
=> string.IsNullOrEmpty(str) ? defaultValue : str;
7+
}

src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,25 @@
3434
<PackageIconUrl>https://raw.githubusercontent.com/SciSharp/BotSharp/master/docs/static/logos/BotSharp.png</PackageIconUrl>
3535
<PackageLicenseUrl>https://raw.githubusercontent.com/SciSharp/BotSharp/master/LICENSE</PackageLicenseUrl>
3636
<PackageIcon>Icon.png</PackageIcon>
37+
<Nullable>enable</Nullable>
3738
</PropertyGroup>
3839

3940
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
4041
<DefineConstants>TRACE;DEBUG</DefineConstants>
42+
<NoWarn>1701;1702</NoWarn>
4143
</PropertyGroup>
4244

4345
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
4446
<DefineConstants>TRACE;</DefineConstants>
47+
<NoWarn>1701;1702</NoWarn>
48+
</PropertyGroup>
49+
50+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
51+
<NoWarn>1701;1702</NoWarn>
52+
</PropertyGroup>
53+
54+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
55+
<NoWarn>1701;1702</NoWarn>
4556
</PropertyGroup>
4657

4758
<ItemGroup>

src/Infrastructure/BotSharp.Core/Conversations/ConversationController.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public async Task<ConversationViewModel> NewConversation([FromRoute] string agen
2626
var service = _services.GetRequiredService<IConversationService>();
2727
var sess = new Conversation
2828
{
29+
UserId = _user.Id,
2930
AgentId = agentId
3031
};
3132
sess = await service.NewConversation(sess);
@@ -45,15 +46,11 @@ public async Task<MessageResponseModel> SendMessage([FromRoute] string agentId,
4546
{
4647
var conv = _services.GetRequiredService<IConversationService>();
4748

48-
var result = await conv.SendMessage(agentId, conversationId, new RoleDialogModel
49-
{
50-
Role = "user",
51-
Text = input.Text
52-
});
49+
var result = await conv.SendMessage(agentId, conversationId, new RoleDialogModel("user", input.Text));
5350

5451
return new MessageResponseModel
5552
{
56-
Content = result
53+
Text = result
5754
};
5855
}
5956
}

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

Lines changed: 23 additions & 9 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>();
@@ -43,8 +53,8 @@ public async Task<Conversation> NewConversation(Conversation sess)
4353
var db = _services.GetRequiredService<AgentDbContext>();
4454

4555
var record = ConversationRecord.FromConversation(sess);
46-
record.Id = Guid.NewGuid().ToString();
47-
record.UserId = _user.Id;
56+
record.Id = sess.Id.IfNullOrEmptyAs(Guid.NewGuid().ToString());
57+
record.UserId = sess.UserId.IfNullOrEmptyAs(_user.Id);
4858
record.Title = "New Conversation";
4959

5060
db.Transaction<IAgentTable>(delegate
@@ -65,18 +75,15 @@ public async Task<string> SendMessage(string agentId, string conversationId, Rol
6575

6676
var response = await SendMessage(agentId, conversationId, wholeDialogs);
6777

68-
_storage.Append(agentId, conversationId, new RoleDialogModel
69-
{
70-
Role = "assistant",
71-
Text = response
72-
});
78+
_storage.Append(agentId, conversationId, new RoleDialogModel("assistant", response));
7379

7480
return response;
7581
}
7682

7783
public async Task<string> SendMessage(string agentId, string conversationId, List<RoleDialogModel> wholeDialogs)
7884
{
7985
var agent = await _services.GetRequiredService<IAgentService>().GetAgent(agentId);
86+
var converation = await GetConversation(conversationId);
8087

8188
// Get relevant domain knowledge
8289
if (_settings.EnableKnowledgeBase)
@@ -94,14 +101,21 @@ public async Task<string> SendMessage(string agentId, string conversationId, Lis
94101
// Before chat completion hook
95102
var hooks = _services.GetServices<IConversationCompletionHook>().ToList();
96103

97-
hooks.ForEach(hook => hook.BeforeCompletion(agent, wholeDialogs));
104+
hooks.ForEach(hook =>
105+
{
106+
hook.SetAgent(agent)
107+
.SetConversation(converation)
108+
.SetDialogs(wholeDialogs)
109+
.SetChatCompletion(chatCompletion)
110+
.BeforeCompletion();
111+
});
98112

99113
var response = await chatCompletion.GetChatCompletionsAsync(agent, wholeDialogs);
100114

101115
// After chat completion hook
102116
hooks.ForEach(async hook =>
103117
{
104-
response = await hook.AfterCompletion(agent, response);
118+
response = await hook.AfterCompletion(response);
105119
});
106120

107121
return response;

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ public List<RoleDialogModel> GetDialogs(string agentId, string conversationId)
2626
var pos = x.IndexOf(':');
2727
var role = x.Substring(0, pos);
2828
var text = x.Substring(pos + 1);
29-
return new RoleDialogModel
30-
{
31-
Role = role,
32-
Text = text
33-
};
29+
return new RoleDialogModel(role, text);
3430
}).ToList();
3531
}
3632

0 commit comments

Comments
 (0)