diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs
index 99d03bdc..256c2ed9 100644
--- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs
+++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs
@@ -2,13 +2,22 @@ namespace BotSharp.Abstraction.Agents.Models;
public class Agent
{
- [StringLength(36)]
public string Id { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public DateTime CreatedDateTime { get; set; }
public DateTime UpdatedDateTime { get; set; }
+ ///
+ /// Instruction
+ ///
+ public string Instruction { get; set; }
+
+ ///
+ /// Samples
+ ///
+ public string Samples { get; set; }
+
///
/// Owner user id
///
diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IChatServiceZone.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IChatServiceZone.cs
new file mode 100644
index 00000000..10b66e01
--- /dev/null
+++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IChatServiceZone.cs
@@ -0,0 +1,11 @@
+using BotSharp.Abstraction.Infrastructures.ContentTransfers;
+
+namespace BotSharp.Abstraction.Conversations;
+
+///
+/// IChatServiceZone is used to manage the chat function.
+/// When user send message to controller, all the registered service zone will process the message respectively.
+///
+public interface IChatServiceZone : IServiceZone
+{
+}
diff --git a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/IServiceZone.cs b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/IServiceZone.cs
index 34b27ede..290cecdf 100644
--- a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/IServiceZone.cs
+++ b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/ContentTransfers/IServiceZone.cs
@@ -4,5 +4,6 @@ namespace BotSharp.Abstraction.Infrastructures.ContentTransfers;
public interface IServiceZone
{
+ int Priority { get; }
Task Serving(ContentContainer content);
}
diff --git a/src/Infrastructure/BotSharp.Abstraction/Models/RoleDialogModel.cs b/src/Infrastructure/BotSharp.Abstraction/Models/RoleDialogModel.cs
index 4fc56cc4..81834102 100644
--- a/src/Infrastructure/BotSharp.Abstraction/Models/RoleDialogModel.cs
+++ b/src/Infrastructure/BotSharp.Abstraction/Models/RoleDialogModel.cs
@@ -3,5 +3,5 @@ namespace BotSharp.Abstraction.Models;
public class RoleDialogModel
{
public string Role { get; set; }
- public string Content { get; set; }
+ public string Text { get; set; }
}
diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.ChatServing.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.ChatServing.cs
new file mode 100644
index 00000000..7dc5d48b
--- /dev/null
+++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.ChatServing.cs
@@ -0,0 +1,16 @@
+namespace BotSharp.Core.Agents.Services;
+
+public partial class AgentService : IChatServiceZone
+{
+ public int Priority => 10;
+
+ ///
+ /// Prepare agent profile and configurations
+ ///
+ ///
+ ///
+ public async Task Serving(ContentContainer content)
+ {
+
+ }
+}
diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs
new file mode 100644
index 00000000..8f1e70c6
--- /dev/null
+++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs
@@ -0,0 +1,29 @@
+using BotSharp.Abstraction.Agents.Models;
+
+namespace BotSharp.Core.Agents.Services;
+
+public partial class AgentService
+{
+ public async Task CreateAgent(Agent agent)
+ {
+ var db = _services.GetRequiredService();
+ var record = db.Agent.FirstOrDefault(x => x.OwnerId == _user.Id && x.Name == agent.Name);
+ if (record != null)
+ {
+ return record.ToAgent();
+ }
+
+ record = AgentRecord.FromAgent(agent);
+ record.Id = Guid.NewGuid().ToString();
+ record.OwnerId = _user.Id;
+ record.CreatedDateTime = DateTime.UtcNow;
+ record.UpdatedDateTime = DateTime.UtcNow;
+
+ db.Transaction(delegate
+ {
+ db.Add(record);
+ });
+
+ return record.ToAgent();
+ }
+}
diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.DeleteAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.DeleteAgent.cs
new file mode 100644
index 00000000..23111fd2
--- /dev/null
+++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.DeleteAgent.cs
@@ -0,0 +1,9 @@
+namespace BotSharp.Core.Agents.Services;
+
+public partial class AgentService
+{
+ public async Task DeleteAgent(string id)
+ {
+ throw new NotImplementedException();
+ }
+}
diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs
new file mode 100644
index 00000000..0d3e2712
--- /dev/null
+++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs
@@ -0,0 +1,15 @@
+using BotSharp.Abstraction.Agents.Models;
+
+namespace BotSharp.Core.Agents.Services;
+
+public partial class AgentService
+{
+ public async Task> GetAgents()
+ {
+ var db = _services.GetRequiredService();
+ var query = from agent in db.Agent
+ where agent.OwnerId == _user.Id
+ select agent.ToAgent();
+ return query.ToList();
+ }
+}
diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs
new file mode 100644
index 00000000..c3a0c423
--- /dev/null
+++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs
@@ -0,0 +1,11 @@
+using BotSharp.Abstraction.Agents.Models;
+
+namespace BotSharp.Core.Agents.Services;
+
+public partial class AgentService
+{
+ public async Task UpdateAgent(Agent agent)
+ {
+ throw new NotImplementedException();
+ }
+}
diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs
index bd8ad04f..2cb3725c 100644
--- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs
+++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs
@@ -1,10 +1,6 @@
-using BotSharp.Abstraction.Agents;
-using BotSharp.Abstraction.Agents.Models;
-using BotSharp.Abstraction.Users;
-
namespace BotSharp.Core.Agents.Services;
-public class AgentService : IAgentService
+public partial class AgentService : IAgentService
{
private readonly IServiceProvider _services;
private readonly IUserIdentity _user;
@@ -14,46 +10,4 @@ public class AgentService : IAgentService
_services = services;
_user = user;
}
-
- public async Task CreateAgent(Agent agent)
- {
- var db = _services.GetRequiredService();
- var record = db.Agent.FirstOrDefault(x => x.OwnerId == _user.Id && x.Name == agent.Name);
- if (record != null)
- {
- return record.ToAgent();
- }
-
- record = AgentRecord.FromAgent(agent);
- record.Id = Guid.NewGuid().ToString();
- record.OwnerId = _user.Id;
- record.CreatedDateTime = DateTime.UtcNow;
- record.UpdatedDateTime = DateTime.UtcNow;
-
- db.Transaction(delegate
- {
- db.Add(record);
- });
-
- return record.ToAgent();
- }
-
- public Task DeleteAgent(string id)
- {
- throw new NotImplementedException();
- }
-
- public async Task> GetAgents()
- {
- var db = _services.GetRequiredService();
- var query = from agent in db.Agent
- where agent.OwnerId == _user.Id
- select agent.ToAgent();
- return query.ToList();
- }
-
- public Task UpdateAgent(Agent agent)
- {
- throw new NotImplementedException();
- }
}
diff --git a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs
index e78d242b..cf65850d 100644
--- a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs
+++ b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs
@@ -9,8 +9,12 @@ public static class BotSharpServiceCollectionExtensions
{
services.AddScoped();
services.AddScoped();
+
services.AddScoped();
+ services.AddScoped();
+
services.AddScoped();
+
services.AddScoped();
services.AddScoped();
diff --git a/src/Infrastructure/BotSharp.Core/Conversations/ConversationController.cs b/src/Infrastructure/BotSharp.Core/Conversations/ConversationController.cs
index 7d88a476..441164bc 100644
--- a/src/Infrastructure/BotSharp.Core/Conversations/ConversationController.cs
+++ b/src/Infrastructure/BotSharp.Core/Conversations/ConversationController.cs
@@ -1,7 +1,5 @@
using BotSharp.Abstraction.ApiAdapters;
-using BotSharp.Abstraction.Conversations;
-using BotSharp.Abstraction.Infrastructures.ContentTransmitters;
-using BotSharp.Abstraction.Models;
+using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Core.Conversations.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@@ -13,49 +11,60 @@ namespace BotSharp.Core.Conversations;
public class ConversationController : ControllerBase, IApiAdapter
{
private readonly IServiceProvider _services;
+ private readonly IUserIdentity _user;
- public ConversationController(IServiceProvider services)
+ public ConversationController(IServiceProvider services,
+ IUserIdentity user)
{
_services = services;
+ _user = user;
}
- [HttpPost("/conversation/session")]
- public async Task NewSession([FromBody] SessionCreationModel session)
+ [HttpPost("/conversation/{agentId}")]
+ public async Task NewSession([FromRoute] string agentId)
{
var service = _services.GetRequiredService();
- var sess = session.ToSession();
+ var sess = new Session
+ {
+ AgentId = agentId
+ };
sess = await service.NewSession(sess);
return SessionViewModel.FromSession(sess);
}
- [HttpDelete("/conversation/session/{sessionId}")]
- public async Task DeleteSession([FromRoute] string sessionId)
+ [HttpDelete("/conversation/{agentId}/{sessionId}")]
+ public async Task DeleteSession([FromRoute] string agentId, [FromRoute] string sessionId)
{
var service = _services.GetRequiredService();
}
- [HttpPost("/conversation/{sessionId}")]
- public async Task SendMessage([FromBody] NewMessageModel input)
+ [HttpPost("/conversation/{agentId}/{sessionId}")]
+ public async Task SendMessage([FromRoute] string agentId,
+ [FromRoute] string sessionId,
+ [FromBody] NewMessageModel input)
{
var transmitter = _services.GetRequiredService();
var container = new ContentContainer
{
+ AgentId = agentId,
+ SessionId = sessionId,
Conversations = new List
{
new RoleDialogModel
{
Role = "user",
- Content = input.Content
+ Text = input.Text
}
- }
+ },
+ UserId = _user.Id
};
var result = await transmitter.Transport(container);
return new MessageResponseModel
{
- Content = result.IsSuccess ? container.Output.Content : result.Messages.First()
+ Content = result.IsSuccess ? container.Output.Text : result.Messages.First()
};
}
}
diff --git a/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/NewMessageModel.cs b/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/NewMessageModel.cs
index 24548bdc..210751fa 100644
--- a/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/NewMessageModel.cs
+++ b/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/NewMessageModel.cs
@@ -1,6 +1,8 @@
+using Microsoft.AspNetCore.Mvc;
+
namespace BotSharp.Core.Conversations.ViewModels;
public class NewMessageModel
{
- public string Content { get; set; }
+ public string Text { get; set; }
}
diff --git a/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/SessionCreationModel.cs b/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/SessionCreationModel.cs
index 948e1d6b..9f0bd890 100644
--- a/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/SessionCreationModel.cs
+++ b/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/SessionCreationModel.cs
@@ -4,13 +4,5 @@ namespace BotSharp.Core.Conversations.ViewModels;
public class SessionCreationModel
{
- public string AgentId { get; set; }
- public Session ToSession()
- {
- return new Session
- {
- AgentId = AgentId
- };
- }
}
diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/ContentTransfer.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/ContentTransfer.cs
index bb3ea673..07690c4c 100644
--- a/src/Infrastructure/BotSharp.Core/Infrastructures/ContentTransfer.cs
+++ b/src/Infrastructure/BotSharp.Core/Infrastructures/ContentTransfer.cs
@@ -1,23 +1,17 @@
-using BotSharp.Abstraction.Infrastructures.ContentTransfers;
-using BotSharp.Abstraction.Infrastructures.ContentTransmitters;
-using BotSharp.Abstraction.Users;
-
namespace BotSharp.Core.Infrastructures;
public class ContentTransfer : IContentTransfer
{
private readonly IServiceProvider _services;
- private readonly IUserIdentity _user;
- public ContentTransfer(IServiceProvider services, IUserIdentity user)
+ public ContentTransfer(IServiceProvider services)
{
_services = services;
- _user = user;
}
public async Task Transport(ContentContainer input)
{
- input.UserId = _user.Id;
+ input.Output = new RoleDialogModel();
var result = new TransportResult
{
@@ -25,12 +19,12 @@ public class ContentTransfer : IContentTransfer
Messages = new List()
};
- var zones = _services.GetServices();
+ var zones = _services.GetServices()
+ .OrderBy(x => x.Priority)
+ .ToList();
foreach (var zone in zones)
{
- input.Output = null;
-
try
{
await zone.Serving(input);
diff --git a/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/ChatCompletionProvider.cs b/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/ChatCompletionProvider.cs
index f2f283c0..190737c4 100644
--- a/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/ChatCompletionProvider.cs
+++ b/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/ChatCompletionProvider.cs
@@ -1,12 +1,9 @@
-using BotSharp.Abstraction.Infrastructures.ContentTransfers;
-using BotSharp.Abstraction.Models;
-using BotSharp.Core.Repository.Collections;
using LLama;
using System.IO;
namespace BotSharp.Plugins.LLamaSharp;
-public class ChatCompletionProvider : IServiceZone
+public class ChatCompletionProvider : IChatServiceZone
{
private readonly IChatModel _model;
private readonly LlamaSharpSettings _settings;
@@ -26,12 +23,14 @@ public class ChatCompletionProvider : IServiceZone
_model.InitChatAntiprompt(new string[] { "user:" });
}
+ public int Priority => 100;
+
public async Task GetChatCompletionsAsync(List conversations,
Func onChunkReceived)
{
string totalResponse = "";
var prompt = GetInstruction();
- var content = string.Join("\n ", conversations.Select(x => $"{x.Role}: {x.Content.Replace("user:", "")}")).Trim();
+ var content = string.Join("\n ", conversations.Select(x => $"{x.Role}: {x.Text.Replace("user:", "")}")).Trim();
content += "\n assistant: ";
foreach (var response in _model.Chat(content, prompt, "UTF-8"))
{
@@ -58,7 +57,7 @@ public class ChatCompletionProvider : IServiceZone
samples.Add(new RoleDialogModel
{
Role = role,
- Content = content
+ Text = content
});
}
}
@@ -76,7 +75,7 @@ public class ChatCompletionProvider : IServiceZone
instruction += "\n";
foreach (var message in GetChatSamples())
{
- instruction += $"\n{message.Role}: {message.Content}";
+ instruction += $"\n{message.Role}: {message.Text}";
}
return instruction;
@@ -86,7 +85,7 @@ public class ChatCompletionProvider : IServiceZone
{
string output = "";
var prompt = GetInstruction();
- var conversations = string.Join("\n ", content.Conversations.Select(x => $"{x.Role}: {x.Content.Replace("user:", "")}")).Trim();
+ var conversations = string.Join("\n ", content.Conversations.Select(x => $"{x.Role}: {x.Text.Replace("user:", "")}")).Trim();
conversations += "\n assistant: ";
foreach (var response in _model.Chat(conversations, prompt, "UTF-8"))
{
diff --git a/src/Infrastructure/BotSharp.Core/Using.cs b/src/Infrastructure/BotSharp.Core/Using.cs
index f3616bc0..b1a8a4a4 100644
--- a/src/Infrastructure/BotSharp.Core/Using.cs
+++ b/src/Infrastructure/BotSharp.Core/Using.cs
@@ -12,6 +12,7 @@ global using BotSharp.Abstraction.Infrastructures.ContentTransmitters;
global using BotSharp.Abstraction.Infrastructures.ContentTransfers;
global using BotSharp.Abstraction.Knowledges;
global using BotSharp.Abstraction.Users;
+global using BotSharp.Abstraction.Models;
global using BotSharp.Core.Repository;
global using BotSharp.Core.Repository.Abstraction;
global using BotSharp.Core.Repository.DbTables;
diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs
index 5c85452c..4eea23e4 100644
--- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs
+++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/AzureOpenAiPlugin.cs
@@ -1,4 +1,4 @@
-using BotSharp.Abstraction.Infrastructures.ContentTransfers;
+using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.Plugins;
using BotSharp.Plugin.AzureOpenAI.Providers;
@@ -19,6 +19,6 @@ public class AzureOpenAiPlugin : IBotSharpPlugin
services.AddSingleton();
services.AddScoped();
- services.AddScoped();
+ services.AddScoped();
}
}
\ No newline at end of file
diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs
index 9b23344b..38cc1bee 100644
--- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs
+++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs
@@ -61,7 +61,7 @@ public class ChatCompletionProvider : IChatCompletion
samples.Add(new RoleDialogModel
{
Role = role,
- Content = content
+ Text = content
});
}
}
@@ -113,12 +113,12 @@ public class ChatCompletionProvider : IChatCompletion
foreach (var message in GetChatSamples())
{
- chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content));
+ chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Text));
}
foreach (var message in conversations)
{
- chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content));
+ chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Text));
}
return chatCompletionsOptions;
diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Services/ChatCompletionService.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Services/ChatCompletionService.cs
index 9d9f0b94..edd28e7c 100644
--- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Services/ChatCompletionService.cs
+++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Services/ChatCompletionService.cs
@@ -1,5 +1,5 @@
using Azure.AI.OpenAI;
-using BotSharp.Abstraction.Infrastructures.ContentTransfers;
+using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Infrastructures.ContentTransmitters;
using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.Models;
@@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace BotSharp.Plugin.AzureOpenAI.Services;
-public class ChatCompletionService : IServiceZone
+public class ChatCompletionService : IChatServiceZone
{
private readonly IChatCompletion _chatCompletion;
@@ -16,6 +16,8 @@ public class ChatCompletionService : IServiceZone
_chatCompletion = chatCompletion;
}
+ public int Priority => 100;
+
public async Task Serving(ContentContainer content)
{
var output = await _chatCompletion.GetChatCompletionsAsync(content.Conversations);
@@ -23,7 +25,7 @@ public class ChatCompletionService : IServiceZone
content.Output = new RoleDialogModel
{
Role = ChatRole.Assistant.ToString(),
- Content = output
+ Text = output
};
}
}
diff --git a/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs b/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs
index 4a6d6d7e..61823d00 100644
--- a/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs
+++ b/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs
@@ -61,7 +61,7 @@ public class ChatbotUiController : ControllerBase, IApiAdapter
var conversations = input.Messages.Skip(1).Select(x => new RoleDialogModel
{
Role = x.Role,
- Content = x.Content
+ Text = x.Content
}).ToList();
/*await _chatCompletionProvider.GetChatCompletionsAsync(conversations,
@@ -79,7 +79,7 @@ public class ChatbotUiController : ControllerBase, IApiAdapter
var result = await transmitter.Transport(container);
- await OnChunkReceived(outputStream, container.Output.Content);
+ await OnChunkReceived(outputStream, container.Output.Text);
await OnEventCompleted(outputStream);
}