Update IConversationCompletionHook.
This commit is contained in:
parent
6b2d1fa30b
commit
12e7d32be1
|
|
@ -0,0 +1,23 @@
|
|||
using BotSharp.Abstraction.Conversations.Models;
|
||||
|
||||
namespace BotSharp.Abstraction.Conversations;
|
||||
|
||||
public abstract class ConversationCompletionHookBase
|
||||
{
|
||||
protected Agent _agent;
|
||||
public Agent Agent => _agent;
|
||||
|
||||
protected Conversation _conversation;
|
||||
public Conversation Conversation => _conversation;
|
||||
|
||||
protected List<RoleDialogModel> _dialogs;
|
||||
public List<RoleDialogModel> Dialogs => _dialogs;
|
||||
|
||||
public IConversationCompletionHook SetContexts(Agent agent, Conversation conversation, List<RoleDialogModel> dialogs)
|
||||
{
|
||||
_agent = agent;
|
||||
_conversation = conversation;
|
||||
_dialogs = dialogs;
|
||||
return this as IConversationCompletionHook;
|
||||
}
|
||||
}
|
||||
|
|
@ -4,6 +4,10 @@ namespace BotSharp.Abstraction.Conversations;
|
|||
|
||||
public interface IConversationCompletionHook
|
||||
{
|
||||
Task BeforeCompletion(Agent agent, List<RoleDialogModel> conversations);
|
||||
Task<string> AfterCompletion(Agent agent, string response);
|
||||
Agent Agent { get; }
|
||||
Conversation Conversation { get; }
|
||||
List<RoleDialogModel> Dialogs { get; }
|
||||
IConversationCompletionHook SetContexts(Agent agent, Conversation conversation, List<RoleDialogModel> dialogs);
|
||||
Task BeforeCompletion();
|
||||
Task<string> AfterCompletion(string response);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ namespace BotSharp.Abstraction.Conversations;
|
|||
public interface IConversationService
|
||||
{
|
||||
Task<Conversation> NewConversation(Conversation conversation);
|
||||
Task<Conversation> GetConversation(string id);
|
||||
Task<List<Conversation>> GetConversations();
|
||||
Task DeleteConversation(string id);
|
||||
Task<string> SendMessage(string agentId, string conversationId, RoleDialogModel lastDalog);
|
||||
|
|
|
|||
|
|
@ -28,6 +28,16 @@ public class ConversationService : IConversationService
|
|||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task<Conversation> GetConversation(string id)
|
||||
{
|
||||
var db = _services.GetRequiredService<AgentDbContext>();
|
||||
var query = from sess in db.Conversation
|
||||
where sess.Id == id
|
||||
orderby sess.CreatedTime descending
|
||||
select sess.ToConversation();
|
||||
return query.FirstOrDefault();
|
||||
}
|
||||
|
||||
public async Task<List<Conversation>> GetConversations()
|
||||
{
|
||||
var db = _services.GetRequiredService<AgentDbContext>();
|
||||
|
|
@ -77,6 +87,7 @@ public class ConversationService : IConversationService
|
|||
public async Task<string> SendMessage(string agentId, string conversationId, List<RoleDialogModel> wholeDialogs)
|
||||
{
|
||||
var agent = await _services.GetRequiredService<IAgentService>().GetAgent(agentId);
|
||||
var converation = await GetConversation(conversationId);
|
||||
|
||||
// Get relevant domain knowledge
|
||||
if (_settings.EnableKnowledgeBase)
|
||||
|
|
@ -94,14 +105,18 @@ public class ConversationService : IConversationService
|
|||
// Before chat completion hook
|
||||
var hooks = _services.GetServices<IConversationCompletionHook>().ToList();
|
||||
|
||||
hooks.ForEach(hook => hook.BeforeCompletion(agent, wholeDialogs));
|
||||
hooks.ForEach(hook =>
|
||||
{
|
||||
hook.SetContexts(agent, converation, wholeDialogs)
|
||||
.BeforeCompletion();
|
||||
});
|
||||
|
||||
var response = await chatCompletion.GetChatCompletionsAsync(agent, wholeDialogs);
|
||||
|
||||
// After chat completion hook
|
||||
hooks.ForEach(async hook =>
|
||||
{
|
||||
response = await hook.AfterCompletion(agent, response);
|
||||
response = await hook.AfterCompletion(response);
|
||||
});
|
||||
|
||||
return response;
|
||||
|
|
|
|||
|
|
@ -51,11 +51,11 @@ public class ChatCompletionProvider : IChatCompletion
|
|||
if (!string.IsNullOrEmpty(sampleText))
|
||||
{
|
||||
var lines = sampleText.Split('\n');
|
||||
for (int i = 0; i < lines.Length; i++)
|
||||
for (int i = 0; i < lines.Length; i += 3)
|
||||
{
|
||||
var line = lines[i];
|
||||
var role = line.Substring(0, line.IndexOf(' ') - 1);
|
||||
var content = line.Substring(line.IndexOf(' ') + 1);
|
||||
var role = line.Substring(0, line.IndexOf(' ') - 1).Trim();
|
||||
var content = line.Substring(line.IndexOf(' ') + 1).Trim();
|
||||
|
||||
samples.Add(new RoleDialogModel
|
||||
{
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ public class ChatbotUiController : ControllerBase, IApiAdapter
|
|||
Response.Headers.Add(HeaderNames.Connection, "keep-alive");
|
||||
var outputStream = Response.Body;
|
||||
|
||||
var conversations = input.Messages.Skip(1).Select(x => new RoleDialogModel
|
||||
var conversations = input.Messages.Select(x => new RoleDialogModel
|
||||
{
|
||||
Role = x.Role,
|
||||
Text = x.Content
|
||||
|
|
@ -67,7 +67,15 @@ public class ChatbotUiController : ControllerBase, IApiAdapter
|
|||
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
|
||||
var result = await conv.SendMessage("", "", conversations.Last());
|
||||
// Check if this conversation exists
|
||||
var converation = await conv.GetConversation(input.ConversationId);
|
||||
var sess = new Conversation
|
||||
{
|
||||
AgentId = input.AgentId
|
||||
};
|
||||
sess = await conv.NewConversation(sess);
|
||||
|
||||
var result = await conv.SendMessage(input.AgentId, input.ConversationId, conversations);
|
||||
|
||||
await OnChunkReceived(outputStream, result);
|
||||
await OnEventCompleted(outputStream);
|
||||
|
|
|
|||
|
|
@ -6,11 +6,14 @@ namespace BotSharp.Plugin.ChatbotUI.ViewModels;
|
|||
|
||||
public class OpenAiMessageInput
|
||||
{
|
||||
public string AgentId { get; set; }
|
||||
public string ConversationId { get; set; }
|
||||
public string Model { get; set; } = string.Empty;
|
||||
public List<OpenAiMessageBody> Messages { get; set; } = new List<OpenAiMessageBody>();
|
||||
[JsonPropertyName("max_tokens")]
|
||||
public int MaxTokens { get; set; } = 4000;
|
||||
public bool Stream { get; set; } = true;
|
||||
public string? SystemPrompt { get; set; }
|
||||
public float Temperature { get; set; } = 0.9f;
|
||||
|
||||
public override string ToString()
|
||||
|
|
|
|||
Loading…
Reference in a new issue