forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebServiceCollectionExtensions.cs
More file actions
126 lines (108 loc) · 5.32 KB
/
WebServiceCollectionExtensions.cs
File metadata and controls
126 lines (108 loc) · 5.32 KB
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
// Copyright (c) Microsoft. All rights reserved.
#pragma warning disable CS0618 // ITextSearch is obsolete - these extension methods provide backward compatibility
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel.Data;
using Microsoft.SemanticKernel.Plugins.Web.Bing;
using Microsoft.SemanticKernel.Plugins.Web.Brave;
using Microsoft.SemanticKernel.Plugins.Web.Google;
using Microsoft.SemanticKernel.Plugins.Web.Tavily;
namespace Microsoft.SemanticKernel;
/// <summary>
/// Extension methods to register <see cref="ITextSearch"/> for use with <see cref="IServiceCollection"/>.
/// </summary>
public static class WebServiceCollectionExtensions
{
/// <summary>
/// Register an <see cref="ITextSearch"/> instance with the specified service ID.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to register the <see cref="ITextSearch"/> on.</param>
/// <param name="apiKey">The API key credential used to authenticate requests against the Search service.</param>
/// <param name="options">Instance of <see cref="BingTextSearchOptions"/> to used when creating the <see cref="BingTextSearch"/>.</param>
/// <param name="serviceId">An optional service id to use as the service key.</param>
public static IServiceCollection AddBingTextSearch(
this IServiceCollection services,
string apiKey,
BingTextSearchOptions? options = null,
string? serviceId = default)
{
Verify.NotNull(services);
services.AddKeyedSingleton<ITextSearch>(
serviceId,
(sp, obj) =>
{
var selectedOptions = options ?? sp.GetService<BingTextSearchOptions>();
return new BingTextSearch(apiKey, selectedOptions);
});
return services;
}
/// <summary>
/// Register an <see cref="ITextSearch"/> instance with the specified service ID.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to register the <see cref="ITextSearch"/> on.</param>
/// <param name="apiKey">The API key credential used to authenticate requests against the Search service.</param>
/// <param name="options">Instance of <see cref="BingTextSearchOptions"/> to used when creating the <see cref="BingTextSearch"/>.</param>
/// <param name="serviceId">An optional service id to use as the service key.</param>
public static IServiceCollection AddBraveTextSearch(
this IServiceCollection services,
string apiKey,
BraveTextSearchOptions? options = null,
string? serviceId = default)
{
services.AddKeyedSingleton<ITextSearch>(
serviceId,
(sp, obj) =>
{
var selectedOptions = options ?? sp.GetService<BraveTextSearchOptions>();
return new BraveTextSearch(apiKey, selectedOptions);
});
return services;
}
/// <summary>
/// Register an <see cref="ITextSearch"/> instance with the specified service ID.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to register the <see cref="ITextSearch"/> on.</param>
/// <param name="searchEngineId">Google Search Engine ID (looks like "a12b345...")</param>
/// <param name="apiKey">The API key credential used to authenticate requests against the Search service.</param>
/// <param name="options">Instance of <see cref="GoogleTextSearchOptions"/> to used when creating the <see cref="GoogleTextSearch"/>.</param>
/// <param name="serviceId">An optional service id to use as the service key.</param>
public static IServiceCollection AddGoogleTextSearch(
this IServiceCollection services,
string searchEngineId,
string apiKey,
GoogleTextSearchOptions? options = null,
string? serviceId = default)
{
Verify.NotNull(services);
services.AddKeyedSingleton<ITextSearch>(
serviceId,
(sp, obj) =>
{
var selectedOptions = options ?? sp.GetService<GoogleTextSearchOptions>();
return new GoogleTextSearch(searchEngineId, apiKey, selectedOptions);
});
return services;
}
/// <summary>
/// Register an <see cref="ITextSearch"/> instance with the specified service ID.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to register the <see cref="ITextSearch"/> on.</param>
/// <param name="apiKey">The API key credential used to authenticate requests against the Search service.</param>
/// <param name="options">Instance of <see cref="TavilyTextSearchOptions"/> to used when creating the <see cref="TavilyTextSearch"/>.</param>
/// <param name="serviceId">An optional service id to use as the service key.</param>
public static IServiceCollection AddTavilyTextSearch(
this IServiceCollection services,
string apiKey,
TavilyTextSearchOptions? options = null,
string? serviceId = default)
{
Verify.NotNull(services);
services.AddKeyedSingleton<ITextSearch>(
serviceId,
(sp, _) =>
{
var selectedOptions = options ?? sp.GetService<TavilyTextSearchOptions>();
return new TavilyTextSearch(apiKey, selectedOptions);
});
return services;
}
}