add agent creation api for ownthink

This commit is contained in:
Oceania2018 2019-03-16 23:50:33 -05:00
parent 7121f19150
commit bedbcd999d
16 changed files with 310 additions and 17 deletions

View file

@ -10,13 +10,11 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.2" />
<PackageReference Include="RestSharp" Version="106.6.9" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BotSharp.Platform.Articulate\BotSharp.Platform.Articulate.csproj" />
<ProjectReference Include="..\BotSharp.Platform.Dialogflow\BotSharp.Platform.Dialogflow.csproj" />
<ProjectReference Include="..\BotSharp.Platform.OwnThink\BotSharp.Platform.OwnThink.csproj" />
<ProjectReference Include="..\BotSharp.Platform.Rasa\BotSharp.Platform.Rasa.csproj" />
</ItemGroup>
</Project>

View file

@ -24,10 +24,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BotSharp.Platform.Articulate\BotSharp.Platform.Articulate.csproj" />
<ProjectReference Include="..\BotSharp.Platform.Dialogflow\BotSharp.Platform.Dialogflow.csproj" />
<ProjectReference Include="..\BotSharp.Platform.OwnThink\BotSharp.Platform.OwnThink.csproj" />
<ProjectReference Include="..\BotSharp.Platform.Rasa\BotSharp.Platform.Rasa.csproj" />
</ItemGroup>
</Project>

View file

@ -1,6 +1,6 @@
using BotSharp.Channel.Weixin.Models;
using BotSharp.Platform.Abstractions;
using BotSharp.Platform.Dialogflow.Models;
using BotSharp.Platform.OwnThink.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Senparc.CO2NET.HttpUtility;

View file

@ -31,8 +31,8 @@ using Senparc.Weixin;
using Senparc.Weixin.MP;
using BotSharp.Platform.Models.AiRequest;
using BotSharp.Platform.Abstractions;
using BotSharp.Platform.Dialogflow.Models;
using Microsoft.Extensions.Configuration;
using BotSharp.Platform.OwnThink.Models;
#if NET45
using System.Web;

View file

