Skip to content

Commit a639e12

Browse files
committed
Added RASA platform emulator.
1 parent bb764cd commit a639e12

31 files changed

+1350
-0
lines changed

BotSharp.Channel.FacebookMessenger/BotSharp.Channel.FacebookMessenger.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
<ItemGroup>
1616
<ProjectReference Include="..\BotSharp.Platform.Dialogflow\BotSharp.Platform.Dialogflow.csproj" />
17+
<ProjectReference Include="..\BotSharp.Platform.Rasa\BotSharp.Platform.Rasa.csproj" />
1718
</ItemGroup>
1819

1920
</Project>

BotSharp.Channel.Weixin/BotSharp.Channel.Weixin.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
<ItemGroup>
2727
<ProjectReference Include="..\BotSharp.Platform.Dialogflow\BotSharp.Platform.Dialogflow.csproj" />
28+
<ProjectReference Include="..\BotSharp.Platform.Rasa\BotSharp.Platform.Rasa.csproj" />
2829
</ItemGroup>
2930

3031
</Project>
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using BotSharp.Platform.Abstraction;
8+
using BotSharp.Platform.Models;
9+
using BotSharp.Platform.Models.Intents;
10+
using BotSharp.Platform.Rasa.Models;
11+
using Newtonsoft.Json;
12+
13+
namespace BotSharp.Platform.Rasa
14+
{
15+
public class AgentImporterInRasa<TAgent> : IAgentImporter<TAgent> where TAgent : AgentModel, new()
16+
{
17+
public string AgentDir { get; set; }
18+
19+
public async Task<TAgent> LoadAgent(AgentImportHeader agentHeader)
20+
{
21+
var agent = new TAgent
22+
{
23+
Id = agentHeader.Id,
24+
Name = agentHeader.Name
25+
};
26+
27+
return agent;
28+
}
29+
30+
public async Task LoadBuildinEntities(TAgent agent)
31+
{
32+
agent.Intents.ForEach(intent =>
33+
{
34+
/*if (intent.UserSays != null)
35+
{
36+
intent.UserSays.ForEach(us =>
37+
{
38+
us.Data.Where(data => data.Meta != null)
39+
.ToList()
40+
.ForEach(data =>
41+
{
42+
LoadBuildinEntityTypePerUserSay(agent, data);
43+
});
44+
});
45+
}*/
46+
});
47+
}
48+
49+
private void LoadBuildinEntityTypePerUserSay(TAgent agent, IntentExpressionPart data)
50+
{
51+
/*var existedEntityType = agent.Entities.FirstOrDefault(x => x.Name == data.Meta);
52+
53+
if (existedEntityType == null)
54+
{
55+
existedEntityType = new EntityType
56+
{
57+
Name = data.Meta,
58+
Entries = new List<EntityEntry>(),
59+
IsOverridable = true
60+
};
61+
62+
agent.Entities.Add(existedEntityType);
63+
}
64+
65+
var entries = existedEntityType.Entries.Select(x => x.Value.ToLower()).ToList();
66+
if (!entries.Contains(data.Text.ToLower()))
67+
{
68+
existedEntityType.Entries.Add(new EntityEntry
69+
{
70+
Value = data.Text,
71+
Synonyms = new List<EntrySynonym>
72+
{
73+
new EntrySynonym
74+
{
75+
Synonym = data.Text
76+
}
77+
}
78+
});
79+
}*/
80+
}
81+
82+
public async Task LoadCustomEntities(TAgent agent)
83+
{
84+
}
85+
86+
public async Task LoadIntents(TAgent agent)
87+
{
88+
string data = File.ReadAllText(Path.Combine(AgentDir, "corpus.json"));
89+
var rasa = JsonConvert.DeserializeObject<RasaAgentImportModel>(data);
90+
91+
agent.Intents = rasa.Data.Intents;
92+
agent.Entities = rasa.Data.Entities;
93+
}
94+
95+
private void ImportIntentUserSays(RasaIntentExpression intent, List<RasaIntentExpression> sentences)
96+
{
97+
var intents = new List<RasaIntentExpression>();
98+
99+
var userSays = sentences.Where(x => x.Intent == intent.Intent).ToList();
100+
101+
userSays.ForEach(say =>
102+
{
103+
var expression = new IntentExpression();
104+
105+
say.Entities = say.Entities.OrderBy(x => x.Start).ToList();
106+
107+
expression.Data = new List<IntentExpressionPart>();
108+
109+
int pos = 0;
110+
for (int entityIdx = 0; entityIdx < say.Entities.Count; entityIdx++)
111+
{
112+
var entity = say.Entities[entityIdx];
113+
114+
// previous
115+
if (entity.Start > 0)
116+
{
117+
expression.Data.Add(new IntentExpressionPart
118+
{
119+
Text = say.Text.Substring(pos, entity.Start - pos),
120+
Start = pos
121+
});
122+
}
123+
124+
// self
125+
expression.Data.Add(new IntentExpressionPart
126+
{
127+
Alias = entity.Entity,
128+
Meta = entity.Entity,
129+
Text = say.Text.Substring(entity.Start, entity.Value.Length),
130+
Start = entity.Start
131+
});
132+
133+
pos = entity.End + 1;
134+
135+
if (pos < say.Text.Length && entityIdx == say.Entities.Count - 1)
136+
{
137+
// end
138+
expression.Data.Add(new IntentExpressionPart
139+
{
140+
Text = say.Text.Substring(pos),
141+
Start = pos
142+
});
143+
}
144+
}
145+
146+
if (say.Entities.Count == 0)
147+
{
148+
expression.Data.Add(new IntentExpressionPart
149+
{
150+
Text = say.Text.Substring(pos)
151+
});
152+
}
153+
154+
int second = 0;
155+
expression.Data.ForEach(x => x.UpdatedTime = DateTime.UtcNow.AddSeconds(second++));
156+
157+
intents.Add(say);
158+
});
159+
}
160+
161+
/*public void AssembleTrainData(TAgent agent)
162+
{
163+
// convert agent to training corpus
164+
agent.Corpus = new TrainingCorpus
165+
{
166+
Entities = new List<TrainingEntity>(),
167+
UserSays = new List<TrainingIntentExpression<TrainingIntentExpressionPart>>()
168+
};
169+
170+
agent.Intents.ForEach(intent =>
171+
{
172+
intent.UserSays.ForEach(say => {
173+
agent.Corpus.UserSays.Add(new TrainingIntentExpression<TrainingIntentExpressionPart>
174+
{
175+
Intent = intent.Name,
176+
Text = String.Join("", say.Data.Select(x => x.Text)),
177+
Entities = say.Data.Where(x => !String.IsNullOrEmpty(x.Meta))
178+
.Select(x => new TrainingIntentExpressionPart
179+
{
180+
Value = x.Text,
181+
Entity = x.Meta,
182+
Start = x.Start
183+
})
184+
.ToList()
185+
});
186+
});
187+
});
188+
}*/
189+
}
190+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.1.3" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\BotSharp\BotSharp.Core\BotSharp.Core.csproj" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using BotSharp.Platform.Rasa.Models;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Newtonsoft.Json.Linq;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
8+
namespace BotSharp.Platform.Rasa.Controllers
9+
{
10+
[Route("[controller]")]
11+
public class ConfigController : ControllerBase
12+
{
13+
[HttpGet]
14+
public ActionResult<RasaVersionModel> Get()
15+
{
16+
var status = new RasaStatusModel
17+
{
18+
AvailableProjects = JObject.FromObject(new RasaProjectModel
19+
{
20+
Status = "ready",
21+
AvailableModels = new List<string> { "model_XXXXXX" },
22+
LoadedModels = new List<string> { "model_XXXXXX" }
23+
})
24+
};
25+
26+
return Ok(status);
27+
}
28+
}
29+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using BotSharp.Platform.Rasa.Models;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace BotSharp.Platform.Rasa.Controllers
5+
{
6+
/// <summary>
7+
/// send a text request
8+
/// </summary>
9+
[Route("[controller]")]
10+
public class ParseController : ControllerBase
11+
{
12+
private readonly RasaAi<AgentModel> builder;
13+
14+
/// <summary>
15+
/// Initialize dialog controller and get a platform instance
16+
/// </summary>
17+
/// <param name="platform"></param>
18+
public ParseController(RasaAi<AgentModel> configuration)
19+
{
20+
builder = configuration;
21+
}
22+
23+
/// <summary>
24+
/// parse request
25+
/// </summary>
26+
/// <param name="request"></param>
27+
/// <returns></returns>
28+
[HttpPost, HttpGet]
29+
public ActionResult<RasaResponse> Parse(RasaRequestModel request)
30+
{
31+
/*var config = new AIConfiguration("", SupportedLanguage.English);
32+
config.SessionId = "rasa nlu";
33+
34+
string body = "";
35+
using (var reader = new StreamReader(Request.Body))
36+
{
37+
body = reader.ReadToEnd();
38+
}
39+
40+
Console.WriteLine($"Got message from {Request.Host}: {body}", Color.Green);
41+
if(request.Project ==null && !String.IsNullOrEmpty(body))
42+
{
43+
request = JsonConvert.DeserializeObject<RasaRequestModel>(body);
44+
}
45+
46+
// Load agent
47+
var projectPath = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Projects", request.Project);
48+
49+
if (String.IsNullOrEmpty(request.Model))
50+
{
51+
request.Model = Directory.GetDirectories(projectPath).Where(x => x.Contains("model_")).Last().Split(Path.DirectorySeparatorChar).Last();
52+
}
53+
54+
var modelPath = Path.Combine(projectPath, request.Model);
55+
56+
var agent = _platform.LoadAgentFromFile(modelPath);
57+
58+
var aIResponse = _platform.TextRequest(new AIRequest
59+
{
60+
AgentDir = projectPath,
61+
Model = request.Model,
62+
Query = new String[] { request.Text }
63+
});
64+
65+
var rasaResponse = new RasaResponse
66+
{
67+
Intent = new RasaResponseIntent
68+
{
69+
Name = aIResponse.Result.Metadata.IntentName,
70+
Confidence = aIResponse.Result.Score
71+
},
72+
Entities = aIResponse.Result.Entities.Select(x => new RasaResponseEntity
73+
{
74+
Extractor = x.Extrator,
75+
Start = x.Start,
76+
Entity = x.Entity,
77+
Value = x.Value
78+
}).ToList(),
79+
Text = request.Text,
80+
Model = request.Model,
81+
Project = agent.Name,
82+
IntentRanking = new List<RasaResponseIntent>
83+
{
84+
new RasaResponseIntent
85+
{
86+
Name = aIResponse.Result.Metadata.IntentName,
87+
Confidence = aIResponse.Result.Score
88+
}
89+
},
90+
Fullfillment = aIResponse.Result.Fulfillment
91+
};
92+
93+
return rasaResponse;*/
94+
95+
return null;
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)