|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System.Text.Json; |
| 4 | +using Microsoft.SemanticKernel.Data; |
| 5 | +using Microsoft.SemanticKernel.Plugins.Web.Tavily; |
| 6 | + |
| 7 | +namespace Search; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// This example shows how to create and use a <see cref="TavilyTextSearch"/>. |
| 11 | +/// </summary> |
| 12 | +public class Tavily_TextSearch(ITestOutputHelper output) : BaseTest(output) |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Show how to create a <see cref="TavilyTextSearch"/> and use it to perform a text search. |
| 16 | + /// </summary> |
| 17 | + [Fact] |
| 18 | + public async Task UsingTavilyTextSearch() |
| 19 | + { |
| 20 | + // Create a logging handler to output HTTP requests and responses |
| 21 | + LoggingHandler handler = new(new HttpClientHandler(), this.Output); |
| 22 | + using HttpClient httpClient = new(handler); |
| 23 | + |
| 24 | + // Create an ITextSearch instance using Tavily search |
| 25 | + var textSearch = new TavilyTextSearch(apiKey: TestConfiguration.Tavily.ApiKey, options: new() { HttpClient = httpClient, IncludeRawContent = true }); |
| 26 | + |
| 27 | + var query = "What is the Semantic Kernel?"; |
| 28 | + |
| 29 | + // Search and return results as a string items |
| 30 | + KernelSearchResults<string> stringResults = await textSearch.SearchAsync(query, new() { Top = 4 }); |
| 31 | + Console.WriteLine("--- String Results ---\n"); |
| 32 | + await foreach (string result in stringResults.Results) |
| 33 | + { |
| 34 | + Console.WriteLine(result); |
| 35 | + WriteHorizontalRule(); |
| 36 | + } |
| 37 | + |
| 38 | + // Search and return results as TextSearchResult items |
| 39 | + KernelSearchResults<TextSearchResult> textResults = await textSearch.GetTextSearchResultsAsync(query, new() { Top = 4 }); |
| 40 | + Console.WriteLine("\n--- Text Search Results ---\n"); |
| 41 | + await foreach (TextSearchResult result in textResults.Results) |
| 42 | + { |
| 43 | + Console.WriteLine($"Name: {result.Name}"); |
| 44 | + Console.WriteLine($"Value: {result.Value}"); |
| 45 | + Console.WriteLine($"Link: {result.Link}"); |
| 46 | + WriteHorizontalRule(); |
| 47 | + } |
| 48 | + |
| 49 | + // Search and return s results as TavilySearchResult items |
| 50 | + KernelSearchResults<object> fullResults = await textSearch.GetSearchResultsAsync(query, new() { Top = 4 }); |
| 51 | + Console.WriteLine("\n--- Tavily Web Page Results ---\n"); |
| 52 | + await foreach (TavilySearchResult result in fullResults.Results) |
| 53 | + { |
| 54 | + Console.WriteLine($"Name: {result.Title}"); |
| 55 | + Console.WriteLine($"Content: {result.Content}"); |
| 56 | + Console.WriteLine($"Url: {result.Url}"); |
| 57 | + Console.WriteLine($"RawContent: {result.RawContent}"); |
| 58 | + Console.WriteLine($"Score: {result.Score}"); |
| 59 | + WriteHorizontalRule(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Show how to create a <see cref="TavilyTextSearch"/> and use it to perform a text search which returns an answer. |
| 65 | + /// </summary> |
| 66 | + [Fact] |
| 67 | + public async Task UsingTavilyTextSearchToGetAnAnswer() |
| 68 | + { |
| 69 | + // Create a logging handler to output HTTP requests and responses |
| 70 | + LoggingHandler handler = new(new HttpClientHandler(), this.Output); |
| 71 | + using HttpClient httpClient = new(handler); |
| 72 | + |
| 73 | + // Create an ITextSearch instance using Tavily search |
| 74 | + var textSearch = new TavilyTextSearch(apiKey: TestConfiguration.Tavily.ApiKey, options: new() { HttpClient = httpClient, IncludeAnswer = true }); |
| 75 | + |
| 76 | + var query = "What is the Semantic Kernel?"; |
| 77 | + |
| 78 | + // Search and return results as a string items |
| 79 | + KernelSearchResults<string> stringResults = await textSearch.SearchAsync(query, new() { Top = 1 }); |
| 80 | + Console.WriteLine("--- String Results ---\n"); |
| 81 | + await foreach (string result in stringResults.Results) |
| 82 | + { |
| 83 | + Console.WriteLine(result); |
| 84 | + WriteHorizontalRule(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Show how to create a <see cref="TavilyTextSearch"/> and use it to perform a text search. |
| 90 | + /// </summary> |
| 91 | + [Fact] |
| 92 | + public async Task UsingTavilyTextSearchAndIncludeEverything() |
| 93 | + { |
| 94 | + // Create a logging handler to output HTTP requests and responses |
| 95 | + LoggingHandler handler = new(new HttpClientHandler(), this.Output); |
| 96 | + using HttpClient httpClient = new(handler); |
| 97 | + |
| 98 | + // Create an ITextSearch instance using Tavily search |
| 99 | + var textSearch = new TavilyTextSearch( |
| 100 | + apiKey: TestConfiguration.Tavily.ApiKey, |
| 101 | + options: new() |
| 102 | + { |
| 103 | + HttpClient = httpClient, |
| 104 | + IncludeRawContent = true, |
| 105 | + IncludeImages = true, |
| 106 | + IncludeImageDescriptions = true, |
| 107 | + IncludeAnswer = true, |
| 108 | + }); |
| 109 | + |
| 110 | + var query = "What is the Semantic Kernel?"; |
| 111 | + |
| 112 | + // Search and return s results as TavilySearchResult items |
| 113 | + KernelSearchResults<object> fullResults = await textSearch.GetSearchResultsAsync(query, new() { Top = 4, Skip = 0 }); |
| 114 | + Console.WriteLine("\n--- Tavily Web Page Results ---\n"); |
| 115 | + await foreach (TavilySearchResult result in fullResults.Results) |
| 116 | + { |
| 117 | + Console.WriteLine($"Name: {result.Title}"); |
| 118 | + Console.WriteLine($"Content: {result.Content}"); |
| 119 | + Console.WriteLine($"Url: {result.Url}"); |
| 120 | + Console.WriteLine($"RawContent: {result.RawContent}"); |
| 121 | + Console.WriteLine($"Score: {result.Score}"); |
| 122 | + WriteHorizontalRule(); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Show how to create a <see cref="TavilyTextSearch"/> with a custom mapper and use it to perform a text search. |
| 128 | + /// </summary> |
| 129 | + [Fact] |
| 130 | + public async Task UsingTavilyTextSearchWithACustomMapperAsync() |
| 131 | + { |
| 132 | + // Create a logging handler to output HTTP requests and responses |
| 133 | + LoggingHandler handler = new(new HttpClientHandler(), this.Output); |
| 134 | + using HttpClient httpClient = new(handler); |
| 135 | + |
| 136 | + // Create an ITextSearch instance using Tavily search |
| 137 | + var textSearch = new TavilyTextSearch(apiKey: TestConfiguration.Tavily.ApiKey, options: new() |
| 138 | + { |
| 139 | + HttpClient = httpClient, |
| 140 | + StringMapper = new TestTextSearchStringMapper(), |
| 141 | + }); |
| 142 | + |
| 143 | + var query = "What is the Semantic Kernel?"; |
| 144 | + |
| 145 | + // Search with TextSearchResult textResult type |
| 146 | + KernelSearchResults<string> stringResults = await textSearch.SearchAsync(query, new() { Top = 2 }); |
| 147 | + Console.WriteLine("--- Serialized JSON Results ---"); |
| 148 | + await foreach (string result in stringResults.Results) |
| 149 | + { |
| 150 | + Console.WriteLine(result); |
| 151 | + WriteHorizontalRule(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// Show how to create a <see cref="TavilyTextSearch"/> with a custom mapper and use it to perform a text search. |
| 157 | + /// </summary> |
| 158 | + [Fact] |
| 159 | + public async Task UsingTavilyTextSearchWithAnIncludeDomainFilterAsync() |
| 160 | + { |
| 161 | + // Create a logging handler to output HTTP requests and responses |
| 162 | + LoggingHandler handler = new(new HttpClientHandler(), this.Output); |
| 163 | + using HttpClient httpClient = new(handler); |
| 164 | + |
| 165 | + // Create an ITextSearch instance using Tavily search |
| 166 | + var textSearch = new TavilyTextSearch(apiKey: TestConfiguration.Tavily.ApiKey, options: new() |
| 167 | + { |
| 168 | + HttpClient = httpClient, |
| 169 | + StringMapper = new TestTextSearchStringMapper(), |
| 170 | + }); |
| 171 | + |
| 172 | + var query = "What is the Semantic Kernel?"; |
| 173 | + |
| 174 | + // Search with TextSearchResult textResult type |
| 175 | + TextSearchOptions searchOptions = new() { Top = 4, Filter = new TextSearchFilter().Equality("include_domain", "devblogs.microsoft.com") }; |
| 176 | + KernelSearchResults<TextSearchResult> textResults = await textSearch.GetTextSearchResultsAsync(query, searchOptions); |
| 177 | + Console.WriteLine("--- Microsoft Developer Blogs Results ---"); |
| 178 | + await foreach (TextSearchResult result in textResults.Results) |
| 179 | + { |
| 180 | + Console.WriteLine(result.Link); |
| 181 | + WriteHorizontalRule(); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + #region private |
| 186 | + /// <summary> |
| 187 | + /// Test mapper which converts an arbitrary search result to a string using JSON serialization. |
| 188 | + /// </summary> |
| 189 | + private sealed class TestTextSearchStringMapper : ITextSearchStringMapper |
| 190 | + { |
| 191 | + /// <inheritdoc /> |
| 192 | + public string MapFromResultToString(object result) |
| 193 | + { |
| 194 | + return JsonSerializer.Serialize(result); |
| 195 | + } |
| 196 | + } |
| 197 | + #endregion |
| 198 | +} |
0 commit comments