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

126 lines
4.3 KiB
C#
Raw Normal View History

2023-06-23 04:43:00 +00:00
using BotSharp.Abstraction.Agents.Models;
2023-09-02 05:10:46 +00:00
using BotSharp.Abstraction.Agents.Settings;
using BotSharp.Abstraction.Functions.Models;
2023-08-26 04:41:01 +00:00
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Repositories.Records;
2023-09-02 05:10:46 +00:00
using BotSharp.Abstraction.Users.Models;
2023-08-28 05:28:14 +00:00
using MongoDB.Bson;
2023-09-02 05:10:46 +00:00
using System.IO;
using Tensorflow;
using static Tensorflow.TensorShapeProto.Types;
2023-06-23 04:43:00 +00:00
namespace BotSharp.Core.Agents.Services;
public partial class AgentService
{
public async Task<Agent> CreateAgent(Agent agent)
{
2023-08-10 04:53:22 +00:00
var db = _services.GetRequiredService<IBotSharpRepository>();
2023-07-25 02:22:18 +00:00
var record = (from a in db.Agent
join ua in db.UserAgent on a.Id equals ua.AgentId
2023-08-10 04:53:22 +00:00
join u in db.User on ua.UserId equals u.Id
2023-09-02 05:10:46 +00:00
where u.ExternalId == _user.Id && a.Name == agent.Name
2023-07-25 02:22:18 +00:00
select a).FirstOrDefault();
2023-06-23 04:43:00 +00:00
if (record != null)
{
return record.ToAgent();
}
record = AgentRecord.FromAgent(agent);
2023-09-02 05:10:46 +00:00
record.Id = Guid.NewGuid().ToString();
2023-08-26 04:41:01 +00:00
record.CreatedTime = DateTime.UtcNow;
record.UpdatedTime = DateTime.UtcNow;
2023-06-23 04:43:00 +00:00
2023-09-02 05:10:46 +00:00
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
var agentSettings = _services.GetRequiredService<AgentSettings>();
var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir);
var foundAgent = FetchAgentInfoFromFile(agent.Name, filePath);
if (foundAgent != null)
{
record.SetId(foundAgent.Id)
.SetInstruction(foundAgent.Instruction)
.SetFunctions(foundAgent.Functions)
.SetResponses(foundAgent.Responses);
}
2023-08-28 05:36:11 +00:00
var user = db.User.FirstOrDefault(x => x.ExternalId == _user.Id);
2023-07-25 02:22:18 +00:00
var userAgentRecord = new UserAgentRecord
{
2023-09-02 05:10:46 +00:00
Id = Guid.NewGuid().ToString(),
UserId = user.Id,
AgentId = foundAgent?.Id ?? record.Id,
2023-07-25 02:22:18 +00:00
CreatedTime = DateTime.UtcNow,
UpdatedTime = DateTime.UtcNow
};
2023-07-21 21:56:14 +00:00
db.Transaction<IBotSharpTable>(delegate
2023-06-23 04:43:00 +00:00
{
2023-07-21 21:56:14 +00:00
db.Add<IBotSharpTable>(record);
2023-07-25 02:22:18 +00:00
db.Add<IBotSharpTable>(userAgentRecord);
2023-06-23 04:43:00 +00:00
});
return record.ToAgent();
}
2023-09-02 05:10:46 +00:00
private JsonSerializerOptions _options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
};
private Agent FetchAgentInfoFromFile(string agentName, string filePath)
{
foreach (var dir in Directory.GetDirectories(filePath))
{
var agentJson = File.ReadAllText(Path.Combine(dir, "agent.json"));
var agent = JsonSerializer.Deserialize<Agent>(agentJson, _options);
if (agent != null && agent.Name == agentName)
{
var functions = FetchFunctionsFromFile(dir);
var instruction = FetchInstructionFromFile(dir);
var responses = FetchResponsesFromFile(dir);
return agent.SetInstruction(instruction).SetFunctions(functions).SetResponses(responses);
}
}
return null;
}
private string FetchInstructionFromFile(string fileDir)
{
var file = Path.Combine(fileDir, "instruction.liquid");
if (!File.Exists(file)) return null;
var instruction = File.ReadAllText(file);
return instruction;
}
private List<string> FetchFunctionsFromFile(string fileDir)
{
var file = Path.Combine(fileDir, "functions.json");
if (!File.Exists(file)) return new List<string>();
var functionsJson = File.ReadAllText(file);
var functionDefs = JsonSerializer.Deserialize<List<Abstraction.Functions.Models.FunctionDef>>(functionsJson, _options);
var functions = functionDefs.Select(x => JsonSerializer.Serialize(x, _options)).ToList();
return functions;
}
private List<string> FetchResponsesFromFile(string fileDir)
{
var responses = new List<string>();
var responseDir = Path.Combine(fileDir, "responses");
if (!Directory.Exists(responseDir)) return responses;
foreach (var file in Directory.GetFiles(responseDir))
{
responses.Add(File.ReadAllText(file));
}
return responses;
}
2023-06-23 04:43:00 +00:00
}