Support conversation session partially.

This commit is contained in:
Haiping Chen 2023-06-12 08:28:49 -05:00
parent 8046ddafe6
commit 08b96c2475
18 changed files with 248 additions and 82 deletions

View file

@ -7,7 +7,7 @@ namespace BotSharp.Abstraction.Agents;
/// </summary>
public interface IAgentService
{
Task<string> CreateAgent(Agent agent);
Task<Agent> CreateAgent(Agent agent);
Task<bool> DeleteAgent(string id);
Task UpdateAgent(Agent agent);
}

View file

@ -4,7 +4,7 @@ namespace BotSharp.Abstraction.Conversations;
public interface ISessionService
{
Task<SessionModel> NewSession(string userId);
List<string> GetAllSessions(string userId);
void DeleteSession(string sessionId);
Task<Session> NewSession(Session sess);
Task<List<Session>> GetSessions();
Task DeleteSession(string sessionId);
}

View file

@ -0,0 +1,11 @@
namespace BotSharp.Abstraction.Conversations.Models;
public class Session
{
public string Id { get; set; } = string.Empty;
public string AgentId { get; set; } = string.Empty;
public string UserId { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;
public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
}

View file

@ -1,7 +0,0 @@
namespace BotSharp.Abstraction.Conversations.Models;
public class SessionModel
{
public string UserId { get; set; } = string.Empty;
public string SessionId { get; set; } = string.Empty;
}

View file

@ -17,8 +17,9 @@ public class AgentController : ControllerBase, IApiAdapter
}
[HttpPost("/agent")]
public async Task<string> CreateAgent(AgentCreationModel agent)
public async Task<AgentViewModel> CreateAgent(AgentCreationModel agent)
{
return await _agentService.CreateAgent(agent.ToAgent());
var createdAgent = await _agentService.CreateAgent(agent.ToAgent());
return AgentViewModel.FromAgent(createdAgent);
}
}

View file

@ -1,32 +1,41 @@
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Core.Repository;
using BotSharp.Core.Repository.Abstraction;
using BotSharp.Core.Repository.DbTables;
using EntityFrameworkCore.BootKit;
using Microsoft.Extensions.DependencyInjection;
using BotSharp.Abstraction.Users;
namespace BotSharp.Core.Agents.Services;
public class AgentService : IAgentService
{
private readonly IServiceProvider _services;
public AgentService(IServiceProvider services)
private readonly ICurrentUser _user;
public AgentService(IServiceProvider services, ICurrentUser user)
{
_services = services;
_user = user;
}
public async Task<string> CreateAgent(Agent agent)
public async Task<Agent> CreateAgent(Agent agent)
{
var db = _services.GetRequiredService<AgentDbContext>();
var record = AgentRecord.FromAgent(agent);
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<IAgentTable>(delegate
{
db.Add<IAgentTable>(record);
});
return record.Id;
return record.ToAgent();
}
public Task<bool> DeleteAgent(string id)

View file

@ -1,8 +1,22 @@
using BotSharp.Abstraction.Agents.Models;
namespace BotSharp.Core.Agents.ViewModels;
public class AgentViewModel
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Description { get; set; }
public DateTime UpdatedDateTime { get; set; }
public static AgentViewModel FromAgent(Agent agent)
{
return new AgentViewModel
{
Id = agent.Id,
Name = agent.Name,
Description = agent.Description,
UpdatedDateTime = agent.UpdatedDateTime
};
}
}

View file

@ -2,12 +2,11 @@ using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Users;
using BotSharp.Core.Agents.Services;
using BotSharp.Core.Conversations;
using BotSharp.Core.Conversations.Services;
using BotSharp.Core.Users.Services;
using BotSharp.Plugins.LLamaSharp;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace BotSharp.Core;
@ -18,8 +17,8 @@ public static class BotSharpServiceCollectionExtensions
services.AddScoped<ICurrentUser, CurrentUser>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<IAgentService, AgentService>();
services.AddSingleton<ISessionService, SessionService>();
services.AddSingleton<IConversationService, ConversationService>();
services.AddScoped<ISessionService, SessionService>();
services.AddScoped<IConversationService, ConversationService>();
RegisterRepository(services, config);

View file

@ -0,0 +1,34 @@
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Conversations;
using BotSharp.Core.Conversations.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace BotSharp.Core.Conversations;
[Authorize]
[ApiController]
public class ConversationController : ControllerBase, IApiAdapter
{
private readonly IServiceProvider _services;
public ConversationController(IServiceProvider services)
{
_services = services;
}
[HttpPost("/conversation/session")]
public async Task<SessionViewModel> NewSession([FromBody] SessionCreationModel session)
{
var service = _services.GetRequiredService<ISessionService>();
var sess = session.ToSession();
sess = await service.NewSession(sess);
return SessionViewModel.FromSession(sess);
}
[HttpDelete("/conversation/session/{sessionId}")]
public async Task DeleteSession([FromRoute] string sessionId)
{
var service = _services.GetRequiredService<ISessionService>();
}
}

View file

