2023-11-27 03:04:48 +00:00
|
|
|
using BotSharp.Abstraction.Conversations.Enums;
|
2023-10-18 11:58:36 +00:00
|
|
|
using BotSharp.Abstraction.Evaluations;
|
|
|
|
|
using BotSharp.Abstraction.Evaluations.Models;
|
|
|
|
|
using BotSharp.Abstraction.Evaluations.Settings;
|
|
|
|
|
using BotSharp.Abstraction.Templating;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Core.Evaluatings;
|
|
|
|
|
|
|
|
|
|
public class EvaluatingService : IEvaluatingService
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly EvaluatorSetting _settings;
|
|
|
|
|
public EvaluatingService(IServiceProvider services, EvaluatorSetting settings)
|
|
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
_settings = settings;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 00:31:49 +00:00
|
|
|
public async Task<Conversation> Execute(string task, EvaluationRequest request)
|
2023-10-18 11:58:36 +00:00
|
|
|
{
|
|
|
|
|
var agentService = _services.GetRequiredService<IAgentService>();
|
|
|
|
|
var evaluator = await agentService.GetAgent(_settings.EvaluatorId);
|
2023-10-23 00:31:49 +00:00
|
|
|
// Task execution mode
|
|
|
|
|
evaluator.Instruction = evaluator.Templates.First(x => x.Name == "instruction.executor").Content;
|
|
|
|
|
var taskPrompt = evaluator.Templates.First(x => x.Name == $"task.{task}").Content;
|
2023-10-18 11:58:36 +00:00
|
|
|
|
|
|
|
|
var render = _services.GetRequiredService<ITemplateRender>();
|
|
|
|
|
var prompt = render.Render(evaluator.Instruction, new Dictionary<string, object>
|
|
|
|
|
{
|
|
|
|
|
{ "task_prompt", taskPrompt}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var service = _services.GetRequiredService<IConversationService>();
|
|
|
|
|
var conv = await service.NewConversation(new Conversation
|
|
|
|
|
{
|
|
|
|
|
AgentId = request.AgentId
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var result = new EvaluationResult
|
|
|
|
|
{
|
|
|
|
|
TaskInstruction = taskPrompt,
|
|
|
|
|
SystemPrompt = evaluator.Instruction
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var textCompletion = CompletionProvider.GetTextCompletion(_services);
|
2023-10-30 16:48:18 +00:00
|
|
|
RoleDialogModel response = new RoleDialogModel(AgentRole.User, "");
|
2023-10-18 11:58:36 +00:00
|
|
|
var dialogs = new List<RoleDialogModel>();
|
|
|
|
|
int roundCount = 0;
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
// var text = string.Join("\r\n", dialogs.Select(x => $"{x.Role}: {x.Content}"));
|
|
|
|
|
// text = instruction + $"\r\n###\r\n{text}\r\n{AgentRole.User}: ";
|
2023-10-30 16:48:18 +00:00
|
|
|
var question = await textCompletion.GetCompletion(prompt, request.AgentId, response.MessageId);
|
2023-10-18 11:58:36 +00:00
|
|
|
dialogs.Add(new RoleDialogModel(AgentRole.User, question));
|
|
|
|
|
prompt += question.Trim();
|
|
|
|
|
|
|
|
|
|
response = await SendMessage(request.AgentId, conv.Id, question);
|
|
|
|
|
dialogs.Add(new RoleDialogModel(AgentRole.Assistant, response.Content));
|
|
|
|
|
prompt += $"\r\n{AgentRole.Assistant}: {response.Content.Trim()}";
|
|
|
|
|
prompt += $"\r\n{AgentRole.User}: ";
|
|
|
|
|
|
|
|
|
|
roundCount++;
|
|
|
|
|
|
2023-10-30 16:48:18 +00:00
|
|
|
if (roundCount > 10)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Conversation ended due to execced max round count {roundCount}", Color.Red);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-18 11:58:36 +00:00
|
|
|
if (response.FunctionName == "conversation_end" ||
|
2023-10-30 16:48:18 +00:00
|
|
|
response.FunctionName == "human_intervention_needed")
|
2023-10-18 11:58:36 +00:00
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Conversation ended by function {response.FunctionName}", Color.Green);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.Dialogs = dialogs;
|
2023-10-23 00:31:49 +00:00
|
|
|
return conv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<EvaluationResult> Evaluate(string conversationId, EvaluationRequest request)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
2023-10-18 11:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<RoleDialogModel> SendMessage(string agentId, string conversationId, string text)
|
|
|
|
|
{
|
|
|
|
|
var conv = _services.GetRequiredService<IConversationService>();
|
2023-11-27 03:44:17 +00:00
|
|
|
conv.SetConversationId(conversationId, new List<string>
|
|
|
|
|
{
|
|
|
|
|
$"channel={ConversationChannel.OpenAPI}"
|
|
|
|
|
});
|
2023-10-18 11:58:36 +00:00
|
|
|
|
|
|
|
|
RoleDialogModel response = default;
|
|
|
|
|
|
|
|
|
|
await conv.SendMessage(agentId,
|
2023-11-27 03:04:48 +00:00
|
|
|
new RoleDialogModel(AgentRole.User, text),
|
2023-10-18 11:58:36 +00:00
|
|
|
async msg => response = msg,
|
|
|
|
|
fnExecuting => Task.CompletedTask,
|
|
|
|
|
fnExecuted => Task.CompletedTask);
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
2023-10-23 00:31:49 +00:00
|
|
|
|
|
|
|
|
public Task<EvaluationResult> Review(string conversationId, EvaluationRequest request)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2023-10-18 11:58:36 +00:00
|
|
|
}
|