Skip to content

Commit 2904c92

Browse files
author
hchen2020
committed
Add IChatCompletion to IConversationCompletionHook
1 parent 12e7d32 commit 2904c92

File tree

12 files changed

+104
-58
lines changed

12 files changed

+104
-58
lines changed
Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using BotSharp.Abstraction.Conversations.Models;
2+
using BotSharp.Abstraction.MLTasks;
23

34
namespace BotSharp.Abstraction.Conversations;
45

5-
public abstract class ConversationCompletionHookBase
6+
public abstract class ConversationCompletionHookBase : IConversationCompletionHook
67
{
78
protected Agent _agent;
89
public Agent Agent => _agent;
@@ -13,11 +14,40 @@ public abstract class ConversationCompletionHookBase
1314
protected List<RoleDialogModel> _dialogs;
1415
public List<RoleDialogModel> Dialogs => _dialogs;
1516

16-
public IConversationCompletionHook SetContexts(Agent agent, Conversation conversation, List<RoleDialogModel> dialogs)
17+
protected IChatCompletion _chatCompletion;
18+
public IChatCompletion ChatCompletion => _chatCompletion;
19+
20+
public IConversationCompletionHook SetAgent(Agent agent)
1721
{
1822
_agent = agent;
23+
return this;
24+
}
25+
26+
public IConversationCompletionHook SetConversation(Conversation conversation)
27+
{
1928
_conversation = conversation;
29+
return this;
30+
}
31+
32+
public IConversationCompletionHook SetDialogs(List<RoleDialogModel> dialogs)
33+
{
2034
_dialogs = dialogs;
21-
return this as IConversationCompletionHook;
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);
2252
}
2353
}
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +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
{
78
Agent Agent { get; }
9+
IConversationCompletionHook SetAgent(Agent agent);
10+
811
Conversation Conversation { get; }
12+
IConversationCompletionHook SetConversation(Conversation conversation);
13+
914
List<RoleDialogModel> Dialogs { get; }
10-
IConversationCompletionHook SetContexts(Agent agent, Conversation conversation, List<RoleDialogModel> dialogs);
15+
IConversationCompletionHook SetDialogs(List<RoleDialogModel> dialogs);
16+
17+
IChatCompletion ChatCompletion { get; }
18+
IConversationCompletionHook SetChatCompletion(IChatCompletion chatCompletion);
19+
1120
Task BeforeCompletion();
1221
Task<string> AfterCompletion(string response);
1322
}

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

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,7 @@ public async Task<MessageResponseModel> SendMessage([FromRoute] string agentId,
4545
{
4646
var conv = _services.GetRequiredService<IConversationService>();
4747

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

5450
return new MessageResponseModel
5551
{

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public async Task<Conversation> NewConversation(Conversation sess)
5353
var db = _services.GetRequiredService<AgentDbContext>();
5454

5555
var record = ConversationRecord.FromConversation(sess);
56-
record.Id = Guid.NewGuid().ToString();
57-
record.UserId = _user.Id;
56+
record.Id = sess.Id ?? Guid.NewGuid().ToString();
57+
record.UserId = sess.UserId ?? _user.Id;
5858
record.Title = "New Conversation";
5959

6060
db.Transaction<IAgentTable>(delegate
@@ -75,11 +75,7 @@ public async Task<string> SendMessage(string agentId, string conversationId, Rol
7575

7676
var response = await SendMessage(agentId, conversationId, wholeDialogs);
7777

78-
_storage.Append(agentId, conversationId, new RoleDialogModel
79-
{
80-
Role = "assistant",
81-
Text = response
82-
});
78+
_storage.Append(agentId, conversationId, new RoleDialogModel("assistant", response));
8379

8480
return response;
8581
}
@@ -107,7 +103,10 @@ public async Task<string> SendMessage(string agentId, string conversationId, Lis
107103

108104
hooks.ForEach(hook =>
109105
{
110-
hook.SetContexts(agent, converation, wholeDialogs)
106+
hook.SetAgent(agent)
107+
.SetConversation(converation)
108+
.SetDialogs(wholeDialogs)
109+
.SetChatCompletion(chatCompletion)
111110
.BeforeCompletion();
112111
});
113112

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

src/Infrastructure/BotSharp.Core/Repository/DataContextHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ public static T GetDbContext<T>(MyDatabaseSettings settings, IServiceProvider se
3030
dc.BindDbContext<IAgentTable, DbContext4SqlServer2>(new DatabaseBind
3131
{
3232
ServiceProvider = serviceProvider,
33-
MasterConnection = new SqlConnection(settings.Agent.Master),
34-
SlaveConnections = settings.Agent.Slavers.Length == 0 ?
35-
new List<DbConnection> { new SqlConnection(settings.Agent.Master) } :
36-
settings.Agent.Slavers.Select(x => new SqlConnection(x) as DbConnection).ToList(),
33+
MasterConnection = new SqlConnection(settings.BotSharp.Master),
34+
SlaveConnections = settings.BotSharp.Slavers.Length == 0 ?
35+
new List<DbConnection> { new SqlConnection(settings.BotSharp.Master) } :
36+
settings.BotSharp.Slavers.Select(x => new SqlConnection(x) as DbConnection).ToList(),
3737
CreateDbIfNotExist = true
3838
});
3939
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
using EntityFrameworkCore.BootKit;
2-
31
namespace BotSharp.Core.Repository;
42

53
public class MyDatabaseSettings : DatabaseSettings
64
{
75
public string[] Assemblies { get; set; }
86
public DbConnectionSetting MongoDb { get; set; }
9-
public DbConnectionSetting Agent { get; set; }
7+
public DbConnectionSetting BotSharp { get; set; }
108
}

src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
1515
config.Bind("AzureOpenAi", settings);
1616
services.AddSingleton(x => settings);
1717

18-
services.AddSingleton<ITextCompletion, TextCompletionProvider>();
18+
services.AddScoped<ITextCompletion, TextCompletionProvider>();
1919
services.AddScoped<IChatCompletion, ChatCompletionProvider>();
2020
}
2121
}

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,31 @@ public ChatCompletionProvider(AzureOpenAiSettings settings)
4848
public List<RoleDialogModel> GetChatSamples(string sampleText)
4949
{
5050
var samples = new List<RoleDialogModel>();
51-
if (!string.IsNullOrEmpty(sampleText))
51+
if (string.IsNullOrEmpty(sampleText))
5252
{
53-
var lines = sampleText.Split('\n');
54-
for (int i = 0; i < lines.Length; i += 3)
53+
return samples;
54+
}
55+
56+
var lines = sampleText.Split('\n');
57+
for (int i = 0; i < lines.Length; i++)
58+
{
59+
var line = lines[i];
60+
if (string.IsNullOrEmpty(line.Trim()))
5561
{
56-
var line = lines[i];
57-
var role = line.Substring(0, line.IndexOf(' ') - 1).Trim();
58-
var content = line.Substring(line.IndexOf(' ') + 1).Trim();
59-
60-
samples.Add(new RoleDialogModel
61-
{
62-
Role = role,
63-
Text = content
64-
});
62+
continue;
6563
}
64+
var role = line.Substring(0, line.IndexOf(' ') - 1).Trim();
65+
var content = line.Substring(line.IndexOf(' ') + 1).Trim();
66+
67+
// comments
68+
if (role == "##")
69+
{
70+
continue;
71+
}
72+
73+
samples.Add(new RoleDialogModel(role, content));
6674
}
75+
6776
return samples;
6877
}
6978

@@ -104,8 +113,9 @@ private ChatCompletionsOptions PrepareOptions(Agent agent, List<RoleDialogModel>
104113
{
105114
chatCompletionsOptions.Messages.Add(new ChatMessage(ChatRole.System, agent.Knowledges));
106115
}
107-
108-
foreach (var message in GetChatSamples(agent.Samples))
116+
117+
var samples = GetChatSamples(agent.Samples);
118+
foreach (var message in samples)
109119
{
110120
chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Text));
111121
}

0 commit comments

Comments
 (0)