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

SearchDialog.cs 8.9 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
  1. namespace Search.Dialogs
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Microsoft.Bot.Builder.Dialogs;
  8. using Microsoft.Bot.Builder.Internals.Fibers;
  9. using Microsoft.Bot.Connector;
  10. using Search.Models;
  11. using Search.Services;
  12. [Serializable]
  13. public abstract class SearchDialog : IDialog<IList<SearchHit>>
  14. {
  15. protected readonly ISearchClient SearchClient;
  16. protected readonly SearchQueryBuilder QueryBuilder;
  17. protected readonly PromptStyler HitStyler;
  18. protected readonly bool MultipleSelection;
  19. private readonly IList<SearchHit> selected = new List<SearchHit>();
  20. private bool firstPrompt = true;
  21. private IList<SearchHit> found;
  22. public SearchDialog(ISearchClient searchClient, SearchQueryBuilder queryBuilder = null, PromptStyler searchHitStyler = null, bool multipleSelection = false)
  23. {
  24. SetField.NotNull(out this.SearchClient, nameof(searchClient), searchClient);
  25. this.QueryBuilder = queryBuilder ?? new SearchQueryBuilder();
  26. this.HitStyler = searchHitStyler ?? new SearchHitStyler();
  27. this.MultipleSelection = multipleSelection;
  28. }
  29. public Task StartAsync(IDialogContext context)
  30. {
  31. return this.InitialPrompt(context);
  32. }
  33. public async Task Search(IDialogContext context, IAwaitable<string> input)
  34. {
  35. string text = input != null ? await input : null;
  36. if (this.MultipleSelection && text != null && text.ToLowerInvariant() == "list")
  37. {
  38. await this.ListAddedSoFar(context);
  39. await this.InitialPrompt(context);
  40. }
  41. else
  42. {
  43. if (text != null)
  44. {
  45. this.QueryBuilder.SearchText = text;
  46. }
  47. var response = await this.ExecuteSearchAsync();
  48. if (response.Results.Count() == 0)
  49. {
  50. await this.NoResultsConfirmRetry(context);
  51. }
  52. else
  53. {
  54. var message = context.MakeMessage();
  55. this.found = response.Results.ToList();
  56. this.HitStyler.Apply(
  57. ref message,
  58. "Here are a few good options I found:",
  59. this.found.ToList().AsReadOnly());
  60. await context.PostAsync(message);
  61. await context.PostAsync(
  62. this.MultipleSelection ?
  63. "You can select one or more to add to your list, *list* what you've selected so far, *refine* these results, see *more* or search *again*." :
  64. "You can select one, *refine* these results, see *more* or search *again*.");
  65. context.Wait(this.ActOnSearchResults);
  66. }
  67. }
  68. }
  69. protected virtual Task InitialPrompt(IDialogContext context)
  70. {
  71. string prompt = "What would you like to search for?";
  72. if (!this.firstPrompt)
  73. {
  74. prompt = "What else would you like to search for?";
  75. if (this.MultipleSelection)
  76. {
  77. prompt += " You can also *list* all items you've added so far.";
  78. }
  79. }
  80. this.firstPrompt = false;
  81. PromptDialog.Text(context, this.Search, prompt);
  82. return Task.CompletedTask;
  83. }
  84. protected virtual Task NoResultsConfirmRetry(IDialogContext context)
  85. {
  86. PromptDialog.Confirm(context, this.ShouldRetry, "Sorry, I didn't find any matches. Do you want to retry your search?");
  87. return Task.CompletedTask;
  88. }
  89. protected virtual async Task ListAddedSoFar(IDialogContext context)
  90. {
  91. var message = context.MakeMessage();
  92. if (this.selected.Count == 0)
  93. {
  94. await context.PostAsync("You have not added anything yet.");
  95. }
  96. else
  97. {
  98. this.HitStyler.Apply(ref message, "Here's what you've added to your list so far.", this.selected.ToList().AsReadOnly());
  99. await context.PostAsync(message);
  100. }
  101. }
  102. protected virtual async Task AddSelectedItem(IDialogContext context, string selection)
  103. {
  104. SearchHit hit = this.found.SingleOrDefault(h => h.Key == selection);
  105. if (hit == null)
  106. {
  107. await this.UnkownActionOnResults(context, selection);
  108. }
  109. else
  110. {
  111. if (!this.selected.Any(h => h.Key == hit.Key))
  112. {
  113. this.selected.Add(hit);
  114. }
  115. if (this.MultipleSelection)
  116. {
  117. await context.PostAsync($"'{hit.Title}' was added to your list!");
  118. PromptDialog.Confirm(context, this.ShouldContinueSearching, "Do you want to continue searching and adding more items?");
  119. }
  120. else
  121. {
  122. context.Done(this.selected);
  123. }
  124. }
  125. }
  126. protected virtual async Task UnkownActionOnResults(IDialogContext context, string action)
  127. {
  128. await context.PostAsync("Not sure what you mean. You can search *again*, *refine*, *list* or select one of the items above. Or are you *done*?");
  129. context.Wait(this.ActOnSearchResults);
  130. }
  131. protected virtual async Task ShouldContinueSearching(IDialogContext context, IAwaitable<bool> input)
  132. {
  133. try
  134. {
  135. bool shouldContinue = await input;
  136. if (shouldContinue)
  137. {
  138. await this.InitialPrompt(context);
  139. }
  140. else
  141. {
  142. context.Done(this.selected);
  143. }
  144. }
  145. catch (TooManyAttemptsException)
  146. {
  147. context.Done(this.selected);
  148. }
  149. }
  150. protected void SelectRefiner(IDialogContext context)
  151. {
  152. var dialog = new SearchSelectRefinerDialog(this.GetTopRefiners(), this.QueryBuilder);
  153. context.Call(dialog, this.Refine);
  154. }
  155. protected async Task Refine(IDialogContext context, IAwaitable<string> input)
  156. {
  157. string refiner = await input;
  158. if (!string.IsNullOrWhiteSpace(refiner))
  159. {
  160. var dialog = new SearchRefineDialog(this.SearchClient, refiner, this.QueryBuilder);
  161. context.Call(dialog, this.ResumeFromRefine);
  162. }
  163. else
  164. {
  165. await this.Search(context, null);
  166. }
  167. }
  168. protected async Task ResumeFromRefine(IDialogContext context, IAwaitable<string> input)
  169. {
  170. await input; // refiner filter is already applied to the SearchQueryBuilder instance we passed in
  171. await this.Search(context, null);
  172. }
  173. protected async Task<GenericSearchResult> ExecuteSearchAsync()
  174. {
  175. return await this.SearchClient.SearchAsync(this.QueryBuilder);
  176. }
  177. protected abstract string[] GetTopRefiners();
  178. private async Task ShouldRetry(IDialogContext context, IAwaitable<bool> input)
  179. {
  180. try
  181. {
  182. bool retry = await input;
  183. if (retry)
  184. {
  185. await this.InitialPrompt(context);
  186. }
  187. else
  188. {
  189. context.Done<IList<SearchHit>>(null);
  190. }
  191. }
  192. catch (TooManyAttemptsException)
  193. {
  194. context.Done<IList<SearchHit>>(null);
  195. }
  196. }
  197. private async Task ActOnSearchResults(IDialogContext context, IAwaitable<IMessageActivity> input)
  198. {
  199. var activity = await input;
  200. var choice = activity.Text;
  201. switch (choice.ToLowerInvariant())
  202. {
  203. case "again":
  204. case "reset":
  205. this.QueryBuilder.Reset();
  206. await this.InitialPrompt(context);
  207. break;
  208. case "more":
  209. this.QueryBuilder.PageNumber++;
  210. await this.Search(context, null);
  211. break;
  212. case "refine":
  213. this.SelectRefiner(context);
  214. break;
  215. case "list":
  216. await this.ListAddedSoFar(context);
  217. context.Wait(this.ActOnSearchResults);
  218. break;
  219. case "done":
  220. context.Done(this.selected);
  221. break;
  222. default:
  223. await this.AddSelectedItem(context, choice);
  224. break;
  225. }
  226. }
  227. }
  228. }
Tip!

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

Comments

Loading...