forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextSearchServiceCollectionExtensions.cs
More file actions
150 lines (135 loc) · 8.27 KB
/
TextSearchServiceCollectionExtensions.cs
File metadata and controls
150 lines (135 loc) · 8.27 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.VectorData;
using Microsoft.SemanticKernel.Data;
using Microsoft.SemanticKernel.Embeddings;
namespace Microsoft.SemanticKernel;
/// <summary>
/// Extension methods to register <see cref="ITextSearch"/> for use with <see cref="IServiceCollection"/>.
/// </summary>
public static class TextSearchServiceCollectionExtensions
{
/// <summary>
/// Register a <see cref="VectorStoreTextSearch{TRecord}"/> instance with the specified service ID.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to register the <see cref="ITextSearch"/> on.</param>
/// <param name="stringMapper"><see cref="ITextSearchStringMapper" /> instance that can map a TRecord to a <see cref="string"/></param>
/// <param name="resultMapper"><see cref="ITextSearchResultMapper" /> instance that can map a TRecord to a <see cref="TextSearchResult"/></param>
/// <param name="options">Options used to construct an instance of <see cref="VectorStoreTextSearch{TRecord}"/></param>
/// <param name="serviceId">An optional service id to use as the service key.</param>
public static IServiceCollection AddVectorStoreTextSearch<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] TRecord>(
this IServiceCollection services,
ITextSearchStringMapper? stringMapper = null,
ITextSearchResultMapper? resultMapper = null,
VectorStoreTextSearchOptions? options = null,
string? serviceId = default)
where TRecord : class
{
// If we are not constructing the dependent services, add the VectorStoreTextSearch as transient, since we
// cannot make assumptions about how dependent services are being managed.
services.AddKeyedTransient<VectorStoreTextSearch<TRecord>>(
serviceId,
(sp, obj) =>
{
stringMapper ??= sp.GetService<ITextSearchStringMapper>();
resultMapper ??= sp.GetService<ITextSearchResultMapper>();
options ??= sp.GetService<VectorStoreTextSearchOptions>();
var vectorSearch = sp.GetService<IVectorSearchable<TRecord>>();
return vectorSearch is null
? throw new InvalidOperationException("No IVectorSearch<TRecord> registered.")
: new VectorStoreTextSearch<TRecord>(vectorSearch, stringMapper, resultMapper, options);
});
return services;
}
/// <summary>
/// Register a <see cref="VectorStoreTextSearch{TRecord}"/> instance with the specified service ID.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to register the <see cref="ITextSearch"/> on.</param>
/// <param name="vectorSearchableServiceId">Service id of the <see cref="IVectorSearchable{TRecord}"/> to use.</param>
/// <param name="stringMapper"><see cref="ITextSearchStringMapper" /> instance that can map a TRecord to a <see cref="string"/></param>
/// <param name="resultMapper"><see cref="ITextSearchResultMapper" /> instance that can map a TRecord to a <see cref="TextSearchResult"/></param>
/// <param name="options">Options used to construct an instance of <see cref="VectorStoreTextSearch{TRecord}"/></param>
/// <param name="serviceId">An optional service id to use as the service key.</param>
public static IServiceCollection AddVectorStoreTextSearch<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] TRecord>(
this IServiceCollection services,
string vectorSearchableServiceId,
ITextSearchStringMapper? stringMapper = null,
ITextSearchResultMapper? resultMapper = null,
VectorStoreTextSearchOptions? options = null,
string? serviceId = default)
where TRecord : class
{
// If we are not constructing the dependent services, add the VectorStoreTextSearch as transient, since we
// cannot make assumptions about how dependent services are being managed.
services.AddKeyedTransient<VectorStoreTextSearch<TRecord>>(
serviceId,
(sp, obj) =>
{
stringMapper ??= sp.GetService<ITextSearchStringMapper>();
resultMapper ??= sp.GetService<ITextSearchResultMapper>();
options ??= sp.GetService<VectorStoreTextSearchOptions>();
var vectorSearch = sp.GetKeyedService<IVectorSearchable<TRecord>>(vectorSearchableServiceId);
if (vectorSearch is not null)
{
return new VectorStoreTextSearch<TRecord>(
vectorSearch,
stringMapper,
resultMapper,
options);
}
throw new InvalidOperationException($"No IVectorSearch<TRecord> for service id {vectorSearchableServiceId} registered.");
});
return services;
}
/// <summary>
/// Register a <see cref="VectorStoreTextSearch{TRecord}"/> instance with the specified service ID.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to register the <see cref="ITextSearch"/> on.</param>
/// <param name="vectorSearchServiceId">Service id of the <see cref="IVectorSearchable{TRecord}"/> to use.</param>
/// <param name="textEmbeddingGenerationServiceId">Service id of the <see cref="ITextEmbeddingGenerationService"/> to use.</param>
/// <param name="stringMapper"><see cref="ITextSearchStringMapper" /> instance that can map a TRecord to a <see cref="string"/></param>
/// <param name="resultMapper"><see cref="ITextSearchResultMapper" /> instance that can map a TRecord to a <see cref="TextSearchResult"/></param>
/// <param name="options">Options used to construct an instance of <see cref="VectorStoreTextSearch{TRecord}"/></param>
/// <param name="serviceId">An optional service id to use as the service key.</param>
[Obsolete("Use the overload which doesn't accept a textEmbeddingGenerationServiceId, and configure an IEmbeddingGenerator instead with the collection represented by vectorSearchServiceId.")]
public static IServiceCollection AddVectorStoreTextSearch<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] TRecord>(
this IServiceCollection services,
string vectorSearchServiceId,
string textEmbeddingGenerationServiceId,
ITextSearchStringMapper? stringMapper = null,
ITextSearchResultMapper? resultMapper = null,
VectorStoreTextSearchOptions? options = null,
string? serviceId = default)
where TRecord : class
{
// If we are not constructing the dependent services, add the VectorStoreTextSearch as transient, since we
// cannot make assumptions about how dependent services are being managed.
services.AddKeyedTransient<VectorStoreTextSearch<TRecord>>(
serviceId,
(sp, obj) =>
{
stringMapper ??= sp.GetService<ITextSearchStringMapper>();
resultMapper ??= sp.GetService<ITextSearchResultMapper>();
options ??= sp.GetService<VectorStoreTextSearchOptions>();
var vectorizedSearch = sp.GetKeyedService<IVectorSearchable<TRecord>>(vectorSearchServiceId);
if (vectorizedSearch is null)
{
throw new InvalidOperationException($"No IVectorizedSearch<TRecord> for service id {vectorSearchServiceId} registered.");
}
var generationService = sp.GetKeyedService<ITextEmbeddingGenerationService>(textEmbeddingGenerationServiceId);
if (vectorizedSearch is not null && generationService is not null)
{
return new VectorStoreTextSearch<TRecord>(
vectorizedSearch,
generationService,
stringMapper,
resultMapper,
options);
}
throw new InvalidOperationException($"No ITextEmbeddingGenerationService for service id {textEmbeddingGenerationServiceId} registered.");
});
return services;
}
}