@ -4,7 +4,7 @@ using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Core.Conversations;
namespace BotSharp.Core.Conversations.Services;
public class ConversationService : IConversationService
{

View file

@ -0,0 +1,49 @@
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Users;
namespace BotSharp.Core.Conversations.Services;
public class SessionService : ISessionService
{
private readonly IServiceProvider _services;
private readonly ICurrentUser _user;
public SessionService(IServiceProvider services, ICurrentUser user)
{
_services = services;
_user = user;
}
public Task DeleteSession(string sessionId)
{
throw new NotImplementedException();
}
public async Task<List<Session>> GetSessions()
{
var db = _services.GetRequiredService<AgentDbContext>();
var query = from sess in db.Session
where sess.UserId == _user.Id
orderby sess.CreatedTime descending
select sess.ToSession();
return query.ToList();
}
public async Task<Session> NewSession(Session sess)
{
var db = _services.GetRequiredService<AgentDbContext>();
var record = SessionRecord.FromSession(sess);
record.Id = Guid.NewGuid().ToString();
record.UserId = _user.Id;
record.Title = "New Session";
db.Transaction<IAgentTable>(delegate
{
db.Add<IAgentTable>(record);
});
return record.ToSession();
}
}

View file

@ -1,50 +0,0 @@
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Core.Repository;
using BotSharp.Core.Repository.Collections;
using EntityFrameworkCore.BootKit;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Bson;
using MongoDB.Driver;
namespace BotSharp.Core.Conversations;
public class SessionService : ISessionService
{
private readonly IServiceProvider _services;
public SessionService(IServiceProvider services)
{
_services = services;
}
public void DeleteSession(string sessionId)
{
throw new NotImplementedException();
}
public List<string> GetAllSessions(string userId)
{
throw new NotImplementedException();
}
public async Task<SessionModel> NewSession(string userId)
{
var mongo = _services.CreateScope().ServiceProvider.GetRequiredService<MongoDbContext>();
var record = new Conversation
{
CreatedAt = DateTime.UtcNow,
Messages = new List<MessageModel>(),
UserId = "anonymous",
Model = "OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5"
};
await mongo.Conversations.InsertOneAsync(record);
return new SessionModel
{
SessionId = record.Id.ToString(),
UserId = record.UserId
};
}
}

View file

@ -0,0 +1,16 @@
using BotSharp.Abstraction.Conversations.Models;
namespace BotSharp.Core.Conversations.ViewModels;
public class SessionCreationModel
{
public string AgentId { get; set; }
public Session ToSession()
{
return new Session
{
AgentId = AgentId
};
}
}

View file

@ -0,0 +1,24 @@
using BotSharp.Abstraction.Conversations.Models;
namespace BotSharp.Core.Conversations.ViewModels;
public class SessionViewModel
{
public string Id { get; set; }
public string AgentId { get; set; }
public string Title { get; set; } = string.Empty;
public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
public static SessionViewModel FromSession(Session sess)
{
return new SessionViewModel
{
Id = sess.Id,
AgentId = sess.AgentId,
Title = sess.Title,
CreatedTime = sess.CreatedTime,
UpdatedTime = sess.UpdatedTime
};
}
}

View file

@ -1,9 +1,8 @@
using BotSharp.Core.Repository.DbTables;
namespace BotSharp.Core.Repository;
public class AgentDbContext : Database
{
public IQueryable<UserRecord> User => Table<UserRecord>();
public IQueryable<AgentRecord> Agent => Table<AgentRecord>();
public IQueryable<SessionRecord> Session => Table<SessionRecord>();
}

View file

@ -34,4 +34,17 @@ public class AgentRecord : DbRecord, IAgentTable
OwnerId = agent.OwerId
};
}
public Agent ToAgent()
{
return new Agent
{
Id = Id,
Name = Name,
Description = Description,
OwerId = OwnerId,
CreatedDateTime = CreatedDateTime,
UpdatedDateTime = UpdatedDateTime
};
}
}

View file

@ -0,0 +1,52 @@
using BotSharp.Abstraction.Conversations.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BotSharp.Core.Repository.DbTables;
[Table("Session")]
public class SessionRecord : DbRecord, IAgentTable
{
[Required]
[MaxLength(36)]
public string AgentId { get; set; } = string.Empty;
[Required]
[MaxLength(36)]
public string UserId { get; set; } = string.Empty;
[MaxLength(64)]
public string Title { get; set; } = string.Empty;
[Required]
public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;
[Required]
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
public static SessionRecord FromSession(Session sess)
{
return new SessionRecord
{
AgentId = sess.AgentId,
UserId = sess.UserId,
Id = sess.Id,
Title = sess.Title,
CreatedTime = sess.CreatedTime,
UpdatedTime = sess.UpdatedTime
};
}
public Session ToSession()
{
return new Session
{
Id = Id,
Title = Title,
UserId = UserId,
AgentId = AgentId,
CreatedTime = CreatedTime,
UpdatedTime = UpdatedTime
};
}
}

View file

@ -2,9 +2,11 @@ global using System;
global using System.Collections.Generic;
global using System.Text;
global using System.Threading.Tasks;
global using BotSharp.Abstraction;
global using System.Linq;
global using Microsoft.Extensions.DependencyInjection;
global using BotSharp.Abstraction.Plugins;
global using EntityFrameworkCore.BootKit;
global using BotSharp.Abstraction;
global using BotSharp.Core.Repository;
global using BotSharp.Core.Repository.Abstraction;
global using BotSharp.Core.Repository.Abstraction;
global using BotSharp.Core.Repository.DbTables;