-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathTavily_TextSearch.cs
198 lines (173 loc) · 7.98 KB
/
Tavily_TextSearch.cs
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
// Copyright (c) Microsoft. All rights reserved.
using System.Text.Json;
using Microsoft.SemanticKernel.Data;
using Microsoft.SemanticKernel.Plugins.Web.Tavily;
namespace Search;
/// <summary>
/// This example shows how to create and use a <see cref="TavilyTextSearch"/>.
/// </summary>
public class Tavily_TextSearch(ITestOutputHelper output) : BaseTest(output)
{
/// <summary>
/// Show how to create a <see cref="TavilyTextSearch"/> and use it to perform a text search.
/// </summary>
[Fact]
public async Task UsingTavilyTextSearch()
{
// Create a logging handler to output HTTP requests and responses
LoggingHandler handler = new(new HttpClientHandler(), this.Output);
using HttpClient httpClient = new(handler);
// Create an ITextSearch instance using Tavily search
var textSearch = new TavilyTextSearch(apiKey: TestConfiguration.Tavily.ApiKey, options: new() { HttpClient = httpClient, IncludeRawContent = true });
var query = "What is the Semantic Kernel?";
// Search and return results as a string items
IAsyncEnumerable<string> stringResults = textSearch.SearchAsync(query, 4);
Console.WriteLine("--- String Results ---\n");
await foreach (string result in stringResults)
{
Console.WriteLine(result);
WriteHorizontalRule();
}
// Search and return results as TextSearchResult items
IAsyncEnumerable<TextSearchResult> textResults = textSearch.GetTextSearchResultsAsync(query, 4);
Console.WriteLine("\n--- Text Search Results ---\n");
await foreach (TextSearchResult result in textResults)
{
Console.WriteLine($"Name: {result.Name}");
Console.WriteLine($"Value: {result.Value}");
Console.WriteLine($"Link: {result.Link}");
WriteHorizontalRule();
}
// Search and return s results as TavilySearchResult items
IAsyncEnumerable<object> fullResults = textSearch.GetSearchResultsAsync(query, 4);
Console.WriteLine("\n--- Tavily Web Page Results ---\n");
await foreach (TavilySearchResult result in fullResults)
{
Console.WriteLine($"Name: {result.Title}");
Console.WriteLine($"Content: {result.Content}");
Console.WriteLine($"Url: {result.Url}");
Console.WriteLine($"RawContent: {result.RawContent}");
Console.WriteLine($"Score: {result.Score}");
WriteHorizontalRule();
}
}
/// <summary>
/// Show how to create a <see cref="TavilyTextSearch"/> and use it to perform a text search which returns an answer.
/// </summary>
[Fact]
public async Task UsingTavilyTextSearchToGetAnAnswer()
{
// Create a logging handler to output HTTP requests and responses
LoggingHandler handler = new(new HttpClientHandler(), this.Output);
using HttpClient httpClient = new(handler);
// Create an ITextSearch instance using Tavily search
var textSearch = new TavilyTextSearch(apiKey: TestConfiguration.Tavily.ApiKey, options: new() { HttpClient = httpClient, IncludeAnswer = true });
var query = "What is the Semantic Kernel?";
// Search and return results as a string items
IAsyncEnumerable<string> stringResults = textSearch.SearchAsync(query, 1);
Console.WriteLine("--- String Results ---\n");
await foreach (string result in stringResults)
{
Console.WriteLine(result);
WriteHorizontalRule();
}
}
/// <summary>
/// Show how to create a <see cref="TavilyTextSearch"/> and use it to perform a text search.
/// </summary>
[Fact]
public async Task UsingTavilyTextSearchAndIncludeEverything()
{
// Create a logging handler to output HTTP requests and responses
LoggingHandler handler = new(new HttpClientHandler(), this.Output);
using HttpClient httpClient = new(handler);
// Create an ITextSearch instance using Tavily search
var textSearch = new TavilyTextSearch(
apiKey: TestConfiguration.Tavily.ApiKey,
options: new()
{
HttpClient = httpClient,
IncludeRawContent = true,
IncludeImages = true,
IncludeImageDescriptions = true,
IncludeAnswer = true,
});
var query = "What is the Semantic Kernel?";
// Search and return s results as TavilySearchResult items
IAsyncEnumerable<object> fullResults = textSearch.GetSearchResultsAsync(query, 4, new() { Skip = 0 });
Console.WriteLine("\n--- Tavily Web Page Results ---\n");
await foreach (TavilySearchResult result in fullResults)
{
Console.WriteLine($"Name: {result.Title}");
Console.WriteLine($"Content: {result.Content}");
Console.WriteLine($"Url: {result.Url}");
Console.WriteLine($"RawContent: {result.RawContent}");
Console.WriteLine($"Score: {result.Score}");
WriteHorizontalRule();
}
}
/// <summary>
/// Show how to create a <see cref="TavilyTextSearch"/> with a custom mapper and use it to perform a text search.
/// </summary>
[Fact]
public async Task UsingTavilyTextSearchWithACustomMapperAsync()
{
// Create a logging handler to output HTTP requests and responses
LoggingHandler handler = new(new HttpClientHandler(), this.Output);
using HttpClient httpClient = new(handler);
// Create an ITextSearch instance using Tavily search
var textSearch = new TavilyTextSearch(apiKey: TestConfiguration.Tavily.ApiKey, options: new()
{
HttpClient = httpClient,
StringMapper = new TestTextSearchStringMapper(),
});
var query = "What is the Semantic Kernel?";
// Search with TextSearchResult textResult type
IAsyncEnumerable<string> stringResults = textSearch.SearchAsync(query, 2);
Console.WriteLine("--- Serialized JSON Results ---");
await foreach (string result in stringResults)
{
Console.WriteLine(result);
WriteHorizontalRule();
}
}
/// <summary>
/// Show how to create a <see cref="TavilyTextSearch"/> with a custom mapper and use it to perform a text search.
/// </summary>
[Fact]
public async Task UsingTavilyTextSearchWithAnIncludeDomainFilterAsync()
{
// Create a logging handler to output HTTP requests and responses
LoggingHandler handler = new(new HttpClientHandler(), this.Output);
using HttpClient httpClient = new(handler);
// Create an ITextSearch instance using Tavily search
var textSearch = new TavilyTextSearch(apiKey: TestConfiguration.Tavily.ApiKey, options: new()
{
HttpClient = httpClient,
StringMapper = new TestTextSearchStringMapper(),
});
var query = "What is the Semantic Kernel?";
// Search with TextSearchResult textResult type
TextSearchOptions searchOptions = new() { Filter = new TextSearchFilter().Equality("include_domain", "devblogs.microsoft.com") };
IAsyncEnumerable<TextSearchResult> textResults = textSearch.GetTextSearchResultsAsync(query, 4, searchOptions);
Console.WriteLine("--- Microsoft Developer Blogs Results ---");
await foreach (TextSearchResult result in textResults)
{
Console.WriteLine(result.Link);
WriteHorizontalRule();
}
}
#region private
/// <summary>
/// Test mapper which converts an arbitrary search result to a string using JSON serialization.
/// </summary>
private sealed class TestTextSearchStringMapper : ITextSearchStringMapper
{
/// <inheritdoc />
public string MapFromResultToString(object result)
{
return JsonSerializer.Serialize(result);
}
}
#endregion
}