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

SearchRefineDialog.cs 2.7 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
  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 Search.Models;
  10. using Search.Services;
  11. [Serializable]
  12. public class SearchRefineDialog : IDialog<string>
  13. {
  14. protected readonly string Refiner;
  15. protected readonly SearchQueryBuilder QueryBuilder;
  16. protected readonly PromptStyler PromptStyler;
  17. protected readonly string Prompt;
  18. protected readonly ISearchClient SearchClient;
  19. public SearchRefineDialog(ISearchClient searchClient, string refiner, SearchQueryBuilder queryBuilder = null, PromptStyler promptStyler = null, string prompt = null)
  20. {
  21. SetField.NotNull(out this.SearchClient, nameof(searchClient), searchClient);
  22. SetField.NotNull(out this.Refiner, nameof(refiner), refiner);
  23. this.QueryBuilder = queryBuilder ?? new SearchQueryBuilder();
  24. this.PromptStyler = promptStyler;
  25. this.Prompt = prompt ?? $"Here's what I found for {this.Refiner}.";
  26. }
  27. public async Task StartAsync(IDialogContext context)
  28. {
  29. var result = await this.SearchClient.SearchAsync(this.QueryBuilder, this.Refiner);
  30. IEnumerable<string> options = result.Facets[this.Refiner].Select(f => this.FormatRefinerOption((string)f.Value, f.Count));
  31. var promptOptions = new CancelablePromptOptions<string>(this.Prompt, cancelPrompt: "Type 'cancel' if you don't want to select any of these.", options: options.ToList(), promptStyler: this.PromptStyler);
  32. CancelablePromptChoice<string>.Choice(context, this.ApplyRefiner, promptOptions);
  33. }
  34. public async Task ApplyRefiner(IDialogContext context, IAwaitable<string> input)
  35. {
  36. string selection = await input;
  37. if (selection == null)
  38. {
  39. context.Done<string>(null);
  40. }
  41. else
  42. {
  43. string value = this.ParseRefinerValue(selection);
  44. if (this.QueryBuilder != null)
  45. {
  46. await context.PostAsync($"Filtering by {this.Refiner}: {value}");
  47. this.QueryBuilder.Refinements.Add(this.Refiner, new string[] { value });
  48. }
  49. context.Done(value);
  50. }
  51. }
  52. protected virtual string FormatRefinerOption(string value, long count)
  53. {
  54. return $"{value} ({count})";
  55. }
  56. protected virtual string ParseRefinerValue(string value)
  57. {
  58. return value.Substring(0, value.LastIndexOf('(') - 1);
  59. }
  60. }
  61. }
Tip!

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

Comments

Loading...