forked from SciSharp/BotSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRasaAi.cs
More file actions
139 lines (117 loc) · 4.94 KB
/
Copy pathRasaAi.cs
File metadata and controls
139 lines (117 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using BotSharp.Core;
using BotSharp.Core.Engines;
using BotSharp.Platform.Abstractions;
using BotSharp.Platform.Models;
using BotSharp.Platform.Models.AiRequest;
using BotSharp.Platform.Models.AiResponse;
using BotSharp.Platform.Models.Contexts;
using BotSharp.Platform.Rasa.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace BotSharp.Platform.Rasa
{
/// <summary>
/// Rasa nlu >= 0.12
/// </summary>
public class RasaAi<TAgent> :
PlatformBuilderBase<TAgent>,
IPlatformBuilder<TAgent>
where TAgent : AgentModel
{
public RasaAi(IAgentStorageFactory<TAgent> agentStorageFactory, IContextStorageFactory<AIContext> contextStorageFactory, IPlatformSettings settings)
: base(agentStorageFactory, contextStorageFactory, settings)
{
}
public async Task<AiResponse> TextRequest(AiRequest request)
{
AiResponse aiResponse = new AiResponse();
/*string model = RasaRequestExtension.GetModelPerContexts(agent, AiConfig, request, dc);
var result = CallRasa(agent.Id, request.Query.First(), model);
result.Content.Log();
RasaResponse response = result.Data;
aiResponse.Id = Guid.NewGuid().ToString();
aiResponse.Lang = agent.Language;
aiResponse.Status = new AIResponseStatus { };
aiResponse.SessionId = AiConfig.SessionId;
aiResponse.Timestamp = DateTime.UtcNow;
var intentResponse = RasaRequestExtension.HandleIntentPerContextIn(agent, AiConfig, request, result.Data, dc);
RasaRequestExtension.HandleParameter(agent, intentResponse, response, request);
RasaRequestExtension.HandleMessage(intentResponse);
aiResponse.Result = new AIResponseResult
{
Source = "agent",
ResolvedQuery = request.Query.First(),
Action = intentResponse?.Action,
Parameters = intentResponse?.Parameters?.ToDictionary(x => x.Name, x => (object)x.Value),
Score = response.Intent.Confidence,
Metadata = new AIResponseMetadata { IntentId = intentResponse?.IntentId, IntentName = intentResponse?.IntentName },
Fulfillment = new AIResponseFulfillment
{
Messages = intentResponse?.Messages?.Select(x => {
if (x.Type == AIResponseMessageType.Custom)
{
return (new
{
x.Type,
Payload = JsonConvert.DeserializeObject(x.PayloadJson)
}) as Object;
}
else
{
return (new { x.Type, x.Speech }) as Object;
}
}).ToList()
}
};
RasaRequestExtension.HandleContext(dc, AiConfig, intentResponse, aiResponse);
Console.WriteLine(JsonConvert.SerializeObject(aiResponse.Result));*/
return aiResponse;
}
public async Task<TrainingCorpus> ExtractorCorpus(TAgent agent)
{
var corpus = new TrainingCorpus()
{
Entities = new List<TrainingEntity>(),
UserSays = new List<TrainingIntentExpression<TrainingIntentExpressionPart>>()
};
List<string> entities = new List<string>();
// generate entity list
agent.Intents.ForEach(intent =>
{
intent.Entities.ForEach(entity =>
{
if (!entities.Contains(entity.Entity))
{
corpus.Entities.Add(new TrainingEntity
{
Entity = entity.Entity,
Values = new List<TrainingEntitySynonym>
{
new TrainingEntitySynonym
{
Value = entity.Value,
Synonyms = new List<string>
{
entity.Value
}
}
}
});
entities.Add(entity.Entity);
}
});
});
agent.Intents.ForEach(intent =>
{
var express = new TrainingIntentExpression<TrainingIntentExpressionPart>()
{
Text = intent.Text,
Intent = intent.Intent,
Entities = intent.Entities
};
corpus.UserSays.Add(express);
});
return corpus;
}
}
}