@ -1,11 +1,13 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace BotSharp.Platform.Models.Agents
{
public class AgentCreationViewModel
public class AgentCreationRequestModel
{
[Required]
public string Name { get; set; }
public string Description { get; set; }

View file

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Platform.Models.Agents
{
public class AgentCreationResponseModel
{
public string AgentId { get; set; }
public string Name { get; set; }
public string ClientAccessToken { get; set; }
}
}

View file

@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="ViewModels\" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.2" />
</ItemGroup>
<ItemGroup>

View file

@ -1,10 +1,56 @@
using System;
using BotSharp.Platform.Models.Agents;
using BotSharp.Platform.OwnThink.Models;
using BotSharp.Platform.OwnThink.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Platform.OwnThink.Controllers
{
public class AgentController
[Route("v1/[controller]")]
public class AgentController : ControllerBase
{
private OwnThinkAi<AgentModel> builder;
/// <summary>
/// Initialize dialog controller and get a platform instance
/// </summary>
/// <param name="platform"></param>
public AgentController(OwnThinkAi<AgentModel> platform)
{
builder = platform;
}
/// <summary>
/// Create agent
/// </summary>
/// <param name="requestViewModel"></param>
/// <returns></returns>
[HttpPost]
[ProducesResponseType(typeof(AgentCreationResponseViewModel), 200)]
public async Task<IActionResult> Create(AgentCreationRequestViewModel requestViewModel)
{
var agent = new AgentModel()
{
Id = Guid.NewGuid().ToString(),
Name = requestViewModel.Name,
Description = requestViewModel.Name,
AppId = requestViewModel.AppId,
ClientAccessToken = Guid.NewGuid().ToString("N"),
DeveloperAccessToken = Guid.NewGuid().ToString("N")
};
await builder.SaveAgent(agent);
return Ok(new AgentCreationResponseViewModel
{
AgentId = agent.Id,
AppId = agent.AppId,
Name = agent.Name,
ClientAccessToken = agent.ClientAccessToken
});
}
}
}

View file

@ -0,0 +1,55 @@
using BotSharp.Platform.Models.AiRequest;
using BotSharp.Platform.OwnThink.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
namespace BotSharp.Platform.OwnThink.Controllers
{
[Route("v1/[controller]")]
public class QueryController : ControllerBase
{
private OwnThinkAi<AgentModel> builder;
public QueryController(OwnThinkAi<AgentModel> platform)
{
builder = platform;
}
/*[HttpGet, HttpPost]
public async Task<ActionResult<AIResponse>> Query(QueryModel request)
{
String clientAccessToken = (User.Identity as ClaimsIdentity).Claims.FirstOrDefault(x => x.Type == "UserId")?.Value;
// find a model according to clientAccessToken
var agents = await builder.GetAllAgents();
var agent = agents.FirstOrDefault(x => x.ClientAccessToken == clientAccessToken);
if (agent == null)
{
return BadRequest("The agent not found.");
}
var aIResponse = await builder.TextRequest<AIResponseResult>(new AiRequest
{
Text = request.Query,
AgentId = agent.Id,
SessionId = request.SessionId
});
return new AIResponse
{
Result = aIResponse,
Id = Guid.NewGuid().ToString(),
Lang = request.Lang,
SessionId = request.SessionId,
Status = new AIResponseStatus(),
Timestamp = DateTime.UtcNow
};
}*/
}
}

View file

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Platform.OwnThink.Models
{
public class AIResponseFulfillment
{
public string Speech { get; set; }
public List<Object> Messages { get; set; }
}
}

View file

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Platform.OwnThink.Models
{
public class AIResponseMetadata
{
public string IntentId { get; set; }
public string IntentName { get; set; }
}
}

View file

@ -0,0 +1,127 @@
using BotSharp.Platform.Models.AiResponse;
using BotSharp.Platform.Models.Contexts;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
namespace BotSharp.Platform.OwnThink.Models
{
public class AIResponseResult : AiResponse
{
public Boolean ActionIncomplete { get; set; }
public String Action { get; set; }
public Dictionary<string, object> Parameters { get; set; }
public AIContext[] Contexts { get; set; }
public AIResponseMetadata Metadata { get; set; }
public AIResponseFulfillment Fulfillment { get; set; }
[JsonIgnore]
public bool HasParameters
{
get
{
return Parameters != null && Parameters.Count > 0;
}
}
public string GetStringParameter(string name, string defaultValue = "")
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException("name");
}
if (Parameters.ContainsKey(name))
{
return Parameters[name].ToString();
}
return defaultValue;
}
public int GetIntParameter(string name, int defaultValue = 0)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException("name");
}
if (Parameters.ContainsKey(name))
{
var parameterValue = Parameters[name].ToString();
int result;
if (int.TryParse(parameterValue, NumberStyles.Integer, CultureInfo.InvariantCulture, out result))
{
return result;
}
float floatResult;
if (float.TryParse(parameterValue, NumberStyles.Float, CultureInfo.InvariantCulture, out floatResult))
{
result = Convert.ToInt32(floatResult);
return result;
}
}
return defaultValue;
}
public float GetFloatParameter(string name, float defaultValue = 0)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException("name");
}
if (Parameters.ContainsKey(name))
{
var parameterValue = Parameters[name].ToString();
float result;
if (float.TryParse(parameterValue, NumberStyles.Float, CultureInfo.InvariantCulture, out result))
{
return result;
}
}
return defaultValue;
}
public JObject GetJsonParameter(string name, JObject defaultValue = null)
{
if (string.IsNullOrEmpty("name"))
{
throw new ArgumentNullException(nameof(name));
}
if (Parameters.ContainsKey(name))
{
var parameter = Parameters[name].ToString();
if (parameter != null)
{
return JObject.FromObject(parameter);
}
}
return defaultValue;
}
public AIContext GetContext(string name)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException("Name must be not empty", nameof(name));
}
return Contexts?.FirstOrDefault(c => string.Equals(c.Name, name, StringComparison.CurrentCultureIgnoreCase));
}
}
}

View file

@ -11,6 +11,8 @@ namespace BotSharp.Platform.OwnThink.Models
{
public class AgentModel : AgentBase
{
public string AppId { get; set; }
public AgentModel()
{

View file

@ -0,0 +1,14 @@
using BotSharp.Platform.Models.Agents;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace BotSharp.Platform.OwnThink.ViewModels
{
public class AgentCreationRequestViewModel : AgentCreationRequestModel
{
[Required]
public string AppId { get; set; }
}
}

View file

@ -0,0 +1,12 @@
using BotSharp.Platform.Models.Agents;
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Platform.OwnThink.ViewModels
{
public class AgentCreationResponseViewModel : AgentCreationResponseModel
{
public string AppId { get; set; }
}
}

View file

@ -14,14 +14,14 @@
"Name": "RasaAi",
"Type": "BotSharp.Platform.Rasa"
},
{
"Name": "OwnThink",
"Type": "BotSharp.Platform.OwnThink"
},
{
"Name": "Articulate",
"Type": "BotSharp.Platform.Articulate"
},
{
"Name": "OwnThink",
"Type": "BotSharp.Platform.OwnThink"
},
{
"Name": "WeixinChannel",
"Type": "BotSharp.Channel.Weixin"