Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

Program.cs 8.2 KB

You have to be logged in to leave a comment. Sign In
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  1. namespace LuisActions.Samples.Console
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel.DataAnnotations;
  6. using System.Configuration;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using Microsoft.Bot.Builder.Luis;
  11. using Microsoft.Bot.Builder.Luis.Models;
  12. using Microsoft.Cognitive.LUIS.ActionBinding;
  13. using Samples;
  14. public class Program
  15. {
  16. public static void Main(string[] args)
  17. {
  18. Console.WriteLine("Query samples:\n");
  19. Console.WriteLine("- What is the time in Miami?");
  20. Console.WriteLine("- Search for 5 stars hotels in Barcelona");
  21. Console.WriteLine("- Tell me the weather in Buenos Aires");
  22. Console.WriteLine("- Find airport with code PMV");
  23. while (true)
  24. {
  25. Console.Write("\n> Your Query? ");
  26. var query = Console.ReadLine();
  27. if (!string.IsNullOrWhiteSpace(query))
  28. {
  29. RunQuery(query).Wait();
  30. }
  31. }
  32. }
  33. private static async Task<ActionExecutionContext> RunActions(ILuisService luisService, IList<ActionExecutionContext> actions)
  34. {
  35. if (actions == null || actions.Count == 0)
  36. {
  37. Console.WriteLine(">> ERROR: Action chain cannot be null or empty.");
  38. return null;
  39. }
  40. var actionExecutionContext = actions.First();
  41. var intentAction = actions.First().Action;
  42. actions.RemoveAt(0);
  43. if (actions.Count > 0)
  44. {
  45. await RunActions(luisService, actions);
  46. }
  47. var validationResults = default(ICollection<ValidationResult>);
  48. bool isValid = intentAction.IsValid(out validationResults);
  49. while (!isValid)
  50. {
  51. var fieldValidation = validationResults.FirstOrDefault();
  52. if (fieldValidation != null)
  53. {
  54. var paramName = fieldValidation.MemberNames.First();
  55. Console.Write("({0}) {1}: ", paramName, fieldValidation.ErrorMessage);
  56. var input = Console.ReadLine();
  57. var queryResult = await LuisActionResolver.QueryValueFromLuisAsync(luisService, intentAction, paramName, input, CancellationToken.None);
  58. if (!queryResult.Succeed && !string.IsNullOrWhiteSpace(queryResult.NewIntent) && queryResult.NewAction != null)
  59. {
  60. var newActionDefinition = LuisActionResolver.GetActionDefinition(queryResult.NewAction);
  61. var currentActionDefinition = LuisActionResolver.GetActionDefinition(intentAction);
  62. var isContextual = false;
  63. if (LuisActionResolver.IsValidContextualAction(queryResult.NewAction, intentAction, out isContextual))
  64. {
  65. var executionContextChain = new List<ActionExecutionContext> { new ActionExecutionContext(queryResult.NewIntent, queryResult.NewAction) };
  66. var executionContext = await RunActions(luisService, executionContextChain);
  67. if (executionContext.ChangeRootSignaling)
  68. {
  69. if (LuisActionResolver.IsContextualAction(intentAction))
  70. {
  71. return executionContext;
  72. }
  73. else
  74. {
  75. intentAction = executionContext.Action;
  76. }
  77. }
  78. }
  79. else if (isContextual && !LuisActionResolver.IsContextualAction(intentAction))
  80. {
  81. Console.WriteLine($"Cannot execute action '{newActionDefinition.FriendlyName}' in the context of '{currentActionDefinition.FriendlyName}' - continuing with current action");
  82. }
  83. else
  84. {
  85. var valid = LuisActionResolver.UpdateIfValidContextualAction(queryResult.NewAction, intentAction, out isContextual);
  86. if (!valid && isContextual)
  87. {
  88. Console.WriteLine($"Cannot switch to action '{newActionDefinition.FriendlyName}' from '{currentActionDefinition.FriendlyName}' due to invalid context - continuing with current action");
  89. }
  90. else if (currentActionDefinition.ConfirmOnSwitchingContext)
  91. {
  92. Console.Write($"You are about to discard the current action '{currentActionDefinition.FriendlyName}' and start executing '{newActionDefinition.FriendlyName}'\nContinue? ");
  93. var response = Console.ReadLine();
  94. if (response.ToUpperInvariant().StartsWith("Y"))
  95. {
  96. if (LuisActionResolver.IsContextualAction(intentAction) && !LuisActionResolver.IsContextualAction(queryResult.NewAction))
  97. {
  98. return new ActionExecutionContext(queryResult.NewIntent, queryResult.NewAction) { ChangeRootSignaling = true };
  99. }
  100. intentAction = queryResult.NewAction;
  101. }
  102. }
  103. else
  104. {
  105. intentAction = queryResult.NewAction;
  106. }
  107. }
  108. }
  109. // re-evaluate
  110. isValid = intentAction.IsValid(out validationResults);
  111. }
  112. }
  113. var result = await intentAction.FulfillAsync();
  114. // We just show the ToString() of the result - not care about the result type here
  115. Console.WriteLine(result != null ? result.ToString() : "Cannot resolve your query");
  116. return actionExecutionContext;
  117. }
  118. private static async Task RunQuery(string query)
  119. {
  120. // Process message
  121. var luisService = new LuisService(new LuisModelAttribute(ConfigurationManager.AppSettings["LUIS_ModelId"], ConfigurationManager.AppSettings["LUIS_SubscriptionKey"]));
  122. var luisResult = await luisService.QueryAsync(query, CancellationToken.None);
  123. // Try to resolve intent to action
  124. var intentName = default(string);
  125. var intentEntities = default(IList<EntityRecommendation>);
  126. var intentAction = new LuisActionResolver(typeof(GetTimeInPlaceAction).Assembly)
  127. .ResolveActionFromLuisIntent(luisResult, out intentName, out intentEntities);
  128. if (intentAction != null)
  129. {
  130. var executionContextChain = new List<ActionExecutionContext> { new ActionExecutionContext(intentName, intentAction) };
  131. while (LuisActionResolver.IsContextualAction(intentAction))
  132. {
  133. var luisActionDefinition = default(LuisActionBindingAttribute);
  134. if (!LuisActionResolver.CanStartWithNoContextAction(intentAction, out luisActionDefinition))
  135. {
  136. Console.WriteLine($"Cannot start contextual action '{luisActionDefinition.FriendlyName}' without a valid context.");
  137. return;
  138. }
  139. intentAction = LuisActionResolver.BuildContextForContextualAction(intentAction, out intentName);
  140. if (intentAction != null)
  141. {
  142. executionContextChain.Insert(0, new ActionExecutionContext(intentName, intentAction));
  143. }
  144. }
  145. await RunActions(luisService, executionContextChain);
  146. }
  147. else
  148. {
  149. Console.WriteLine("Could not understand the input.");
  150. }
  151. }
  152. }
  153. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...