Abstract Bot Platform query interface, add platform dependency injection.

This commit is contained in:
Oceania2018 2018-07-31 23:29:55 -05:00
parent 6f1ea96289
commit 51113f98b2
8 changed files with 125 additions and 20 deletions

View file

@ -1,4 +1,5 @@
using BotSharp.Core.Models;
using BotSharp.Core.Agents;
using BotSharp.Core.Models;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
@ -8,6 +9,13 @@ namespace BotSharp.Core.Engines
{
public interface IBotPlatform
{
/// <summary>
/// Load agent profile
/// </summary>
/// <param name="id">agentId, clientAccessToken, developerAccessToken</param>
/// <returns></returns>
Agent LoadAgent(string id);
AIResponse TextRequest(AIRequest request);
void Train();

View file

@ -31,11 +31,6 @@ namespace BotSharp.Core.Engines
DbInitializerPath = $"{Database.ContentRootPath}App_Data{Path.DirectorySeparatorChar}DbInitializer{Path.DirectorySeparatorChar}";
}
/// <summary>
/// Load Agent
/// </summary>
/// <param name="id">agentId, clientAccessToken, developerAccessToken</param>
/// <returns></returns>
public Agent LoadAgent(string id)
{
if (agent == null)

View file

@ -1,13 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.RestApi
{
[Route("v1/[controller]/[action]")]
public class ConversationController : ControllerBase
{
}
}

View file

@ -0,0 +1,62 @@
using BotSharp.Core.Engines;
using BotSharp.Core.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BotSharp.RestApi.Dialogs
{
/// <summary>
/// Conversation controller
/// </summary>
[Route("v1/[controller]")]
public class DialogController : ControllerBase
{
private readonly IBotPlatform _platform;
/// <summary>
/// Initialize dialog controller and get a platform instance
/// </summary>
/// <param name="platform"></param>
public DialogController(IBotPlatform platform)
{
_platform = platform;
}
/// <summary>
/// The query endpoint is used to process natural language in the form of text.
/// The query requests return structured data in JSON format with an action and parameters for that action.
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[AllowAnonymous]
[HttpPost("/v1/query")]
public ActionResult<AIResponse> Query([FromBody] QueryModel request)
{
String clientAccessToken = Request.Headers["Authorization"];
if (String.IsNullOrEmpty(clientAccessToken))
{
return Unauthorized();
}
var config = new AIConfiguration(clientAccessToken, SupportedLanguage.English);
config.SessionId = request.SessionId;
_platform.LoadAgent(clientAccessToken);
var aIResponse = _platform.TextRequest(new AIRequest
{
Timezone = request.Timezone,
Contexts = request.Contexts.Select(x => new AIContext { Name = x }).ToList(),
Language = request.Lang,
Query = new String[] { request.Query }
});
return aIResponse;
}
}
}

View file

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace BotSharp.RestApi.Dialogs
{
public class QueryModel
{
public List<String> Contexts { get; set; }
public String Event { get;set; }
public String Lang { get; set; }
/// <summary>
/// User says
/// </summary>
[Required]
public String Query { get; set; }
[Required]
public String SessionId { get; set; }
public String Timezone { get; set; }
}
}

View file

@ -1,3 +1,4 @@
{
"Assemblies": "BotSharp.Core"
"Assemblies": "BotSharp.Core",
"BotPlatform": "ApiAi"
}

View file

@ -1,10 +1,13 @@
using System;
using System.IO;
using System.Linq;
using BotSharp.Core.Engines;
using DotNetToolkit;
using EntityFrameworkCore.BootKit;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.PlatformAbstractions;
@ -43,6 +46,17 @@ namespace BotSharp.WebHost
var filePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "BotSharp.RestApi.xml");
c.IncludeXmlComments(filePath);
});
// register platform dependency
services.AddTransient<IBotPlatform>((provider) =>
{
var implements = TypeHelper.GetClassesWithInterface<IBotPlatform>(Database.Assemblies);
string platform = Database.Configuration.GetValue<String>("BotPlatform");
var implement = implements.FirstOrDefault(x => x.Name.Split('.').Last() == platform);
var instance = (IBotPlatform)Activator.CreateInstance(implement);
return instance;
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
@ -73,6 +87,17 @@ namespace BotSharp.WebHost
app.UseCors(builder => builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials());
app.Use(async (context, next) =>
{
string token = context.Request.Headers["Authorization"];
if (string.IsNullOrWhiteSpace(token))
{
}
await next.Invoke();
});
app.UseAuthentication();
app.UseMvc();
Database.Configuration = Configuration;

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB