Skip to content

Commit 0989696

Browse files
authored
Merge pull request #86 from hchen2020/master
Some fixes.
2 parents 1ecb441 + 0d6b547 commit 0989696

File tree

24 files changed

+118
-98
lines changed

24 files changed

+118
-98
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace BotSharp.Abstraction.Agents.Settings;
2+
3+
public class AgentSettings
4+
{
5+
public string DataDir { get; set; }
6+
}

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/Agents/Services/AgentService.CreateAgent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public partial class AgentService
66
{
77
public async Task<Agent> CreateAgent(Agent agent)
88
{
9-
var db = _services.GetRequiredService<AgentDbContext>();
9+
var db = _services.GetRequiredService<BotSharpDbContext>();
1010
var record = db.Agent.FirstOrDefault(x => x.OwnerId == _user.Id && x.Name == agent.Name);
1111
if (record != null)
1212
{
@@ -19,9 +19,9 @@ record = AgentRecord.FromAgent(agent);
1919
record.CreatedDateTime = DateTime.UtcNow;
2020
record.UpdatedDateTime = DateTime.UtcNow;
2121

22-
db.Transaction<IAgentTable>(delegate
22+
db.Transaction<IBotSharpTable>(delegate
2323
{
24-
db.Add<IAgentTable>(record);
24+
db.Add<IBotSharpTable>(record);
2525
});
2626

2727
return record.ToAgent();

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public partial class AgentService
77
{
88
public async Task<List<Agent>> GetAgents()
99
{
10-
var db = _services.GetRequiredService<AgentDbContext>();
10+
var db = _services.GetRequiredService<BotSharpDbContext>();
1111
var query = from agent in db.Agent
1212
where agent.OwnerId == _user.Id
1313
select agent.ToAgent();
@@ -16,7 +16,7 @@ public async Task<List<Agent>> GetAgents()
1616

1717
public async Task<Agent> GetAgent(string id)
1818
{
19-
var db = _services.GetRequiredService<AgentDbContext>();
19+
var db = _services.GetRequiredService<BotSharpDbContext>();
2020
var query = from agent in db.Agent
2121
where agent.Id == id
2222
select agent.ToAgent();

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ public partial class AgentService
77
{
88
public async Task UpdateAgent(Agent agent)
99
{
10-
var db = _services.GetRequiredService<AgentDbContext>();
10+
var db = _services.GetRequiredService<BotSharpDbContext>();
1111

12-
db.Transaction<IAgentTable>(delegate
12+
db.Transaction<IBotSharpTable>(delegate
1313
{
1414
var record = db.Agent.FirstOrDefault(x => x.OwnerId == agent.OwerId && x.Id == agent.Id);
1515

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ public partial class AgentService : IAgentService
66
{
77
private readonly IServiceProvider _services;
88
private readonly IUserIdentity _user;
9+
private readonly AgentSettings _settings;
910

10-
public AgentService(IServiceProvider services, IUserIdentity user)
11+
public AgentService(IServiceProvider services, IUserIdentity user, AgentSettings settings)
1112
{
1213
_services = services;
1314
_user = user;
15+
_settings = settings;
1416
}
1517

1618
public string GetAgentDataDir(string agentId)
1719
{
18-
var dir = Path.Combine("data", agentId);
20+
var dir = Path.Combine(_settings.DataDir, agentId);
1921
if (!Directory.Exists(dir))
2022
{
2123
Directory.CreateDirectory(dir);

src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using BotSharp.Abstraction.Conversations.Settings;
21
using Microsoft.AspNetCore.Builder;
32
using Microsoft.Extensions.Configuration;
43

@@ -13,22 +12,40 @@ public static IServiceCollection AddBotSharp(this IServiceCollection services, I
1312

1413
services.AddScoped<IAgentService, AgentService>();
1514

15+
var agentSettings = new AgentSettings();
16+
config.Bind("Agent", agentSettings);
17+
services.AddSingleton((IServiceProvider x) => agentSettings);
18+
1619
var convsationSettings = new ConversationSetting();
1720
config.Bind("Conversation", convsationSettings);
1821
services.AddSingleton((IServiceProvider x) => convsationSettings);
1922

2023
services.AddScoped<IConversationStorage, ConversationStorage>();
2124
services.AddScoped<IConversationService, ConversationService>();
2225

23-
RegisterRepository(services, config);
24-
2526
RegisterPlugins(services, config);
2627

2728
return services;
2829
}
2930

30-
public static void ConfigureBotSharp(this IServiceCollection services)
31+
public static IServiceCollection ConfigureBotSharpRepository<Tdb>(this IServiceCollection services, IConfiguration config)
32+
where Tdb : DataContext
3133
{
34+
var databaseSettings = new DatabaseSettings();
35+
config.Bind("Database", databaseSettings);
36+
services.AddSingleton((IServiceProvider x) => databaseSettings);
37+
38+
var myDatabaseSettings = new MyDatabaseSettings();
39+
config.Bind("Database", myDatabaseSettings);
40+
services.AddSingleton((IServiceProvider x) => databaseSettings);
41+
42+
services.AddScoped((IServiceProvider x)
43+
=> DataContextHelper.GetDbContext<MongoDbContext, Tdb>(myDatabaseSettings, x));
44+
45+
services.AddScoped((IServiceProvider x)
46+
=> DataContextHelper.GetDbContext<BotSharpDbContext, Tdb>(myDatabaseSettings, x));
47+
48+
return services;
3249
}
3350

3451
public static IApplicationBuilder UseBotSharp(this IApplicationBuilder app)
@@ -43,27 +60,6 @@ public static IApplicationBuilder UseBotSharp(this IApplicationBuilder app)
4360
return app;
4461
}
4562

46-
public static void RegisterRepository(IServiceCollection services, IConfiguration config)
47-
{
48-
var databaseSettings = new DatabaseSettings();
49-
config.Bind("Database", databaseSettings);
50-
services.AddSingleton((IServiceProvider x) => databaseSettings);
51-
52-
var myDatabaseSettings = new MyDatabaseSettings();
53-
config.Bind("Database", myDatabaseSettings);
54-
services.AddSingleton((IServiceProvider x) => databaseSettings);
55-
56-
services.AddScoped((IServiceProvider x) =>
57-
{
58-
return DataContextHelper.GetDbContext<MongoDbContext>(myDatabaseSettings, x);
59-
});
60-
61-
services.AddScoped((IServiceProvider x) =>
62-
{
63-
return DataContextHelper.GetDbContext<AgentDbContext>(myDatabaseSettings, x);
64-
});
65-
}
66-
6763
public static void RegisterPlugins(IServiceCollection services, IConfiguration config)
6864
{
6965
var pluginSettings = new PluginLoaderSettings();

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public Task DeleteConversation(string id)
3030

3131
public async Task<Conversation> GetConversation(string id)
3232
{
33-
var db = _services.GetRequiredService<AgentDbContext>();
33+
var db = _services.GetRequiredService<BotSharpDbContext>();
3434
var query = from sess in db.Conversation
3535
where sess.Id == id
3636
orderby sess.CreatedTime descending
@@ -40,7 +40,7 @@ orderby sess.CreatedTime descending
4040

4141
public async Task<List<Conversation>> GetConversations()
4242
{
43-
var db = _services.GetRequiredService<AgentDbContext>();
43+
var db = _services.GetRequiredService<BotSharpDbContext>();
4444
var query = from sess in db.Conversation
4545
where sess.UserId == _user.Id
4646
orderby sess.CreatedTime descending
@@ -50,16 +50,16 @@ orderby sess.CreatedTime descending
5050

5151
public async Task<Conversation> NewConversation(Conversation sess)
5252
{
53-
var db = _services.GetRequiredService<AgentDbContext>();
53+
var db = _services.GetRequiredService<BotSharpDbContext>();
5454

5555
var record = ConversationRecord.FromConversation(sess);
5656
record.Id = sess.Id.IfNullOrEmptyAs(Guid.NewGuid().ToString());
5757
record.UserId = sess.UserId.IfNullOrEmptyAs(_user.Id);
5858
record.Title = "New Conversation";
5959

60-
db.Transaction<IAgentTable>(delegate
60+
db.Transaction<IBotSharpTable>(delegate
6161
{
62-
db.Add<IAgentTable>(record);
62+
db.Add<IBotSharpTable>(record);
6363
});
6464

6565
_storage.InitStorage(sess.AgentId, record.Id);
@@ -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)

0 commit comments

Comments
 (0)